Merge pull request #1801 from monarchdodra/setIntersection

rework setIntersection
This commit is contained in:
Andrei Alexandrescu 2013-12-29 07:40:46 -08:00
commit 6c6c40f299

View file

@ -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. types of the ranges must have a common type.
*/ */
struct SetIntersection(alias less = "a < b", Rs...) struct SetIntersection(alias less = "a < b", Rs...)
if (allSatisfy!(isInputRange, Rs) && if (Rs.length >= 2 && allSatisfy!(isInputRange, Rs) &&
!is(CommonType!(staticMap!(ElementType, Rs)) == void)) !is(CommonType!(staticMap!(ElementType, Rs)) == void))
{ {
private: private:
@ -11206,25 +11206,25 @@ private:
// Positions to the first elements that are all equal // Positions to the first elements that are all equal
void adjustPosition() void adjustPosition()
{ {
outer: if (empty) return;
while (!empty)
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]; alias next = _input[(i + 1) % Rs.length];
if (comp(r.front, next.front))
{
r.popFront();
continue outer;
}
if (comp(next.front, r.front)) if (comp(next.front, r.front))
{ {
next.popFront(); do {
continue outer; next.popFront();
if (next.empty) return;
} while(comp(next.front, r.front));
done = Rs.length;
} }
if (--done == 0) return;
} }
return;
} }
} }
@ -11248,10 +11248,10 @@ public:
void popFront() void popFront()
{ {
assert(!empty); assert(!empty);
foreach (i, ref r; _input[0 .. $ - 1]) static if (Rs.length > 1) foreach (i, ref r; _input)
{ {
alias next = _input[i + 1]; alias next = _input[(i + 1) % Rs.length];
assert(!comp(r.front, next.front) && !comp(next.front, r.front)); assert(!comp(r.front, next.front));
} }
foreach (ref r; _input) foreach (ref r; _input)
@ -11272,9 +11272,9 @@ public:
@property SetIntersection save() @property SetIntersection save()
{ {
auto ret = this; auto ret = this;
foreach (ti, elem; _input) foreach (i, ref r; _input)
{ {
ret._input[ti] = elem.save; ret._input[i] = r.save;
} }
return ret; return ret;
} }
@ -11282,9 +11282,8 @@ public:
} }
/// Ditto /// Ditto
SetIntersection!(less, Rs) setIntersection(alias less = "a < b", Rs...) SetIntersection!(less, Rs) setIntersection(alias less = "a < b", Rs...)(Rs ranges)
(Rs ranges) if (Rs.length >= 2 && allSatisfy!(isInputRange, Rs) &&
if (allSatisfy!(isInputRange, Rs) &&
!is(CommonType!(staticMap!(ElementType, Rs)) == void)) !is(CommonType!(staticMap!(ElementType, Rs)) == void))
{ {
return typeof(return)(ranges); return typeof(return)(ranges);