1515#include " pybind11/pybind11.h"
1616#include " pybind11/common.h"
1717#include " pybind11/complex.h"
18- // Because of layout, else xiterator and xtensor_forward are sufficient
18+
19+ // Because of layout, otherwise xiterator and xtensor_forward are sufficient
1920#include " xtensor/xcontainer.hpp"
2021
2122namespace xt
@@ -144,26 +145,11 @@ namespace xt
144145 pycontainer (pycontainer&&) = default ;
145146 pycontainer& operator =(pycontainer&&) = default ;
146147
147- void fill_default_strides (const shape_type& shape,
148- layout l,
149- strides_type& strides);
150-
151148 static derived_type ensure (pybind11::handle h);
152149 static bool check_ (pybind11::handle h);
153150 static PyObject* raw_array_t (PyObject* ptr);
154151
155152 PyArrayObject* python_array ();
156-
157- private:
158-
159- template <size_t dim = 0 >
160- size_type data_offset (const inner_strides_type&) const ;
161-
162- template <size_t dim = 0 , class ... Args>
163- size_type data_offset (const inner_strides_type& strides, size_type i, Args... args) const ;
164-
165- template <class It >
166- size_type element_offset (const inner_strides_type& strides, It first, It last) const ;
167153 };
168154
169155 namespace detail
@@ -241,36 +227,6 @@ namespace xt
241227 throw pybind11::error_already_set ();
242228 }
243229
244- template <class D >
245- inline void pycontainer<D>::fill_default_strides(const shape_type& shape, layout l, strides_type& strides)
246- {
247- typename strides_type::value_type data_size = 1 ;
248- if (l == layout::row_major)
249- {
250- for (size_type i = strides.size (); i != 0 ; --i)
251- {
252- strides[i - 1 ] = data_size;
253- data_size = strides[i - 1 ] * shape[i - 1 ];
254- if (shape[i - 1 ] == 1 )
255- {
256- strides[i - 1 ] = 0 ;
257- }
258- }
259- }
260- else
261- {
262- for (size_type i = 0 ; i < strides.size (); ++i)
263- {
264- strides[i] = data_size;
265- data_size = strides[i] * shape[i];
266- if (shape[i] == 1 )
267- {
268- strides[i] = 0 ;
269- }
270- }
271- }
272- }
273-
274230 template <class D >
275231 inline auto pycontainer<D>::ensure(pybind11::handle h) -> derived_type
276232 {
@@ -306,29 +262,6 @@ namespace xt
306262 return reinterpret_cast <PyArrayObject*>(this ->m_ptr );
307263 }
308264
309- template <class D >
310- template <size_t dim>
311- inline auto pycontainer<D>::data_offset(const inner_strides_type&) const -> size_type
312- {
313- return 0 ;
314- }
315-
316- template <class D >
317- template <size_t dim, class ... Args>
318- inline auto pycontainer<D>::data_offset(const inner_strides_type& strides, size_type i, Args... args) const -> size_type
319- {
320- return i * strides[dim] + data_offset<dim + 1 >(strides, args...);
321- }
322-
323- template <class D >
324- template <class It >
325- inline auto pycontainer<D>::element_offset(const inner_strides_type& strides, It, It last) const -> size_type
326- {
327- It first = last;
328- first -= strides.size ();
329- return std::inner_product (strides.begin (), strides.end (), first, size_type (0 ));
330- }
331-
332265 template <class D >
333266 inline auto pycontainer<D>::size() const -> size_type
334267 {
@@ -344,7 +277,7 @@ namespace xt
344277 template <class D >
345278 inline auto pycontainer<D>::shape() const -> const inner_shape_type&
346279 {
347- return static_cast <const derived_type*>(this )-> shape_impl ();
280+ return static_cast <const derived_type*>(this )->shape_impl ();
348281 }
349282
350283 template <class D >
@@ -372,7 +305,7 @@ namespace xt
372305 inline void pycontainer<D>::reshape(const shape_type& shape, layout l)
373306 {
374307 strides_type strides (shape.size ());
375- fill_default_strides (shape, l, strides);
308+ compute_strides (shape, l, strides);
376309 reshape (shape, strides);
377310 }
378311
@@ -387,15 +320,15 @@ namespace xt
387320 template <class ... Args>
388321 inline auto pycontainer<D>::operator ()(Args... args) -> reference
389322 {
390- size_type index = data_offset (strides (), static_cast <size_type>(args)...);
323+ size_type index = data_offset<size_type> (strides (), static_cast <size_type>(args)...);
391324 return data ()[index];
392325 }
393326
394327 template <class D >
395328 template <class ... Args>
396329 inline auto pycontainer<D>::operator ()(Args... args) const -> const_reference
397330 {
398- size_type index = data_offset (strides (), static_cast <size_type>(args)...);
331+ size_type index = data_offset<size_type> (strides (), static_cast <size_type>(args)...);
399332 return data ()[index];
400333 }
401334
@@ -415,14 +348,14 @@ namespace xt
415348 template <class It >
416349 inline auto pycontainer<D>::element(It first, It last) -> reference
417350 {
418- return data ()[element_offset (strides (), first, last)];
351+ return data ()[element_offset<size_type> (strides (), first, last)];
419352 }
420353
421354 template <class D >
422355 template <class It >
423356 inline auto pycontainer<D>::element(It first, It last) const -> const_reference
424357 {
425- return data ()[element_offset (strides (), first, last)];
358+ return data ()[element_offset<size_type> (strides (), first, last)];
426359 }
427360
428361 template <class D >
@@ -602,7 +535,6 @@ namespace xt
602535 size_type offset = shape.size () - dimension ();
603536 return const_stepper (static_cast <const derived_type*>(this ), data ().end (), offset);
604537 }
605-
606538}
607539
608540#endif
0 commit comments