2 // Copyright (c) 1999-2006 by Digital Mars
4 // written by Walter Bright
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.
21 #include "gdc_alloca.h"
29 #include <sys/types.h>
43 /********************************* Array ****************************/
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
)
79 reserve(newdim
- dim
);
87 { data
= (void **)mem
.realloc(data
, dim
* sizeof(*data
));
92 void Array::push(void *ptr
)
103 void Array::shift(void *ptr
)
106 memmove(data
+ 1, data
, dim
* sizeof(*data
));
111 void Array::insert(unsigned index
, void *ptr
)
114 memmove(data
+ index
+ 1, data
+ index
, (dim
- index
) * sizeof(*data
));
120 void Array::insert(unsigned index
, Array
*a
)
128 memmove(data
+ index
+ d
, data
+ index
, (dim
- index
) * sizeof(*data
));
129 memcpy(data
+ index
, a
->data
, d
* sizeof(*data
));
135 /***********************************
136 * Append array a to this array.
139 void Array::append(Array
*a
)
144 void Array::remove(unsigned i
)
146 memmove(data
+ i
, data
+ i
+ 1, (dim
- i
) * sizeof(data
[0]));
150 char *Array::toChars()
158 buf
= (char **)alloca(dim
* sizeof(char *));
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
);
169 for (u
= 0; u
< dim
; u
++)
173 len
= strlen(buf
[u
]);
174 memcpy(p
,buf
[u
],len
);
184 memset(data
,0,dim
* sizeof(data
[0]));
189 return dim
? data
[dim
- 1] : NULL
;
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
);
208 qsort(data
, dim
, sizeof(Object
*), Array_sort_compare
);
214 Array
*a
= new Array();
217 memcpy(a
->data
, data
, dim
* sizeof(void *));