-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy patharray.c
43 lines (36 loc) · 860 Bytes
/
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
#include <assert.h>
#include <mrsh/array.h>
#include <stdlib.h>
#define INITIAL_SIZE 8
bool mrsh_array_reserve(struct mrsh_array *array, size_t new_cap) {
if (array->cap >= new_cap) {
return true;
}
void *new_data = realloc(array->data, new_cap * sizeof(void *));
if (new_data == NULL) {
return false;
}
array->data = new_data;
array->cap = new_cap;
return true;
}
ssize_t mrsh_array_add(struct mrsh_array *array, void *value) {
assert(array->len <= array->cap);
if (array->len == array->cap) {
size_t new_cap = 2 * array->cap;
if (new_cap < INITIAL_SIZE) {
new_cap = INITIAL_SIZE;
}
if (!mrsh_array_reserve(array, new_cap)) {
return -1;
}
}
size_t i = array->len;
array->data[i] = value;
array->len++;
return i;
}
void mrsh_array_finish(struct mrsh_array *array) {
free(array->data);
array->cap = array->len = 0;
}