Skip to content

Commit

Permalink
Merge pull request #1801 from monarchdodra/setIntersection
Browse files Browse the repository at this point in the history
rework setIntersection
  • Loading branch information
andralex committed Dec 29, 2013
2 parents 4bc307f + 766f551 commit 6c6c40f
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions std/algorithm.d
Expand Up @@ -11195,7 +11195,7 @@ ranges). The ranges are assumed to be sorted by $(D less). The element
types of the ranges must have a common type.
*/
struct SetIntersection(alias less = "a < b", Rs...)
if (allSatisfy!(isInputRange, Rs) &&
if (Rs.length >= 2 && allSatisfy!(isInputRange, Rs) &&
!is(CommonType!(staticMap!(ElementType, Rs)) == void))
{
private:
Expand All @@ -11206,25 +11206,25 @@ private:
// Positions to the first elements that are all equal
void adjustPosition()
{
outer:
while (!empty)
if (empty) return;

size_t done = Rs.length;
static if (Rs.length > 1) while (true)
{
foreach (i, ref r; _input[0 .. $ - 1])
foreach (i, ref r; _input)
{
alias next = _input[i + 1];
if (comp(r.front, next.front))
{
r.popFront();
continue outer;
}
alias next = _input[(i + 1) % Rs.length];

if (comp(next.front, r.front))
{
next.popFront();
continue outer;
do {
next.popFront();
if (next.empty) return;
} while(comp(next.front, r.front));
done = Rs.length;
}
if (--done == 0) return;
}

return;
}
}

Expand All @@ -11248,10 +11248,10 @@ public:
void popFront()
{
assert(!empty);
foreach (i, ref r; _input[0 .. $ - 1])
static if (Rs.length > 1) foreach (i, ref r; _input)
{
alias next = _input[i + 1];
assert(!comp(r.front, next.front) && !comp(next.front, r.front));
alias next = _input[(i + 1) % Rs.length];
assert(!comp(r.front, next.front));
}

foreach (ref r; _input)
Expand All @@ -11272,19 +11272,18 @@ public:
@property SetIntersection save()
{
auto ret = this;
foreach (ti, elem; _input)
foreach (i, ref r; _input)
{
ret._input[ti] = elem.save;
ret._input[i] = r.save;
}
return ret;
}
}
}

/// Ditto
SetIntersection!(less, Rs) setIntersection(alias less = "a < b", Rs...)
(Rs ranges)
if (allSatisfy!(isInputRange, Rs) &&
SetIntersection!(less, Rs) setIntersection(alias less = "a < b", Rs...)(Rs ranges)
if (Rs.length >= 2 && allSatisfy!(isInputRange, Rs) &&
!is(CommonType!(staticMap!(ElementType, Rs)) == void))
{
return typeof(return)(ranges);
Expand Down

0 comments on commit 6c6c40f

Please sign in to comment.