question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Auto-wrapping `std::vector<T>` etc. as view

See original GitHub issue

It would be really useful to have a way to convert std::vector<T> to T[::1] in Cython. This would allow zero-copy usage of std::vector<T>'s data in Python or other Cython contexts.

Here’s some dummy code illustrating a bit more how one might like to use this.

cdef vector[uint8_t] v = memoryview(b"abc")
cdef uint8_t[::1] d = v

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:4
  • Comments:19 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
scodercommented, Apr 23, 2020

Do you have thoughts on how we handle this templating in this new object?

Hmm, right, mapping the item type is an issue. A case like vector[int] is simple (items would be Python int objects), but a vector[vector[int]] would then return copied int-lists as items again, and it’s not obvious how to let users map those.

Although, why not listview(container, mapping_function)? Where the optional mapping_function could also be cython.listview, amongst other things, and the default would just be the normal C->Python type mapping.

I don’t see this as a plain object implementation. It can well be a language feature with deep integration into the type system. It can be implemented with Cython code generation, like fused types or the C++ type mappings. Maybe with a base class that can then be shared across different Cython modules. (Not sure if the latter is helpful, maybe for type checks?)

And if the item type is one that is supported by the buffer protocol (including structs etc.), then the generated type could include support for the buffer protocol. (vector can apparently provide access to its buffer).

1reaction
da-woodscommented, Apr 23, 2020

Practically it would have to be generated code like the code for struct conversion since it isn’t possible to template cdef classes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using std::vector as view on to raw memory - Stack Overflow
The reason is that I need to apply algorithms from <algorithm> (sorting, swaping elements etc.) on that data. On the other hand changing...
Read more >
How can I wrap functions which take C++ containers as ...
This technique is used in the file scitbx/include/scitbx/array_family/boost_python/flex_wrapper.h in the "scitbx" package. The file could easily ...
Read more >
Everything You Need to Know About std::variant from C++17
Let's see how this brand new std::variant from C++17 works and ... that use “advanced” types such as vectors, strings, containers, etc, etc....
Read more >
A true heterogeneous container in C++ - Andy G's Blog
The client can simply direct us on how heterogeneous they want that container to be! And then we start wrapping up the vector...
Read more >
Memory Management, C++ FAQ - Standard C++
Can I drop the [] when delete ing an array of some built-in type ( char , int , etc)?; After p =...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found