#include <ArrayNDScanner.h>
Public Member Functions | |
unsigned | dim () const |
void | getIndex (unsigned *index, unsigned indexBufferLen) const |
bool | isValid () const |
unsigned long | maxState () const |
void | operator++ (int) |
ArrayNDScanner & | operator++ () |
void | reset () |
void | setState (const unsigned long state) |
unsigned long | state () const |
ArrayNDScanner (const unsigned *shape, const unsigned lenShape) | |
ArrayNDScanner (const std::vector< unsigned > &shape) | |
Private Member Functions | |
ArrayNDScanner () | |
void | initialize (const unsigned *shape, unsigned lenShape) |
Private Attributes | |
unsigned | dim_ |
unsigned long | maxState_ |
unsigned long | state_ |
unsigned long | strides_ [CHAR_BIT *sizeof(unsigned long)] |
This class can be used to iterate over array indices without actually building the array or requesting any memory from the heap. Typical use:
for (ArrayNDScanner scanner(shape); scanner.isValid(); ++scanner) { scanner.getIndex(indexArray, indexArrayLen); .... Do what is necessary with multidimensional index .... .... Extract linear index: ............................... scanner.state(); }
This can be useful, for example, in case one needs to iterate over slices of some array (so that the array itself can not be used to obtain similar information easily).
Definition at line 36 of file ArrayNDScanner.h.
npstat::ArrayNDScanner::ArrayNDScanner | ( | const unsigned * | shape, |
const unsigned | lenShape | ||
) | [inline] |
Constructor from a multidimensional array shape
Definition at line 41 of file ArrayNDScanner.h.
References initialize().
{initialize(shape, lenShape);}
npstat::ArrayNDScanner::ArrayNDScanner | ( | const std::vector< unsigned > & | shape | ) | [inline] |
Definition at line 44 of file ArrayNDScanner.h.
References initialize().
{initialize(shape.empty() ? static_cast<unsigned*>(0) : &shape[0], shape.size());}
npstat::ArrayNDScanner::ArrayNDScanner | ( | ) | [private] |
unsigned npstat::ArrayNDScanner::dim | ( | ) | const [inline] |
Dimensionality of the scan
Definition at line 50 of file ArrayNDScanner.h.
References dim_.
{return dim_;}
void npstat::ArrayNDScanner::getIndex | ( | unsigned * | index, |
unsigned | indexBufferLen | ||
) | const |
Retrieve current multidimensional index
Definition at line 29 of file ArrayNDScanner.cc.
References dim_, i, customizeTrackingMonitorSeedNumber::idx, prof2calltree::l, maxState_, state_, and strides_.
{ if (indexBufferLen < dim_) throw npstat::NpstatInvalidArgument( "In npstat::ArrayNDScanner::getIndex: " "insufficient length of the output buffer"); if (state_ >= maxState_) throw npstat::NpstatRuntimeError( "In npstat::ArrayNDScanner::getIndex: invalid scanner state"); assert(ix); unsigned long l = state_; for (unsigned i=0; i<dim_; ++i) { unsigned long idx = l / strides_[i]; ix[i] = static_cast<unsigned>(idx); l -= (idx * strides_[i]); } }
void npstat::ArrayNDScanner::initialize | ( | const unsigned * | shape, |
unsigned | lenShape | ||
) | [private] |
Definition at line 7 of file ArrayNDScanner.cc.
References dim_, j, maxState_, state_, and strides_.
Referenced by ArrayNDScanner().
{ // Check argument validity if (lenShape == 0U || lenShape >= CHAR_BIT*sizeof(unsigned long)) throw npstat::NpstatInvalidArgument( "In npstat::ArrayNDScanner::initialize: invalid scan shape"); assert(shape); for (unsigned j=0; j<lenShape; ++j) if (!shape[j]) throw npstat::NpstatInvalidArgument( "In npstat::ArrayNDScanner::initialize: " "number of scans must be positive in each dimension"); // Initialize the scanner data state_ = 0UL; dim_ = lenShape; strides_[dim_ - 1] = 1UL; for (unsigned j=dim_ - 1; j>0; --j) strides_[j - 1] = strides_[j]*shape[j]; maxState_ = strides_[0]*shape[0]; }
bool npstat::ArrayNDScanner::isValid | ( | void | ) | const [inline] |
Returns false when iteration is complete
Definition at line 59 of file ArrayNDScanner.h.
References maxState_, and state_.
Referenced by npstat::LinInterpolatedTableND< Numeric, Axis >::invertRatioResponse(), and npstat::LinInterpolatedTableND< Numeric, Axis >::invertWRTAxis().
unsigned long npstat::ArrayNDScanner::maxState | ( | ) | const [inline] |
Maximum possible state (i.e., linear index of the scan)
Definition at line 56 of file ArrayNDScanner.h.
References maxState_.
{return maxState_;}
void npstat::ArrayNDScanner::operator++ | ( | int | ) | [inline] |
ArrayNDScanner& npstat::ArrayNDScanner::operator++ | ( | void | ) | [inline] |
void npstat::ArrayNDScanner::reset | ( | void | ) | [inline] |
Reset the state (as if the object has just been constructed)
Definition at line 65 of file ArrayNDScanner.h.
References state_.
{state_ = 0UL;}
void npstat::ArrayNDScanner::setState | ( | const unsigned long | state | ) | [inline] |
unsigned long npstat::ArrayNDScanner::state | ( | ) | const [inline] |
Retrieve current state (i.e., linear index of the scan)
Definition at line 53 of file ArrayNDScanner.h.
References state_.
{return state_;}
unsigned npstat::ArrayNDScanner::dim_ [private] |
Definition at line 86 of file ArrayNDScanner.h.
Referenced by dim(), getIndex(), and initialize().
unsigned long npstat::ArrayNDScanner::maxState_ [private] |
Definition at line 85 of file ArrayNDScanner.h.
Referenced by getIndex(), initialize(), isValid(), maxState(), operator++(), and setState().
unsigned long npstat::ArrayNDScanner::state_ [private] |
Definition at line 84 of file ArrayNDScanner.h.
Referenced by getIndex(), initialize(), isValid(), operator++(), reset(), setState(), and state().
unsigned long npstat::ArrayNDScanner::strides_[CHAR_BIT *sizeof(unsigned long)] [private] |
Definition at line 83 of file ArrayNDScanner.h.
Referenced by getIndex(), and initialize().