"the last change" for ranges

MLT none at anone.com
Wed May 20 15:47:44 PDT 2009


Andrei Alexandrescu Wrote:

> In wake of a few discussion I've witnessed, I'm thinking of a last 
> change for ranges. (In fact there's one more, but that's minor.)
> 
> The problem is that input ranges and forward ranges have the same 
> syntactic interface, but different semantic interfaces. Consider the 
> problem of finding the first two identical adjacent items in a range:
> 
> R adjacentFind(R)(R r)
> {
>      if (r.empty) return r;
>      R last = r;
>      r.popFront;
>      for (; !r.empty && last.front != r.front; last.popFront, r.popFront)
>      {
>      }
>      return r;
> }
> 
> This will work properly on lists and vectors, but horrendously on files 
> and sockets. This is because input ranges can't be saved for later use: 
> incrementing r also increments popFront and essentially forces both to 
> look at the same current value.
> 

I think that if
>      R last = r;
then after
>      r.popFront;
the order of elements in "last" should not change, no matter what type of range you are dealing with. (That means that input operations would be buffered to the leftmost range that still extsts.)

If I understood the logic of ranges, popFront() just changes the range, and not the elements it points to.



More information about the Digitalmars-d mailing list