C++ for Scientists - Technische Universität Dresden
C++ for Scientists - Technische Universität Dresden
C++ for Scientists - Technische Universität Dresden
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
4.10. CURSORS AND PROPERTY MAPS 123<br />
As illustrated above, iterators are usually used in pairs, where one is used <strong>for</strong> the actual iteration<br />
and the second serves to mark the end of the collection. The iterators are created by the<br />
corresponding container class using standard methods such as begin() and end(). The iterator<br />
returned by begin() points to the first element. The iterator returned by end() points past the<br />
end of elements to mark the end. All algorithms are implemented with right-open intervals<br />
[b, e) operating on the value referred by b until b = e. There<strong>for</strong>e intervals of the <strong>for</strong>m [x, x) are<br />
regarded empty.<br />
A more general (and more useful) algorithm is the linear search on an arbitrary sequence. This<br />
is provided by the STL function find in the following fashion:<br />
template <br />
InputIterator find(InputIterator first, InputIterator last, const T& value) {<br />
while (first != last && ∗first != value)<br />
++first;<br />
return first;<br />
}<br />
find takes three arguments: two iterators that define the right-open interval of the search space,<br />
and a value to search <strong>for</strong> in that range. Each entry referred by ‘first’ is compared with ‘value’.<br />
When a match is found, the iterator pointing to it is returned. If the value is not contained<br />
in the sequence, an iterator equal to ‘last’ is returned. Thus, the caller can test whether the<br />
search was successful by comparing its result with ‘last’. In fact, one must per<strong>for</strong>m this test<br />
because after a failed search the returned iterator cannot dereferred correctly (it points outside<br />
the given range and might cause segmentation violations or corrupt data).<br />
This section only scratched the surface of STL and was primarily intended to introduce the<br />
iterator concept that we will generalize in the following section.<br />
4.10 Cursors and Property Maps<br />
The essential idea of iterators is to represent a position and a referred value. A further generalization<br />
of this idea is to decouple the the notion of position and value. Dietmar Kühl<br />
proposed this mechanism in his master thesis (Diplomarbeit) [?] <strong>for</strong> the generic treatment of<br />
grahps. The Boost Graph Library [?] provides the notion of property maps in the <strong>for</strong>m that<br />
properties are available <strong>for</strong> vertices and edges and all properties can be accessed independently<br />
from each other and from the traversal of the graph.<br />
As case study we implement a simple sparse matrix class with cursors and property maps. The<br />
minimalistic implementation of the sparse matrix is:<br />
#include <br />
#include <br />
#include <br />
#include <br />
template <br />
class coo matrix<br />
{<br />
typedef Value value type; // better in trait<br />
public:<br />
coo matrix(int nr, int nc) : nr(nr), nc(nc) {}