2 * This is a public domain version of qsort.d. All it does is call C's
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
9 module core
.internal
.qsort
;
13 import core
.stdc
.stdlib
;
21 else version (WatchOS
)
24 // qsort_r was added in glibc in 2.8. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88127
25 version (CRuntime_Glibc
)
29 import gcc
.config
: Have_Qsort_R
;
30 enum Glibc_Qsort_R
= Have_Qsort_R
;
34 enum Glibc_Qsort_R
= true;
39 enum Glibc_Qsort_R
= false;
42 static if (Glibc_Qsort_R
)
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
);
47 extern (C
) void[] _adSort(return scope void[] a
, TypeInfo ti
)
49 extern (C
) int cmp(scope const void* p1
, scope const void* p2
, scope void* ti
)
51 return (cast(TypeInfo
)ti
).compare(p1
, p2
);
53 qsort_r(a
.ptr
, a
.length
, ti
.tsize
, &cmp, cast(void*)ti
);
57 else version (FreeBSD
)
59 alias extern (C
) int function(scope void *, scope const void *, scope const void *) Cmp
;
60 extern (C
) void qsort_r(scope void *base
, size_t nmemb
, size_t size
, scope void *thunk
, Cmp
cmp);
62 extern (C
) void[] _adSort(return scope void[] a
, TypeInfo ti
)
64 extern (C
) int cmp(scope void* ti
, scope const void* p1
, scope const void* p2
)
66 return (cast(TypeInfo
)ti
).compare(p1
, p2
);
68 qsort_r(a
.ptr
, a
.length
, ti
.tsize
, cast(void*)ti
, &cmp);
72 else version (DragonFlyBSD
)
74 alias extern (C
) int function(scope void *, scope const void *, scope const void *) Cmp
;
75 extern (C
) void qsort_r(scope void *base
, size_t nmemb
, size_t size
, scope void *thunk
, Cmp
cmp);
77 extern (C
) void[] _adSort(return scope void[] a
, TypeInfo ti
)
79 extern (C
) int cmp(scope void* ti
, scope const void* p1
, scope const void* p2
)
81 return (cast(TypeInfo
)ti
).compare(p1
, p2
);
83 qsort_r(a
.ptr
, a
.length
, ti
.tsize
, cast(void*)ti
, &cmp);
89 alias extern (C
) int function(scope void *, scope const void *, scope const void *) Cmp
;
90 extern (C
) void qsort_r(scope void *base
, size_t nmemb
, size_t size
, scope void *thunk
, Cmp
cmp);
92 extern (C
) void[] _adSort(return scope void[] a
, TypeInfo ti
)
94 extern (C
) int cmp(scope void* ti
, scope const void* p1
, scope const void* p2
)
96 return (cast(TypeInfo
)ti
).compare(p1
, p2
);
98 qsort_r(a
.ptr
, a
.length
, ti
.tsize
, cast(void*)ti
, &cmp);
102 else version (CRuntime_UClibc
)
104 alias extern (C
) int function(scope const void *, scope const void *, scope void *) __compar_d_fn_t
;
105 extern (C
) void qsort_r(scope void *base
, size_t nmemb
, size_t size
, __compar_d_fn_t
cmp, scope void *arg
);
107 extern (C
) void[] _adSort(return scope void[] a
, TypeInfo ti
)
109 extern (C
) int cmp(scope const void* p1
, scope const void* p2
, scope void* ti
)
111 return (cast(TypeInfo
)ti
).compare(p1
, p2
);
113 qsort_r(a
.ptr
, a
.length
, ti
.tsize
, &cmp, cast(void*)ti
);
119 private TypeInfo tiglobal
;
121 extern (C
) void[] _adSort(return scope void[] a
, TypeInfo ti
)
123 extern (C
) int cmp(scope const void* p1
, scope const void* p2
)
125 return tiglobal
.compare(p1
, p2
);
128 qsort(a
.ptr
, a
.length
, ti
.tsize
, &cmp);
135 debug(qsort
) printf("array.sort.unittest()\n");
137 int[] a
= new int[10];
150 _adSort(*cast(void[]*)&a
, typeid(a
[0]));
152 for (int i
= 0; i
< a
.length
- 1; i
++)
154 //printf("i = %d", i);
155 //printf(" %d %d\n", a[i], a[i + 1]);
156 assert(a
[i
] <= a
[i
+ 1]);
162 debug(qsort
) printf("struct.sort.unittest()\n");
164 static struct TestStruct
168 int opCmp(const TestStruct rhs
) const
170 return value
<= rhs
.value ?
171 value
< rhs
.value ?
-1 : 0 : 1;
175 TestStruct
[] a
= new TestStruct
[10];
177 a
[0] = TestStruct(23);
178 a
[1] = TestStruct(1);
179 a
[2] = TestStruct(64);
180 a
[3] = TestStruct(5);
181 a
[4] = TestStruct(6);
182 a
[5] = TestStruct(5);
183 a
[6] = TestStruct(17);
184 a
[7] = TestStruct(3);
185 a
[8] = TestStruct(0);
186 a
[9] = TestStruct(-1);
188 _adSort(*cast(void[]*)&a
, typeid(TestStruct
));
190 for (int i
= 0; i
< a
.length
- 1; i
++)
192 //printf("i = %d", i);
193 //printf(" %d %d\n", a[i], a[i + 1]);
194 assert(a
[i
] <= a
[i
+ 1]);