Mercurial > projects > ldc
comparison druntime/src/compiler/ldc/qsort2.d @ 1458:e0b2d67cfe7c
Added druntime (this should be removed once it works).
author | Robert Clipsham <robert@octarineparrot.com> |
---|---|
date | Tue, 02 Jun 2009 17:43:06 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1456:7b218ec1044f | 1458:e0b2d67cfe7c |
---|---|
1 /** | |
2 * This is a public domain version of qsort.d. All it does is call C's | |
3 * qsort(), but runs a little slower since it needs to synchronize a global | |
4 * variable. | |
5 * | |
6 * Copyright: Copyright Digital Mars 2000 - 2009. | |
7 * License: <a href="http://www.boost.org/LICENSE_1_0.txt>Boost License 1.0</a>. | |
8 * Authors: Walter Bright | |
9 * | |
10 * Copyright Digital Mars 2000 - 2009. | |
11 * Distributed under the Boost Software License, Version 1.0. | |
12 * (See accompanying file LICENSE_1_0.txt or copy at | |
13 * http://www.boost.org/LICENSE_1_0.txt) | |
14 */ | |
15 module rt.qsort2; | |
16 | |
17 //debug=qsort; | |
18 | |
19 private import core.stdc.stdlib; | |
20 | |
21 struct Array | |
22 { | |
23 size_t length; | |
24 void* ptr; | |
25 } | |
26 | |
27 private TypeInfo tiglobal; | |
28 | |
29 extern (C) int cmp(void* p1, void* p2) | |
30 { | |
31 return tiglobal.compare(p1, p2); | |
32 } | |
33 | |
34 extern (C) long _adSort(Array a, TypeInfo ti) | |
35 { | |
36 synchronized | |
37 { | |
38 tiglobal = ti; | |
39 qsort(a.ptr, a.length, cast(size_t)ti.tsize(), &cmp); | |
40 } | |
41 return *cast(long*)(&a); | |
42 } | |
43 | |
44 | |
45 | |
46 unittest | |
47 { | |
48 debug(qsort) printf("array.sort.unittest()\n"); | |
49 | |
50 int a[] = new int[10]; | |
51 | |
52 a[0] = 23; | |
53 a[1] = 1; | |
54 a[2] = 64; | |
55 a[3] = 5; | |
56 a[4] = 6; | |
57 a[5] = 5; | |
58 a[6] = 17; | |
59 a[7] = 3; | |
60 a[8] = 0; | |
61 a[9] = -1; | |
62 | |
63 a.sort; | |
64 | |
65 for (int i = 0; i < a.length - 1; i++) | |
66 { | |
67 //printf("i = %d", i); | |
68 //printf(" %d %d\n", a[i], a[i + 1]); | |
69 assert(a[i] <= a[i + 1]); | |
70 } | |
71 } |