Auto-wrapping `std::vector<T>` etc. as view
See original GitHub issueIt 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:
- Created 4 years ago
- Reactions:4
- Comments:19 (13 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hmm, right, mapping the item type is an issue. A case like
vector[int]
is simple (items would be Python int objects), but avector[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 optionalmapping_function
could also becython.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).Practically it would have to be generated code like the code for struct conversion since it isn’t possible to template cdef classes.