Bug 5871 – schwartzSort with stable SwapStrategy errors
Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2011-04-22T18:47:00Z
Last change time
2014-03-19T17:34:14Z
Keywords
rejects-valid, wrong-code
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2011-04-22T18:47:13Z
This D2 program:
import std.algorithm;
void main() {
char[] s = "test".dup;
int[] indexes = [0, 1, 2, 3];
schwartzSort!((int i){ return s.count(s[i]); }, "b < a", SwapStrategy.stable)(indexes);
}
With DMD 2.052 gives the error:
...\dmd\src\phobos\std\algorithm.d(6121): Error: undefined identifier __dollar
Once "fixed" that line replacing $ with r.length more successive similar errors appear.
Comment #1 by bearophile_hugs — 2011-04-22T19:36:53Z
This compiles, s is a int[] instead of char[]:
import std.algorithm, std.stdio;
void main() {
int[] s = [10, 20, 30, 10, 20, 10];
int[] indexes = [0, 1, 2, 3, 4, 5];
schwartzSort!((int i){ return s.count(s[i]); }, "b < a", SwapStrategy.stable)(indexes);
writeln(indexes); // output: [0, 1, 2, 3, 4, 5]
}
Comment #2 by kennytm — 2011-04-22T23:44:54Z
1. Your 2nd program does not compile for me, both the 2.052 and the git master version.
/usr/include/phobos/std/algorithm.d(5841): Error: undefined identifier __dollar
2. After changing 3 '$' to '.length' both program compiles successfully.
diff --git a/std/algorithm.d b/std/algorithm.d
index fd5df21..376c906 100644
--- a/std/algorithm.d
+++ b/std/algorithm.d
@@ -5838,7 +5838,7 @@ Range partition(alias predicate,
const middle = r.length / 2;
alias .partition!(pred, ss, Range) recurse;
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]);
return r[r.length - lower.length - upper.length .. r.length];
}
@@ -6385,11 +6385,11 @@ void sortImpl(alias less, SwapStrategy ss, Range)(Range r)
// find the last occurrence of the pivot
bool pred2(Elem a) { return less(pivot, a); }
//auto lastPivotPos = find!(pred2)(pivotsRight[1 .. $]).ptr;
- auto pivotRunLen = find!(pred2)(pivotSpan[1 .. $]).length;
+ auto pivotRunLen = find!(pred2)(pivotSpan[1 .. pivotSpan.length]).length;
pivotSpan = pivotSpan[0 .. pivotRunLen + 1];
// now rotate firstPivotPos..lastPivotPos to the front
bringToFront(r, pivotSpan);
- r = r[pivotSpan.length .. $];
+ r = r[pivotSpan.length .. r.length];
}
else
{
3. Even with this patch (I don't know if it's correct), the 2nd program won't work correctly because of bug 4584 and bug 3638 (they're the same?).
Comment #3 by bearophile_hugs — 2011-04-23T03:58:37Z
(In reply to comment #2)
> 1. Your 2nd program does not compile for me, both the 2.052 and the git master
> version.
You are right.
Comment #4 by lt.infiltrator — 2014-03-19T17:34:14Z
The code provided in the description compiler as of v2.065.