@@ -41,8 +41,10 @@ namespace xt
4141 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
4242 using reverse_iterator = const_reverse_iterator;
4343
44+ using shape_type = size_t *;
45+
4446 pystrides_adaptor () = default ;
45- pystrides_adaptor (const_pointer data, size_type size);
47+ pystrides_adaptor (const_pointer data, size_type size, shape_type shape );
4648
4749 bool empty () const noexcept ;
4850 size_type size () const noexcept ;
@@ -66,6 +68,7 @@ namespace xt
6668
6769 const_pointer p_data;
6870 size_type m_size;
71+ shape_type p_shape;
6972 };
7073
7174 /* *********************************
@@ -84,21 +87,23 @@ namespace xt
8487 using reference = typename pystrides_adaptor<N>::const_reference;
8588 using difference_type = typename pystrides_adaptor<N>::difference_type;
8689 using iterator_category = std::random_access_iterator_tag;
90+ using shape_pointer = typename pystrides_adaptor<N>::shape_type;
8791
88- inline pystrides_iterator (pointer current)
92+ inline pystrides_iterator (pointer current, shape_pointer shape )
8993 : p_current(current)
94+ , p_shape(shape)
9095 {
9196 }
9297
9398 inline reference operator *() const
9499 {
95- return *p_current / N;
100+ return *p_shape == size_t ( 1 ) ? 0 : * p_current / N;
96101 }
97102
98103 inline pointer operator ->() const
99104 {
100105 // Returning the address of a temporary
101- value_type res = *p_current / N ;
106+ value_type res = this -> operator *() ;
102107 return &res;
103108 }
104109
@@ -110,49 +115,55 @@ namespace xt
110115 inline self_type& operator ++()
111116 {
112117 ++p_current;
118+ ++p_shape;
113119 return *this ;
114120 }
115121
116122 inline self_type& operator --()
117123 {
118124 --p_current;
125+ --p_shape;
119126 return *this ;
120127 }
121128
122129 inline self_type operator ++(int )
123130 {
124131 self_type tmp (*this );
125132 ++p_current;
133+ ++p_shape;
126134 return tmp;
127135 }
128136
129137 inline self_type operator --(int )
130138 {
131139 self_type tmp (*this );
132140 --p_current;
141+ --p_shape;
133142 return tmp;
134143 }
135144
136145 inline self_type& operator +=(difference_type n)
137146 {
138147 p_current += n;
148+ p_shape += n;
139149 return *this ;
140150 }
141151
142152 inline self_type& operator -=(difference_type n)
143153 {
144154 p_current -= n;
155+ p_shape -= n;
145156 return *this ;
146157 }
147158
148159 inline self_type operator +(difference_type n) const
149160 {
150- return self_type (p_current + n);
161+ return self_type (p_current + n, p_shape + n );
151162 }
152163
153164 inline self_type operator -(difference_type n) const
154165 {
155- return self_type (p_current - n);
166+ return self_type (p_current - n, p_shape - n );
156167 }
157168
158169 inline difference_type operator -(const self_type& rhs) const
@@ -166,6 +177,7 @@ namespace xt
166177 private:
167178
168179 pointer p_current;
180+ shape_pointer p_shape;
169181 };
170182
171183 template <std::size_t N>
@@ -215,8 +227,8 @@ namespace xt
215227 ************************************/
216228
217229 template <std::size_t N>
218- inline pystrides_adaptor<N>::pystrides_adaptor(const_pointer data, size_type size)
219- : p_data(data), m_size(size)
230+ inline pystrides_adaptor<N>::pystrides_adaptor(const_pointer data, size_type size, shape_type shape )
231+ : p_data(data), m_size(size), p_shape(shape)
220232 {
221233 }
222234
@@ -235,19 +247,19 @@ namespace xt
235247 template <std::size_t N>
236248 inline auto pystrides_adaptor<N>::operator [](size_type i) const -> const_reference
237249 {
238- return p_data[i] / N;
250+ return p_shape[i] == size_t ( 1 ) ? 0 : p_data[i] / N;
239251 }
240252
241253 template <std::size_t N>
242254 inline auto pystrides_adaptor<N>::front() const -> const_reference
243255 {
244- return p_data[ 0 ] / N ;
256+ return this -> operator []( 0 ) ;
245257 }
246258
247259 template <std::size_t N>
248260 inline auto pystrides_adaptor<N>::back() const -> const_reference
249261 {
250- return p_data[ m_size - 1 ] / N ;
262+ return this -> operator []( m_size - 1 ) ;
251263 }
252264
253265 template <std::size_t N>
@@ -265,13 +277,13 @@ namespace xt
265277 template <std::size_t N>
266278 inline auto pystrides_adaptor<N>::cbegin() const -> const_iterator
267279 {
268- return const_iterator (p_data);
280+ return const_iterator (p_data, p_shape );
269281 }
270282
271283 template <std::size_t N>
272284 inline auto pystrides_adaptor<N>::cend() const -> const_iterator
273285 {
274- return const_iterator (p_data + m_size);
286+ return const_iterator (p_data + m_size, p_shape + m_size );
275287 }
276288
277289 template <std::size_t N>
0 commit comments