Aggregate of two input iterator adapters. More...
#include <Range.hpp>
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. | |
| T | 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). | |
Aggregate of two input iterator adapters.
| T | Value 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
Definition at line 90 of file Range.hpp.
| TRTK::Range< T >::Range | ( | const Iterator< T > & | begin, |
| const Iterator< T > & | end | ||
| ) |
| bool TRTK::Range< T >::isEmpty | ( | ) | const |
| 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
vector<double> vec;
Range<double> range = make_range(vec);
if (range) doSomething(); // instead of using it.isValid()
| bool TRTK::Range< T >::operator== | ( | const Range< T > & | other ) | const |
Comparison operator.
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
| size_t TRTK::Range< T >::size | ( | ) | const |
| 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.
Documentation generated by Doxygen