Chemical Data Processing Library C++ API - Version 1.2.0
|
A STL compatible random access iterator for container elements accessible by index. More...
#include <IndexedElementIterator.hpp>
Public Member Functions | |
template<typename ValueType2 , typename AccessFunc2 , typename IndexType2 > | |
IndexedElementIterator (const IndexedElementIterator< ValueType2, AccessFunc2, IndexType2 > &it) | |
Constructs and initializes the iterator with another iterator object. More... | |
IndexedElementIterator (const AccessFunc &access_func, IndexType start_idx) | |
Constructs and initializes the iterator with the access function access_func and the start element index start_idx. More... | |
const AccessFunc & | getAccessFunc () const |
IndexType | getIndex () const |
template<typename ValueType2 , typename AccessFunc2 , typename IndexType2 > | |
CDPL::Util::IndexedElementIterator< ValueType, AccessFunc, IndexType >::DifferenceType | distance_to (const IndexedElementIterator< ValueType2, AccessFunc2, IndexType2 > &it) const |
A STL compatible random access iterator for container elements accessible by index.
The rationale for IndexedElementIterator
is to allow STL-style iteration over the elements of container classes which do not provide suitable iterators on their own, but allow element access by some sort of consecutive index. IndexedElementIterator
is a random access iterator and therefore provides both increment and decrement operations and constant-time methods for moving forward and backward in arbitrary-sized steps. Random access iterators essentially provide all of the operations of ordinary C/C++ pointer arithmetic (see [STLRAI] for details).
As an example consider the class StringContainer
which provides access to stored C++ strings via a method getString(std::size_t idx)
:
To implement an iterator for string traversal using IndexedElementIterator
, one first has to define a StringContainer
access functor that returns a reference to the string stored at a particular index.
A possible implementation of this functor may look like this:
With StringAccessFunc
in hands, the type of the string traversal iterator for StringContainer
is defined as:
StringIterator
can then easily be used with STL-algorithms, e.g. to find a particular string in a StringContainer
instance:
ValueType | The type of the elements accessed by the iterator. |
AccessFunc | The type of the functor object used to access the elements by index. |
IndexType | The type of the indices used to access the elements. |
|
inline |
Constructs and initializes the iterator with another iterator object.
it | The other iterator. |
|
inline |
Constructs and initializes the iterator with the access function access_func and the start element index start_idx.
access_func | The element access function to use. |
start_idx | The index of the first element the iterator will point to. |
const AccessFunc & CDPL::Util::IndexedElementIterator< ValueType, AccessFunc, IndexType >::getAccessFunc |
IndexType CDPL::Util::IndexedElementIterator< ValueType, AccessFunc, IndexType >::getIndex |
CDPL::Util::IndexedElementIterator<ValueType, AccessFunc, IndexType>::DifferenceType CDPL::Util::IndexedElementIterator< ValueType, AccessFunc, IndexType >::distance_to | ( | const IndexedElementIterator< ValueType2, AccessFunc2, IndexType2 > & | it | ) | const |