Skip to content

Commit 5eb466e

Browse files
committed
Use base xiterable
1 parent be9452a commit 5eb466e

File tree

3 files changed

+37
-115
lines changed

3 files changed

+37
-115
lines changed

include/xtensor-python/pyarray.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ namespace xt
8181
const array_type* p_a;
8282
};
8383

84+
template <class T>
85+
struct xiterable_inner_types<pyarray<T>>
86+
: pycontainer_iterable_types<pyarray<T>>
87+
{
88+
};
89+
8490
template <class T>
8591
struct xcontainer_inner_types<pyarray<T>>
8692
{

include/xtensor-python/pycontainer.hpp

Lines changed: 24 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace xt
2323
{
2424

2525
template <class D>
26-
class pycontainer : public pybind11::object
26+
class pycontainer : public pybind11::object, public xiterable<D>
2727
{
2828

2929
public:
@@ -46,14 +46,16 @@ namespace xt
4646
using inner_shape_type = typename inner_types::inner_shape_type;
4747
using inner_strides_type = typename inner_types::inner_strides_type;
4848

49-
using iterator = typename container_type::iterator;
50-
using const_iterator = typename container_type::const_iterator;
49+
using iterable_base = xiterable<D>;
5150

52-
using stepper = xstepper<D>;
53-
using const_stepper = xstepper<const D>;
51+
using iterator = typename iterable_base::iterator;
52+
using const_iterator = typename iterable_base::const_iterator;
5453

55-
using broadcast_iterator = xiterator<stepper, inner_shape_type*>;
56-
using const_broadcast_iterator = xiterator<const_stepper, inner_shape_type*>;
54+
using stepper = typename iterable_base::stepper;
55+
using const_stepper = typename iterable_base::const_stepper;
56+
57+
using broadcast_iterator = typename iterable_base::broadcast_iterator;
58+
using const_broadcast_iterator = typename iterable_base::broadcast_iterator;
5759

5860
size_type size() const;
5961
size_type dimension() const;
@@ -98,28 +100,6 @@ namespace xt
98100
const_iterator cbegin() const;
99101
const_iterator cend() const;
100102

101-
broadcast_iterator xbegin();
102-
broadcast_iterator xend();
103-
104-
const_broadcast_iterator xbegin() const;
105-
const_broadcast_iterator xend() const;
106-
const_broadcast_iterator cxbegin() const;
107-
const_broadcast_iterator cxend() const;
108-
109-
template <class S>
110-
xiterator<stepper, S> xbegin(const S& shape);
111-
template <class S>
112-
xiterator<stepper, S> xend(const S& shape);
113-
114-
template <class S>
115-
xiterator<const_stepper, S> xbegin(const S& shape) const;
116-
template <class S>
117-
xiterator<const_stepper, S> xend(const S& shape) const;
118-
template <class S>
119-
xiterator<const_stepper, S> cxbegin(const S& shape) const;
120-
template <class S>
121-
xiterator<const_stepper, S> cxend(const S& shape) const;
122-
123103
template <class S>
124104
stepper stepper_begin(const S& shape);
125105
template <class S>
@@ -152,6 +132,17 @@ namespace xt
152132
PyArrayObject* python_array();
153133
};
154134

135+
template <class D>
136+
struct pycontainer_iterable_types
137+
: xcontainer_iterable_types<D>
138+
{
139+
using stepper = xstepper<D>;
140+
using const_stepper = xstepper<const D>;
141+
using inner_shape_type = typename xcontainer_inner_types<D>::shape_type;
142+
using broadcast_iterator = xiterator<stepper, inner_shape_type*>;
143+
using const_broadcast_iterator = xiterator<const_stepper, inner_shape_type*>;
144+
};
145+
155146
namespace detail
156147
{
157148
// TODO : switch on pybind11::is_fmt_numeric when it is available
@@ -401,107 +392,25 @@ namespace xt
401392
template <class D>
402393
inline auto pycontainer<D>::begin() const -> const_iterator
403394
{
404-
return data().cbegin();
395+
return cbegin();
405396
}
406397

407398
template <class D>
408399
inline auto pycontainer<D>::end() const -> const_iterator
409400
{
410-
return data().cend();
401+
return cend();
411402
}
412403

413404
template <class D>
414405
inline auto pycontainer<D>::cbegin() const -> const_iterator
415406
{
416-
return begin();
407+
return data().cbegin();
417408
}
418409

419410
template <class D>
420411
inline auto pycontainer<D>::cend() const -> const_iterator
421412
{
422-
return end();
423-
}
424-
425-
template <class D>
426-
inline auto pycontainer<D>::xbegin() -> broadcast_iterator
427-
{
428-
const inner_shape_type& sh = shape();
429-
return broadcast_iterator(stepper_begin(sh), sh);
430-
}
431-
432-
template <class D>
433-
inline auto pycontainer<D>::xend() -> broadcast_iterator
434-
{
435-
const inner_shape_type& sh = shape();
436-
return broadcast_iterator(stepper_end(sh), sh);
437-
}
438-
439-
template <class D>
440-
inline auto pycontainer<D>::xbegin() const -> const_broadcast_iterator
441-
{
442-
const inner_shape_type& sh = shape();
443-
return const_broadcast_iterator(stepper_begin(sh), sh);
444-
}
445-
446-
template <class D>
447-
inline auto pycontainer<D>::xend() const -> const_broadcast_iterator
448-
{
449-
const inner_shape_type& sh = shape();
450-
return const_broadcast_iterator(stepper_end(sh), sh);
451-
}
452-
453-
template <class D>
454-
inline auto pycontainer<D>::cxbegin() const -> const_broadcast_iterator
455-
{
456-
return xbegin();
457-
}
458-
459-
template <class D>
460-
inline auto pycontainer<D>::cxend() const -> const_broadcast_iterator
461-
{
462-
return xend();
463-
}
464-
465-
template <class D>
466-
template <class S>
467-
inline auto pycontainer<D>::xbegin(const S& shape) -> xiterator<stepper, S>
468-
{
469-
return xiterator<stepper, S>(stepper_begin(shape), shape);
470-
}
471-
472-
template <class D>
473-
template <class S>
474-
inline auto pycontainer<D>::xend(const S& shape) -> xiterator<stepper, S>
475-
{
476-
return xiterator<stepper, S>(stepper_end(shape), shape);
477-
}
478-
479-
template <class D>
480-
template <class S>
481-
inline auto pycontainer<D>::xbegin(const S& shape) const -> xiterator<const_stepper, S>
482-
{
483-
return xiterator<const_stepper, S>(stepper_begin(shape), shape);
484-
}
485-
486-
template <class D>
487-
template <class S>
488-
inline auto pycontainer<D>::xend(const S& shape) const -> xiterator<const_stepper, S>
489-
{
490-
return xiterator<const_stepper, S>(stepper_end(shape), shape);
491-
}
492-
493-
template <class D>
494-
template <class S>
495-
inline auto pycontainer<D>::cxbegin(const S& shape) const -> xiterator<const_stepper, S>
496-
{
497-
return xbegin(shape);
498-
}
499-
500-
template <class D>
501-
template <class S>
502-
inline auto pycontainer<D>::cxend(const S& shape) const -> xiterator<const_stepper, S>
503-
{
504-
return xend(shape);
413+
return data().cend();
505414
}
506415

507416
template <class D>

include/xtensor-python/pytensor.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ namespace pybind11
6161

6262
namespace xt
6363
{
64+
65+
template <class T, std::size_t N>
66+
struct xiterable_inner_types<pytensor<T, N>>
67+
: pycontainer_iterable_types<pytensor<T, N>>
68+
{
69+
};
70+
6471
template <class T, std::size_t N>
6572
struct xcontainer_inner_types<pytensor<T, N>>
6673
{

0 commit comments

Comments
 (0)