forked from 3RUN/Dynamic-Array
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_array.c
227 lines (173 loc) · 4.37 KB
/
dynamic_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
Array *_array_create(size_t type_size)
{
Array *array = sys_malloc(sizeof(Array));
if (!array)
return NULL;
array->capacity = ARRAY_INITIAL_CAPACITY;
array->size = 0;
array->type_size = type_size;
array->data = sys_malloc(ARRAY_INITIAL_CAPACITY * type_size);
if (!array->data)
return NULL;
return array;
}
void array_destroy(Array *array)
{
if (!array)
return;
sys_free(array->data);
sys_free(array);
}
void array_change_capacity(Array *array, size_t new_capacity)
{
if (!array)
return;
if (new_capacity <= 0)
new_capacity = array->capacity * 2;
ArrayData *new_data = sys_malloc(new_capacity * array->type_size);
memcpy(new_data, array->data, array->size * array->type_size);
sys_free(array->data);
array->data = new_data;
array->capacity = new_capacity;
}
size_t array_size(Array *array)
{
if (!array)
return -1;
return array->size;
}
size_t array_capacity(Array *array)
{
if (!array)
return -1;
return array->capacity;
}
ArrayData *array_data(Array *array)
{
if (!array)
return NULL;
return array->data;
}
bool is_array_empty(Array *array)
{
if (!array)
return true;
return array_size(array) <= 0;
}
bool is_valid_index(Array *array, int index)
{
if (!array)
return false;
if (is_array_empty(array))
return false;
return index > 0 && index < array->size;
}
ArrayData *_array_at(Array *array, int index)
{
if (!array)
return NULL;
if (index < 0 || index > array->size)
{
#ifdef DEBUG_ARRAY
error("Can't get item. Index out of range!");
#endif
return NULL;
}
return (unsigned char *)(array->data) + index * array->type_size;
}
void array_insert_at(Array *array, int index, ArrayData *item)
{
if (!array)
return;
if (index < 0 || index > array->size)
{
#ifdef DEBUG_ARRAY
error("Can't insert item. Index out of range!");
#endif
return NULL;
}
array->size++;
if (array->size >= array->capacity)
array_change_capacity(array, array->capacity * 2);
int i = 0;
for (i = array_size(array); i > index; i--)
array_change_at(ArrayData *, array, i, array_get_at(ArrayData *, array, i - 1));
array_change_at(ArrayData *, array, index, item);
}
void array_add(Array *array, ArrayData *item)
{
if (!array)
return;
if (array->size >= array->capacity)
array_change_capacity(array, array->capacity * 2);
array_change_at(ArrayData *, array, array->size, item);
array->size++;
}
void array_remove_last(Array *array)
{
if (!array)
return;
if (is_array_empty(array))
return;
array_last(ArrayData *, array) = NULL;
array->size--;
if (array->size < (array->capacity * 0.5))
array_change_capacity(array, array->capacity * 0.5);
}
void array_remove_at(Array *array, int index)
{
if (!array)
return;
if (is_array_empty(array))
return;
if (index < 0 || index > array->size)
{
#ifdef DEBUG_ARRAY
error("Can't remove item. Index out of range!");
#endif
return;
}
if (index == array->size)
return;
int i = 0;
for (i = index; i < array_size(array) - 1; i++)
array_change_at(ArrayData *, array, i, array_get_at(ArrayData *, array, i + 1));
array_remove_last(array);
}
void array_clear(Array *array)
{
if (!array)
return;
if (is_array_empty(array))
return;
int i = 0, size = array_size(array);
for (i = 0; i < size; i++)
array_remove_last(array);
}
int array_find(Array *array, ArrayData *item)
{
if (!array)
return -1;
if (is_array_empty(array))
return -1;
int i = 0;
for (i = 0; i < array_size(array); i++)
{
if (array_get_at(ArrayData *, array, i) != item)
continue;
return i;
}
return -1;
}
void array_swap(Array *array, int index_a, int index_b)
{
if (!array)
return;
if (is_array_empty(array))
return;
if (index_a < 0 || index_b < 0 || index_a >= array->size || index_b >= array->size)
return;
ArrayData *temp_data = array_get_at(ArrayData *, array, index_a);
array_change_at(ArrayData *, array, index_a, array_get_at(ArrayData *, array, index_b));
array_change_at(ArrayData *, array, index_b, temp_data);
}