eigenpy 3.12.0
Bindings between Numpy and Eigen using Boost.Python
Loading...
Searching...
No Matches
std-array.hpp
1
4
5#ifndef __eigenpy_utils_std_array_hpp__
6#define __eigenpy_utils_std_array_hpp__
7
8#include <boost/python/suite/indexing/indexing_suite.hpp>
9#include "eigenpy/std-vector.hpp"
10
11#include <array>
12
13namespace eigenpy {
14
15template <typename Container, bool NoProxy, class SliceAllocator,
16 class DerivedPolicies>
18namespace details {
19
20template <typename Container, bool NoProxy, class SliceAllocator>
23 Container, NoProxy, SliceAllocator,
24 final_array_derived_policies<Container, NoProxy, SliceAllocator>> {};
25} // namespace details
26
27template <typename Container, bool NoProxy = false,
28 class SliceAllocator = std::allocator<typename Container::value_type>,
29 class DerivedPolicies = details::final_array_derived_policies<
30 Container, NoProxy, SliceAllocator>>
32 : public bp::vector_indexing_suite<Container, NoProxy, DerivedPolicies> {
33 public:
34 typedef typename Container::value_type data_type;
35 typedef typename Container::value_type key_type;
36 typedef typename Container::size_type index_type;
37 typedef typename Container::size_type size_type;
38 typedef typename Container::difference_type difference_type;
39 typedef std::vector<data_type, SliceAllocator> slice_vector_type;
40 static constexpr std::size_t Size = std::tuple_size<Container>{};
41
42 template <class Class>
43 static void extension_def(Class &) {}
44
45 // throws exception
46 static void delete_item(Container &, index_type) {
47 PyErr_SetString(PyExc_NotImplementedError,
48 "Cannot delete item from std::array type.");
49 bp::throw_error_already_set();
50 }
51
52 // throws exception
53 static void delete_slice(Container &, index_type, index_type) {
54 PyErr_SetString(PyExc_NotImplementedError,
55 "Cannot delete slice from std::array type.");
56 bp::throw_error_already_set();
57 }
58
59 static void set_slice(Container &container, index_type from, index_type to,
60 data_type const &v) {
61 if (from >= to) {
62 PyErr_SetString(PyExc_NotImplementedError,
63 "Setting this slice would insert into an std::array, "
64 "which is not supported.");
65 bp::throw_error_already_set();
66 } else {
67 std::fill(container.begin() + from, container.begin() + to, v);
68 }
69 }
70
71 template <class Iter>
72 static void set_slice(Container &container, index_type from, index_type to,
73 Iter first, Iter last) {
74 if (from >= to) {
75 PyErr_SetString(PyExc_NotImplementedError,
76 "Setting this slice would insert into an std::array, "
77 "which is not supported.");
78 bp::throw_error_already_set();
79 } else {
80 if (long(to - from) == std::distance(first, last)) {
81 std::copy(first, last, container.begin() + from);
82 } else {
83 PyErr_SetString(PyExc_NotImplementedError,
84 "Size of std::array slice and size of right-hand side "
85 "iterator are incompatible.");
86 bp::throw_error_already_set();
87 }
88 }
89 }
90
91 static bp::object get_slice(Container &container, index_type from,
92 index_type to) {
93 if (from > to) return bp::object(slice_vector_type());
94 slice_vector_type out;
95 for (size_t i = from; i < to; i++) {
96 out.push_back(container[i]);
97 }
98 return bp::object(std::move(out));
99 }
100};
101
109template <typename array_type, bool NoProxy = false,
110 class SliceAllocator =
111 std::allocator<typename array_type::value_type>>
113 typedef typename array_type::value_type value_type;
114
115 static ::boost::python::list tolist(array_type &self, const bool deep_copy) {
116 return details::build_list<array_type, NoProxy>::run(self, deep_copy);
117 }
118
119 static void expose(const std::string &class_name,
120 const std::string &doc_string = "") {
121 expose(class_name, doc_string, EmptyPythonVisitor());
122 }
123
124 template <typename DerivedVisitor>
125 static void expose(const std::string &class_name,
126 const bp::def_visitor<DerivedVisitor> &visitor) {
127 expose(class_name, "", visitor);
128 }
129
130 template <typename DerivedVisitor>
131 static void expose(const std::string &class_name,
132 const std::string &doc_string,
133 const bp::def_visitor<DerivedVisitor> &visitor) {
135 bp::class_<array_type> cl(class_name.c_str(), doc_string.c_str());
136 cl.def(bp::init<const array_type &>(bp::args("self", "other"),
137 "Copy constructor"));
138 cl.def(IdVisitor<array_type>());
139
141 cl.def(indexing_suite)
142 .def(visitor)
143 .def("tolist", tolist,
144 (bp::arg("self"), bp::arg("deep_copy") = false),
145 "Returns the std::array as a Python list.");
146 }
147 }
148};
149
151template <typename MatrixType, std::size_t Size>
152void exposeStdArrayEigenSpecificType(const char *name) {
153 std::ostringstream oss;
154 oss << "StdArr";
155 oss << Size << "_" << name;
156 typedef std::array<MatrixType, Size> array_type;
157 StdArrayPythonVisitor<array_type, false,
158 Eigen::aligned_allocator<MatrixType>>::
159 expose(oss.str(),
161}
162
163} // namespace eigenpy
164
165#endif // ifndef __eigenpy_utils_std_array_hpp__
void expose()
Call the expose function of a given type T.
Definition expose.hpp:23
void exposeStdArrayEigenSpecificType(const char *name)
Exposes std::array<MatrixType, Size>
bool register_symbolic_link_to_registered_type()
Symlink to the current scope the already registered class T.
Add the Python method id to retrieving a unique id for a given object exposed with Boost....
Definition id.hpp:18
Expose an std::array (a C++11 fixed-size array) from a given type.
Change the behavior of indexing (method getitem in Python). This is suitable for container of Eigen m...