comparison tango/lib/compiler/llvmdc/qsort2.d @ 132:1700239cab2e trunk

[svn r136] MAJOR UNSTABLE UPDATE!!! Initial commit after moving to Tango instead of Phobos. Lots of bugfixes... This build is not suitable for most things.
author lindquist
date Fri, 11 Jan 2008 17:57:40 +0100
parents
children d9d5d59873d8
comparison
equal deleted inserted replaced
131:5825d48b27d1 132:1700239cab2e
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 private import tango.stdc.stdlib;
19
20 struct Array
21 {
22 size_t length;
23 void* ptr;
24 }
25
26 private TypeInfo tiglobal;
27
28 extern (C) int cmp(void* p1, void* p2)
29 {
30 return tiglobal.compare(p1, p2);
31 }
32
33 extern (C) long _adSort(Array a, TypeInfo ti)
34 {
35 synchronized
36 {
37 tiglobal = ti;
38 tango.stdc.stdlib.qsort(a.ptr, a.length, cast(size_t)ti.tsize(), &cmp);
39 }
40 return *cast(long*)(&a);
41 }
42
43
44
45 unittest
46 {
47 debug(qsort) printf("array.sort.unittest()\n");
48
49 int a[] = new int[10];
50
51 a[0] = 23;
52 a[1] = 1;
53 a[2] = 64;
54 a[3] = 5;
55 a[4] = 6;
56 a[5] = 5;
57 a[6] = 17;
58 a[7] = 3;
59 a[8] = 0;
60 a[9] = -1;
61
62 a.sort;
63
64 for (int i = 0; i < a.length - 1; i++)
65 {
66 //printf("i = %d", i);
67 //printf(" %d %d\n", a[i], a[i + 1]);
68 assert(a[i] <= a[i + 1]);
69 }
70 }