SSAGES  0.8.5
Software Suite for Advanced General Ensemble Simulations
Public Types | Public Member Functions | Private Attributes | List of all members
SSAGES::Grid< T >::GridIterator< R > Class Template Reference

Custom Iterator. More...

#include <Grid.h>

Public Types

typedef GridIterator self_type
 Type name of the iterator.
 
typedef int difference_type
 Difference type is an int.
 
typedef R value_type
 Either T or const T for iterator and const_iterator, respectively.
 
typedef R * pointer
 Either T* or T const* for iterator and const_iterator, respectively.
 
typedef R & reference
 Either T& or T const& for iterator and const_iterator, respectively.
 
typedef std::bidirectional_iterator_tag iterator_category
 HistIterator is a bidirectional iterator.
 

Public Member Functions

 GridIterator ()=default
 Use default constructor.
 
 GridIterator (const std::vector< int > &indices, Grid< T > *grid)
 Constructor. More...
 
 GridIterator (const std::vector< int > &indices, const Grid< T > *grid)
 Const constructor. More...
 
 GridIterator (const GridIterator &other)
 Copy constructor. More...
 
reference operator* ()
 Dereference operator. More...
 
self_typeoperator++ ()
 Pre-increment operator. More...
 
self_type operator++ (int)
 Post-increment operator. More...
 
self_typeoperator+= (std::vector< int > shift)
 Addition assignment operator. More...
 
const self_type operator+ (std::vector< int > shift)
 Addition operator. More...
 
self_typeoperator-- ()
 Pre-decrement operator. More...
 
self_type operator-- (int)
 Post-decrement operator. More...
 
self_typeoperator-= (std::vector< int > shift)
 Subtraction assignment operator. More...
 
const self_type operator- (std::vector< int > shift)
 Subtraction iterator. More...
 
bool operator== (const self_type &rhs) const
 Equality operator. More...
 
bool operator!= (const self_type &rhs) const
 Non-equality operator. More...
 
std::vector< int > & indices ()
 Access indices. More...
 
int & index (size_t d)
 Access a specific index. More...
 
std::vector< double > coordinates () const
 Access coordinates. More...
 
double coordinate (size_t d) const
 Access specific coordinate dimension. More...
 

Private Attributes

std::vector< int > indices_
 Indices of current grid point.
 
Grid< T > * grid_
 Pointer to grid to iterate over.
 

Detailed Description

template<typename T>
template<typename R>
class SSAGES::Grid< T >::GridIterator< R >

Custom Iterator.

This iterator is designed of travesing through a grid. The starting point is at grid index 0 for each dimension. The last valid grid point has the num_points in each dimension, where num_points is the number of grid points in the respective dimension.

The iterator can be used as a standard iterator with operator* accessing the grid point at which the iterator currently is.

Additionally, the functions GridIterator::indices() and GridIterator::coordinates() are provided. These functions return the indices of the current grid point and the center of the grid point interval in real space, respectively.

The iterator can be moved to an arbitrary position. As indices() returns a reference (and not a const reference), it can be used to move the iterator. For example:

HistIterator it = hist->begin();
it.indices() = {1,1,1};

moves the iterator to the grid point [1, 1, 1].

The iterator can be traversed in a standard fashion with the increment and decrement operators operator++ and operator--. When the increment operator is invoked, the bin index for the lowest dimension is increased by 1. If it moves beyond the valid range in this dimension, the index is reset to 0 and the index of the next higher dimension is increased by 1. The decrement operator traveses the grid in the same fashion but opposite direction.

Additionaly, the iterator can be shifted by adding or subtracting a vector of ints. The vector needs to have the same dimension as the histogram.

Definition at line 247 of file Grid.h.

Constructor & Destructor Documentation

template<typename T>
template<typename R >
SSAGES::Grid< T >::GridIterator< R >::GridIterator ( const std::vector< int > &  indices,
Grid< T > *  grid 
)
inline

Constructor.

Parameters
indicesBin indices specifying the current position of the iterator.
gridPointer to the grid to iterate over.

Definition at line 276 of file Grid.h.

277  : indices_(indices), grid_(grid)
278  {
279  }
Grid< T > * grid_
Pointer to grid to iterate over.
Definition: Grid.h:511
std::vector< int > indices_
Indices of current grid point.
Definition: Grid.h:508
template<typename T>
template<typename R >
SSAGES::Grid< T >::GridIterator< R >::GridIterator ( const std::vector< int > &  indices,
const Grid< T > *  grid 
)
inline

Const constructor.

Parameters
indicesBin indices specifying the current position of the iterator.
gridPointer to the grid to iterate over.

Definition at line 287 of file Grid.h.

288  : indices_(indices), grid_(grid)
289  {
290  }
Grid< T > * grid_
Pointer to grid to iterate over.
Definition: Grid.h:511
std::vector< int > indices_
Indices of current grid point.
Definition: Grid.h:508
template<typename T>
template<typename R >
SSAGES::Grid< T >::GridIterator< R >::GridIterator ( const GridIterator< R > &  other)
inline

Copy constructor.

Parameters
otherGridIterator to be copied.

Definition at line 296 of file Grid.h.

297  : indices_(other.indices_), grid_(other.grid_)
298  {
299  }
Grid< T > * grid_
Pointer to grid to iterate over.
Definition: Grid.h:511
std::vector< int > indices_
Indices of current grid point.
Definition: Grid.h:508

Member Function Documentation

template<typename T>
template<typename R >
double SSAGES::Grid< T >::GridIterator< R >::coordinate ( size_t  d) const
inline

Access specific coordinate dimension.

Parameters
dDimension of the coordinate.
Returns
Center of the current grid point in the specified dimension.

Definition at line 501 of file Grid.h.

References SSAGES::Grid< T >::GridIterator< R >::coordinates().

502  {
503  return coordinates()[d];
504  }
std::vector< double > coordinates() const
Access coordinates.
Definition: Grid.h:491

Here is the call graph for this function:

template<typename T>
template<typename R >
std::vector<double> SSAGES::Grid< T >::GridIterator< R >::coordinates ( ) const
inline

Access coordinates.

Returns
Center point of the current grid point.

Definition at line 491 of file Grid.h.

References SSAGES::Grid< T >::GridIterator< R >::grid_, and SSAGES::Grid< T >::GridIterator< R >::indices_.

Referenced by SSAGES::Grid< T >::GridIterator< R >::coordinate().

492  {
493  return grid_->GetCoordinates(indices_);
494  }
Grid< T > * grid_
Pointer to grid to iterate over.
Definition: Grid.h:511
std::vector< int > indices_
Indices of current grid point.
Definition: Grid.h:508

Here is the caller graph for this function:

template<typename T>
template<typename R >
int& SSAGES::Grid< T >::GridIterator< R >::index ( size_t  d)
inline

Access a specific index.

Parameters
dDimension of the index.
Returns
Index of the current grid point in the specified dimension.

Definition at line 482 of file Grid.h.

References SSAGES::Grid< T >::GridIterator< R >::indices().

483  {
484  return indices()[d];
485  }
std::vector< int > & indices()
Access indices.
Definition: Grid.h:471

Here is the call graph for this function:

template<typename T>
template<typename R >
std::vector<int>& SSAGES::Grid< T >::GridIterator< R >::indices ( )
inline

Access indices.

Returns
Indices of current grid point.
Note
This function returns a reference and can be used to move the current grid point.

Definition at line 471 of file Grid.h.

References SSAGES::Grid< T >::GridIterator< R >::indices_.

Referenced by SSAGES::Grid< Eigen::VectorXd >::begin(), SSAGES::Grid< Eigen::VectorXd >::end(), and SSAGES::Grid< T >::GridIterator< R >::index().

472  {
473  return indices_;
474  }
std::vector< int > indices_
Indices of current grid point.
Definition: Grid.h:508

Here is the caller graph for this function:

template<typename T>
template<typename R >
bool SSAGES::Grid< T >::GridIterator< R >::operator!= ( const self_type rhs) const
inline

Non-equality operator.

Parameters
rhsIterator to which this iterator is compared.
Returns
False if both iterators access the same grid point on the same grid. Else return True.

Definition at line 459 of file Grid.h.

460  {
461  return !( (*this) == rhs );
462  }
template<typename T>
template<typename R >
reference SSAGES::Grid< T >::GridIterator< R >::operator* ( )
inline

Dereference operator.

Returns
Reference to the value at the current grid position.

Definition at line 305 of file Grid.h.

References SSAGES::Grid< T >::GridIterator< R >::grid_, and SSAGES::Grid< T >::GridIterator< R >::indices_.

305 { return grid_->at(indices_); }
Grid< T > * grid_
Pointer to grid to iterate over.
Definition: Grid.h:511
std::vector< int > indices_
Indices of current grid point.
Definition: Grid.h:508
template<typename T>
template<typename R >
const self_type SSAGES::Grid< T >::GridIterator< R >::operator+ ( std::vector< int >  shift)
inline

Addition operator.

Parameters
shiftAmount of shift in each dimension.
Returns
Copy of iterator after shift.

Shift the iterator by a given vector.

Definition at line 377 of file Grid.h.

References SSAGES::Grid< T >::GridIterator< R >::GridIterator().

378  {
379  return GridIterator(*this) += shift;
380  }
GridIterator()=default
Use default constructor.

Here is the call graph for this function:

template<typename T>
template<typename R >
self_type& SSAGES::Grid< T >::GridIterator< R >::operator++ ( )
inline

Pre-increment operator.

Returns
Reference to iterator.

Increments the bin index of lowest dimension. If an index moves beyond the maximum value (num_points-1), it is reset to 0 and the index of the next higher dimension is increased by 1.

Definition at line 315 of file Grid.h.

References SSAGES::Grid< T >::GridIterator< R >::grid_, and SSAGES::Grid< T >::GridIterator< R >::indices_.

316  {
317  indices_.at(0) += 1;
318  for (size_t i = 0; i < grid_->GetDimension() - 1; ++i) {
319  if (indices_.at(i) >= grid_->numPoints_[i]) {
320  indices_.at(i) = 0;
321  indices_.at(i+1) += 1;
322  }
323  }
324 
325  return *this;
326  }
Grid< T > * grid_
Pointer to grid to iterate over.
Definition: Grid.h:511
std::vector< int > indices_
Indices of current grid point.
Definition: Grid.h:508
template<typename T>
template<typename R >
self_type SSAGES::Grid< T >::GridIterator< R >::operator++ ( int  )
inline

Post-increment operator.

Returns
Copy of iterator before incrementing.

Definition at line 332 of file Grid.h.

333  {
334  GridIterator it(*this);
335  ++(*this);
336  return it;
337  }
GridIterator()=default
Use default constructor.
template<typename T>
template<typename R >
self_type& SSAGES::Grid< T >::GridIterator< R >::operator+= ( std::vector< int >  shift)
inline

Addition assignment operator.

Parameters
shiftVector of shifts in each dimension.
Returns
Reference to itself.

This operator shifts the current position of the iterator by the given amount in each dimension.

Example:

if += {1,1,1};

In this example the current position of the iterator is shifted diagonally.

Definition at line 356 of file Grid.h.

References SSAGES::Grid< T >::GridIterator< R >::grid_, and SSAGES::Grid< T >::GridIterator< R >::indices_.

357  {
358  if (shift.size() != grid_->GetDimension()) {
359  throw std::invalid_argument("Vector to shift iterator does not "
360  "match grid dimension.");
361  }
362 
363  for (size_t i = 0; i < grid_->GetDimension(); ++i) {
364  indices_.at(i) += shift.at(i);
365  }
366 
367  return *this;
368  }
Grid< T > * grid_
Pointer to grid to iterate over.
Definition: Grid.h:511
std::vector< int > indices_
Indices of current grid point.
Definition: Grid.h:508
template<typename T>
template<typename R >
const self_type SSAGES::Grid< T >::GridIterator< R >::operator- ( std::vector< int >  shift)
inline

Subtraction iterator.

Parameters
shiftVector to be subtracted from the current grid indices.
Returns
Copy of iterator after shift.

Definition at line 437 of file Grid.h.

References SSAGES::Grid< T >::GridIterator< R >::GridIterator().

438  {
439  return GridIterator(*this) -= shift;
440  }
GridIterator()=default
Use default constructor.

Here is the call graph for this function:

template<typename T>
template<typename R >
self_type& SSAGES::Grid< T >::GridIterator< R >::operator-- ( )
inline

Pre-decrement operator.

Returns
Reference to iterator after decrementing.

Traveses the histogram in the opposite direction to the increment operator.

Definition at line 389 of file Grid.h.

References SSAGES::Grid< T >::GridIterator< R >::grid_, and SSAGES::Grid< T >::GridIterator< R >::indices_.

390  {
391  indices_.at(0) -= 1;
392  for (size_t i = 0; i < grid_->GetDimension() - 1; ++i) {
393  if (indices_.at(i) < 0) {
394  indices_.at(i) = grid_->numPoints_[i]-1;
395  indices_.at(i+1) -= 1;
396  }
397  }
398 
399  return *this;
400  }
Grid< T > * grid_
Pointer to grid to iterate over.
Definition: Grid.h:511
std::vector< int > indices_
Indices of current grid point.
Definition: Grid.h:508
template<typename T>
template<typename R >
self_type SSAGES::Grid< T >::GridIterator< R >::operator-- ( int  )
inline

Post-decrement operator.

Returns
Copy of iterator before decrementing.

Definition at line 406 of file Grid.h.

407  {
408  GridIterator it(*this);
409  --(*this);
410  return it;
411  }
GridIterator()=default
Use default constructor.
template<typename T>
template<typename R >
self_type& SSAGES::Grid< T >::GridIterator< R >::operator-= ( std::vector< int >  shift)
inline

Subtraction assignment operator.

Parameters
shiftVector to be subtracted from the current grid indices.
Returns
Reference to iterator.

Definition at line 418 of file Grid.h.

References SSAGES::Grid< T >::GridIterator< R >::grid_, and SSAGES::Grid< T >::GridIterator< R >::indices_.

419  {
420  if (shift.size() != grid_->GetDimension()) {
421  throw std::invalid_argument("Vector to shift iterator does not "
422  "match histogram dimension.");
423  }
424 
425  for (size_t i = 0; i < grid_->GetDimension(); ++i) {
426  indices_.at(i) -= shift.at(i);
427  }
428 
429  return *this;
430  }
Grid< T > * grid_
Pointer to grid to iterate over.
Definition: Grid.h:511
std::vector< int > indices_
Indices of current grid point.
Definition: Grid.h:508
template<typename T>
template<typename R >
bool SSAGES::Grid< T >::GridIterator< R >::operator== ( const self_type rhs) const
inline

Equality operator.

Parameters
rhsIterator to which this iterator is compared.
Returns
True if both iterators access the same grid point on the same grid. Else return False.

Definition at line 448 of file Grid.h.

References SSAGES::Grid< T >::GridIterator< R >::grid_, and SSAGES::Grid< T >::GridIterator< R >::indices_.

449  {
450  return indices_ == rhs.indices_ && grid_ == rhs.grid_;
451  }
Grid< T > * grid_
Pointer to grid to iterate over.
Definition: Grid.h:511
std::vector< int > indices_
Indices of current grid point.
Definition: Grid.h:508

The documentation for this class was generated from the following file: