Mercurial > projects > ldc
comparison druntime/src/compiler/ldc/qsort2.d @ 759:d3eb054172f9
Added copy of druntime from DMD 2.020 modified for LDC.
author | Tomas Lindquist Olsen <tomas.l.olsen@gmail.com> |
---|---|
date | Tue, 11 Nov 2008 01:52:37 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
758:f04dde6e882c | 759:d3eb054172f9 |
---|---|
1 | |
2 /* | |
3 * Placed into Public Domain | |
4 * written by Walter Bright | |
5 * www.digitalmars.com | |
6 * | |
7 * This is a public domain version of qsort.d. | |
8 * All it does is call C's qsort(), but runs a little slower since | |
9 * it needs to synchronize a global variable. | |
10 */ | |
11 | |
12 /* | |
13 * Modified by Sean Kelly <sean@f4.ca> for use with Tango. | |
14 */ | |
15 | |
16 //debug=qsort; | |
17 | |
18 version( D_Version2 ) | |
19 private import stdlib = stdc.stdlib; | |
20 else | |
21 private import stdlib = tango.stdc.stdlib; | |
22 | |
23 private TypeInfo tiglobal; | |
24 | |
25 extern (C) int cmp(in void* p1, in void* p2) | |
26 { | |
27 return tiglobal.compare(p1, p2); | |
28 } | |
29 | |
30 extern (C) void[] _adSort(void[] a, TypeInfo ti) | |
31 { | |
32 synchronized | |
33 { | |
34 tiglobal = ti; | |
35 stdlib.qsort(a.ptr, a.length, cast(size_t)ti.tsize(), &cmp); | |
36 } | |
37 return a; | |
38 } | |
39 | |
40 | |
41 | |
42 unittest | |
43 { | |
44 debug(qsort) printf("array.sort.unittest()\n"); | |
45 | |
46 int a[] = new int[10]; | |
47 | |
48 a[0] = 23; | |
49 a[1] = 1; | |
50 a[2] = 64; | |
51 a[3] = 5; | |
52 a[4] = 6; | |
53 a[5] = 5; | |
54 a[6] = 17; | |
55 a[7] = 3; | |
56 a[8] = 0; | |
57 a[9] = -1; | |
58 | |
59 a.sort; | |
60 | |
61 for (int i = 0; i < a.length - 1; i++) | |
62 { | |
63 //printf("i = %d", i); | |
64 //printf(" %d %d\n", a[i], a[i + 1]); | |
65 assert(a[i] <= a[i + 1]); | |
66 } | |
67 } |