Chemical Data Processing Library C++ API - Version 1.1.1
Public Member Functions | List of all members
CDPL::Util::IndexedElementIterator< ValueType, AccessFunc, IndexType > Class Template Reference

A STL compatible random access iterator for container elements accessible by index. More...

#include <IndexedElementIterator.hpp>

+ Inheritance diagram for CDPL::Util::IndexedElementIterator< ValueType, AccessFunc, IndexType >:

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
 

Detailed Description

template<typename ValueType, typename AccessFunc, typename IndexType = std::size_t>
class CDPL::Util::IndexedElementIterator< ValueType, AccessFunc, IndexType >

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):

class StringContainer
{
public:
.....
std::string getString(std::size_t idx); // retrieves a reference to a stored string by its index
std::size_t getNumStrings() const; // returns the total number of stored strings
.....
};

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:

class StringAccessFunc
{
public:
StringAccessFunc(StringContainer* cntnr): container(ctnr) {}
std::string& operator()(std::size_t idx) const {
return container->getString(idx);
}
bool operator==(const StringAccessFunc& func) const {
return (func.container == container);
}
private:
StringContainer* cntnr;
};

With StringAccessFunc in hands, the type of the string traversal iterator for StringContainer is defined as:

typedef IndexedElementIterator<std::string, StringAccessFunc> StringIterator;

StringIterator can then easily be used with STL-algorithms, e.g. to find a particular string in a StringContainer instance:

StringContainer str_molGraph;
..... // do something with 'str_container'
// does 'str_container' contain the string "foobar"?
bool contains_foobar = (std::find(StringIterator(&str_container, 0),
StringIterator(&str_container, str_container.getNumStrings()),
std::string("foobar"))
!= StringIterator(&str_container, str_container.getNumStrings()));
Template Parameters
ValueTypeThe type of the elements accessed by the iterator.
AccessFuncThe type of the functor object used to access the elements by index.
IndexTypeThe type of the indices used to access the elements.

Constructor & Destructor Documentation

◆ IndexedElementIterator() [1/2]

template<typename ValueType , typename AccessFunc , typename IndexType = std::size_t>
template<typename ValueType2 , typename AccessFunc2 , typename IndexType2 >
CDPL::Util::IndexedElementIterator< ValueType, AccessFunc, IndexType >::IndexedElementIterator ( const IndexedElementIterator< ValueType2, AccessFunc2, IndexType2 > &  it)
inline

Constructs and initializes the iterator with another iterator object.

Parameters
itThe other iterator.

◆ IndexedElementIterator() [2/2]

template<typename ValueType , typename AccessFunc , typename IndexType = std::size_t>
CDPL::Util::IndexedElementIterator< ValueType, AccessFunc, IndexType >::IndexedElementIterator ( const AccessFunc &  access_func,
IndexType  start_idx 
)
inline

Constructs and initializes the iterator with the access function access_func and the start element index start_idx.

Parameters
access_funcThe element access function to use.
start_idxThe index of the first element the iterator will point to.

Member Function Documentation

◆ getAccessFunc()

template<typename ValueType , typename AccessFunc , typename IndexType >
const AccessFunc & CDPL::Util::IndexedElementIterator< ValueType, AccessFunc, IndexType >::getAccessFunc

◆ getIndex()

template<typename ValueType , typename AccessFunc , typename IndexType >
IndexType CDPL::Util::IndexedElementIterator< ValueType, AccessFunc, IndexType >::getIndex

◆ distance_to()

template<typename ValueType , typename AccessFunc , typename IndexType = std::size_t>
template<typename ValueType2 , typename AccessFunc2 , typename IndexType2 >
CDPL::Util::IndexedElementIterator<ValueType, AccessFunc, IndexType>::DifferenceType CDPL::Util::IndexedElementIterator< ValueType, AccessFunc, IndexType >::distance_to ( const IndexedElementIterator< ValueType2, AccessFunc2, IndexType2 > &  it) const

The documentation for this class was generated from the following file:
CDPL::Util::operator==
bool operator==(const Array< ValueType > &array1, const Array< ValueType > &array2)
Equality comparison operator.