std-array.hpp
Go to the documentation of this file.
1 
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 
13 namespace eigenpy {
14 
15 template <typename Container, bool NoProxy, class SliceAllocator,
16  class DerivedPolicies>
18 namespace details {
19 
20 template <typename Container, bool NoProxy, class SliceAllocator>
22  : public array_indexing_suite<
23  Container, NoProxy, SliceAllocator,
24  final_array_derived_policies<Container, NoProxy, SliceAllocator> > {};
25 } // namespace details
26 
27 template <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());
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 
109 template <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) {
134  if (!register_symbolic_link_to_registered_type<array_type>()) {
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 
151 template <typename MatrixType, std::size_t Size>
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__
eigenpy::exposeStdArrayEigenSpecificType
void exposeStdArrayEigenSpecificType(const char *name)
Exposes std::array<MatrixType, Size>
Definition: std-array.hpp:152
eigenpy::StdArrayPythonVisitor::expose
static void expose(const std::string &class_name, const bp::def_visitor< DerivedVisitor > &visitor)
Definition: std-array.hpp:125
eigenpy::array_indexing_suite::delete_slice
static void delete_slice(Container &, index_type, index_type)
Definition: std-array.hpp:53
fill
void fill(Eigen::Ref< MatType > mat, const typename MatType::Scalar &value)
Definition: eigen_ref.cpp:62
eigenpy::array_indexing_suite::slice_vector_type
std::vector< data_type, SliceAllocator > slice_vector_type
Definition: std-array.hpp:39
eigenpy::StdArrayPythonVisitor
Expose an std::array (a C++11 fixed-size array) from a given type.
Definition: std-array.hpp:112
eigenpy::array_indexing_suite::delete_item
static void delete_item(Container &, index_type)
Definition: std-array.hpp:46
eigenpy::StdArrayPythonVisitor::tolist
::boost::python::list tolist(array_type &self, const bool deep_copy)
Definition: std-array.hpp:115
eigenpy
Definition: alignment.hpp:14
eigenpy::array_indexing_suite::Size
static constexpr std::size_t Size
Definition: std-array.hpp:40
eigenpy::array_indexing_suite
Definition: std-array.hpp:17
eigenpy::details::overload_base_get_item_for_std_vector
Change the behavior of indexing (method getitem in Python). This is suitable for container of Eigen m...
Definition: std-vector.hpp:78
copy
ReturnMatrix copy(const Eigen::MatrixBase< Matrix > &mat)
Definition: matrix.cpp:131
eigenpy::EmptyPythonVisitor
Definition: empty-visitor.hpp:8
eigenpy::StdArrayPythonVisitor::value_type
array_type::value_type value_type
Definition: std-array.hpp:113
eigenpy::IdVisitor
Add the Python method id to retrieving a unique id for a given object exposed with Boost....
Definition: id.hpp:18
eigenpy::array_indexing_suite::size_type
Container::size_type size_type
Definition: std-array.hpp:37
eigenpy::array_indexing_suite::difference_type
Container::difference_type difference_type
Definition: std-array.hpp:38
eigenpy::expose
void expose()
Call the expose function of a given type T.
Definition: expose.hpp:23
eigenpy::array_indexing_suite::data_type
Container::value_type data_type
Definition: std-array.hpp:34
setup.name
name
Definition: setup.in.py:179
eigenpy::StdArrayPythonVisitor::expose
static void expose(const std::string &class_name, const std::string &doc_string="")
Definition: std-array.hpp:119
eigenpy::array_indexing_suite::set_slice
static void set_slice(Container &container, index_type from, index_type to, Iter first, Iter last)
Definition: std-array.hpp:72
eigenpy::array_indexing_suite::index_type
Container::size_type index_type
Definition: std-array.hpp:36
eigenpy::details::build_list::run
::boost::python::list run(vector_type &vec, const bool deep_copy)
Definition: std-vector.hpp:55
eigenpy::array_indexing_suite::get_slice
static bp::object get_slice(Container &container, index_type from, index_type to)
Definition: std-array.hpp:91
eigenpy::StdArrayPythonVisitor::expose
static void expose(const std::string &class_name, const std::string &doc_string, const bp::def_visitor< DerivedVisitor > &visitor)
Definition: std-array.hpp:131
test_geometry.v
v
Definition: test_geometry.py:32
std-vector.hpp
eigenpy::details::final_array_derived_policies
Definition: std-array.hpp:21
eigenpy::array_indexing_suite::extension_def
static void extension_def(Class &)
Definition: std-array.hpp:43
eigenpy::array_indexing_suite::set_slice
static void set_slice(Container &container, index_type from, index_type to, data_type const &v)
Definition: std-array.hpp:59
eigenpy::array_indexing_suite::key_type
Container::value_type key_type
Definition: std-array.hpp:35


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Sat Nov 2 2024 02:14:45