CMS 3D CMS Logo

Public Member Functions | Private Member Functions | Private Attributes

npstat::ArrayNDScanner Class Reference

#include <ArrayNDScanner.h>

List of all members.

Public Member Functions

unsigned dim () const
void getIndex (unsigned *index, unsigned indexBufferLen) const
bool isValid () const
unsigned long maxState () const
void operator++ (int)
ArrayNDScanneroperator++ ()
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)]

Detailed Description

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.


Constructor & Destructor Documentation

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]

Member Function Documentation

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().

{return state_ < maxState_;}
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]

Postfix increment (distinguished by the dummy "int" parameter)

Definition at line 72 of file ArrayNDScanner.h.

References maxState_, and state_.

{if (state_ < maxState_) ++state_;}
ArrayNDScanner& npstat::ArrayNDScanner::operator++ ( void  ) [inline]

Prefix increment

Definition at line 68 of file ArrayNDScanner.h.

References maxState_, and state_.

            {if (state_ < maxState_) ++state_; return *this;}
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]

Set the state directly

Definition at line 75 of file ArrayNDScanner.h.

References maxState_, and state_.

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_;}

Member Data Documentation

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().