-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathn_array_remove_nth.c
More file actions
36 lines (28 loc) · 903 Bytes
/
n_array_remove_nth.c
File metadata and controls
36 lines (28 loc) · 903 Bytes
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
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "n_array_int.h"
tn_array *n_array_remove_nth(tn_array *arr, int i)
{
register unsigned int pos = arr->start_index + i;
trurl_die__if_frozen(arr);
n_assert(i >= 0);
n_assert(arr->allocated > 0);
n_assert(arr->items > 0);
if ((size_t) i >= arr->items || i < 0) {
trurl_die("n_array_remove_nth: index(%d) out of bounds(0 - %d)\n", i,
arr->items);
return NULL;
}
/* if slot is not empty, free node data */
if (arr->data[pos] != NULL && arr->free_fn != NULL)
arr->free_fn(arr->data[pos]);
if (pos == arr->items)
arr->data[pos] = NULL;
else
memmove(&arr->data[pos], &arr->data[pos + 1],
(arr->allocated - 1 - pos) * sizeof(*arr->data));
arr->data[arr->allocated - 1] = NULL;
arr->items--;
return arr;
}