Merge pull request #4425 from JackStouffer/issue16073

Partial Fix for Issue 16073 (part 2)
This commit is contained in:
Steven Schveighoffer 2016-06-14 11:01:59 -04:00 committed by GitHub
commit c5bb43ce54

View file

@ -387,7 +387,7 @@ See_Also:
*/ */
Range partition(alias predicate, Range partition(alias predicate,
SwapStrategy ss = SwapStrategy.unstable, Range)(Range r) SwapStrategy ss = SwapStrategy.unstable, Range)(Range r)
if ((ss == SwapStrategy.stable && isRandomAccessRange!(Range)) if ((ss == SwapStrategy.stable && isRandomAccessRange!(Range) && hasLength!Range && hasSlicing!Range)
|| (ss != SwapStrategy.stable && isForwardRange!(Range))) || (ss != SwapStrategy.stable && isForwardRange!(Range)))
{ {
alias pred = unaryFun!(predicate); alias pred = unaryFun!(predicate);
@ -403,7 +403,7 @@ Range partition(alias predicate,
const middle = r.length / 2; const middle = r.length / 2;
alias recurse = .partition!(pred, ss, Range); alias recurse = .partition!(pred, ss, Range);
auto lower = recurse(r[0 .. middle]); auto lower = recurse(r[0 .. middle]);
auto upper = recurse(r[middle .. $]); auto upper = recurse(r[middle .. r.length]);
bringToFront(lower, r[middle .. r.length - upper.length]); bringToFront(lower, r[middle .. r.length - upper.length]);
return r[r.length - lower.length - upper.length .. r.length]; return r[r.length - lower.length - upper.length .. r.length];
} }
@ -440,7 +440,7 @@ Range partition(alias predicate,
{ {
for (;;) for (;;)
{ {
if (lo > hi) return r[lo .. $]; if (lo > hi) return r[lo .. r.length];
if (!pred(r[lo])) break; if (!pred(r[lo])) break;
++lo; ++lo;
} }
@ -448,7 +448,7 @@ Range partition(alias predicate,
assert(lo <= hi); assert(lo <= hi);
for (;;) for (;;)
{ {
if (lo == hi) return r[lo .. $]; if (lo == hi) return r[lo .. r.length];
if (pred(r[hi])) break; if (pred(r[hi])) break;
--hi; --hi;
} }
@ -602,7 +602,7 @@ BUGS: stable $(D partition3) has not been implemented yet.
auto partition3(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable, Range, E) auto partition3(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable, Range, E)
(Range r, E pivot) (Range r, E pivot)
if (ss == SwapStrategy.unstable && isRandomAccessRange!Range if (ss == SwapStrategy.unstable && isRandomAccessRange!Range
&& hasSwappableElements!Range && hasLength!Range && hasSwappableElements!Range && hasLength!Range && hasSlicing!Range
&& is(typeof(binaryFun!less(r.front, pivot)) == bool) && is(typeof(binaryFun!less(r.front, pivot)) == bool)
&& is(typeof(binaryFun!less(pivot, r.front)) == bool) && is(typeof(binaryFun!less(pivot, r.front)) == bool)
&& is(typeof(binaryFun!less(r.front, r.front)) == bool)) && is(typeof(binaryFun!less(r.front, r.front)) == bool))
@ -2338,7 +2338,8 @@ $(D !less(e2, r[nth])). Effectively, it finds the nth smallest
$(BIGOH r.length) (if unstable) or $(BIGOH r.length * log(r.length)) $(BIGOH r.length) (if unstable) or $(BIGOH r.length * log(r.length))
(if stable) evaluations of $(D less) and $(D swap). (if stable) evaluations of $(D less) and $(D swap).
If $(D n >= r.length), the algorithm has no effect and returns `r[0 .. $]`. If $(D n >= r.length), the algorithm has no effect and returns
`r[0 .. r.length]`.
Params: Params:
less = The predicate to sort by. less = The predicate to sort by.
@ -2363,7 +2364,7 @@ auto topN(alias less = "a < b",
static assert(ss == SwapStrategy.unstable, static assert(ss == SwapStrategy.unstable,
"Stable topN not yet implemented"); "Stable topN not yet implemented");
if (nth >= r.length) return r[0 .. $]; if (nth >= r.length) return r[0 .. r.length];
auto ret = r[0 .. nth]; auto ret = r[0 .. nth];
for (;;) for (;;)
@ -2404,7 +2405,7 @@ auto topN(alias less = "a < b",
break; break;
} }
++pivot; // skip the pivot ++pivot; // skip the pivot
r = r[pivot .. $]; r = r[pivot .. r.length];
nth -= pivot; nth -= pivot;
} }
return ret; return ret;