CMS 3D CMS Logo

List of all members | Public Member Functions | Private Member Functions | Private Attributes
npstat::ArrayNDScanner Class Reference

#include <ArrayNDScanner.h>

Public Member Functions

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

42  {initialize(shape, lenShape);}
void initialize(const unsigned *shape, unsigned lenShape)
npstat::ArrayNDScanner::ArrayNDScanner ( const std::vector< unsigned > &  shape)
inlineexplicit

Definition at line 44 of file ArrayNDScanner.h.

References initialize().

45  {initialize(shape.empty() ? static_cast<unsigned*>(0) :
46  &shape[0], shape.size());}
void initialize(const unsigned *shape, unsigned lenShape)
npstat::ArrayNDScanner::ArrayNDScanner ( )
private

Referenced by setState().

Member Function Documentation

unsigned npstat::ArrayNDScanner::dim ( ) const
inline

Dimensionality of the scan

Definition at line 50 of file ArrayNDScanner.h.

References dim_.

50 {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_, mps_fire::i, training_settings::idx, checklumidiff::l, maxState_, state_, and strides_.

Referenced by isValid().

31  {
32  if (indexBufferLen < dim_) throw npstat::NpstatInvalidArgument(
33  "In npstat::ArrayNDScanner::getIndex: "
34  "insufficient length of the output buffer");
36  "In npstat::ArrayNDScanner::getIndex: invalid scanner state");
37  assert(ix);
38 
39  unsigned long l = state_;
40  for (unsigned i=0; i<dim_; ++i)
41  {
42  unsigned long idx = l / strides_[i];
43  ix[i] = static_cast<unsigned>(idx);
44  l -= (idx * strides_[i]);
45  }
46  }
unsigned long maxState_
unsigned long strides_[CHAR_BIT *sizeof(unsigned long)]
void npstat::ArrayNDScanner::initialize ( const unsigned *  shape,
unsigned  lenShape 
)
private

Definition at line 7 of file ArrayNDScanner.cc.

References dim_, maxState_, state_, strides_, and mitigatedMETSequence_cff::U.

Referenced by ArrayNDScanner(), and setState().

9  {
10  // Check argument validity
11  if (lenShape == 0U || lenShape >= CHAR_BIT*sizeof(unsigned long))
13  "In npstat::ArrayNDScanner::initialize: invalid scan shape");
14  assert(shape);
15  for (unsigned j=0; j<lenShape; ++j)
16  if (!shape[j]) throw npstat::NpstatInvalidArgument(
17  "In npstat::ArrayNDScanner::initialize: "
18  "number of scans must be positive in each dimension");
19 
20  // Initialize the scanner data
21  state_ = 0UL;
22  dim_ = lenShape;
23  strides_[dim_ - 1] = 1UL;
24  for (unsigned j=dim_ - 1; j>0; --j)
25  strides_[j - 1] = strides_[j]*shape[j];
26  maxState_ = strides_[0]*shape[0];
27  }
unsigned long maxState_
unsigned long strides_[CHAR_BIT *sizeof(unsigned long)]
bool npstat::ArrayNDScanner::isValid ( void  ) const
inline
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_.

56 {return maxState_;}
unsigned long maxState_
ArrayNDScanner& npstat::ArrayNDScanner::operator++ ( void  )
inline

Prefix increment

Definition at line 68 of file ArrayNDScanner.h.

References maxState_, and state_.

69  {if (state_ < maxState_) ++state_; return *this;}
unsigned long 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_.

72 {if (state_ < maxState_) ++state_;}
unsigned long maxState_
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_.

65 {state_ = 0UL;}
void npstat::ArrayNDScanner::setState ( const unsigned long  state)
inline

Set the state directly

Definition at line 75 of file ArrayNDScanner.h.

References ArrayNDScanner(), initialize(), maxState_, and state_.

unsigned long maxState_
unsigned long state() const
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_.

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