Accept abitrary containers and iterators for shape/strides

This adds support for constructing `buffer_info` and `array`s using
arbitrary containers or iterator pairs instead of requiring a vector.

This is primarily needed by PR #782 (which makes strides signed to
properly support negative strides, and will likely also make shape and
itemsize to avoid mixed integer issues), but also needs to preserve
backwards compatibility with 2.1 and earlier which accepts the strides
parameter as a vector of size_t's.

Rather than adding nearly duplicate constructors for each stride-taking
constructor, it seems nicer to simply allow any type of container (or
iterator pairs).  This works by replacing the existing vector arguments
with a new `detail::any_container` class that handles implicit
conversion of arbitrary containers into a vector of the desired type.
It can also be explicitly instantiated with a pair of iterators (e.g.
by passing {begin, end} instead of the container).
This commit is contained in:
Jason Rhinelander
2017-04-07 15:49:54 -04:00
parent dbb4c5b531
commit 5f38386293
5 changed files with 98 additions and 60 deletions

View File

@@ -13,7 +13,6 @@
#include <pybind11/stl.h>
#include <cstdint>
#include <vector>
using arr = py::array;
using arr_t = py::array_t<uint16_t, 0>;
@@ -119,8 +118,8 @@ test_initializer numpy_array([](py::module &m) {
sm.def("wrap", [](py::array a) {
return py::array(
a.dtype(),
std::vector<size_t>(a.shape(), a.shape() + a.ndim()),
std::vector<size_t>(a.strides(), a.strides() + a.ndim()),
{a.shape(), a.shape() + a.ndim()},
{a.strides(), a.strides() + a.ndim()},
a.data(),
a
);