comparison lphobos/internal/qsort2.d @ 94:61615fa85940 trunk

[svn r98] Added support for std.c.stdlib.alloca via pragma(LLVM_internal, "alloca"). Added support for array .sort and .reverse properties. Fixed some bugs with pointer arithmetic. Disabled some DMD AST optimizations that was messing things up, destroying valuable information. Added a KDevelop project file, this is what I use for coding LLVMDC now :) Other minor stuff.
author lindquist
date Mon, 12 Nov 2007 06:32:46 +0100
parents
children 5ab8e92611f9
comparison
equal deleted inserted replaced
93:08508eebbb3e 94:61615fa85940
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 //debug=qsort;
14
15 import std.c.stdlib;
16
17 struct Array
18 {
19 size_t length;
20 void *ptr;
21 }
22
23 private TypeInfo tiglobal;
24
25 extern (C) int cmp(void* p1, void* p2)
26 {
27 return tiglobal.compare(p1, p2);
28 }
29
30 extern (C) long _adSort(Array a, TypeInfo ti)
31 {
32 synchronized
33 {
34 tiglobal = ti;
35 std.c.stdlib.qsort(a.ptr, a.length, cast(size_t)ti.tsize(), &cmp);
36 }
37 return *cast(long*)(&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 }
68