mirror of
https://github.com/pybind/pybind11.git
synced 2026-05-21 05:19:35 +00:00
Modified Vector STL bind initialization from a buffer type with optimization for simple arrays (#2298)
* Modified Vector STL bind initialization from a buffer type with optimization for simple arrays * Add subtests to demonstrate processing Python buffer protocol objects with step > 1 * Fixed memoryview step test to only run on Python 3+ * Modified Vector constructor from buffer to return by value for readability
This commit is contained in:
@@ -397,14 +397,19 @@ vector_buffer(Class_& cl) {
|
||||
if (!detail::compare_buffer_info<T>::compare(info) || (ssize_t) sizeof(T) != info.itemsize)
|
||||
throw type_error("Format mismatch (Python: " + info.format + " C++: " + format_descriptor<T>::format() + ")");
|
||||
|
||||
auto vec = std::unique_ptr<Vector>(new Vector());
|
||||
vec->reserve((size_t) info.shape[0]);
|
||||
T *p = static_cast<T*>(info.ptr);
|
||||
ssize_t step = info.strides[0] / static_cast<ssize_t>(sizeof(T));
|
||||
T *end = p + info.shape[0] * step;
|
||||
for (; p != end; p += step)
|
||||
vec->push_back(*p);
|
||||
return vec.release();
|
||||
if (step == 1) {
|
||||
return Vector(p, end);
|
||||
}
|
||||
else {
|
||||
Vector vec;
|
||||
vec.reserve((size_t) info.shape[0]);
|
||||
for (; p != end; p += step)
|
||||
vec.push_back(*p);
|
||||
return vec;
|
||||
}
|
||||
}));
|
||||
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user