Top of document
©Copyright 1999 Rogue Wave Software

Tips and Techniques for Building Algorithms

This sections describes some techniques that use features of iterators to increase the flexibility and efficiency of your algorithms.

The iterator_category Primitive

Sometimes an algorithm that can be implemented most efficiently with a random access iterator can also work with less powerful iterators. The Standard C++ Library includes primitives that allow a single algorithm to provide several different implementations, depending upon the power of the iterator passed into it. The following example demonstrates the usual technique for setting up multiple versions of the same algorithm.

 // Note, this requires that the iterators be derived from 
 // Standard base types, unless the iterators are simple pointers.
 
 namespace my_namespace {
 
 template <class Iterator>
 Iterator union(Iterator first1, Iterator last1,
                Iterator first2, Iterator last2,
                Iterator Result)
 {  
   return union_aux(first1,last1,first2,last2,Result,
                    iterator_category(first1));
 }
 
 template <class Iterator>
 Iterator union_aux(Iterator first1, Iterator last1,
                Iterator first2, Iterator last2,
                Iterator Result, forward_iterator_tag)
 {
   // General but less efficient implementation
 }
 
 template <class Iterator>
 Iterator union_aux(Iterator first1, Iterator last1,
                Iterator first2, Iterator last2,
                Iterator Result,                     
                random_access_iterator_tag)
 {
   // More efficient implementation
 }
 
 } // End of my_namespace
 

The iterator primitive iterator_category() returns a tag that selects and uses the best available implementation of the algorithm. In order for iterator_category() to work, the iterator provided to the algorithm must be either a simple pointer type, or derived from one of the basic Standard C++ Library iterator types. When you use the iterator_category() primitive, the default implementation of the algorithm should expect at most a forward iterator. This default version will be used if the algorithm encounters an iterator that is not a simple pointer or derived from a basic standard iterator. (Note that input and output iterators are less capable than forward iterators, but that the requirements of an algorithms generally mandate read/write capabilities.)

The distance and advance Primitives

The value_type primitive lets you determine the type of value pointed to by an iterator. Similarly, you can use the distance_type primitive to get a type that represents distances between iterators.

In order to efficiently find the distance between two iterators, regardless of their capabilities, you can use the distance primitive. The distance primitive uses the technique shown above to send a calling program to one of four different implementations. This offers a considerable gain in efficiency, since an implementation for a forward iterator must step through the range defined by the two iterators:

Distance d = 0;
 while (start++ != end)
   d++;
 

whereas an implementation for a random access iterator can simply subtract the start iterator from the end iterator:

Distance d = end - start;
 

Similar gains are available with the advance primitive, which allows you to step forward (or backward) an arbitrary number of steps as efficiently as possible for a particular iterator.


Top of document