-
Notifications
You must be signed in to change notification settings - Fork 13
/
arraylist.c
69 lines (63 loc) · 1.51 KB
/
arraylist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include "dtypes.h"
#include "arraylist.h"
arraylist_t *arraylist_new(arraylist_t *a, size_t size)
{
a->len = 0;
if (size <= AL_N_INLINE) {
a->items = &a->_space[0];
a->max = AL_N_INLINE;
}
else {
a->items = (void**)LLT_ALLOC(size*sizeof(void*));
a->max = size;
}
if (a->items == NULL) return NULL;
return a;
}
void arraylist_free(arraylist_t *a)
{
if (a->items != &a->_space[0])
LLT_FREE(a->items);
a->len = 0;
a->max = AL_N_INLINE;
a->items = &a->_space[0];
}
static void al_grow(arraylist_t *a, size_t n)
{
if (a->len+n > a->max) {
if (a->items == &a->_space[0]) {
void **p = LLT_ALLOC((a->len+n)*sizeof(void*));
if (p == NULL) return;
memcpy(p, a->items, a->len*sizeof(void*));
a->items = p;
a->max = a->len+n;
}
else {
size_t nm = a->max*2;
if (nm == 0) nm = 1;
while (a->len+n > nm) nm*=2;
void **p = LLT_REALLOC(a->items, nm*sizeof(void*));
if (p == NULL) return;
a->items = p;
a->max = nm;
}
}
a->len += n;
}
void arraylist_push(arraylist_t *a, void *elt)
{
al_grow(a, 1);
a->items[a->len-1] = elt;
}
void *arraylist_pop(arraylist_t *a)
{
if (a->len == 0) return NULL;
void *p = a->items[--a->len];
a->items[a->len] = NULL;
return p;
}