]>
jfr.im git - irc/quakenet/newserv.git/blob - lib/array.c
3 dynamic UNORDERED array (yes this got me...)
5 will grow by allocchunksize (by default 100) when full and
6 new space is requested.
8 will shrink by freechunksize (by default 150) if that many
9 items are not occupied at the end.
11 clean written from the function prototypes.
17 #include "../core/error.h"
21 void array_init(array
*a
, unsigned int itemsize
) {
23 a
->cursi
= 0; /* count */
25 a
->itemsize
= itemsize
;
28 /* allochunksize MUST be <= freechunksize */
29 a
->allocchunksize
= 100;
30 a
->freechunksize
= 150;
33 /* should be called free_and_reinit really */
34 void array_free(array
*a
) {
37 array_init(a
, a
->itemsize
);
40 int array_getfreeslot(array
*a
) {
41 if(a
->cursi
+ 1 >= a
->capacity
) {
42 a
->capacity
+= a
->allocchunksize
;
44 /* we can be evil and use the same pointer as we're gonna exit if we fail */
45 a
->content
= realloc(a
->content
, a
->capacity
* a
->itemsize
);
48 Error("array", ERR_STOP
, "Array resize failed.");
54 void array_delslot(array
*a
, unsigned int index
) {
55 /* if we're not deleting the end item then swap the last item into the deleted items space */
56 /* unordered so we can do this, and we can also use memcpy */
57 if(--a
->cursi
!= index
)
58 memcpy((char *)a
->content
+ index
* a
->itemsize
, (char *)a
->content
+ a
->cursi
* a
->itemsize
, a
->itemsize
);
60 if(a
->cursi
+ a
->freechunksize
< a
->capacity
) {
62 a
->capacity
= a
->cursi
+ a
->allocchunksize
;
64 if(a
->capacity
== 0) {
69 void *newp
= realloc(a
->content
, a
->capacity
* a
->itemsize
);
72 Error("array", ERR_WARNING
, "Unable to shrink array!");