CMS 3D CMS Logo

Public Member Functions | Private Member Functions | Private Attributes

npstat::BoxNDScanner< Numeric > Class Template Reference

#include <BoxNDScanner.h>

List of all members.

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

            : box_(box), state_(0UL) 
            {initialize(shape.empty() ? static_cast<unsigned*>(0) : 
                        &shape[0], shape.size());}
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().

            : box_(box), state_(0UL) {initialize(shape, lenShape);}
template<typename Numeric >
npstat::BoxNDScanner< Numeric >::BoxNDScanner ( ) [private]

Member Function Documentation

template<typename Numeric >
unsigned npstat::BoxNDScanner< Numeric >::dim ( ) const [inline]

Dimensionality of the scan

Definition at line 58 of file BoxNDScanner.h.

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

{return box_.dim();}
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 i, UserOptions_cff::idx, and prof2calltree::l.

    {
        const unsigned dim = strides_.size();
        if (nx < dim) throw npstat::NpstatInvalidArgument(
            "In npstat::BoxNDScanner::getCoords: "
            "insufficient length of the output buffer");
        if (state_ >= maxState_) throw npstat::NpstatRuntimeError(
            "In npstat::BoxNDScanner::getCoords: invalid scanner state");
        assert(x);

        unsigned long l = state_;
        for (unsigned i=0; i<dim; ++i)
        {
            unsigned long idx = l / strides_[i];
            x[i] = box_[i].min() + (idx + 0.5)*bw_[i];
            l -= (idx * strides_[i]);
        }
    }
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 i, UserOptions_cff::idx, and prof2calltree::l.

    {
        const unsigned dim = strides_.size();
        if (nx < dim) throw npstat::NpstatInvalidArgument(
            "In npstat::BoxNDScanner::getIndex: "
            "insufficient length of the output buffer");
        if (state_ >= maxState_) throw npstat::NpstatRuntimeError(
            "In npstat::BoxNDScanner::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]);
        }
    }
template<typename Numeric >
void npstat::BoxNDScanner< Numeric >::initialize ( const unsigned *  shape,
unsigned  lenShape 
) [private]

Definition at line 107 of file BoxNDScanner.h.

References j.

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

    {
        if (!(dim && box_.dim() == dim)) throw npstat::NpstatInvalidArgument(
            "In npstat::BoxNDScanner::initialize: incompatible scan shape");
        assert(shape);
        for (unsigned j=0; j<dim; ++j)
            if (!shape[j]) throw npstat::NpstatInvalidArgument(
                "In npstat::BoxNDScanner::initialize: "
                "number of scans must be positive in each dimension");

        strides_.resize(dim);
        strides_[dim - 1] = 1UL;
        for (unsigned j=dim - 1; j>0; --j)
            strides_[j - 1] = strides_[j]*shape[j];
        maxState_ = strides_[0]*shape[0];

        bw_.reserve(dim);
        for (unsigned j=0; j<dim; ++j)
            bw_.push_back(box_[j].length()*1.0/shape[j]);
    }
template<typename Numeric >
bool npstat::BoxNDScanner< Numeric >::isValid ( void  ) const [inline]

Returns false when iteration is complete

Definition at line 67 of file BoxNDScanner.h.

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

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

{return maxState_;}
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_.

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

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

{state_ = 0UL;}
template<typename Numeric >
void npstat::BoxNDScanner< Numeric >::setState ( const unsigned long  state) [inline]

Set the state directly

Definition at line 86 of file BoxNDScanner.h.

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

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_.

{return state_;}

Member Data Documentation

template<typename Numeric >
BoxND<Numeric> npstat::BoxNDScanner< Numeric >::box_ [private]

Definition at line 94 of file BoxNDScanner.h.

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

template<typename Numeric >
std::vector<double> npstat::BoxNDScanner< Numeric >::bw_ [private]

Definition at line 96 of file BoxNDScanner.h.

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]

Definition at line 95 of file BoxNDScanner.h.