comparison dmd2/root/array.c @ 1452:638d16625da2

LDC 2 compiles again.
author Robert Clipsham <robert@octarineparrot.com>
date Sat, 30 May 2009 17:23:32 +0100
parents
children f0423003caa6
comparison
equal deleted inserted replaced
1423:42bd767ec5a4 1452:638d16625da2
1
2 // Copyright (c) 1999-2009 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 (defined (__SVR4) && defined (__sun))
17 #include <alloca.h>
18 #endif
19
20 #if _MSC_VER || __MINGW32__
21 #include <malloc.h>
22 #endif
23
24 #if IN_GCC
25 #include "gdc_alloca.h"
26 #endif
27
28 #if _WIN32
29 #include <windows.h>
30 #endif
31
32 #ifndef _WIN32
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <errno.h>
37 #include <unistd.h>
38 #include <utime.h>
39 #endif
40
41 #include "port.h"
42 #include "root.h"
43 #include "dchar.h"
44 #include "rmem.h"
45
46
47 /********************************* Array ****************************/
48
49 Array::Array()
50 {
51 data = NULL;
52 dim = 0;
53 allocdim = 0;
54 }
55
56 Array::~Array()
57 {
58 mem.free(data);
59 }
60
61 void Array::mark()
62 { unsigned u;
63
64 mem.mark(data);
65 for (u = 0; u < dim; u++)
66 mem.mark(data[u]); // BUG: what if arrays of Object's?
67 }
68
69 void Array::reserve(unsigned nentries)
70 {
71 //printf("Array::reserve: size = %d, offset = %d, nbytes = %d\n", size, offset, nbytes);
72 if (allocdim - dim < nentries)
73 {
74 allocdim = dim + nentries;
75 data = (void **)mem.realloc(data, allocdim * sizeof(*data));
76 }
77 }
78
79 void Array::setDim(unsigned newdim)
80 {
81 if (dim < newdim)
82 {
83 reserve(newdim - dim);
84 }
85 dim = newdim;
86 }
87
88 void Array::fixDim()
89 {
90 if (dim != allocdim)
91 { data = (void **)mem.realloc(data, dim * sizeof(*data));
92 allocdim = dim;
93 }
94 }
95
96 void Array::push(void *ptr)
97 {
98 reserve(1);
99 data[dim++] = ptr;
100 }
101
102 void *Array::pop()
103 {
104 return data[--dim];
105 }
106
107 void Array::shift(void *ptr)
108 {
109 reserve(1);
110 memmove(data + 1, data, dim * sizeof(*data));
111 data[0] = ptr;
112 dim++;
113 }
114
115 void Array::insert(unsigned index, void *ptr)
116 {
117 reserve(1);
118 memmove(data + index + 1, data + index, (dim - index) * sizeof(*data));
119 data[index] = ptr;
120 dim++;
121 }
122
123
124 void Array::insert(unsigned index, Array *a)
125 {
126 if (a)
127 { unsigned d;
128
129 d = a->dim;
130 reserve(d);
131 if (dim != index)
132 memmove(data + index + d, data + index, (dim - index) * sizeof(*data));
133 memcpy(data + index, a->data, d * sizeof(*data));
134 dim += d;
135 }
136 }
137
138
139 /***********************************
140 * Append array a to this array.
141 */
142
143 void Array::append(Array *a)
144 {
145 insert(dim, a);
146 }
147
148 void Array::remove(unsigned i)
149 {
150 memmove(data + i, data + i + 1, (dim - i) * sizeof(data[0]));
151 dim--;
152 }
153
154 char *Array::toChars()
155 {
156 unsigned len;
157 unsigned u;
158 char **buf;
159 char *str;
160 char *p;
161
162 buf = (char **)alloca(dim * sizeof(char *));
163 len = 2;
164 for (u = 0; u < dim; u++)
165 {
166 buf[u] = ((Object *)data[u])->toChars();
167 len += strlen(buf[u]) + 1;
168 }
169 str = (char *)mem.malloc(len);
170
171 str[0] = '[';
172 p = str + 1;
173 for (u = 0; u < dim; u++)
174 {
175 if (u)
176 *p++ = ',';
177 len = strlen(buf[u]);
178 memcpy(p,buf[u],len);
179 p += len;
180 }
181 *p++ = ']';
182 *p = 0;
183 return str;
184 }
185
186 void Array::zero()
187 {
188 memset(data,0,dim * sizeof(data[0]));
189 }
190
191 void *Array::tos()
192 {
193 return dim ? data[dim - 1] : NULL;
194 }
195
196 int
197 #if _WIN32
198 __cdecl
199 #endif
200 Array_sort_compare(const void *x, const void *y)
201 {
202 Object *ox = *(Object **)x;
203 Object *oy = *(Object **)y;
204
205 return ox->compare(oy);
206 }
207
208 void Array::sort()
209 {
210 if (dim)
211 {
212 qsort(data, dim, sizeof(Object *), Array_sort_compare);
213 }
214 }
215
216 Array *Array::copy()
217 {
218 Array *a = new Array();
219
220 a->setDim(dim);
221 memcpy(a->data, data, dim * sizeof(void *));
222 return a;
223 }
224