cartobase 6.0.6
carto::line_NDIterator< T > Class Template Reference

N-dimensional array line iterator. More...

#include <cartobase/containers/nditerator.h>

Inheritance diagram for carto::line_NDIterator< T >:
Collaboration diagram for carto::line_NDIterator< T >:

Public Member Functions

 line_NDIterator (T *buffer, const std::vector< int > &dims)
 
 line_NDIterator (T *buffer, const std::vector< int > &dims, const std::vector< int > &strides, bool optimize_direction=false)
 the optional "optimize_direction" parameter allows the iterator to find a contiguous direction (with a stride == 1) and use it as the line dimension.
 
 line_NDIterator (T *buffer, const std::vector< int > &dims, const std::vector< std::size_t > &strides, bool optimize_direction=false)
 
 line_NDIterator (T *buffer, const std::vector< int > &dims, const std::vector< std::ptrdiff_t > &strides, bool optimize_direction=false)
 
T & operator* () const
 
T * operator-> () const
 
void inc_line_ptr (T *&p) const
 increment a "line" pointer (advancing through the line)
 
void inc_line_ptr (const T *&p) const
 increment a "line" pointer (advancing through the line)
 
std::ptrdiff_t line_length () const
 line_size * line stride: end of line
 
- Public Member Functions inherited from carto::line_NDIterator_base
 line_NDIterator_base (const std::vector< int > &dims)
 
 line_NDIterator_base (const std::vector< int > &dims, const std::vector< int > &strides, bool optimize_direction=false)
 
 line_NDIterator_base (const std::vector< int > &dims, const std::vector< std::size_t > &strides, bool optimize_direction=false)
 
 line_NDIterator_base (const std::vector< int > &dims, const std::vector< std::ptrdiff_t > &strides, bool optimize_direction=false)
 
NDIterator_baseoperator++ ()
 
int line_size () const
 
int line_direction () const
 
bool is_contiguous () const
 
- Public Member Functions inherited from carto::NDIterator_base
 NDIterator_base (const std::vector< int > &dims)
 
 NDIterator_base (const std::vector< int > &dims, const std::vector< int > &strides)
 
 NDIterator_base (const std::vector< int > &dims, const std::vector< std::size_t > &strides)
 
 NDIterator_base (const std::vector< int > &dims, const std::vector< std::ptrdiff_t > &strides)
 
const std::vector< int > & position () const
 
std::ptrdiff_t offset () const
 
NDIterator_baseoperator++ ()
 
bool ended () const
 
void reset ()
 

Protected Attributes

T * _buffer
 
- Protected Attributes inherited from carto::line_NDIterator_base
int _line_directon
 
std::ptrdiff_t _line_stride
 
bool _contiguous
 
- Protected Attributes inherited from carto::NDIterator_base
std::vector< int > _dims
 
std::vector< std::ptrdiff_t > _strides
 
std::vector< int > _position
 
std::ptrdiff_t _offset
 
bool _ended
 

Additional Inherited Members

- Protected Member Functions inherited from carto::line_NDIterator_base
void set_optimized_direction (bool optimize_direction=false)
 
- Static Protected Member Functions inherited from carto::NDIterator_base
template<typename T>
static std::vector< std::ptrdiff_t > to_ptrdiff_v (const std::vector< T > &v)
 

Detailed Description

template<typename T>
class carto::line_NDIterator< T >

N-dimensional array line iterator.

Iterates over every "line" of a N-D array, using strides. The "lines" are arrays over the smallest dimension of the array. Thus the iterator iterates over all dimensions but the smallest one. This allows to optimize code iterating directly on the smallest dimension, which is much more efficient than the regular NDIterator: for a very simple item operation using a line_NDIterator instead of a NDIterator can be about 20 times faster.

float *data; // fill it...
std::vector<int> dimensions;
std::vector<std::ptrdiff_t> strides;
// fill dimensions and strides...
float sum = 0.;
line_NDIterator<float> it( data, dimensions, strides );
float *p, *pp;
for( ; !it.ended(); ++it )
{
p = &*it;
for( pp=p + it.line_length(); p!=pp; it.inc_line_ptr( p ) )
sum += *p;
}
line_NDIterator(T *buffer, const std::vector< int > &dims)
Definition nditerator.h:583

Definition at line 201 of file nditerator.h.

Constructor & Destructor Documentation

◆ line_NDIterator() [1/4]

template<typename T>
carto::line_NDIterator< T >::line_NDIterator ( T * buffer,
const std::vector< int > & dims )
inline

Definition at line 583 of file nditerator.h.

References _buffer, and carto::line_NDIterator_base::line_NDIterator_base().

◆ line_NDIterator() [2/4]

template<typename T>
carto::line_NDIterator< T >::line_NDIterator ( T * buffer,
const std::vector< int > & dims,
const std::vector< int > & strides,
bool optimize_direction = false )
inline

the optional "optimize_direction" parameter allows the iterator to find a contiguous direction (with a stride == 1) and use it as the line dimension.

This way a line pointer can be incremented instead of adding strides. In many cases it does not change much in performance.

Definition at line 591 of file nditerator.h.

References _buffer, and carto::line_NDIterator_base::line_NDIterator_base().

◆ line_NDIterator() [3/4]

template<typename T>
carto::line_NDIterator< T >::line_NDIterator ( T * buffer,
const std::vector< int > & dims,
const std::vector< std::size_t > & strides,
bool optimize_direction = false )
inline

Definition at line 602 of file nditerator.h.

References _buffer, and carto::line_NDIterator_base::line_NDIterator_base().

◆ line_NDIterator() [4/4]

template<typename T>
carto::line_NDIterator< T >::line_NDIterator ( T * buffer,
const std::vector< int > & dims,
const std::vector< std::ptrdiff_t > & strides,
bool optimize_direction = false )
inline

Definition at line 613 of file nditerator.h.

References _buffer, and carto::line_NDIterator_base::line_NDIterator_base().

Member Function Documentation

◆ inc_line_ptr() [1/2]

template<typename T>
void carto::line_NDIterator< T >::inc_line_ptr ( const T *& p) const
inline

increment a "line" pointer (advancing through the line)

Definition at line 646 of file nditerator.h.

References carto::line_NDIterator_base::_contiguous, and carto::line_NDIterator_base::_line_stride.

◆ inc_line_ptr() [2/2]

template<typename T>
void carto::line_NDIterator< T >::inc_line_ptr ( T *& p) const
inline

increment a "line" pointer (advancing through the line)

Definition at line 636 of file nditerator.h.

References carto::line_NDIterator_base::_contiguous, and carto::line_NDIterator_base::_line_stride.

◆ line_length()

template<typename T>
std::ptrdiff_t carto::line_NDIterator< T >::line_length ( ) const
inline

◆ operator*()

template<typename T>
T & carto::line_NDIterator< T >::operator* ( ) const
inline

Definition at line 623 of file nditerator.h.

References _buffer, and carto::NDIterator_base::_offset.

◆ operator->()

template<typename T>
T * carto::line_NDIterator< T >::operator-> ( ) const
inline

Definition at line 629 of file nditerator.h.

References _buffer, and carto::NDIterator_base::_offset.

Member Data Documentation

◆ _buffer

template<typename T>
T* carto::line_NDIterator< T >::_buffer
protected

The documentation for this class was generated from the following file: