-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharray.h
58 lines (43 loc) · 1.65 KB
/
array.h
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
#ifndef ARRAY_H
#define ARRAY_H value
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#define array(type, name, initial_length) \
type *name = __array_alloc(sizeof(type), initial_length)
#define aforeach(it, array) \
for (unsigned it = 0; \
it < alength(array); \
++it)
#define __header(array) \
((struct __array_header *) array - 1)
#define alength(array) \
(__header(array)->length)
#define afree(array) \
free(__header(array))
#define apush(array, elem) \
__array_resize((void **) &array, sizeof *array, 1); \
array[alength(array)-1] = elem
#define apop(array) \
aremove(array, (alength(array) - 1))
#define aremove(array, index) \
assert(alength(array) > index); \
memmove(array + index, array + index + 1, sizeof *array * (alength(array) - index - 1)); \
__array_resize((void **) &array, sizeof *array, -1)
#define ainsert(array, index, elem) \
__array_resize((void **) &array, sizeof *array, index >= alength(array) ? index - alength(array) + 1 : 1); \
memmove(array + index + 1, array + index, sizeof *array * (alength(array) - index - 1)); \
array[index] = elem
#define acontains(array, elem) \
__array_search(array, &elem, sizeof elem)
#define __arrayallocated(array) \
(__header(array)->allocated)
struct __array_header {
unsigned length;
unsigned allocated;
};
unsigned __bump_up(unsigned n);
void *__array_alloc(size_t size, unsigned length);
void __array_resize(void **array, size_t size, int difference);
int __array_search(void *array, void *elem, size_t size);
#endif /* ifndef ARRAY_H */