Skip to content

Commit

Permalink
Modified Vector STL bind initialization from a buffer type with optim…
Browse files Browse the repository at this point in the history
…ization for simple arrays
  • Loading branch information
Marc Chiesa committed Jul 14, 2020
1 parent aa982e1 commit ddc4704
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions include/pybind11/stl_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,14 +397,20 @@ 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) {
auto vec = std::unique_ptr<Vector>(new Vector(p, end));
return vec.release();
}
else {
auto vec = std::unique_ptr<Vector>(new Vector());
vec->reserve((size_t) info.shape[0]);
for (; p != end; p += step)
vec->push_back(*p);
return vec.release();
}
}));

return;
Expand Down

0 comments on commit ddc4704

Please sign in to comment.