-
Notifications
You must be signed in to change notification settings - Fork 0
/
os_da.h
110 lines (93 loc) · 4.38 KB
/
os_da.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
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
110
#ifndef OSDA_INCLUDE_OS_DA_H
#define OSDA_INCLUDE_OS_DA_H
#include <stddef.h>
#include <stdlib.h>
#ifndef OSDADEF
#ifdef OSDA_STATIC
#define OSDADEF static
#else
#define OSDADEF extern
#endif
#endif
#ifndef OSDA_INITIAL_CAPACITY
#define OSDA_INITIAL_CAPACITY (8)
#endif
#ifndef OSDA_FACTOR
#define OSDA_FACTOR (2)
#endif
/*
Dynamic Array Type template:
typedef struct
{
void *items;
size_t count;
size_t capacity;
} os_dynamic_array_t;
*/
#ifndef OSDA_REALLOC
#define OSDA_REALLOC realloc
#endif
#ifndef OSDA_FREE
#define OSDA_FREE free
#endif
#define os_da_free(da) \
do \
{ \
if ((da)->items) \
{ \
da__free((da)->items); \
(da)->items = NULL; \
} \
(da)->count = 0; \
(da)->capacity = 0; \
} while (0)
static void *da__realloc(void *ptr, size_t size);
static void da__free(void *ptr);
#define os_da_append(da, item) \
do \
{ \
if ((da)->count >= (da)->capacity) \
{ \
size_t new_capacity = (da)->capacity == 0 \
? OSDA_INITIAL_CAPACITY \
: (da)->capacity * OSDA_FACTOR; \
while ((da)->count >= new_capacity) \
{ \
(da)->capacity *= OSDA_FACTOR; \
} \
(da)->items = da__realloc((da)->items, \
new_capacity * sizeof(*(da)->items)); \
(da)->capacity = new_capacity; \
} \
(da)->items[(da)->count++] = item; \
} while (0)
#define os_da_pop_tail(da) \
do \
{ \
if ((da)->count > 0) \
--(da)->count; \
} while (0)
#define os_da_pop_head(type, da) \
do \
{ \
if ((da)->count <= 1) \
{ \
os_da_pop_tail((da)); \
} \
else \
{ \
memmove((da)->items, (type *) (da)->items + 1, sizeof(type)); \
--(da)->count; \
} \
} while (0)
#endif
#ifdef OS_DA_IMPLEMENTATION
static void *da__realloc(void *ptr, size_t size)
{
return OSDA_REALLOC(ptr, size);
}
static void da__free(void *ptr)
{
OSDA_FREE(ptr);
}
#endif