From 4ded7719e58f078bb0e5ac9e61263b992ac81c3f Mon Sep 17 00:00:00 2001 From: Pauli Virtanen <pauli.t.virtanen@jyu.fi> Date: Fri, 5 Aug 2022 18:59:42 +0300 Subject: [PATCH] array: enable other storage than std::vector in StoredArray --- src/array.hpp | 12 +++++------- src/pythonutil.hpp | 10 ++++------ 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/array.hpp b/src/array.hpp index 3c1a079..17ed072 100644 --- a/src/array.hpp +++ b/src/array.hpp @@ -469,19 +469,17 @@ public: * * \brief Array that is also the storage for the data. */ -template <typename Scalar, typename Shape, size_t Alignment=0, bool BoundsCheck=true, typename StorageType_=Scalar> +template <typename Scalar, typename Shape, size_t Alignment=0, bool BoundsCheck=true, typename Storage_=std::vector<Scalar> > class StoredArray : public ArrayView<Scalar, Shape, Alignment, BoundsCheck> { public: - typedef StorageType_ StorageType; + typedef Storage_ Storage; private: - static_assert(sizeof(Scalar) % sizeof(StorageType) == 0, "incommensurate sizes"); - static constexpr size_t units = sizeof(Scalar) / sizeof(StorageType); + static_assert(sizeof(Scalar) % sizeof(typename Storage::value_type) == 0, "incommensurate sizes"); + static constexpr size_t units = sizeof(Scalar) / sizeof(typename Storage::value_type); typedef ArrayView<Scalar, Shape, Alignment, BoundsCheck> BaseType; - typedef std::vector<StorageType> Storage; - Storage storage_; public: @@ -520,7 +518,7 @@ public: }; template <typename Scalar, typename Shape, size_t Alignment=0, bool BoundsCheck=true> -using StoredComplexArray = StoredArray<Scalar, Shape, Alignment, BoundsCheck, typename Scalar::value_type>; +using StoredComplexArray = StoredArray<Scalar, Shape, Alignment, BoundsCheck, std::vector<typename Scalar::value_type> >; } // namespace array diff --git a/src/pythonutil.hpp b/src/pythonutil.hpp index 27793aa..53bfd24 100644 --- a/src/pythonutil.hpp +++ b/src/pythonutil.hpp @@ -124,10 +124,10 @@ namespace array { namespace python PY_VISIBILITY { } }; - template <typename Scalar, typename Shape, size_t Alignment=0, bool BoundsCheck=true, typename StorageType=Scalar> + template <typename Scalar, typename Shape, size_t Alignment=0, bool BoundsCheck=true, typename Storage=std::vector<Scalar> > struct array_caster { private: - typedef StoredArray<Scalar, Shape, Alignment, BoundsCheck, StorageType> ArrayType; + typedef StoredArray<Scalar, Shape, Alignment, BoundsCheck, Storage> ArrayType; static constexpr auto array_type_name = pybind11::detail::const_name<ArrayType>(); public: @@ -137,8 +137,6 @@ namespace array { namespace python PY_VISIBILITY { pybind11::return_value_policy /* policy */, pybind11::handle /* parent */) { - typedef std::vector<StorageType> Storage; - ArrayType moved = std::move(src); Storage *store = new Storage(std::move(moved.storage())); @@ -165,7 +163,7 @@ namespace array { namespace python PY_VISIBILITY { using is_py_array_view = std::is_base_of<PyArrayView<typename T::Scalar, typename T::Shape, T::Alignment, T::BoundsCheck>, T>; template <typename T> - using is_stored_array = std::is_base_of<StoredArray<typename T::Scalar, typename T::Shape, T::Alignment, T::BoundsCheck, typename T::StorageType>, T>; + using is_stored_array = std::is_base_of<StoredArray<typename T::Scalar, typename T::Shape, T::Alignment, T::BoundsCheck, typename T::Storage>, T>; } } } // namespace array::python @@ -178,7 +176,7 @@ namespace pybind11 { namespace detail { template <typename Type> struct type_caster<Type, enable_if_t<array::python::detail::is_stored_array<Type>::value> > - : public array::python::detail::array_caster<typename Type::Scalar, typename Type::Shape, Type::Alignment, Type::BoundsCheck, typename Type::StorageType> + : public array::python::detail::array_caster<typename Type::Scalar, typename Type::Shape, Type::Alignment, Type::BoundsCheck, typename Type::Storage> { }; } } // namespace pybind11::detail -- GitLab