1
|
1
|
|
2 // Copyright (c) 1999-2006 by Digital Mars
|
|
3 // All Rights Reserved
|
|
4 // written by Walter Bright
|
|
5 // www.digitalmars.com
|
|
6 // License for redistribution is by either the Artistic License
|
|
7 // in artistic.txt, or the GNU General Public License in gnu.txt.
|
|
8 // See the included readme.txt for details.
|
|
9
|
|
10 #include <stdio.h>
|
|
11 #include <stdlib.h>
|
|
12 #include <stdarg.h>
|
|
13 #include <string.h>
|
|
14 #include <assert.h>
|
|
15
|
|
16 #if _MSC_VER
|
|
17 #include <malloc.h>
|
|
18 #endif
|
|
19
|
|
20 #if IN_GCC
|
|
21 #include "gdc_alloca.h"
|
|
22 #endif
|
|
23
|
|
24 #if _WIN32
|
|
25 #include <windows.h>
|
|
26 #endif
|
|
27
|
|
28 #ifndef _WIN32
|
|
29 #include <sys/types.h>
|
|
30 #include <sys/stat.h>
|
|
31 #include <fcntl.h>
|
|
32 #include <errno.h>
|
|
33 #include <unistd.h>
|
|
34 #include <utime.h>
|
|
35 #endif
|
|
36
|
|
37 #include "port.h"
|
|
38 #include "root.h"
|
|
39 #include "dchar.h"
|
|
40 #include "mem.h"
|
|
41
|
|
42
|
|
43 /********************************* Array ****************************/
|
|
44
|
|
45 Array::Array()
|
|
46 {
|
|
47 data = NULL;
|
|
48 dim = 0;
|
|
49 allocdim = 0;
|
|
50 }
|
|
51
|
|
52 Array::~Array()
|
|
53 {
|
|
54 mem.free(data);
|
|
55 }
|
|
56
|
|
57 void Array::mark()
|
|
58 { unsigned u;
|
|
59
|
|
60 mem.mark(data);
|
|
61 for (u = 0; u < dim; u++)
|
|
62 mem.mark(data[u]); // BUG: what if arrays of Object's?
|
|
63 }
|
|
64
|
|
65 void Array::reserve(unsigned nentries)
|
|
66 {
|
|
67 //printf("Array::reserve: size = %d, offset = %d, nbytes = %d\n", size, offset, nbytes);
|
|
68 if (allocdim - dim < nentries)
|
|
69 {
|
|
70 allocdim = dim + nentries;
|
|
71 data = (void **)mem.realloc(data, allocdim * sizeof(*data));
|
|
72 }
|
|
73 }
|
|
74
|
|
75 void Array::setDim(unsigned newdim)
|
|
76 {
|
|
77 if (dim < newdim)
|
|
78 {
|
|
79 reserve(newdim - dim);
|
|
80 }
|
|
81 dim = newdim;
|
|
82 }
|
|
83
|
|
84 void Array::fixDim()
|
|
85 {
|
|
86 if (dim != allocdim)
|
|
87 { data = (void **)mem.realloc(data, dim * sizeof(*data));
|
|
88 allocdim = dim;
|
|
89 }
|
|
90 }
|
|
91
|
|
92 void Array::push(void *ptr)
|
|
93 {
|
|
94 reserve(1);
|
|
95 data[dim++] = ptr;
|
|
96 }
|
|
97
|
|
98 void *Array::pop()
|
|
99 {
|
|
100 return data[--dim];
|
|
101 }
|
|
102
|
|
103 void Array::shift(void *ptr)
|
|
104 {
|
|
105 reserve(1);
|
|
106 memmove(data + 1, data, dim * sizeof(*data));
|
|
107 data[0] = ptr;
|
|
108 dim++;
|
|
109 }
|
|
110
|
|
111 void Array::insert(unsigned index, void *ptr)
|
|
112 {
|
|
113 reserve(1);
|
|
114 memmove(data + index + 1, data + index, (dim - index) * sizeof(*data));
|
|
115 data[index] = ptr;
|
|
116 dim++;
|
|
117 }
|
|
118
|
|
119
|
|
120 void Array::insert(unsigned index, Array *a)
|
|
121 {
|
|
122 if (a)
|
|
123 { unsigned d;
|
|
124
|
|
125 d = a->dim;
|
|
126 reserve(d);
|
|
127 if (dim != index)
|
|
128 memmove(data + index + d, data + index, (dim - index) * sizeof(*data));
|
|
129 memcpy(data + index, a->data, d * sizeof(*data));
|
|
130 dim += d;
|
|
131 }
|
|
132 }
|
|
133
|
|
134
|
|
135 /***********************************
|
|
136 * Append array a to this array.
|
|
137 */
|
|
138
|
|
139 void Array::append(Array *a)
|
|
140 {
|
|
141 insert(dim, a);
|
|
142 }
|
|
143
|
|
144 void Array::remove(unsigned i)
|
|
145 {
|
|
146 memmove(data + i, data + i + 1, (dim - i) * sizeof(data[0]));
|
|
147 dim--;
|
|
148 }
|
|
149
|
|
150 char *Array::toChars()
|
|
151 {
|
|
152 unsigned len;
|
|
153 unsigned u;
|
|
154 char **buf;
|
|
155 char *str;
|
|
156 char *p;
|
|
157
|
|
158 buf = (char **)alloca(dim * sizeof(char *));
|
|
159 len = 2;
|
|
160 for (u = 0; u < dim; u++)
|
|
161 {
|
|
162 buf[u] = ((Object *)data[u])->toChars();
|
|
163 len += strlen(buf[u]) + 1;
|
|
164 }
|
|
165 str = (char *)mem.malloc(len);
|
|
166
|
|
167 str[0] = '[';
|
|
168 p = str + 1;
|
|
169 for (u = 0; u < dim; u++)
|
|
170 {
|
|
171 if (u)
|
|
172 *p++ = ',';
|
|
173 len = strlen(buf[u]);
|
|
174 memcpy(p,buf[u],len);
|
|
175 p += len;
|
|
176 }
|
|
177 *p++ = ']';
|
|
178 *p = 0;
|
|
179 return str;
|
|
180 }
|
|
181
|
|
182 void Array::zero()
|
|
183 {
|
|
184 memset(data,0,dim * sizeof(data[0]));
|
|
185 }
|
|
186
|
|
187 void *Array::tos()
|
|
188 {
|
|
189 return dim ? data[dim - 1] : NULL;
|
|
190 }
|
|
191
|
|
192 int
|
|
193 #if _WIN32
|
|
194 __cdecl
|
|
195 #endif
|
|
196 Array_sort_compare(const void *x, const void *y)
|
|
197 {
|
|
198 Object *ox = *(Object **)x;
|
|
199 Object *oy = *(Object **)y;
|
|
200
|
|
201 return ox->compare(oy);
|
|
202 }
|
|
203
|
|
204 void Array::sort()
|
|
205 {
|
|
206 if (dim)
|
|
207 {
|
|
208 qsort(data, dim, sizeof(Object *), Array_sort_compare);
|
|
209 }
|
|
210 }
|
|
211
|
|
212 Array *Array::copy()
|
|
213 {
|
|
214 Array *a = new Array();
|
|
215
|
|
216 a->setDim(dim);
|
|
217 memcpy(a->data, data, dim * sizeof(void *));
|
|
218 return a;
|
|
219 }
|
|
220
|