| 1 |
/** |
| 2 |
* This is a public domain version of qsort.d. All it does is call C's |
| 3 |
* qsort(). |
| 4 |
* |
| 5 |
* Copyright: Copyright Digital Mars 2000 - 2010. |
| 6 |
* License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0). |
| 7 |
* Authors: Walter Bright, Martin Nowak |
| 8 |
*/ |
| 9 |
module core.internal.qsort; |
| 10 |
|
| 11 |
//debug=qsort; |
| 12 |
|
| 13 |
debug (qsort) import core.stdc.stdio : printf; |
| 14 |
|
| 15 |
version (OSX) |
| 16 |
version = Darwin; |
| 17 |
else version (iOS) |
| 18 |
version = Darwin; |
| 19 |
else version (TVOS) |
| 20 |
version = Darwin; |
| 21 |
else version (WatchOS) |
| 22 |
version = Darwin; |
| 23 |
|
| 24 |
// qsort_r was added in glibc in 2.8. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88127 |
| 25 |
version (CRuntime_Glibc) |
| 26 |
{ |
| 27 |
version (GNU) |
| 28 |
{ |
| 29 |
import gcc.config : Have_Qsort_R; |
| 30 |
enum Glibc_Qsort_R = Have_Qsort_R; |
| 31 |
} |
| 32 |
else |
| 33 |
{ |
| 34 |
enum Glibc_Qsort_R = true; |
| 35 |
} |
| 36 |
} |
| 37 |
else |
| 38 |
{ |
| 39 |
enum Glibc_Qsort_R = false; |
| 40 |
} |
| 41 |
|
| 42 |
static if (Glibc_Qsort_R) |
| 43 |
{ |
| 44 |
alias extern (C) int function(scope const void *, scope const void *, scope void *) Cmp; |
| 45 |
extern (C) void qsort_r(scope void *base, size_t nmemb, size_t size, Cmp cmp, scope void *arg); |
| 46 |
|
| 47 |
extern (C) void[] _adSort(return scope void[] a, TypeInfo ti) |
| 48 |
{ |
| 49 |
extern (C) int cmp(scope const void* p1, scope const void* p2, scope void* ti) |
| 50 |
{ |
| 51 |
return (cast(TypeInfo)ti).compare(p1, p2); |
| 52 |
} |
| 53 |
qsort_r(a.ptr, a.length, ti.tsize, &cmp, cast(void*)ti); |
| 54 |
return a; |
| 55 |
} |
| 56 |
} |
| 57 |
else version (FreeBSD) |
| 58 |
{ |
| 59 |
import core.sys.freebsd.config : __FreeBSD_version; |
| 60 |
|
| 61 |
static if (__FreeBSD_version >= 1400000) |
| 62 |
{ |
| 63 |
// FreeBSD changed qsort_r function signature to POSIX in FreeBSD 14.0 |
| 64 |
alias extern (C) int function(scope const void*, scope const void*, scope void*) Cmp; |
| 65 |
extern (C) void qsort_r(scope void* base, size_t nmemb, size_t size, Cmp cmp, scope void* thunk); |
| 66 |
|
| 67 |
extern (C) void[] _adSort(return scope void[] a, TypeInfo ti) |
| 68 |
{ |
| 69 |
extern (C) int cmp(scope const void* p1, scope const void* p2, scope void* ti) |
| 70 |
{ |
| 71 |
return (cast(TypeInfo)ti).compare(p1, p2); |
| 72 |
} |
| 73 |
qsort_r(a.ptr, a.length, ti.tsize, &cmp, cast(void*)ti); |
| 74 |
return a; |
| 75 |
} |
| 76 |
} |
| 77 |
else |
| 78 |
{ |
| 79 |
alias extern (C) int function(scope void *, scope const void *, scope const void *) Cmp; |
| 80 |
extern (C) void qsort_r(scope void* base, size_t nmemb, size_t size, scope void* thunk, Cmp cmp); |
| 81 |
|
| 82 |
extern (C) void[] _adSort(return scope void[] a, TypeInfo ti) |
| 83 |
{ |
| 84 |
extern (C) int cmp(scope void* ti, scope const void* p1, scope const void* p2) |
| 85 |
{ |
| 86 |
return (cast(TypeInfo)ti).compare(p1, p2); |
| 87 |
} |
| 88 |
qsort_r(a.ptr, a.length, ti.tsize, cast(void*)ti, &cmp); |
| 89 |
return a; |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
else version (DragonFlyBSD) |
| 94 |
{ |
| 95 |
alias extern (C) int function(scope void *, scope const void *, scope const void *) Cmp; |
| 96 |
extern (C) void qsort_r(scope void *base, size_t nmemb, size_t size, scope void *thunk, Cmp cmp); |
| 97 |
|
| 98 |
extern (C) void[] _adSort(return scope void[] a, TypeInfo ti) |
| 99 |
{ |
| 100 |
extern (C) int cmp(scope void* ti, scope const void* p1, scope const void* p2) |
| 101 |
{ |
| 102 |
return (cast(TypeInfo)ti).compare(p1, p2); |
| 103 |
} |
| 104 |
qsort_r(a.ptr, a.length, ti.tsize, cast(void*)ti, &cmp); |
| 105 |
return a; |
| 106 |
} |
| 107 |
} |
| 108 |
else version (Darwin) |
| 109 |
{ |
| 110 |
alias extern (C) int function(scope void *, scope const void *, scope const void *) Cmp; |
| 111 |
extern (C) void qsort_r(scope void *base, size_t nmemb, size_t size, scope void *thunk, Cmp cmp); |
| 112 |
|
| 113 |
extern (C) void[] _adSort(return scope void[] a, TypeInfo ti) |
| 114 |
{ |
| 115 |
extern (C) int cmp(scope void* ti, scope const void* p1, scope const void* p2) |
| 116 |
{ |
| 117 |
return (cast(TypeInfo)ti).compare(p1, p2); |
| 118 |
} |
| 119 |
qsort_r(a.ptr, a.length, ti.tsize, cast(void*)ti, &cmp); |
| 120 |
return a; |
| 121 |
} |
| 122 |
} |
| 123 |
else version (CRuntime_UClibc) |
| 124 |
{ |
| 125 |
alias extern (C) int function(scope const void *, scope const void *, scope void *) __compar_d_fn_t; |
| 126 |
extern (C) void qsort_r(scope void *base, size_t nmemb, size_t size, __compar_d_fn_t cmp, scope void *arg); |
| 127 |
|
| 128 |
extern (C) void[] _adSort(return scope void[] a, TypeInfo ti) |
| 129 |
{ |
| 130 |
extern (C) int cmp(scope const void* p1, scope const void* p2, scope void* ti) |
| 131 |
{ |
| 132 |
return (cast(TypeInfo)ti).compare(p1, p2); |
| 133 |
} |
| 134 |
qsort_r(a.ptr, a.length, ti.tsize, &cmp, cast(void*)ti); |
| 135 |
return a; |
| 136 |
} |
| 137 |
} |
| 138 |
else |
| 139 |
{ |
| 140 |
import core.stdc.stdlib : qsort; |
| 141 |
|
| 142 |
private TypeInfo tiglobal; |
| 143 |
|
| 144 |
extern (C) void[] _adSort(return scope void[] a, TypeInfo ti) |
| 145 |
{ |
| 146 |
extern (C) int cmp(scope const void* p1, scope const void* p2) |
| 147 |
{ |
| 148 |
return tiglobal.compare(p1, p2); |
| 149 |
} |
| 150 |
tiglobal = ti; |
| 151 |
qsort(a.ptr, a.length, ti.tsize, &cmp); |
| 152 |
return a; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
unittest |
| 157 |
{ |
| 158 |
debug(qsort) printf("array.sort.unittest()\n"); |
| 159 |
|
| 160 |
int[] a = new int[10]; |
| 161 |
|
| 162 |
a[0] = 23; |
| 163 |
a[1] = 1; |
| 164 |
a[2] = 64; |
| 165 |
a[3] = 5; |
| 166 |
a[4] = 6; |
| 167 |
a[5] = 5; |
| 168 |
a[6] = 17; |
| 169 |
a[7] = 3; |
| 170 |
a[8] = 0; |
| 171 |
a[9] = -1; |
| 172 |
|
| 173 |
_adSort(*cast(void[]*)&a, typeid(a[0])); |
| 174 |
|
| 175 |
for (int i = 0; i < a.length - 1; i++) |
| 176 |
{ |
| 177 |
debug(qsort) printf("i = %d", i); |
| 178 |
debug(qsort) printf(" %d %d\n", a[i], a[i + 1]); |
| 179 |
assert(a[i] <= a[i + 1]); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
unittest |
| 184 |
{ |
| 185 |
debug(qsort) printf("struct.sort.unittest()\n"); |
| 186 |
|
| 187 |
static struct TestStruct |
| 188 |
{ |
| 189 |
int value; |
| 190 |
|
| 191 |
int opCmp(const TestStruct rhs) const |
| 192 |
{ |
| 193 |
return value <= rhs.value ? |
| 194 |
value < rhs.value ? -1 : 0 : 1; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
TestStruct[] a = new TestStruct[10]; |
| 199 |
|
| 200 |
a[0] = TestStruct(23); |
| 201 |
a[1] = TestStruct(1); |
| 202 |
a[2] = TestStruct(64); |
| 203 |
a[3] = TestStruct(5); |
| 204 |
a[4] = TestStruct(6); |
| 205 |
a[5] = TestStruct(5); |
| 206 |
a[6] = TestStruct(17); |
| 207 |
a[7] = TestStruct(3); |
| 208 |
a[8] = TestStruct(0); |
| 209 |
a[9] = TestStruct(-1); |
| 210 |
|
| 211 |
_adSort(*cast(void[]*)&a, typeid(TestStruct)); |
| 212 |
|
| 213 |
for (int i = 0; i < a.length - 1; i++) |
| 214 |
{ |
| 215 |
//printf("i = %d", i); |
| 216 |
//printf(" %d %d\n", a[i], a[i + 1]); |
| 217 |
assert(a[i] <= a[i + 1]); |
| 218 |
} |
| 219 |
} |