Public Types | Public Member Functions | Related Functions

TRTK::Range< T > Class Template Reference

Aggregate of two input iterator adapters. More...

#include <Range.hpp>

List of all members.

Public Types

typedef Iterator< T > const_iterator

Public Member Functions

 Range ()
 Default constructor.
 Range (const Iterator< T > &begin, const Iterator< T > &end)
 Constructor.
 Range (const Range< T > &other)
 Copy constructor.
virtual ~Range ()
 Destructor.
Range< T > & operator= (const Range< T > &other)
 Assignment operator.
bool operator== (const Range< T > &other) const
 Comparison operator.
bool operator!= (const Range< T > &other) const
 Comparison operator.
 operator bool_type () const
Iterator< T > begin () const
 Returns the first element of the range.
Iterator< T > end () const
 Returns the last element of the range (as done in the STL).
bool isValid () const
 Returns true if begin() and end() reference iterators.
bool isEmpty () const
 Returns true if the sequence does not contain any elements.
void first () const
 Sets the internal iterator to the begin of the range.
bool isDone () const
 Returns true if the traversal is done or if a sequence is empty or uninitialized.
void next () const
 Selects the next element in the range.
currentItem () const
 Returns the current element of the range traversal.
size_t size () const
 Returns the size of the sequence.

Related Functions

(Note that these are not member functions.)


template<class Sequence >
Range< typename
Sequence::value_type > 
make_range (const Sequence &sequence)
 Convenience function for creating a range of an arbitrary sequence (i.e. an STL container).

Detailed Description

template<class T>
class TRTK::Range< T >

Aggregate of two input iterator adapters.

Template Parameters:
TValue type of the dereferenced iterators (note: this is not necessarily the type of the container element)

Many algorithms (like sorting) can be formulated such that they do not depend on a certain data structure. Access to the data might be abstracted via data traversal. This can be realized via iterators, for instance, as done in the STL; also many algorithms in TRTK are implemented in this manner. However, this generally involves passing two arguments; one for the begin of a data sequence and one for its end. Using ranges, this verbosity can be significantly reduced.

The range of a sequence is the half-open interval [first, last) which means that the first element is included but but not the last.

A range can be traversed using next(). The actual element is obtained via currentItem(). To check whether a traversal is done use isDone(). The position can be reseted via first().

Example:

 vector<double> vec;
 vec.push_back(1);
 vec.push_back(2);
 vec.push_back(3);

 // Iterator<double> first = make_iterator(vec.begin());
 // Iterator<double> last = make_iterator(vec.end());
 // Range<double> range(first, last);
 //
 // or simply

 Range<double> range = make_range(vec);

 while(!range.isDone())
 {
     echo << range.currentItem() << " ";
     range.next();
 }

Output:

 1 2 3
Author:
Christoph Haenisch
Version:
0.1.1
Date:
last changed on 2014-08-31

Definition at line 90 of file Range.hpp.


Constructor & Destructor Documentation

template<class T >
TRTK::Range< T >::Range ( const Iterator< T > &  begin,
const Iterator< T > &  end 
)

Constructor.

Parameters:
[in]beginFirst element of a sequence.
[in]endLast element of a sequence (as done in the STL).

The range is a half-open intervall [begin, last).

Definition at line 157 of file Range.hpp.


Member Function Documentation

template<class T >
bool TRTK::Range< T >::isEmpty (  ) const

Returns true if the sequence does not contain any elements.

If the range is uninitialized true is returned.

Definition at line 329 of file Range.hpp.

template<class T >
bool TRTK::Range< T >::isValid (  ) const

Returns true if begin() and end() reference iterators.

The default instantiation does not reference an iterators and thus returns false.

 Range<double> range = Range<double>();
 range.isValid(); // returns false
Note:
For convenience Range<T> also provides a boolean conversion operator that allows you to write code like this:
       vector<double> vec;
       Range<double> range = make_range(vec);
       if (range) doSomething(); // instead of using it.isValid()

Definition at line 362 of file Range.hpp.

template<class T >
bool TRTK::Range< T >::operator== ( const Range< T > &  other ) const

Comparison operator.

Note:
The internally stored iterators must belong to the same sequence and must be of the same type, otherwise the behavior is undefined. However, comparing subranges is well defined.
Example:
         vector<double> vec;
         vec.push_back(1);
         vector<double> vec2 = vec;
         vec.begin() == vec2.begin(); // undefined behavior
         make_range(vec) == make_range(vec2); // undefined behavior
Returns:
If both ranges are valid, true is returned in the case of mutually identic iterators begin() and end(). If both ranges are uninitialized, true is returned as well. In all other cases false is returned.

Definition at line 207 of file Range.hpp.

template<class T >
size_t TRTK::Range< T >::size (  ) const

Returns the size of the sequence.

Coputational complexity: O(n) where n is the number of elements in the range.

Definition at line 397 of file Range.hpp.


Friends And Related Function Documentation

template<class Sequence >
Range< typename Sequence::value_type > make_range ( const Sequence &  sequence ) [related]

Convenience function for creating a range of an arbitrary sequence (i.e. an STL container).

The sequence must provide its value type via Sequence::value_type. It must also provide two input iterators via the begin() and end() methods. See Range for an example.

Returns:
Returns an instance of the Range<T> class.
See also:
Range, Iterator, IteratorAdapter

Definition at line 424 of file Range.hpp.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines