CMS 3D CMS Logo

List of all members | Public Member Functions | Private Member Functions | Private Attributes
npstat::BoxNDScanner< Numeric > Class Template Reference

#include <BoxNDScanner.h>

Public Member Functions

unsigned dim () const
 
void getCoords (Numeric *x, unsigned nx) const
 
void getIndex (unsigned *index, unsigned indexBufferLen) const
 
bool isValid () const
 
unsigned long maxState () const
 
BoxNDScanneroperator++ ()
 
void operator++ (int)
 
void reset ()
 
void setState (const unsigned long state)
 
unsigned long state () const
 
 BoxNDScanner (const BoxND< Numeric > &box, const std::vector< unsigned > &shape)
 
 BoxNDScanner (const BoxND< Numeric > &box, const unsigned *shape, const unsigned lenShape)
 

Private Member Functions

 BoxNDScanner ()
 
void initialize (const unsigned *shape, unsigned lenShape)
 

Private Attributes

BoxND< Numeric > box_
 
std::vector< double > bw_
 
unsigned long maxState_
 
unsigned long state_
 
std::vector< unsigned long > strides_
 

Detailed Description

template<typename Numeric>
class npstat::BoxNDScanner< Numeric >

A class for iterating over all coordinates in a multidimensional box (but not a full-fledeged iterator). The expected usage pattern is as follows:

double* coords = ... (the buffer size should be at least box.dim())
for (BoxNDScanner<double> scan(box,shape); scan.isValid(); ++scan)
{
scan.getCoords(coords, coordsBufferSize);
.... Do what is necessary with coordinates ....
.... Extract linear bin number: ..............
scan.state();
}

The coordinates will be in the middle of the bins (imagine a multivariate histogram with boundaries defined by the given box).

Definition at line 38 of file BoxNDScanner.h.

Constructor & Destructor Documentation

template<typename Numeric >
npstat::BoxNDScanner< Numeric >::BoxNDScanner ( const BoxND< Numeric > &  box,
const std::vector< unsigned > &  shape 
)
inline

Constructor from a bounding box and a multidimensional array shape

Definition at line 46 of file BoxNDScanner.h.

References npstat::BoxNDScanner< Numeric >::initialize().

48  : box_(box), state_(0UL)
49  {initialize(shape.empty() ? static_cast<unsigned*>(0) :
50  &shape[0], shape.size());}
void initialize(const unsigned *shape, unsigned lenShape)
Definition: BoxNDScanner.h:107
BoxND< Numeric > box_
Definition: BoxNDScanner.h:94
unsigned long state_
Definition: BoxNDScanner.h:97
template<typename Numeric >
npstat::BoxNDScanner< Numeric >::BoxNDScanner ( const BoxND< Numeric > &  box,
const unsigned *  shape,
const unsigned  lenShape 
)
inline

Definition at line 52 of file BoxNDScanner.h.

References npstat::BoxNDScanner< Numeric >::initialize().

54  : box_(box), state_(0UL) {initialize(shape, lenShape);}
void initialize(const unsigned *shape, unsigned lenShape)
Definition: BoxNDScanner.h:107
BoxND< Numeric > box_
Definition: BoxNDScanner.h:94
unsigned long state_
Definition: BoxNDScanner.h:97
template<typename Numeric >
npstat::BoxNDScanner< Numeric >::BoxNDScanner ( )
private

Member Function Documentation

template<typename Numeric >
unsigned npstat::BoxNDScanner< Numeric >::dim ( ) const
inline
template<typename Numeric >
void npstat::BoxNDScanner< Numeric >::getCoords ( Numeric *  x,
unsigned  nx 
) const

Retrieve current coordinates inside the box

Definition at line 130 of file BoxNDScanner.h.

References npstat::BoxNDScanner< Numeric >::box_, npstat::BoxNDScanner< Numeric >::bw_, npstat::BoxNDScanner< Numeric >::dim(), mps_fire::i, training_settings::idx, checklumidiff::l, npstat::BoxNDScanner< Numeric >::maxState_, npstat::BoxNDScanner< Numeric >::state_, and npstat::BoxNDScanner< Numeric >::strides_.

Referenced by npstat::BoxNDScanner< Numeric >::isValid().

131  {
132  const unsigned dim = strides_.size();
133  if (nx < dim) throw npstat::NpstatInvalidArgument(
134  "In npstat::BoxNDScanner::getCoords: "
135  "insufficient length of the output buffer");
137  "In npstat::BoxNDScanner::getCoords: invalid scanner state");
138  assert(x);
139 
140  unsigned long l = state_;
141  for (unsigned i=0; i<dim; ++i)
142  {
143  unsigned long idx = l / strides_[i];
144  x[i] = box_[i].min() + (idx + 0.5)*bw_[i];
145  l -= (idx * strides_[i]);
146  }
147  }
unsigned long maxState_
Definition: BoxNDScanner.h:98
BoxND< Numeric > box_
Definition: BoxNDScanner.h:94
unsigned long state_
Definition: BoxNDScanner.h:97
std::vector< unsigned long > strides_
Definition: BoxNDScanner.h:95
unsigned dim() const
Definition: BoxNDScanner.h:58
std::vector< double > bw_
Definition: BoxNDScanner.h:96
template<typename Numeric >
void npstat::BoxNDScanner< Numeric >::getIndex ( unsigned *  index,
unsigned  indexBufferLen 
) const

Retrieve current multidimensional index

Definition at line 150 of file BoxNDScanner.h.

References npstat::BoxNDScanner< Numeric >::dim(), mps_fire::i, training_settings::idx, checklumidiff::l, npstat::BoxNDScanner< Numeric >::maxState_, npstat::BoxNDScanner< Numeric >::state_, and npstat::BoxNDScanner< Numeric >::strides_.

Referenced by npstat::BoxNDScanner< Numeric >::isValid().

151  {
152  const unsigned dim = strides_.size();
153  if (nx < dim) throw npstat::NpstatInvalidArgument(
154  "In npstat::BoxNDScanner::getIndex: "
155  "insufficient length of the output buffer");
157  "In npstat::BoxNDScanner::getIndex: invalid scanner state");
158  assert(ix);
159 
160  unsigned long l = state_;
161  for (unsigned i=0; i<dim; ++i)
162  {
163  unsigned long idx = l / strides_[i];
164  ix[i] = static_cast<unsigned>(idx);
165  l -= (idx * strides_[i]);
166  }
167  }
unsigned long maxState_
Definition: BoxNDScanner.h:98
unsigned long state_
Definition: BoxNDScanner.h:97
std::vector< unsigned long > strides_
Definition: BoxNDScanner.h:95
unsigned dim() const
Definition: BoxNDScanner.h:58
template<typename Numeric >
void npstat::BoxNDScanner< Numeric >::initialize ( const unsigned *  shape,
unsigned  lenShape 
)
private

Definition at line 107 of file BoxNDScanner.h.

References npstat::BoxNDScanner< Numeric >::box_, npstat::BoxNDScanner< Numeric >::bw_, npstat::BoxNDScanner< Numeric >::dim(), npstat::BoxNDScanner< Numeric >::maxState_, and npstat::BoxNDScanner< Numeric >::strides_.

Referenced by npstat::BoxNDScanner< Numeric >::BoxNDScanner(), and npstat::BoxNDScanner< Numeric >::setState().

109  {
110  if (!(dim && box_.dim() == dim)) throw npstat::NpstatInvalidArgument(
111  "In npstat::BoxNDScanner::initialize: incompatible scan shape");
112  assert(shape);
113  for (unsigned j=0; j<dim; ++j)
114  if (!shape[j]) throw npstat::NpstatInvalidArgument(
115  "In npstat::BoxNDScanner::initialize: "
116  "number of scans must be positive in each dimension");
117 
118  strides_.resize(dim);
119  strides_[dim - 1] = 1UL;
120  for (unsigned j=dim - 1; j>0; --j)
121  strides_[j - 1] = strides_[j]*shape[j];
122  maxState_ = strides_[0]*shape[0];
123 
124  bw_.reserve(dim);
125  for (unsigned j=0; j<dim; ++j)
126  bw_.push_back(box_[j].length()*1.0/shape[j]);
127  }
unsigned long maxState_
Definition: BoxNDScanner.h:98
BoxND< Numeric > box_
Definition: BoxNDScanner.h:94
std::vector< unsigned long > strides_
Definition: BoxNDScanner.h:95
unsigned dim() const
Definition: BoxNDScanner.h:58
std::vector< double > bw_
Definition: BoxNDScanner.h:96
template<typename Numeric >
bool npstat::BoxNDScanner< Numeric >::isValid ( void  ) const
inline
template<typename Numeric >
unsigned long npstat::BoxNDScanner< Numeric >::maxState ( ) const
inline

Maximum possible state (i.e., linear index of the scan)

Definition at line 64 of file BoxNDScanner.h.

References npstat::BoxNDScanner< Numeric >::maxState_.

64 {return maxState_;}
unsigned long maxState_
Definition: BoxNDScanner.h:98
template<typename Numeric >
BoxNDScanner& npstat::BoxNDScanner< Numeric >::operator++ ( void  )
inline

Prefix increment

Definition at line 79 of file BoxNDScanner.h.

References npstat::BoxNDScanner< Numeric >::maxState_, and npstat::BoxNDScanner< Numeric >::state_.

80  {if (state_ < maxState_) ++state_; return *this;}
unsigned long maxState_
Definition: BoxNDScanner.h:98
unsigned long state_
Definition: BoxNDScanner.h:97
template<typename Numeric >
void npstat::BoxNDScanner< Numeric >::operator++ ( int  )
inline

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

Definition at line 83 of file BoxNDScanner.h.

References npstat::BoxNDScanner< Numeric >::maxState_, and npstat::BoxNDScanner< Numeric >::state_.

83 {if (state_ < maxState_) ++state_;}
unsigned long maxState_
Definition: BoxNDScanner.h:98
unsigned long state_
Definition: BoxNDScanner.h:97
template<typename Numeric >
void npstat::BoxNDScanner< Numeric >::reset ( void  )
inline

Reset the state (as if the object has just been constructed)

Definition at line 76 of file BoxNDScanner.h.

References npstat::BoxNDScanner< Numeric >::state_.

76 {state_ = 0UL;}
unsigned long state_
Definition: BoxNDScanner.h:97
template<typename Numeric >
void npstat::BoxNDScanner< Numeric >::setState ( const unsigned long  state)
inline
template<typename Numeric >
unsigned long npstat::BoxNDScanner< Numeric >::state ( ) const
inline

Retrieve current state (i.e., linear index of the scan)

Definition at line 61 of file BoxNDScanner.h.

References npstat::BoxNDScanner< Numeric >::state_.

61 {return state_;}
unsigned long state_
Definition: BoxNDScanner.h:97

Member Data Documentation

template<typename Numeric >
BoxND<Numeric> npstat::BoxNDScanner< Numeric >::box_
private
template<typename Numeric >
std::vector<double> npstat::BoxNDScanner< Numeric >::bw_
private
template<typename Numeric >
unsigned long npstat::BoxNDScanner< Numeric >::maxState_
private
template<typename Numeric >
unsigned long npstat::BoxNDScanner< Numeric >::state_
private
template<typename Numeric >
std::vector<unsigned long> npstat::BoxNDScanner< Numeric >::strides_
private