cartobase  5.0.5
nditerator.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 #ifndef CARTOBASE_CONTAINER_NDITERATOR_H
35 #define CARTOBASE_CONTAINER_NDITERATOR_H
36 
38 #include <vector>
39 
40 
41 namespace carto
42 {
43 
50  {
51  public:
52  NDIterator_base( const std::vector<int> & dims );
53  NDIterator_base( const std::vector<int> & dims,
54  const std::vector<int> & strides );
55  NDIterator_base( const std::vector<int> & dims,
56  const std::vector<size_t> & strides );
57  NDIterator_base( const std::vector<int> & dims,
58  const std::vector<long> & strides );
59  // virtual ~NDIterator_base() {}
60  const std::vector<int> & position() const;
61  uint64_t offset() const;
62 
64 
65  bool ended() const;
66  void reset();
67 
68  protected:
69  template <typename T>
70  static std::vector<size_t> to_int_v( const std::vector<T> & v );
71 
72  std::vector<int> _dims;
73  std::vector<size_t> _strides;
74  std::vector<int> _position;
75  uint64_t _offset;
76  bool _ended;
77  };
78 
79 
81  {
82  public:
83  line_NDIterator_base( const std::vector<int> & dims );
84  line_NDIterator_base( const std::vector<int> & dims,
85  const std::vector<int> & strides );
86  line_NDIterator_base( const std::vector<int> & dims,
87  const std::vector<size_t> & strides );
88  line_NDIterator_base( const std::vector<int> & dims,
89  const std::vector<long> & strides );
90  // virtual ~line_NDIterator_base() {}
91 
93  };
94 
95 
118  template <typename T> class NDIterator : public NDIterator_base
119  {
120  public:
121  NDIterator( T* buffer, const std::vector<int> & dims );
122  NDIterator( T* buffer, const std::vector<int> & dims,
123  const std::vector<int> & strides );
124  NDIterator( T*buffer, const std::vector<int> & dims,
125  const std::vector<size_t> & strides );
126  NDIterator( T* buffer, const std::vector<int> & dims,
127  const std::vector<long> & strides );
128  // virtual ~NDIterator() {}
129 
130  T & operator * () const;
131 
132  protected:
134  };
135 
136 
138  template <typename T> class const_NDIterator : public NDIterator_base
139  {
140  public:
141  const_NDIterator( const T* buffer, const std::vector<int> & dims );
142  const_NDIterator( const T* buffer, const std::vector<int> & dims,
143  const std::vector<int> & strides );
144  const_NDIterator( const T* buffer, const std::vector<int> & dims,
145  const std::vector<size_t> & strides );
146  const_NDIterator( const T* buffer, const std::vector<int> & dims,
147  const std::vector<long> & strides );
148  // virtual ~const_NDIterator() {}
149 
150  const T & operator * () const;
151 
152  protected:
153  const T* _buffer;
154  };
155 
156 
183  template <typename T> class line_NDIterator : public line_NDIterator_base
184  {
185  public:
186  line_NDIterator( T* buffer, const std::vector<int> & dims );
187  line_NDIterator( T* buffer, const std::vector<int> & dims,
188  const std::vector<int> & strides );
189  line_NDIterator( T* buffer, const std::vector<int> & dims,
190  const std::vector<size_t> & strides );
191  line_NDIterator( T* buffer, const std::vector<int> & dims,
192  const std::vector<long> & strides );
193  // virtual ~line_NDIterator() {}
194 
195  T & operator * () const;
196 
197  protected:
199  };
200 
201 
203  template <typename T> class const_line_NDIterator
204  : public line_NDIterator_base
205  {
206  public:
207  const_line_NDIterator( const T* buffer, const std::vector<int> & dims );
208  const_line_NDIterator( const T* buffer, const std::vector<int> & dims,
209  const std::vector<int> & strides );
210  const_line_NDIterator( const T* buffer, const std::vector<int> & dims,
211  const std::vector<size_t> & strides );
212  const_line_NDIterator( const T* buffer, const std::vector<int> & dims,
213  const std::vector<long> & strides );
214  // virtual ~const_line_NDIterator() {}
215 
216  const T & operator * () const;
217 
218  protected:
219  const T* _buffer;
220  };
221 
222 
223  // -- implementation --
224 
225  inline NDIterator_base::NDIterator_base( const std::vector<int> & dims )
226  : _dims( dims ), _position( dims.size(), 0 ), _offset( 0 ), _ended( false )
227  {
228  if( dims.empty() )
229  _ended = true;
230  }
231 
232 
233  inline NDIterator_base::NDIterator_base( const std::vector<int> & dims,
234  const std::vector<int> & strides )
235  : _dims( dims ), _strides( NDIterator_base::to_int_v( strides ) ),
236  _position( dims.size(), 0 ),
237  _offset( 0 ), _ended( false )
238  {
239  if( dims.empty() )
240  _ended = true;
241  }
242 
243 
244  inline NDIterator_base::NDIterator_base( const std::vector<int> & dims,
245  const std::vector<size_t> & strides )
246  : _dims( dims ), _strides( strides ), _position( dims.size(), 0 ),
247  _offset( 0 ), _ended( false )
248  {
249  if( dims.empty() )
250  _ended = true;
251  }
252 
253 
254  inline NDIterator_base::NDIterator_base( const std::vector<int> & dims,
255  const std::vector<long> & strides )
256  : _dims( dims ), _strides( NDIterator_base::to_int_v( strides ) ),
257  _position( dims.size(), 0 ),
258  _offset( 0 ), _ended( false )
259  {
260  if( dims.empty() )
261  _ended = true;
262  }
263 
264 
265  inline const std::vector<int> & NDIterator_base::position() const
266  {
267  return _position;
268  }
269 
270 
271  inline uint64_t NDIterator_base::offset() const
272  {
273  return _offset;
274  }
275 
276 
277  inline bool NDIterator_base::ended() const
278  {
279  return _ended;
280  }
281 
282 
284  {
285  _offset = 0;
286  _position = std::vector<int>( _dims.size(), 0 );
287  }
288 
289 
290  template <typename T> inline
291  std::vector<size_t> NDIterator_base::to_int_v( const std::vector<T> & v )
292  {
293  std::vector<size_t> ov( v.size() );
294  std::vector<size_t>::iterator io = ov.begin();
295  typename std::vector<T>::const_iterator i, e = v.end();
296  for( i=v.begin(); i!=e; ++i, ++io )
297  *io = (size_t) *i;
298  return ov;
299  }
300 
301 
303  {
304  size_t dim, ndim = _dims.size();
305  bool nextrow = true, stride = !_strides.empty();
306  for( dim=0; nextrow && dim<ndim; ++dim )
307  {
308  if( nextrow )
309  {
310  ++_position[dim];
311  if( stride )
312  _offset += _strides[dim];
313  if( _position[dim] == _dims[dim] )
314  {
315  if( dim == ndim - 1 )
316  _ended = true;
317  _position[dim] = 0;
318  if( stride )
319  _offset -= _strides[dim] * _dims[dim];
320  }
321  else
322  nextrow = false;
323  }
324  }
325  return *this;
326  }
327 
328  // --
329 
331  const std::vector<int> & dims )
332  : NDIterator_base( dims )
333  {
334  if( dims.size() < 2 )
335  _ended = true;
336  }
337 
338 
340  const std::vector<int> & dims, const std::vector<int> & strides )
341  : NDIterator_base( dims, strides )
342  {
343  if( dims.size() < 2 )
344  _ended = true;
345  }
346 
347 
349  const std::vector<int> & dims, const std::vector<size_t> & strides )
350  : NDIterator_base( dims, strides )
351  {
352  if( dims.size() < 2 )
353  _ended = true;
354  }
355 
356 
358  const std::vector<int> & dims, const std::vector<long> & strides )
359  : NDIterator_base( dims, strides )
360  {
361  if( dims.size() < 2 )
362  _ended = true;
363  }
364 
365 
367  {
368  size_t dim, ndim = _dims.size();
369  bool nextrow = true, stride = !_strides.empty();
370  for( dim=1; nextrow && dim<ndim; ++dim )
371  {
372  if( nextrow )
373  {
374  ++_position[dim];
375  if( stride )
376  _offset += _strides[dim];
377  if( _position[dim] == _dims[dim] )
378  {
379  if( dim == ndim - 1 )
380  _ended = true;
381  _position[dim] = 0;
382  if( stride )
383  _offset -= _strides[dim] * _dims[dim];
384  }
385  else
386  nextrow = false;
387  }
388  }
389  return *this;
390  }
391 
392  // --
393 
394  template <typename T> inline
395  NDIterator<T>::NDIterator( T* buffer, const std::vector<int> & dims )
396  : NDIterator_base( dims ), _buffer( buffer )
397  {
398  }
399 
400 
401  template <typename T> inline
402  NDIterator<T>::NDIterator( T* buffer, const std::vector<int> & dims,
403  const std::vector<int> & strides )
404  : NDIterator_base( dims, strides ), _buffer( buffer )
405  {
406  }
407 
408 
409  template <typename T> inline
410  NDIterator<T>::NDIterator( T* buffer, const std::vector<int> & dims,
411  const std::vector<size_t> & strides )
412  : NDIterator_base( dims, strides ), _buffer( buffer )
413  {
414  }
415 
416 
417  template <typename T> inline
418  NDIterator<T>::NDIterator( T* buffer, const std::vector<int> & dims,
419  const std::vector<long> & strides )
420  : NDIterator_base( dims, strides ), _buffer( buffer )
421  {
422  }
423 
424 
425  template <typename T> inline T & NDIterator<T>::operator * () const
426  {
427  return _buffer[ _offset ];
428  }
429 
430  // --
431 
432  template <typename T> inline
434  const T* buffer, const std::vector<int> & dims )
435  : NDIterator_base( dims ), _buffer( buffer )
436  {
437  }
438 
439 
440  template <typename T> inline
442  const T* buffer, const std::vector<int> & dims,
443  const std::vector<int> & strides )
444  : NDIterator_base( dims, strides ), _buffer( buffer )
445  {
446  }
447 
448 
449  template <typename T> const inline T &
451  {
452  return _buffer[ _offset ];
453  }
454 
455  // --
456 
457  template <typename T> inline
459  T* buffer, const std::vector<int> & dims )
460  : line_NDIterator_base( dims ), _buffer( buffer )
461  {
462  }
463 
464 
465  template <typename T> inline
466  line_NDIterator<T>::line_NDIterator( T* buffer, const std::vector<int> & dims,
467  const std::vector<int> & strides )
468  : line_NDIterator_base( dims, strides ), _buffer( buffer )
469  {
470  }
471 
472 
473  template <typename T> inline
474  line_NDIterator<T>::line_NDIterator( T* buffer, const std::vector<int> & dims,
475  const std::vector<size_t> & strides )
476  : line_NDIterator_base( dims, strides ), _buffer( buffer )
477  {
478  }
479 
480 
481  template <typename T> inline
482  line_NDIterator<T>::line_NDIterator( T* buffer, const std::vector<int> & dims,
483  const std::vector<long> & strides )
484  : line_NDIterator_base( dims, strides ), _buffer( buffer )
485  {
486  }
487 
488 
489  template <typename T> inline T & line_NDIterator<T>::operator * () const
490  {
491  return _buffer[ _offset ];
492  }
493 
494  // --
495 
496  template <typename T> inline
498  const T* buffer, const std::vector<int> & dims )
499  : line_NDIterator_base( dims ), _buffer( buffer )
500  {
501  }
502 
503 
504  template <typename T> inline
506  const T* buffer, const std::vector<int> & dims,
507  const std::vector<int> & strides )
508  : line_NDIterator_base( dims, strides ), _buffer( buffer )
509  {
510  }
511 
512 
513  template <typename T> inline
515  const T* buffer, const std::vector<int> & dims,
516  const std::vector<size_t> & strides )
517  : line_NDIterator_base( dims, strides ), _buffer( buffer )
518  {
519  }
520 
521 
522  template <typename T> inline
524  const T* buffer, const std::vector<int> & dims,
525  const std::vector<long> & strides )
526  : line_NDIterator_base( dims, strides ), _buffer( buffer )
527  {
528  }
529 
530 
531  template <typename T> inline const T &
533  {
534  return _buffer[ _offset ];
535  }
536 
537 }
538 
539 #endif
line_NDIterator(T *buffer, const std::vector< int > &dims)
Definition: nditerator.h:458
N-dimensional array line iterator.
Definition: nditerator.h:183
std::vector< int > _dims
Definition: nditerator.h:72
const T & operator*() const
Definition: nditerator.h:450
bool ended() const
Definition: nditerator.h:277
T & operator*() const
Definition: nditerator.h:489
uint64_t offset() const
Definition: nditerator.h:271
const T & operator*() const
Definition: nditerator.h:532
NDIterator(T *buffer, const std::vector< int > &dims)
Definition: nditerator.h:395
const_line_NDIterator(const T *buffer, const std::vector< int > &dims)
Definition: nditerator.h:497
line_NDIterator_base(const std::vector< int > &dims)
Definition: nditerator.h:330
const_NDIterator(const T *buffer, const std::vector< int > &dims)
Definition: nditerator.h:433
static std::vector< size_t > to_int_v(const std::vector< T > &v)
Definition: nditerator.h:291
VoxelRGB operator*(const VoxelRGB &aa, const uint8_t &bb)
Definition: voxelrgb_def.h:637
const variant of the line_NDIterator
Definition: nditerator.h:203
NDIterator_base & operator++()
Definition: nditerator.h:302
NDIterator_base & operator++()
Definition: nditerator.h:366
const variant of the NDIterator
Definition: nditerator.h:138
const std::vector< int > & position() const
Definition: nditerator.h:265
N-dimensional array iterator.
Definition: nditerator.h:118
std::vector< int > _position
Definition: nditerator.h:74
Base class for N-dimensional array iterators.
Definition: nditerator.h:49
std::vector< size_t > _strides
Definition: nditerator.h:73
T & operator*() const
Definition: nditerator.h:425
NDIterator_base(const std::vector< int > &dims)
Definition: nditerator.h:225