| 1 |
|
| /* array.c - automatic dynamic array for pointers */ |
| 2 |
|
| |
| 3 |
|
| /* |
| 4 |
|
| * Copyright (c) 2008, Natacha Porté |
| 5 |
|
| * |
| 6 |
|
| * Permission to use, copy, modify, and distribute this software for any |
| 7 |
|
| * purpose with or without fee is hereby granted, provided that the above |
| 8 |
|
| * copyright notice and this permission notice appear in all copies. |
| 9 |
|
| * |
| 10 |
|
| * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 |
|
| * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 |
|
| * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 |
|
| * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 |
|
| * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 |
|
| * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 |
|
| * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 |
|
| */ |
| 18 |
|
| |
| 19 |
|
| #include "array.h" |
| 20 |
|
| |
| 21 |
|
| #include <string.h> |
| 22 |
|
| |
| 23 |
|
| |
| 24 |
|
| /*************************** |
| 25 |
|
| * STATIC HELPER FUNCTIONS * |
| 26 |
|
| ***************************/ |
| 27 |
|
| |
| 28 |
|
| /* arr_realloc • realloc memory of a struct array */ |
| 29 |
|
| static int |
| 30 |
0 |
| arr_realloc(struct array* arr, int neosz) { |
| 31 |
|
| void* neo; |
| 32 |
0 |
| neo = realloc(arr->base, neosz * arr->unit); |
| 33 |
0 |
▶ | if (neo == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (neo == 0) return 0; |
0 |
0 |
0 |
|
| 34 |
0 |
| arr->base = neo; |
| 35 |
0 |
| arr->asize = neosz; |
| 36 |
0 |
▶ | if (arr->size > neosz) arr->size = neosz; |
Branch: 1 branch
| Branch | True | False | Total |
| if (arr->size > neosz) arr->size = neosz; |
0 |
0 |
0 |
|
| 37 |
0 |
| return 1; } |
| 38 |
|
| |
| 39 |
|
| |
| 40 |
|
| /* parr_realloc • realloc memory of a struct parray */ |
| 41 |
|
| static int |
| 42 |
3 |
| parr_realloc(struct parray* arr, int neosz) { |
| 43 |
|
| void* neo; |
| 44 |
3 |
| neo = realloc(arr->item, neosz * sizeof (void*)); |
| 45 |
3 |
▶ | if (neo == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (neo == 0) return 0; |
0 |
3 |
3 |
|
| 46 |
3 |
| arr->item = neo; |
| 47 |
3 |
| arr->asize = neosz; |
| 48 |
3 |
▶ | if (arr->size > neosz) arr->size = neosz; |
Branch: 1 branch
| Branch | True | False | Total |
| if (arr->size > neosz) arr->size = neosz; |
0 |
3 |
3 |
|
| 49 |
3 |
| return 1; } |
| 50 |
|
| |
| 51 |
|
| |
| 52 |
|
| |
| 53 |
|
| /*************************** |
| 54 |
|
| * GENERIC ARRAY FUNCTIONS * |
| 55 |
|
| ***************************/ |
| 56 |
|
| |
| 57 |
|
| /* arr_adjust • shrink the allocated memory to fit exactly the needs */ |
| 58 |
|
| int |
| 59 |
0 |
| arr_adjust(struct array *arr) { |
| 60 |
0 |
| return arr_realloc(arr, arr->size); } |
| 61 |
|
| |
| 62 |
|
| |
| 63 |
|
| /* arr_free • frees the structure contents (buf NOT the struct itself) */ |
| 64 |
|
| void |
| 65 |
1 |
| arr_free(struct array *arr) { |
| 66 |
1 |
▶ | if (!arr) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!arr) return; |
0 |
1 |
1 |
|
| 67 |
1 |
| free(arr->base); |
| 68 |
1 |
| arr->base = 0; |
| 69 |
1 |
| arr->size = arr->asize = 0; } |
| 70 |
|
| |
| 71 |
|
| |
| 72 |
|
| /* arr_grow • increases the array size to fit the given number of elements */ |
| 73 |
|
| int |
| 74 |
0 |
| arr_grow(struct array *arr, int need) { |
| 75 |
0 |
▶ | if (arr->asize >= need) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (arr->asize >= need) return 1; |
0 |
0 |
0 |
|
| 76 |
0 |
| else return arr_realloc(arr, need); } |
| 77 |
|
| |
| 78 |
|
| |
| 79 |
|
| /* arr_init • initialization of the contents of the struct */ |
| 80 |
|
| void |
| 81 |
1 |
| arr_init(struct array *arr, size_t unit) { |
| 82 |
1 |
| arr->base = 0; |
| 83 |
1 |
| arr->size = arr->asize = 0; |
| 84 |
1 |
| arr->unit = unit; } |
| 85 |
|
| |
| 86 |
|
| |
| 87 |
|
| /* arr_insert • inserting nb elements before the nth one */ |
| 88 |
|
| int |
| 89 |
0 |
| arr_insert(struct array *arr, int nb, int n) { |
| 90 |
|
| char *src, *dst; |
| 91 |
|
| size_t len; |
| 92 |
0 |
▶ | if (!arr || nb <= 0 || n < 0 |
Branch: 3 branches
| Branch | True | False | Total |
| if (!arr || nb <= 0 || n < 0 |
0 |
0 |
0 |
| if (!arr || nb <= 0 || n < 0 |
0 |
0 |
0 |
| if (!arr || nb <= 0 || n < 0 |
0 |
0 |
0 |
|
| 93 |
0 |
▶ | || !arr_grow(arr, arr->size + nb)) |
Branch: 1 branch
| Branch | True | False | Total |
| || !arr_grow(arr, arr->size + nb)) |
0 |
0 |
0 |
|
| 94 |
0 |
| return 0; |
| 95 |
0 |
▶ | if (n < arr->size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < arr->size) { |
0 |
0 |
0 |
|
| 96 |
0 |
| src = arr->base; |
| 97 |
0 |
| src += n * arr->unit; |
| 98 |
0 |
| dst = src + nb * arr->unit; |
| 99 |
0 |
| len = (arr->size - n) * arr->unit; |
| 100 |
0 |
| memmove(dst, src, len); } |
| 101 |
0 |
| arr->size += nb; |
| 102 |
0 |
| return 1; } |
| 103 |
|
| |
| 104 |
|
| |
| 105 |
|
| /* arr_item • returns a pointer to the n-th element */ |
| 106 |
|
| void * |
| 107 |
0 |
| arr_item(struct array *arr, int no) { |
| 108 |
|
| char *ptr; |
| 109 |
0 |
▶ | if (!arr || no < 0 || no >= arr->size) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!arr || no < 0 || no >= arr->size) return 0; |
0 |
0 |
0 |
| if (!arr || no < 0 || no >= arr->size) return 0; |
0 |
0 |
0 |
| if (!arr || no < 0 || no >= arr->size) return 0; |
0 |
0 |
0 |
|
| 110 |
0 |
| ptr = arr->base; |
| 111 |
0 |
| ptr += no * arr->unit; |
| 112 |
0 |
| return ptr; } |
| 113 |
|
| |
| 114 |
|
| |
| 115 |
|
| /* arr_newitem • returns the index of a new element appended to the array */ |
| 116 |
|
| int |
| 117 |
0 |
| arr_newitem(struct array *arr) { |
| 118 |
0 |
▶ | if (!arr_grow(arr, arr->size + 1)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!arr_grow(arr, arr->size + 1)) return -1; |
0 |
0 |
0 |
|
| 119 |
0 |
| arr->size += 1; |
| 120 |
0 |
| return arr->size - 1; } |
| 121 |
|
| |
| 122 |
|
| |
| 123 |
|
| /* arr_remove • removes the n-th elements of the array */ |
| 124 |
|
| void |
| 125 |
0 |
| arr_remove(struct array *arr, int idx) { |
| 126 |
0 |
▶ | if (!arr || idx < 0 || idx >= arr->size) return; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!arr || idx < 0 || idx >= arr->size) return; |
0 |
0 |
0 |
| if (!arr || idx < 0 || idx >= arr->size) return; |
0 |
0 |
0 |
| if (!arr || idx < 0 || idx >= arr->size) return; |
0 |
0 |
0 |
|
| 127 |
0 |
| arr->size -= 1; |
| 128 |
0 |
▶ | if (idx < arr->size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (idx < arr->size) { |
0 |
0 |
0 |
|
| 129 |
0 |
| char *dst = arr->base; |
| 130 |
|
| char *src; |
| 131 |
0 |
| dst += idx * arr->unit; |
| 132 |
0 |
| src = dst + arr->unit; |
| 133 |
0 |
| memmove(dst, src, (arr->size - idx) * arr->unit); } } |
| 134 |
|
| |
| 135 |
|
| |
| 136 |
|
| /* arr_sorted_find • O(log n) search in a sorted array, returning entry */ |
| 137 |
|
| void * |
| 138 |
0 |
| arr_sorted_find(struct array *arr, void *key, array_cmp_fn cmp) { |
| 139 |
|
| int mi, ma, cu, ret; |
| 140 |
0 |
| char *ptr = arr->base; |
| 141 |
0 |
| mi = -1; |
| 142 |
0 |
| ma = arr->size; |
| 143 |
0 |
▶ | while (mi < ma - 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (mi < ma - 1) { |
0 |
0 |
0 |
|
| 144 |
0 |
| cu = mi + (ma - mi) / 2; |
| 145 |
0 |
| ret = cmp(key, ptr + cu * arr->unit); |
| 146 |
0 |
▶ | if (ret == 0) return ptr + cu * arr->unit; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == 0) return ptr + cu * arr->unit; |
0 |
0 |
0 |
|
| 147 |
0 |
▶ | else if (ret < 0) ma = cu; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ret < 0) ma = cu; |
0 |
0 |
0 |
|
| 148 |
0 |
| else /* if (ret > 0) */ mi = cu; } |
| 149 |
0 |
| return 0; } |
| 150 |
|
| |
| 151 |
|
| |
| 152 |
|
| /* arr_sorted_find_i • O(log n) search in a sorted array, |
| 153 |
|
| * returning index of the smallest element larger than the key */ |
| 154 |
|
| int |
| 155 |
0 |
| arr_sorted_find_i(struct array *arr, void *key, array_cmp_fn cmp) { |
| 156 |
|
| int mi, ma, cu, ret; |
| 157 |
0 |
| char *ptr = arr->base; |
| 158 |
0 |
| mi = -1; |
| 159 |
0 |
| ma = arr->size; |
| 160 |
0 |
▶ | while (mi < ma - 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (mi < ma - 1) { |
0 |
0 |
0 |
|
| 161 |
0 |
| cu = mi + (ma - mi) / 2; |
| 162 |
0 |
| ret = cmp(key, ptr + cu * arr->unit); |
| 163 |
0 |
▶ | if (ret == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == 0) { |
0 |
0 |
0 |
|
| 164 |
0 |
▶ | while (cu < arr->size && ret == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (cu < arr->size && ret == 0) { |
0 |
0 |
0 |
| while (cu < arr->size && ret == 0) { |
0 |
0 |
0 |
|
| 165 |
0 |
| cu += 1; |
| 166 |
0 |
| ret = cmp(key, ptr + cu * arr->unit); } |
| 167 |
0 |
| return cu; } |
| 168 |
0 |
▶ | else if (ret < 0) ma = cu; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ret < 0) ma = cu; |
0 |
0 |
0 |
|
| 169 |
0 |
| else /* if (ret > 0) */ mi = cu; } |
| 170 |
0 |
| return ma; } |
| 171 |
|
| |
| 172 |
|
| |
| 173 |
|
| |
| 174 |
|
| /*************************** |
| 175 |
|
| * POINTER ARRAY FUNCTIONS * |
| 176 |
|
| ***************************/ |
| 177 |
|
| |
| 178 |
|
| /* parr_adjust • shrinks the allocated memory to fit exactly the needs */ |
| 179 |
|
| int |
| 180 |
0 |
| parr_adjust(struct parray* arr) { |
| 181 |
0 |
| return parr_realloc (arr, arr->size); } |
| 182 |
|
| |
| 183 |
|
| |
| 184 |
|
| /* parr_free • frees the structure contents (buf NOT the struct itself) */ |
| 185 |
|
| void |
| 186 |
2 |
| parr_free(struct parray *arr) { |
| 187 |
2 |
▶ | if (!arr) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!arr) return; |
0 |
2 |
2 |
|
| 188 |
2 |
| free (arr->item); |
| 189 |
2 |
| arr->item = 0; |
| 190 |
2 |
| arr->size = 0; |
| 191 |
2 |
| arr->asize = 0; } |
| 192 |
|
| |
| 193 |
|
| |
| 194 |
|
| /* parr_grow • increases the array size to fit the given number of elements */ |
| 195 |
|
| int |
| 196 |
3 |
| parr_grow(struct parray *arr, int need) { |
| 197 |
3 |
▶ | if (arr->asize >= need) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (arr->asize >= need) return 1; |
0 |
3 |
3 |
|
| 198 |
3 |
| else return parr_realloc (arr, need); } |
| 199 |
|
| |
| 200 |
|
| |
| 201 |
|
| /* parr_init • initialization of the struct (which is equivalent to zero) */ |
| 202 |
|
| void |
| 203 |
2 |
| parr_init(struct parray *arr) { |
| 204 |
2 |
| arr->item = 0; |
| 205 |
2 |
| arr->size = 0; |
| 206 |
2 |
| arr->asize = 0; } |
| 207 |
|
| |
| 208 |
|
| |
| 209 |
|
| /* parr_insert • inserting nb elements before the nth one */ |
| 210 |
|
| int |
| 211 |
0 |
| parr_insert(struct parray *parr, int nb, int n) { |
| 212 |
|
| char *src, *dst; |
| 213 |
|
| size_t len, i; |
| 214 |
0 |
▶ | if (!parr || nb <= 0 || n < 0 |
Branch: 3 branches
| Branch | True | False | Total |
| if (!parr || nb <= 0 || n < 0 |
0 |
0 |
0 |
| if (!parr || nb <= 0 || n < 0 |
0 |
0 |
0 |
| if (!parr || nb <= 0 || n < 0 |
0 |
0 |
0 |
|
| 215 |
0 |
▶ | || !parr_grow(parr, parr->size + nb)) |
Branch: 1 branch
| Branch | True | False | Total |
| || !parr_grow(parr, parr->size + nb)) |
0 |
0 |
0 |
|
| 216 |
0 |
| return 0; |
| 217 |
0 |
▶ | if (n < parr->size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < parr->size) { |
0 |
0 |
0 |
|
| 218 |
0 |
| src = (void *)parr->item; |
| 219 |
0 |
| src += n * sizeof (void *); |
| 220 |
0 |
| dst = src + nb * sizeof (void *); |
| 221 |
0 |
| len = (parr->size - n) * sizeof (void *); |
| 222 |
0 |
| memmove(dst, src, len); |
| 223 |
0 |
▶ | for (i = 0; i < (size_t)nb; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < (size_t)nb; ++i) |
0 |
0 |
0 |
|
| 224 |
0 |
| parr->item[n + i] = 0; } |
| 225 |
0 |
| parr->size += nb; |
| 226 |
0 |
| return 1; } |
| 227 |
|
| |
| 228 |
|
| |
| 229 |
|
| /* parr_pop • pops the last item of the array and returns it */ |
| 230 |
|
| void * |
| 231 |
0 |
| parr_pop(struct parray *arr) { |
| 232 |
0 |
▶ | if (arr->size <= 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (arr->size <= 0) return 0; |
0 |
0 |
0 |
|
| 233 |
0 |
| arr->size -= 1; |
| 234 |
0 |
| return arr->item[arr->size]; } |
| 235 |
|
| |
| 236 |
|
| |
| 237 |
|
| /* parr_push • pushes a pointer at the end of the array (= append) */ |
| 238 |
|
| int |
| 239 |
3 |
| parr_push(struct parray *arr, void *i) { |
| 240 |
3 |
▶ | if (!parr_grow(arr, arr->size + 1)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!parr_grow(arr, arr->size + 1)) return 0; |
0 |
3 |
3 |
|
| 241 |
3 |
| arr->item[arr->size] = i; |
| 242 |
3 |
| arr->size += 1; |
| 243 |
3 |
| return 1; } |
| 244 |
|
| |
| 245 |
|
| |
| 246 |
|
| /* parr_remove • removes the n-th element of the array and returns it */ |
| 247 |
|
| void * |
| 248 |
0 |
| parr_remove(struct parray *arr, int idx) { |
| 249 |
|
| void* ret; |
| 250 |
|
| int i; |
| 251 |
0 |
▶ | if (!arr || idx < 0 || idx >= arr->size) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!arr || idx < 0 || idx >= arr->size) return 0; |
0 |
0 |
0 |
| if (!arr || idx < 0 || idx >= arr->size) return 0; |
0 |
0 |
0 |
| if (!arr || idx < 0 || idx >= arr->size) return 0; |
0 |
0 |
0 |
|
| 252 |
0 |
| ret = arr->item[idx]; |
| 253 |
0 |
▶ | for (i = idx+1; i < arr->size; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = idx+1; i < arr->size; ++i) |
0 |
0 |
0 |
|
| 254 |
0 |
| arr->item[i - 1] = arr->item[i]; |
| 255 |
0 |
| arr->size -= 1; |
| 256 |
0 |
| return ret; } |
| 257 |
|
| |
| 258 |
|
| |
| 259 |
|
| /* parr_sorted_find • O(log n) search in a sorted array, returning entry */ |
| 260 |
|
| void * |
| 261 |
0 |
| parr_sorted_find(struct parray *arr, void *key, array_cmp_fn cmp) { |
| 262 |
|
| int mi, ma, cu, ret; |
| 263 |
0 |
| mi = -1; |
| 264 |
0 |
| ma = arr->size; |
| 265 |
0 |
▶ | while (mi < ma - 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (mi < ma - 1) { |
0 |
0 |
0 |
|
| 266 |
0 |
| cu = mi + (ma - mi) / 2; |
| 267 |
0 |
| ret = cmp(key, arr->item[cu]); |
| 268 |
0 |
▶ | if (ret == 0) return arr->item[cu]; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == 0) return arr->item[cu]; |
0 |
0 |
0 |
|
| 269 |
0 |
▶ | else if (ret < 0) ma = cu; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ret < 0) ma = cu; |
0 |
0 |
0 |
|
| 270 |
0 |
| else /* if (ret > 0) */ mi = cu; } |
| 271 |
0 |
| return 0; } |
| 272 |
|
| |
| 273 |
|
| |
| 274 |
|
| /* parr_sorted_find_i • O(log n) search in a sorted array, |
| 275 |
|
| * returning index of the smallest element larger than the key */ |
| 276 |
|
| int |
| 277 |
0 |
| parr_sorted_find_i(struct parray *arr, void *key, array_cmp_fn cmp) { |
| 278 |
|
| int mi, ma, cu, ret; |
| 279 |
0 |
| mi = -1; |
| 280 |
0 |
| ma = arr->size; |
| 281 |
0 |
▶ | while (mi < ma - 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (mi < ma - 1) { |
0 |
0 |
0 |
|
| 282 |
0 |
| cu = mi + (ma - mi) / 2; |
| 283 |
0 |
| ret = cmp(key, arr->item[cu]); |
| 284 |
0 |
▶ | if (ret == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == 0) { |
0 |
0 |
0 |
|
| 285 |
0 |
▶ | while (cu < arr->size && ret == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (cu < arr->size && ret == 0) { |
0 |
0 |
0 |
| while (cu < arr->size && ret == 0) { |
0 |
0 |
0 |
|
| 286 |
0 |
| cu += 1; |
| 287 |
0 |
| ret = cmp(key, arr->item[cu]); } |
| 288 |
0 |
| return cu; } |
| 289 |
0 |
▶ | else if (ret < 0) ma = cu; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ret < 0) ma = cu; |
0 |
0 |
0 |
|
| 290 |
0 |
| else /* if (ret > 0) */ mi = cu; } |
| 291 |
0 |
| return ma; } |
| 292 |
|
| |
| 293 |
|
| |
| 294 |
|
| /* parr_top • returns the top the stack (i.e. the last element of the array) */ |
| 295 |
|
| void * |
| 296 |
0 |
| parr_top(struct parray *arr) { |
| 297 |
0 |
▶ | if (arr == 0 || arr->size <= 0) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (arr == 0 || arr->size <= 0) return 0; |
0 |
0 |
0 |
| if (arr == 0 || arr->size <= 0) return 0; |
0 |
0 |
0 |
|
| 298 |
0 |
| else return arr->item[arr->size - 1]; } |
| 299 |
|
| |
| 300 |
|
| /* vim: set filetype=c: */ |