Get rid of isInfinite()?

Jonathan M Davis jmdavisProg at gmx.com
Mon Jun 25 23:05:30 PDT 2012


On Tuesday, June 26, 2012 05:40:10 Mehrdad wrote:
> On Monday, 25 June 2012 at 23:03:59 UTC, Jonathan M Davis wrote:
> > You could store those elements internally as you iterate over
> > them
> 
> That's *precisely* the point of my wrapper... sorry if that
> wasn't clear.
> 
> Why shouldn't that be sufficient for making it random-access?
> 
> > If you can somehow figure out how to do that via buffering,
> > then you could make it a forward range as well as whatever
> > other range types you could define the functions for, but you'd
> > have to figure out a way to define save.
> 
> OK, now we're at the same place. :P
> 
> What I'm saying is, I __CAN__ get the buffering to work.
> 
> What I __cannot__ figure out what to do with is 'length'... I
> can't think of anything reasonable to return, since it's not
> infinite (which I might represent as ulong.max, if anything) but
> it's unbounded.
> 
> 
> So the _ONLY_ problem I'm running into right now is length() --
> any ideas how I could fix that?

If you can't calculate the length in O(1), then you're stuck using walkLength, 
which means iterating over the entire range to get its length. And if that's 
the case, then you can't make it a random access range, because for it to be a 
random access range, it either needs to have a length property (which must be 
O(1)) or be infinite. And I would fully expect for your code to run into a 
variety of bugs if you tried to claim that it was a length that it wasn't 
(e.g. ulong.max), since a number of functions will expect that that's its 
actual length and won't work properly if it's not.

For a range to be random access, it must fulfill these requirements:

template isRandomAccessRange(R)
{
    enum bool isRandomAccessRange = is(typeof(
    (inout int _dummy=0)
    {
        static assert(isBidirectionalRange!R ||
                      isForwardRange!R && isInfinite!R);
        R r = void;
        auto e = r[1];
        static assert(!isNarrowString!R);
        static assert(hasLength!R || isInfinite!R);
    }));
}

and I'm not sure that you can squeeze your range into that. You could do it if 
you just read in the entire range and created another range from it (be it an 
array or whatever), but you appear to be attempting something which probably 
won't quite work with the current range design. I suspect that you'd do better 
trying to claim that it wasn infinite rather than had a length, but then you'd 
have to be able to deal with the fact that empty is always false.

- Jonathan M Davis


More information about the Digitalmars-d mailing list