-
Notifications
You must be signed in to change notification settings - Fork 3
/
array.c
executable file
·300 lines (245 loc) · 7.65 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* array.c - automatic dynamic array for pointers */
/*
* Copyright (c) 2008, Natacha Porté
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "array.h"
#include <string.h>
/***************************
* STATIC HELPER FUNCTIONS *
***************************/
/* arr_realloc • realloc memory of a struct array */
static int
arr_realloc(struct array* arr, int neosz) {
void* neo;
neo = realloc(arr->base, neosz * arr->unit);
if (neo == 0) return 0;
arr->base = neo;
arr->asize = neosz;
if (arr->size > neosz) arr->size = neosz;
return 1; }
/* parr_realloc • realloc memory of a struct parray */
static int
parr_realloc(struct parray* arr, int neosz) {
void* neo;
neo = realloc(arr->item, neosz * sizeof (void*));
if (neo == 0) return 0;
arr->item = neo;
arr->asize = neosz;
if (arr->size > neosz) arr->size = neosz;
return 1; }
/***************************
* GENERIC ARRAY FUNCTIONS *
***************************/
/* arr_adjust • shrink the allocated memory to fit exactly the needs */
int
arr_adjust(struct array *arr) {
return arr_realloc(arr, arr->size); }
/* arr_free • frees the structure contents (buf NOT the struct itself) */
void
arr_free(struct array *arr) {
if (!arr) return;
free(arr->base);
arr->base = 0;
arr->size = arr->asize = 0; }
/* arr_grow • increases the array size to fit the given number of elements */
int
arr_grow(struct array *arr, int need) {
if (arr->asize >= need) return 1;
else return arr_realloc(arr, need); }
/* arr_init • initialization of the contents of the struct */
void
arr_init(struct array *arr, size_t unit) {
arr->base = 0;
arr->size = arr->asize = 0;
arr->unit = unit; }
/* arr_insert • inserting nb elements before the nth one */
int
arr_insert(struct array *arr, int nb, int n) {
char *src, *dst;
size_t len;
if (!arr || nb <= 0 || n < 0
|| !arr_grow(arr, arr->size + nb))
return 0;
if (n < arr->size) {
src = arr->base;
src += n * arr->unit;
dst = src + nb * arr->unit;
len = (arr->size - n) * arr->unit;
memmove(dst, src, len); }
arr->size += nb;
return 1; }
/* arr_item • returns a pointer to the n-th element */
void *
arr_item(struct array *arr, int no) {
char *ptr;
if (!arr || no < 0 || no >= arr->size) return 0;
ptr = arr->base;
ptr += no * arr->unit;
return ptr; }
/* arr_newitem • returns the index of a new element appended to the array */
int
arr_newitem(struct array *arr) {
if (!arr_grow(arr, arr->size + 1)) return -1;
arr->size += 1;
return arr->size - 1; }
/* arr_remove • removes the n-th elements of the array */
void
arr_remove(struct array *arr, int idx) {
if (!arr || idx < 0 || idx >= arr->size) return;
arr->size -= 1;
if (idx < arr->size) {
char *dst = arr->base;
char *src;
dst += idx * arr->unit;
src = dst + arr->unit;
memmove(dst, src, (arr->size - idx) * arr->unit); } }
/* arr_sorted_find • O(log n) search in a sorted array, returning entry */
void *
arr_sorted_find(struct array *arr, void *key, array_cmp_fn cmp) {
int mi, ma, cu, ret;
char *ptr = arr->base;
mi = -1;
ma = arr->size;
while (mi < ma - 1) {
cu = mi + (ma - mi) / 2;
ret = cmp(key, ptr + cu * arr->unit);
if (ret == 0) return ptr + cu * arr->unit;
else if (ret < 0) ma = cu;
else /* if (ret > 0) */ mi = cu; }
return 0; }
/* arr_sorted_find_i • O(log n) search in a sorted array,
* returning index of the smallest element larger than the key */
int
arr_sorted_find_i(struct array *arr, void *key, array_cmp_fn cmp) {
int mi, ma, cu, ret;
char *ptr = arr->base;
mi = -1;
ma = arr->size;
while (mi < ma - 1) {
cu = mi + (ma - mi) / 2;
ret = cmp(key, ptr + cu * arr->unit);
if (ret == 0) {
while (cu < arr->size && ret == 0) {
cu += 1;
ret = cmp(key, ptr + cu * arr->unit); }
return cu; }
else if (ret < 0) ma = cu;
else /* if (ret > 0) */ mi = cu; }
return ma; }
/***************************
* POINTER ARRAY FUNCTIONS *
***************************/
/* parr_adjust • shrinks the allocated memory to fit exactly the needs */
int
parr_adjust(struct parray* arr) {
return parr_realloc (arr, arr->size); }
/* parr_free • frees the structure contents (buf NOT the struct itself) */
void
parr_free(struct parray *arr) {
if (!arr) return;
free (arr->item);
arr->item = 0;
arr->size = 0;
arr->asize = 0; }
/* parr_grow • increases the array size to fit the given number of elements */
int
parr_grow(struct parray *arr, int need) {
if (arr->asize >= need) return 1;
else return parr_realloc (arr, need); }
/* parr_init • initialization of the struct (which is equivalent to zero) */
void
parr_init(struct parray *arr) {
arr->item = 0;
arr->size = 0;
arr->asize = 0; }
/* parr_insert • inserting nb elements before the nth one */
int
parr_insert(struct parray *parr, int nb, int n) {
char *src, *dst;
size_t len, i;
if (!parr || nb <= 0 || n < 0
|| !parr_grow(parr, parr->size + nb))
return 0;
if (n < parr->size) {
src = (void *)parr->item;
src += n * sizeof (void *);
dst = src + nb * sizeof (void *);
len = (parr->size - n) * sizeof (void *);
memmove(dst, src, len);
for (i = 0; i < (size_t)nb; ++i)
parr->item[n + i] = 0; }
parr->size += nb;
return 1; }
/* parr_pop • pops the last item of the array and returns it */
void *
parr_pop(struct parray *arr) {
if (arr->size <= 0) return 0;
arr->size -= 1;
return arr->item[arr->size]; }
/* parr_push • pushes a pointer at the end of the array (= append) */
int
parr_push(struct parray *arr, void *i) {
if (!parr_grow(arr, arr->size + 1)) return 0;
arr->item[arr->size] = i;
arr->size += 1;
return 1; }
/* parr_remove • removes the n-th element of the array and returns it */
void *
parr_remove(struct parray *arr, int idx) {
void* ret;
int i;
if (!arr || idx < 0 || idx >= arr->size) return 0;
ret = arr->item[idx];
for (i = idx+1; i < arr->size; ++i)
arr->item[i - 1] = arr->item[i];
arr->size -= 1;
return ret; }
/* parr_sorted_find • O(log n) search in a sorted array, returning entry */
void *
parr_sorted_find(struct parray *arr, void *key, array_cmp_fn cmp) {
int mi, ma, cu, ret;
mi = -1;
ma = arr->size;
while (mi < ma - 1) {
cu = mi + (ma - mi) / 2;
ret = cmp(key, arr->item[cu]);
if (ret == 0) return arr->item[cu];
else if (ret < 0) ma = cu;
else /* if (ret > 0) */ mi = cu; }
return 0; }
/* parr_sorted_find_i • O(log n) search in a sorted array,
* returning index of the smallest element larger than the key */
int
parr_sorted_find_i(struct parray *arr, void *key, array_cmp_fn cmp) {
int mi, ma, cu, ret;
mi = -1;
ma = arr->size;
while (mi < ma - 1) {
cu = mi + (ma - mi) / 2;
ret = cmp(key, arr->item[cu]);
if (ret == 0) {
while (cu < arr->size && ret == 0) {
cu += 1;
ret = cmp(key, arr->item[cu]); }
return cu; }
else if (ret < 0) ma = cu;
else /* if (ret > 0) */ mi = cu; }
return ma; }
/* parr_top • returns the top the stack (i.e. the last element of the array) */
void *
parr_top(struct parray *arr) {
if (arr == 0 || arr->size <= 0) return 0;
else return arr->item[arr->size - 1]; }
/* vim: set filetype=c: */