-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.h
210 lines (175 loc) · 11.1 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* C array implementation.
Copyright (C) 2009 Morten Hustveit <morten@rashbox.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ARRAY_H_
#define ARRAY_H_ 1
#include <assert.h>
#include <stdlib.h>
/*** Example usage: ***
struct my_struct_t
{
ARRAY_MEMBERS(char);
int other_member;
};
// Variable declarations
struct my_struct_t my_struct;
ARRAY(const char*) my_array;
// Set all members to zero
ARRAY_INIT(&my_struct);
ARRAY_INIT(&my_array);
ARRAY_ADD(&my_struct, 'x');
// Example error handling. Should really be done always.
if(ARRAY_RESULT(&my_struct))
err(EX_OSERR, "memory allocation failed");
ARRAY_ADD_SEVERAL(&my_struct, "hest", 4);
ARRAY_ADD(&my_array, "eple");
// Printing a non-null-terminated string
printf("%.*s\n", (int) ARRAY_COUNT(&my_struct), &ARRAY_GET(&my_struct, 0));
ARRAY_ADD(&my_struct, 0);
// Printing a null-terminated string
printf("%s\n", &ARRAY_GET(&struct, 0));
ARRAY_FREE(&my_array);
ARRAY_FREE(&my_struct);
*/
int
array_grow(void* elements, size_t* alloc, size_t new_alloc,
size_t element_size);
#define ARRAY_MEMBERS(type) \
type* array_elements; \
size_t array_element_count; \
size_t array_element_alloc; \
int array_result; \
#define ARRAY(type) \
struct \
{ \
ARRAY_MEMBERS(type); \
} \
#define ARRAY_INIT(array) \
do \
{ \
(array)->array_elements = 0; \
(array)->array_element_count = 0; \
(array)->array_element_alloc = 0; \
(array)->array_result = 0; \
} \
while(0)
#define ARRAY_RESERVE(array, count) \
do \
{ \
size_t ccount = (count); \
if((array)->array_element_alloc < ccount) \
{ \
if(-1 == ((array)->array_result = \
array_grow(&(array)->array_elements, \
&(array)->array_element_alloc, ccount, \
sizeof(*(array)->array_elements)))) \
break; \
} \
} \
while(0)
#define ARRAY_ADD(array, value) \
do \
{ \
assert((array)->array_result == 0); \
if((array)->array_element_count == (array)->array_element_alloc) \
{ \
size_t new_alloc = (array)->array_element_alloc * 3 / 2 + 16; \
if(-1 == ((array)->array_result = \
array_grow(&(array)->array_elements, \
&(array)->array_element_alloc, new_alloc, \
sizeof(*(array)->array_elements)))) \
break; \
} \
(array)->array_elements[(array)->array_element_count++] = value; \
} \
while(0)
#define ARRAY_REMOVE(array, index) \
do \
{ \
assert((array)->array_result == 0); \
--(array)->array_element_count; \
memmove((array)->array_elements + (index), \
(array)->array_elements + (array)->array_element_count, \
sizeof(*(array)->array_elements)); \
} \
while(0)
#define ARRAY_REMOVE_PTR(array, pointer) \
ARRAY_REMOVE(array, (pointer) - (array)->array_elements)
#define ARRAY_ADD_SEVERAL(array, values, count) \
do \
{ \
size_t total; \
assert((array)->array_result == 0); \
total = (array)->array_element_count + (count); \
if(total > (array)->array_element_alloc) \
{ \
if(-1 == ((array)->array_result = \
array_grow(&(array)->array_elements, \
&(array)->array_element_alloc, total * 3 / 2, \
sizeof(*(array)->array_elements)))) \
break; \
} \
memcpy((array)->array_elements + (array)->array_element_count, \
(values), (count) * sizeof(*(array)->array_elements)); \
(array)->array_element_count += (count); \
} \
while(0)
#define ARRAY_INSERT_SEVERAL(array, index, values, count) \
do \
{ \
size_t total, cindex, ccount; \
assert((array)->array_result == 0); \
cindex = (index); \
ccount = (count); \
total = (array)->array_element_count + ccount; \
if(total > (array)->array_element_alloc) \
{ \
if(-1 == ((array)->array_result = \
array_grow(&(array)->array_elements, \
&(array)->array_element_alloc, total * 3 / 2, \
sizeof(*(array)->array_elements)))) \
break; \
} \
memmove((array)->array_elements + cindex + ccount, \
(array)->array_elements + cindex, \
(((array)->array_element_count - cindex) \
* sizeof(*(array)->array_elements))); \
memcpy((array)->array_elements + cindex, \
(values), ccount * sizeof(*(array)->array_elements)); \
(array)->array_element_count += (count); \
} \
while(0)
#define ARRAY_CONSUME(array, count) \
do \
{ \
(array)->array_element_count -= (count); \
memmove((array)->array_elements, \
(array)->array_elements + count, \
(array)->array_element_count * sizeof(*(array)->array_elements)); \
} \
while(0)
#define ARRAY_COUNT(array) (array)->array_element_count
#define ARRAY_GET(array, index) (array)->array_elements[(index)]
#define ARRAY_RESULT(array) (array)->array_result
#define ARRAY_RESET(array) do { (array)->array_element_count = 0; } while(0)
#define ARRAY_FREE(array) \
do \
{ \
free((array)->array_elements); \
(array)->array_elements = 0; \
(array)->array_element_count = 0; \
(array)->array_element_alloc = 0; \
(array)->array_result = 0; \
} \
while(0)
#endif /* !ARRAY_H_ */