Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue 8085 #987

Merged
merged 4 commits into from
Dec 17, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 95 additions & 8 deletions std/algorithm.d
Original file line number Diff line number Diff line change
Expand Up @@ -2803,17 +2803,16 @@ if (isInputRange!RoR && isInputRange!(ElementType!RoR))
private:
RoR _items;
ElementType!RoR _current;
bool _valid_current;
void prepare()
{
for (;; _items.popFront())
// Skip over empty subranges.
if (_items.empty) return;
while (_items.front.empty)
{
_items.popFront();
if (_items.empty) return;
if (!_items.front.empty) break;
}
_current = _items.front;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd need to make this (provided we have forward iteration):

_current = _items.front.save;

To be able to make joiner a true forward range.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, that's not enough. If the subranges are themselves ranges of ranges, then their respective subranges may be consumed if the user iterates over the .save'd range.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah... But now we're back to the square one argument: That is just the element type of the "RoR" that we are iterating over. They are just elements, but not part of our range interface. This "third level" range is just as irrelevant to us as the "second level" range is irrelevant to array.save.

We were tasked with transforming the RoR into a R. The fact that those elements are also themselves ranges is not our problem, and shouldn't be our problem. Maybe the user wanted to have a RoR whose elements are mutable char[], which are not meant to be duplicated. What do we know?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, the .save exposed by joiner only guarantees saving the state of the joined ranged itself, it says nothing about the state of the elements themselves. My bad.

_valid_current = true;
_items.popFront();
}
public:
this(RoR r)
Expand All @@ -2829,7 +2828,7 @@ if (isInputRange!RoR && isInputRange!(ElementType!RoR))
{
@property auto empty()
{
return !_valid_current || _current.empty;
return _items.empty;
}
}
@property auto ref front()
Expand All @@ -2841,7 +2840,12 @@ if (isInputRange!RoR && isInputRange!(ElementType!RoR))
{
assert(!_current.empty);
_current.popFront();
if (_current.empty) prepare();
if (_current.empty)
{
assert(!_items.empty);
_items.popFront();
prepare();
}
}
static if (isForwardRange!RoR && isForwardRange!(ElementType!RoR))
{
Expand All @@ -2850,7 +2854,6 @@ if (isInputRange!RoR && isInputRange!(ElementType!RoR))
Result copy;
copy._items = _items.save;
copy._current = _current.save;
copy._valid_current = _valid_current;
return copy;
}
}
Expand Down Expand Up @@ -2897,6 +2900,90 @@ unittest
assert(!equal(js2, js));
}

unittest
{
struct TransientRange
{
int[128] _buf;
int[][] _values;
this(int[][] values)
{
_values = values;
}
@property bool empty()
{
return _values.length == 0;
}
@property auto front()
{
foreach (i; 0 .. _values.front.length)
{
_buf[i] = _values[0][i];
}
return _buf[0 .. _values.front.length];
}
void popFront()
{
_values = _values[1 .. $];
}
}

auto rr = TransientRange([[1,2], [3,4,5], [], [6,7]]);

// Can't use array() or equal() directly because they fail with transient
// .front.
int[] result;
foreach (c; rr.joiner()) {
result ~= c;
}

assert(equal(result, [1,2,3,4,5,6,7]));
}

// Temporarily disable this unittest due to issue 9131 on OSX/64.
version = Issue9131;
version(Issue9131) {} else
unittest
{
struct TransientRange
{
dchar[128] _buf;
dstring[] _values;
this(dstring[] values)
{
_values = values;
}
@property bool empty()
{
return _values.length == 0;
}
@property auto front()
{
foreach (i; 0 .. _values.front.length)
{
_buf[i] = _values[0][i];
}
return _buf[0 .. _values.front.length];
}
void popFront()
{
_values = _values[1 .. $];
}
}

auto rr = TransientRange(["abc"d, "12"d, "def"d, "34"d]);

// Can't use array() or equal() directly because they fail with transient
// .front.
dchar[] result;
foreach (c; rr.joiner()) {
result ~= c;
}

assert(equal(result, "abc12def34"d),
"Unexpected result: '%s'"d.format(result));
}

// uniq
/**
Iterates unique consecutive elements of the given range (functionality
Expand Down