Correctly implementing a bidirectional range on a linked list?

Alex Parrill via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 6 14:30:34 PDT 2015


On Monday, 6 July 2015 at 20:50:19 UTC, Gary Willoughby wrote:
> The problem is when I call the save method of the forward range 
> interface I don't get a copy I only get another view to the 
> same state. So when i remove nodes from the original list the 
> range becomes invalid.
>

This is why modifying a sequence while iterating over it is 
generally forbidden; there's no good way to make it work. You 
could duplicate the entire list for each range, but that's 
expensive for the usual case of simply reading the list.

Forward ranges don't require that ranges be independent of the 
data they iterate over [1]:

> Saving a range is not duplicating it; in the example above, r1
> and r2 still refer to the same underlying data. They just
> navigate that data independently.

IMO your implementation is fine.

[1]: 
http://dlang.org/phobos/std_range_primitives.html#isForwardRange


More information about the Digitalmars-d-learn mailing list