Iterator of the Week

This is on going series of articles showcasing
different uses for STL style iterators in C++. All source code presented here
is copyright 1998-2002 James M. Curran, but may be used freely. All comment and suggestion are welcome.

The common usage of an iterator is to have it start at the
beginning and progress to the end. But
what if you don’t want to get off the roller coaster? What if you want through
the container over and over again, just like a circular buffer. For that, we need a “circular” iterator.

First of all, we are going to want to use it
for many different types of containers, so it’s going to have to be a template.
(Even if we only want to use it for, say, vectors, the compiler still considers
vector<int> and vector<string> different types, so we’ll still need
to templatize the iterator.)

std::iterator<> base class is used to tell the
compiler that this iterator has all the abilities of a forward iterator, and
can be used wherever one is required.
The class is called “const_circular_iter” because we can only read
values from it; we cannot modify the container using this iterator. (We’ll get
to a modifiable circular iterator in a future article)

protected:
T::iterator begin;
T::iterator end;
T::iterator iter;

Now, clearly the principle behind this is that we act just
like a normal iterator, until the point where we reach the end, where we jump
back to the beginning. So, clearly, we are going to need to know, where the end is, where the beginning is, and where
we are now. These three iterators handle that. You might think we can get by without begin & end, if we
hold a reference to the container we are iterating through. True, but that limits us to step completely
through the container. This way we can loop over a few elements in the middle of a large container. Further, this allows us to iterators that
don’t point into a container. We’ll get do some of them in a future article as well.

And yes, I did cringe over making data elements protected
instead of private, but we will soon be getting to deriving the modifiable
circular iterator from this, and that is so tightly linked to this that
avoiding using protected would cause a lot of pointless extra work.

Here, finally, we reach the heart of the class: the prefix
increment operator. It’s operation is
quite obvious. Increment the internal
iterator; when we reach the end, point back to the beginning. That’s it.
From here on, the rest is pretty much just boilerplate and buck passing.

The postfix increment operator delegates much of its work to the prefix increment. The dereference operator passes its work onto the internal iterator dereference operator; and the inequity operator uses the equity operator, and we’re done.

Now you may be asking, “If we are going to create a
modifiable circular iterator, why are we wasting our time now with a non-modifiable
one?”Well, sometimes, the underlying iterators is so opposed to being used to modify what it points to, it won’t even compile
the modifiable template. What kind of iterator is this? Remember when I said
some iterators don’t point into containers?
Stay tuned.