-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patharray.c
109 lines (99 loc) · 1.83 KB
/
array.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <stdlib.h>
#include <string.h>
#include "array.h"
static void array_alloc(array_t *a, int hint)
{
int base, best;
base = a->size / a->dsize;
if (!base) {
base = 1;
}
while (base * a->dsize < hint) {
base *= 2;
}
best = base;
while (base * a->dsize > hint) {
best = base;
base /= 2;
}
base = best;
a->size = base *a->dsize;
a->mem = realloc(a->mem, a->size);
}
array_t *array_init(int dsize, int init_count)
{
array_t *a = (array_t *)calloc(1, sizeof(array_t));
a->dsize = dsize;
if (init_count < 0) {
init_count = ARRAY_INIT_COUNT;
}
if (init_count > 0) {
array_alloc(a, init_count * dsize);
}
return a;
}
void array_destroy(array_t *a)
{
if (a->mem) {
free(a->mem);
}
free(a);
}
void array_put(array_t *a, void *d)
{
int total = a->dsize * (a->count+1);
if (a->size < total) {
array_alloc(a, total);
}
memcpy((char *)a->mem+a->count*a->dsize, d, a->dsize);
a->count++;
}
void array_set(array_t *a, int i, void *d)
{
if (a->count <= i) {
int total = a->dsize * (i + 1);
array_alloc(a, total);
}
memcpy((char *)a->mem+i*a->dsize, d, a->dsize);
if (a->count <= i) {
a->count = i+1;
}
}
void* array_get(array_t *a, int i)
{
if (i >= a->count) abort();
return (char*)a->mem + a->dsize*i;
}
int array_length(array_t *a)
{
return a->count;
}
void array_del(array_t *a, int i)
{
if (i >= a->count) abort();
while (i+1 < a->count) {
array_set(a, i, array_get(a, i+1));
i++;
}
a->count--;
}
void array_shrink(array_t *a)
{
int space, best;
space = a->size / a->dsize;
best = 0;
while (1) {
space /= 2;
if (space > a->count) {
best = space;
} else {
break;
}
}
if (best) {
if (best < ARRAY_INIT_COUNT) {
best = ARRAY_INIT_COUNT;
}
}
array_alloc(a, best*a->dsize);
}