comparison druntime/src/compiler/dmd/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 for use with the D Runtime Project
14 */
15
16 module rt.qsort2;
17
18 //debug=qsort;
19
20 private import stdc.stdlib;
21
22 struct Array
23 {
24 size_t length;
25 void* ptr;
26 }
27
28 private TypeInfo tiglobal;
29
30 extern (C) int cmp(void* p1, void* p2)
31 {
32 return tiglobal.compare(p1, p2);
33 }
34
35 extern (C) long _adSort(Array a, TypeInfo ti)
36 {
37 synchronized
38 {
39 tiglobal = ti;
40 qsort(a.ptr, a.length, cast(size_t)ti.tsize(), &cmp);
41 }
42 return *cast(long*)(&a);
43 }
44
45
46
47 unittest
48 {
49 debug(qsort) printf("array.sort.unittest()\n");
50
51 int a[] = new int[10];
52
53 a[0] = 23;
54 a[1] = 1;
55 a[2] = 64;
56 a[3] = 5;
57 a[4] = 6;
58 a[5] = 5;
59 a[6] = 17;
60 a[7] = 3;
61 a[8] = 0;
62 a[9] = -1;
63
64 a.sort;
65
66 for (int i = 0; i < a.length - 1; i++)
67 {
68 //printf("i = %d", i);
69 //printf(" %d %d\n", a[i], a[i + 1]);
70 assert(a[i] <= a[i + 1]);
71 }
72 }