RFC on SlidingSplitter Range
monarch_dodra via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Oct 3 12:31:27 PDT 2014
On Friday, 3 October 2014 at 19:12:54 UTC, Nordlöw wrote:
> On Friday, 3 October 2014 at 17:46:18 UTC, monarch_dodra wrote:
>> If your implementation use two ranges that you slice "on the
>> fly", then you can trivially support strings, thanks to
>> popFront.
>
> Very clever. That's what I wanted.
>
>> I threw this together.
>
> Superb!
>
> It could be motivated to use
>
> static if (isRandomAccessRange!R)
> {
> // my solution for member functions...
> private R _data;
> private size_t _index;
> else // assumes R is either string or wstring
> {
> // your solution for member functions...
> private R _original;
> private R _right;
> }
>
> to save space in the RA-case, right?
The idea is to try to keep as much code in common as possible.
You can keep your version, provided you write this for popFront:
void popFront()
{
if (_index < _data.length)
{
static if (isNarrowString!R)
_index += stride(_data, _index)
else
++_index;
}
}
> Is isRandomAccessRange!R the correct way to check for this?
Either that or isNarrowString. given the restrictions, both are
correct.
> Nice unittest ;)
Thanks ;)
>> I left out checks for infinity in favor of brevity:
>
> Last thing for now:
> - What do you mean by this?
I mean that this would not work if you passed in a "repeat(5)"
even though repeat verifies RA and hasSlicing. That's because
there's a point where you check for length.
The "correct" restrictions should have been:
if (isSomeString || hasSlicing && !isInfinite)
Note that this works regardless of operator precedence.
> - Do I have to indicate that this range is not infinite?
You indicate a range is infinite with an "enum empty = false;".
By having a run-time "empty", you are implicitly stating you are
not infinite.
...
That said...
You could perfectly well support infinite ranges, with the
correct static ifs. You'd produce an infinite sliding splitter.
More information about the Digitalmars-d-learn
mailing list