-
Notifications
You must be signed in to change notification settings - Fork 0
/
dyn_array.c
117 lines (87 loc) · 2 KB
/
dyn_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
#include <stdlib.h>
#include "dyn_array.h"
DynArray *new_dyn_array(size_t elem_size) {
DynArray *new_arr = (DynArray *)malloc(sizeof(DynArray));
if (!new_arr) {
return NULL;
}
new_arr->array = calloc(INIT_CAPACITY, elem_size);
new_arr->elem = malloc(elem_size);
if (!new_arr->array || !new_arr->elem) {
if (new_arr->array) free(new_arr->array);
if (new_arr->elem) free(new_arr->elem);
free(new_arr);
return NULL;
}
new_arr->capacity = INIT_CAPACITY;
new_arr->size = 0;
new_arr->elem_size = elem_size;
return new_arr;
}
void destroy_dyn_array(DynArray *arr) {
free(arr->array);
free(arr->elem);
free(arr);
}
bool expand(DynArray *arr) {
void *new_array = realloc(arr->array, arr->capacity * 2);
if (new_array) {
arr->capacity *= 2;
arr->array = new_array;
return true;
}
else {
return false;
}
}
bool shrink(DynArray *arr) {
if (arr->capacity <= 2) return false;
void *new_array = realloc(arr->array, arr->capacity / 2);
if (new_array) {
arr->capacity /= 2;
arr->array = new_array;
return true;
}
else {
return false;
}
}
bool insert(void *elem, DynArray *arr, size_t index) {
if (index > arr->size) {
return false;
}
if (arr->size == arr->capacity) {
bool success = expand(arr);
if (!success) return false;
}
size_t e_s = arr->elem_size;
char *a = (char*)arr->array;
for (int i = arr->size; i > index; --i) {
memcpy(a + i*e_s, a + (i - 1)*e_s, e_s);
}
memcpy(a + index*e_s, elem, e_s);
++(arr->size);
return true;
}
void *remove(DynArray *arr, size_t index) {
if (index >= arr->size) {
return NULL;
}
size_t e_s = arr->elem_size;
char*a = (char*)arr->array;
memcpy(arr->elem, a + index*e_s, e_s);
for (int i = index; i < arr->size; ++i) {
memcpy(a + i*e_s, a + (i + 1)*e_s, e_s);
}
--(arr->size);
if ((arr->size > 1) && (arr->size < arr->capacity / 3)) {
shrink(arr);
}
return arr->elem;
}
bool push(void *elem, DynArray *arr) {
return insert(elem, arr, arr->size);
}
void *pop(DynArray *arr) {
return remove(arr, arr->size - 1);
}