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