annotate dmd/Array.d @ 2:7427ded8caf7

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