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