We need to rethink remove in std.container

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Tue Feb 22 06:23:00 PST 2011


On 2/22/11 7:58 AM, Steven Schveighoffer wrote:
> A cursor in dcollections is actually a zero or one element range. It can
> only reference exactly one element. Since it has no references to other
> elements, it is immune to operations that use the surrounding elements.

At this exact point I had what would be either a good idea or a brainfart.

I've been thinking for a good while to define a "singleton range", a 
range that has at most one element. I had the intuition that a singleton 
range is a useful concept, but I felt it was a bit tenuous to argue in 
its favor (mostly because one could easily simulate it with repeat(x, 
1)), so I never put it in std.range.

The definition would go like this:

auto singletonRange(T)(T element)
{
     static struct Result
     {
         private T _element;
         private bool _done;
         @property bool empty() { return _done; }
         @property auto front() { assert(!empty); return _element; }
         void popFront() { assert(!empty); _done = true; }
         auto save() { return this; }
     }

     return Result(element, false);
}

But now I realized that singleton ranges are your cursors, so I'll 
definitely add them. And you also made me realize that a singleton range 
is very different from other ranges in the same way 1 is different from 
other numbers. Great.

This may as well be The Great Unification that was needed to converge 
std.container with dcollections in a harmonious whole.


Andrei


More information about the Digitalmars-d mailing list