|
| 1 | +/// Copyright (c) 2023 CNRS INRIA |
| 2 | + |
| 3 | +#ifndef __eigenpy_utils_std_array_hpp__ |
| 4 | +#define __eigenpy_utils_std_array_hpp__ |
| 5 | + |
| 6 | +#include <boost/python/suite/indexing/indexing_suite.hpp> |
| 7 | +#include "eigenpy/std-vector.hpp" |
| 8 | + |
| 9 | +#include <array> |
| 10 | + |
| 11 | +namespace eigenpy { |
| 12 | + |
| 13 | +template <typename Container, bool NoProxy, class SliceAllocator, |
| 14 | + class DerivedPolicies> |
| 15 | +class array_indexing_suite; |
| 16 | +namespace details { |
| 17 | + |
| 18 | +template <typename Container, bool NoProxy, class SliceAllocator> |
| 19 | +class final_array_derived_policies |
| 20 | + : public array_indexing_suite< |
| 21 | + Container, NoProxy, SliceAllocator, |
| 22 | + final_array_derived_policies<Container, NoProxy, SliceAllocator> > {}; |
| 23 | +} // namespace details |
| 24 | + |
| 25 | +template <typename Container, bool NoProxy = false, |
| 26 | + class SliceAllocator = std::allocator<typename Container::value_type>, |
| 27 | + class DerivedPolicies = details::final_array_derived_policies< |
| 28 | + Container, NoProxy, SliceAllocator> > |
| 29 | +class array_indexing_suite |
| 30 | + : public bp::vector_indexing_suite<Container, NoProxy, DerivedPolicies> { |
| 31 | + public: |
| 32 | + typedef typename Container::value_type data_type; |
| 33 | + typedef typename Container::value_type key_type; |
| 34 | + typedef typename Container::size_type index_type; |
| 35 | + typedef typename Container::size_type size_type; |
| 36 | + typedef typename Container::difference_type difference_type; |
| 37 | + typedef std::vector<data_type, SliceAllocator> slice_vector_type; |
| 38 | + static constexpr std::size_t Size = std::tuple_size<Container>{}; |
| 39 | + |
| 40 | + template <class Class> |
| 41 | + static void extension_def(Class &) {} |
| 42 | + |
| 43 | + // throws exception |
| 44 | + static void delete_item(Container &, index_type) { |
| 45 | + PyErr_SetString(PyExc_NotImplementedError, |
| 46 | + "Cannot delete item from std::array type."); |
| 47 | + bp::throw_error_already_set(); |
| 48 | + } |
| 49 | + |
| 50 | + // throws exception |
| 51 | + static void delete_slice(Container &, index_type, index_type) { |
| 52 | + PyErr_SetString(PyExc_NotImplementedError, |
| 53 | + "Cannot delete slice from std::array type."); |
| 54 | + bp::throw_error_already_set(); |
| 55 | + } |
| 56 | + |
| 57 | + static void set_slice(Container &container, index_type from, index_type to, |
| 58 | + data_type const &v) { |
| 59 | + if (from >= to) { |
| 60 | + PyErr_SetString(PyExc_NotImplementedError, |
| 61 | + "Setting this slice would insert into an std::array, " |
| 62 | + "which is not supported."); |
| 63 | + bp::throw_error_already_set(); |
| 64 | + } else { |
| 65 | + std::fill(container.begin() + from, container.begin() + to, v); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + template <class Iter> |
| 70 | + static void set_slice(Container &container, index_type from, index_type to, |
| 71 | + Iter first, Iter last) { |
| 72 | + if (from >= to) { |
| 73 | + PyErr_SetString(PyExc_NotImplementedError, |
| 74 | + "Setting this slice would insert into an std::array, " |
| 75 | + "which is not supported."); |
| 76 | + bp::throw_error_already_set(); |
| 77 | + } else { |
| 78 | + if (long(to - from) == std::distance(first, last)) { |
| 79 | + std::copy(first, last, container.begin() + from); |
| 80 | + } else { |
| 81 | + PyErr_SetString(PyExc_NotImplementedError, |
| 82 | + "Size of std::array slice and size of right-hand side " |
| 83 | + "iterator are incompatible."); |
| 84 | + bp::throw_error_already_set(); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + static bp::object get_slice(Container &container, index_type from, |
| 90 | + index_type to) { |
| 91 | + if (from > to) return bp::object(slice_vector_type()); |
| 92 | + slice_vector_type out; |
| 93 | + for (size_t i = from; i < to; i++) { |
| 94 | + out.push_back(container[i]); |
| 95 | + } |
| 96 | + return bp::object(std::move(out)); |
| 97 | + } |
| 98 | +}; |
| 99 | + |
| 100 | +/// \brief Expose an std::array (a C++11 fixed-size array) from a given type |
| 101 | +/// \tparam array_type std::array type to expose |
| 102 | +/// \tparam NoProxy When set to false, the elements will be copied when |
| 103 | +/// returned to Python. |
| 104 | +/// \tparam SliceAllocator Allocator type to use for slices of std::array type |
| 105 | +/// accessed using e.g. __getitem__[0:4] in Python. These slices are returned as |
| 106 | +/// std::vector (dynamic size). |
| 107 | +template <typename array_type, bool NoProxy = false, |
| 108 | + class SliceAllocator = |
| 109 | + std::allocator<typename array_type::value_type> > |
| 110 | +struct StdArrayPythonVisitor { |
| 111 | + typedef typename array_type::value_type value_type; |
| 112 | + |
| 113 | + static ::boost::python::list tolist(array_type &self) { |
| 114 | + return details::build_list<array_type, NoProxy>::run(self); |
| 115 | + } |
| 116 | + |
| 117 | + static void expose(const std::string &class_name, |
| 118 | + const std::string &doc_string = "") { |
| 119 | + expose(class_name, doc_string, EmptyPythonVisitor()); |
| 120 | + } |
| 121 | + |
| 122 | + template <typename DerivedVisitor> |
| 123 | + static void expose(const std::string &class_name, |
| 124 | + const bp::def_visitor<DerivedVisitor> &visitor) { |
| 125 | + expose(class_name, "", visitor); |
| 126 | + } |
| 127 | + |
| 128 | + template <typename DerivedVisitor> |
| 129 | + static void expose(const std::string &class_name, |
| 130 | + const std::string &doc_string, |
| 131 | + const bp::def_visitor<DerivedVisitor> &visitor) { |
| 132 | + if (!register_symbolic_link_to_registered_type<array_type>()) { |
| 133 | + bp::class_<array_type> cl(class_name.c_str(), doc_string.c_str()); |
| 134 | + cl.def(bp::init<const array_type &>(bp::args("self", "other"), |
| 135 | + "Copy constructor")); |
| 136 | + |
| 137 | + array_indexing_suite<array_type, NoProxy, SliceAllocator> indexing_suite; |
| 138 | + cl.def(indexing_suite) |
| 139 | + .def(visitor) |
| 140 | + .def("tolist", tolist, bp::arg("self"), |
| 141 | + "Returns the std::array as a Python list."); |
| 142 | + } |
| 143 | + } |
| 144 | +}; |
| 145 | + |
| 146 | +/// Exposes std::array<MatrixType, Size> |
| 147 | +template <typename MatrixType, std::size_t Size> |
| 148 | +void exposeStdArrayEigenSpecificType(const char *name) { |
| 149 | + std::ostringstream oss; |
| 150 | + oss << "StdArr"; |
| 151 | + oss << Size << "_" << name; |
| 152 | + typedef std::array<MatrixType, Size> array_type; |
| 153 | + StdArrayPythonVisitor<array_type, false, |
| 154 | + Eigen::aligned_allocator<MatrixType> >:: |
| 155 | + expose(oss.str(), |
| 156 | + details::overload_base_get_item_for_std_vector<array_type>()); |
| 157 | +} |
| 158 | + |
| 159 | +} // namespace eigenpy |
| 160 | + |
| 161 | +#endif // ifndef __eigenpy_utils_std_array_hpp__ |
0 commit comments