Mercurial > projects > ddmd
comparison dmd/ArrayT.d @ 0:10317f0c89a5
Initial commit
author | korDen |
---|---|
date | Sat, 24 Oct 2009 08:42:06 +0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:10317f0c89a5 |
---|---|
1 module dmd.ArrayT; | |
2 | |
3 import std.contracts; | |
4 import core.stdc.stdlib; | |
5 import core.stdc.string; | |
6 | |
7 class ArrayT(T) | |
8 { | |
9 uint dim; | |
10 uint allocdim; | |
11 T* data; | |
12 | |
13 ~this() | |
14 { | |
15 free(data); | |
16 } | |
17 | |
18 void mark() | |
19 { | |
20 } | |
21 | |
22 string toString() | |
23 { | |
24 char *p; | |
25 | |
26 ///char **buf = cast(char**)alloca(dim * (char*).sizeof); | |
27 scope string[] buf = new string[dim]; | |
28 uint len = 2; | |
29 for (uint u = 0; u < dim; u++) { | |
30 buf[u] = (cast(Object)data[u]).toString(); | |
31 len += buf[u].length + 1; | |
32 } | |
33 | |
34 char* str = cast(char*)malloc(len); | |
35 | |
36 str[0] = '['; | |
37 p = str + 1; | |
38 | |
39 for (uint u = 0; u < dim; u++) | |
40 { | |
41 if (u != 0) { | |
42 *p++ = ','; | |
43 } | |
44 uint length = buf[u].length; | |
45 p[0..length] = buf[u][]; | |
46 | |
47 p += length; | |
48 } | |
49 | |
50 *p++ = ']'; | |
51 *p = 0; | |
52 | |
53 return assumeUnique(str[0..len]); | |
54 } | |
55 | |
56 final void reserve(uint nentries) | |
57 { | |
58 //printf("ArrayT::reserve: size = %d, offset = %d, nbytes = %d\n", size, offset, nbytes); | |
59 if (allocdim - dim < nentries) { | |
60 allocdim = dim + nentries; | |
61 data = cast(T*)realloc(data, allocdim * (*data).sizeof); | |
62 } | |
63 } | |
64 | |
65 final void setDim(uint newdim) | |
66 { | |
67 if (dim < newdim) { | |
68 reserve(newdim - dim); | |
69 } | |
70 | |
71 dim = newdim; | |
72 } | |
73 | |
74 final void fixDim() | |
75 { | |
76 if (dim != allocdim) | |
77 { | |
78 data = cast(T*)realloc(data, dim * (*data).sizeof); | |
79 allocdim = dim; | |
80 } | |
81 } | |
82 | |
83 final void push(void* ptr) | |
84 { | |
85 reserve(1); | |
86 data[dim++] = ptr; | |
87 } | |
88 | |
89 final void* pop() | |
90 { | |
91 return data[--dim]; | |
92 } | |
93 | |
94 final void shift(void* ptr) | |
95 { | |
96 reserve(1); | |
97 memmove(data + 1, data, dim * (*data).sizeof); | |
98 data[0] = ptr; | |
99 dim++; | |
100 } | |
101 | |
102 final void insert(uint index, void* ptr) | |
103 { | |
104 reserve(1); | |
105 memmove(data + index + 1, data + index, (dim - index) * (*data).sizeof); | |
106 data[index] = ptr; | |
107 dim++; | |
108 } | |
109 | |
110 final void insert(uint index, ArrayT a) | |
111 { | |
112 if (a !is null) { | |
113 uint d = a.dim; | |
114 reserve(d); | |
115 | |
116 if (dim != index) { | |
117 memmove(data + index + d, data + index, (dim - index) * (*data).sizeof); | |
118 } | |
119 | |
120 memcpy(data + index, a.data, d * (*data).sizeof); | |
121 dim += d; | |
122 } | |
123 } | |
124 | |
125 /*********************************** | |
126 * Append array a to this array. | |
127 */ | |
128 final void append(ArrayT a) | |
129 { | |
130 insert(dim, a); | |
131 } | |
132 | |
133 final void remove(uint i) | |
134 { | |
135 memmove(data + i, data + i + 1, (dim - i) * (*data).sizeof); | |
136 dim--; | |
137 } | |
138 | |
139 final void zero() | |
140 { | |
141 memset(data, 0, dim * (*data).sizeof); | |
142 } | |
143 | |
144 final void* tos() | |
145 { | |
146 return dim ? data[dim - 1] : null; | |
147 } | |
148 | |
149 private static extern (C) int Array_sort_compare(const(void*) x, const(void*) y) | |
150 { | |
151 Object ox = *cast(Object *)x; | |
152 Object oy = *cast(Object *)y; | |
153 | |
154 return ox.opCmp(oy); | |
155 } | |
156 | |
157 final void sort() | |
158 { | |
159 if (dim) { | |
160 qsort(cast(void*)data, dim, Object.sizeof, &ArrayT_sort_compare); | |
161 } | |
162 } | |
163 | |
164 final ArrayT copy() | |
165 { | |
166 ArrayT a = new TArray(); | |
167 a.setDim(dim); | |
168 | |
169 memcpy(a.data, data, dim * (*data).sizeof); | |
170 | |
171 return a; | |
172 } | |
173 } |