Warn about "assert X,Y"
[delight/core.git] / dmd2 / array.c
blobb5a322840b4dd82ecc4a2be6e3b2b9bd330ad81b
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.
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <string.h>
14 #include <assert.h>
16 #if _MSC_VER
17 #include <malloc.h>
18 #endif
20 #if IN_GCC
21 #include "gdc_alloca.h"
22 #endif
24 #if _WIN32
25 #include <windows.h>
26 #endif
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
37 #include "port.h"
38 #include "root.h"
39 #include "dchar.h"
40 #include "mem.h"
43 /********************************* Array ****************************/
45 Array::Array()
47 data = NULL;
48 dim = 0;
49 allocdim = 0;
52 Array::~Array()
54 mem.free(data);
57 void Array::mark()
58 { unsigned u;
60 mem.mark(data);
61 for (u = 0; u < dim; u++)
62 mem.mark(data[u]); // BUG: what if arrays of Object's?
65 void Array::reserve(unsigned nentries)
67 //printf("Array::reserve: size = %d, offset = %d, nbytes = %d\n", size, offset, nbytes);
68 if (allocdim - dim < nentries)
70 allocdim = dim + nentries;
71 data = (void **)mem.realloc(data, allocdim * sizeof(*data));
75 void Array::setDim(unsigned newdim)
77 if (dim < newdim)
79 reserve(newdim - dim);
81 dim = newdim;
84 void Array::fixDim()
86 if (dim != allocdim)
87 { data = (void **)mem.realloc(data, dim * sizeof(*data));
88 allocdim = dim;
92 void Array::push(void *ptr)
94 reserve(1);
95 data[dim++] = ptr;
98 void *Array::pop()
100 return data[--dim];
103 void Array::shift(void *ptr)
105 reserve(1);
106 memmove(data + 1, data, dim * sizeof(*data));
107 data[0] = ptr;
108 dim++;
111 void Array::insert(unsigned index, void *ptr)
113 reserve(1);
114 memmove(data + index + 1, data + index, (dim - index) * sizeof(*data));
115 data[index] = ptr;
116 dim++;
120 void Array::insert(unsigned index, Array *a)
122 if (a)
123 { unsigned d;
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;
135 /***********************************
136 * Append array a to this array.
139 void Array::append(Array *a)
141 insert(dim, a);
144 void Array::remove(unsigned i)
146 memmove(data + i, data + i + 1, (dim - i) * sizeof(data[0]));
147 dim--;
150 char *Array::toChars()
152 unsigned len;
153 unsigned u;
154 char **buf;
155 char *str;
156 char *p;
158 buf = (char **)alloca(dim * sizeof(char *));
159 len = 2;
160 for (u = 0; u < dim; u++)
162 buf[u] = ((Object *)data[u])->toChars();
163 len += strlen(buf[u]) + 1;
165 str = (char *)mem.malloc(len);
167 str[0] = '[';
168 p = str + 1;
169 for (u = 0; u < dim; u++)
171 if (u)
172 *p++ = ',';
173 len = strlen(buf[u]);
174 memcpy(p,buf[u],len);
175 p += len;
177 *p++ = ']';
178 *p = 0;
179 return str;
182 void Array::zero()
184 memset(data,0,dim * sizeof(data[0]));
187 void *Array::tos()
189 return dim ? data[dim - 1] : NULL;
193 #if _WIN32
194 __cdecl
195 #endif
196 Array_sort_compare(const void *x, const void *y)
198 Object *ox = *(Object **)x;
199 Object *oy = *(Object **)y;
201 return ox->compare(oy);
204 void Array::sort()
206 if (dim)
208 qsort(data, dim, sizeof(Object *), Array_sort_compare);
212 Array *Array::copy()
214 Array *a = new Array();
216 a->setDim(dim);
217 memcpy(a->data, data, dim * sizeof(void *));
218 return a;