Revert "Merge pull request #1169 from andralex/5924"

This reverts commit 118d9182a9, reversing
changes made to 75f578228c.
This commit is contained in:
jmdavis 2013-02-24 17:14:32 -08:00
parent 35d08ef3d8
commit a8f180a7c7

View file

@ -9056,7 +9056,8 @@ corresponding $(D sort), but $(D schwartzSort) evaluates $(D
transform) only $(D r.length) times (less than half when compared to transform) only $(D r.length) times (less than half when compared to
regular sorting). The usage can be best illustrated with an example. regular sorting). The usage can be best illustrated with an example.
Examples: Example:
---- ----
uint hashFun(string) { ... expensive computation ... } uint hashFun(string) { ... expensive computation ... }
string[] array = ...; string[] array = ...;
@ -9076,51 +9077,24 @@ To check whether an array was sorted and benefit of the speedup of
Schwartz sorting, a function $(D schwartzIsSorted) is not provided Schwartz sorting, a function $(D schwartzIsSorted) is not provided
because the effect can be achieved by calling $(D because the effect can be achieved by calling $(D
isSorted!less(map!transform(r))). isSorted!less(map!transform(r))).
Returns: The initial range wrapped as a $(D SortedRange) with the
predicate $(D (a, b) => binaryFun!less(transform(a),
transform(b))).
*/ */
SortedRange!(R, ((a, b) => binaryFun!less(transform(a), transform(b)))) void schwartzSort(alias transform, alias less = "a < b",
schwartzSort(alias transform, alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable, Range)(Range r)
SwapStrategy ss = SwapStrategy.unstable, R)(R r) if (isRandomAccessRange!(Range) && hasLength!(Range))
if (isRandomAccessRange!R && hasLength!R)
{ {
import core.stdc.stdlib; alias typeof(transform(r.front)) XformType;
alias T = typeof(transform(r.front)); auto xform = new XformType[r.length];
auto xform1 = (cast(T*) malloc(r.length * T.sizeof))[0 .. r.length]; foreach (i, e; r)
size_t length;
scope(exit)
{ {
static if (hasElaborateDestructor!T) xform[i] = transform(e);
{
foreach (i; 0 .. length) collectException(destroy(xform1[i]));
} }
free(xform1); auto z = zip(xform, r);
} alias typeof(z.front) ProxyType;
for (; length != r.length; ++length) bool myLess(ProxyType a, ProxyType b)
{ {
emplace(xform1.ptr + length, transform(r[length])); return binaryFun!less(a[0], b[0]);
} }
// Make sure we use ubyte[] and ushort[], not char[] and wchar[] sort!(myLess, ss)(z);
// for the intermediate array, lest zip gets confused.
static if (isNarrowString!(typeof(xform1)))
{
auto xform = xform1.representation();
}
else
{
alias xform = xform1;
}
zip(xform, r).sort!((a, b) => binaryFun!less(a[0], b[0]), ss)();
return typeof(return)(r);
}
unittest
{
// issue 5924
Tuple!(char)[] chars;
schwartzSort!((Tuple!(char) c){ return c[0]; })(chars);
} }
unittest unittest