forked from dogecoinfoundation/libdogecoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.c
306 lines (257 loc) · 7.47 KB
/
vector.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
The MIT License (MIT)
Copyright (c) 2015 Jonas Schnelli
Copyright (c) 2022 bluezr
Copyright (c) 2022 The Dogecoin Foundation
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#include <dogecoin/mem.h>
#include <dogecoin/vector.h>
/**
* @brief This function creates a new vector object
* and initializes it to 0.
*
* @param res The size of memory to allocate for the vector's contents.
* @param free_f The function that will be called when a vector element is freed.
*
* @return A pointer to the new vector object.
*/
vector* vector_new(size_t res, void (*free_f)(void*))
{
vector* vec = dogecoin_calloc(1, sizeof(vector));
if (!vec)
return NULL;
vec->alloc = 8;
while (vec->alloc < res)
vec->alloc *= 2;
vec->elem_free_f = free_f;
vec->data = dogecoin_calloc(1, vec->alloc * sizeof(void*));
if (!vec->data) {
dogecoin_free(vec);
return NULL;
}
return vec;
}
/**
* @brief This function frees all of a vector's elements,
* calling the function associated with its free operation
* set during vector creation. The vector object itself
* is not freed.
*
* @param vec The pointer to the vector to be freed.
*
* @return Nothing.
*/
static void vector_free_data(vector* vec)
{
if (!vec->data)
return;
if (vec->elem_free_f) {
unsigned int i;
for (i = 0; i < vec->len; i++)
if (vec->data[i]) {
vec->elem_free_f(vec->data[i]);
vec->data[i] = NULL;
}
}
dogecoin_free(vec->data);
vec->data = NULL;
vec->alloc = 0;
vec->len = 0;
}
/**
* @brief This function frees an entire vector
* object and all of its elements if specified.
*
* @param vec The pointer to the vector to be freed.
* @param free_array The flag denoting whether to free the vector's elements.
*
* @return Nothing.
*/
void vector_free(vector* vec, dogecoin_bool free_array)
{
if (!vec) {
return;
}
if (free_array) {
vector_free_data(vec);
}
dogecoin_mem_zero(vec, sizeof(*vec));
dogecoin_free(vec);
}
/**
* @brief This function grows the vector by doubling
* in size until it is larger than the size specified.
*
* @param vec The pointer to the vector to be grown.
* @param min_sz The minimum size the vector must be grown to.
*
* @return 1 if the vector is grown successfully, 0 if it reaches the max size allowed.
*/
static dogecoin_bool vector_grow(vector* vec, size_t min_sz)
{
size_t new_alloc = vec->alloc;
while (new_alloc < min_sz) {
new_alloc *= 2;
}
if (vec->alloc == new_alloc) {
return true;
}
void* new_data = dogecoin_realloc(vec->data, new_alloc * sizeof(void*));
if (!new_data) {
return false;
}
vec->data = new_data;
vec->alloc = new_alloc;
return true;
}
/**
* @brief This function finds and returns the first element
* in the vector whose data matches the data specified.
*
* @param vec The pointer to the vector to search.
* @param data The data to match.
*
* @return The index of the data if it exists in the vector, -1 otherwise.
*/
ssize_t vector_find(vector* vec, void* data)
{
if (vec && vec->len) {
size_t i;
for (i = 0; i < vec->len; i++) {
if (vec->data[i] == data) {
return (ssize_t)i;
}
}
}
return -1;
}
/**
* @brief This function adds an element to an existing
* vector, growing it by one if necessary.
*
* @param vec The pointer to the vector to add to.
* @param data The data to be added into the vector.
*
* @return 1 if the element was added successfully, 0 otherwise.
*/
dogecoin_bool vector_add(vector* vec, void* data)
{
if (vec->len == vec->alloc) {
if (!vector_grow(vec, vec->len + 1)) {
return false;
}
}
vec->data[vec->len] = data;
vec->len++;
return true;
}
/**
* @brief This function deletes a range of consecutive
* elements from the specified vector.
*
* @param vec The pointer to the vector to edit.
* @param pos The index of the first item to remove.
* @param len The number of consecutive elements to remove.
*
* @return Nothing.
*/
void vector_remove_range(vector* vec, size_t pos, size_t len)
{
if (!vec || ((pos + len) > vec->len)) {
return;
}
if (vec->elem_free_f) {
unsigned int i, count;
for (i = pos, count = 0; count < len; i++, count++) {
vec->elem_free_f(vec->data[i]);
}
}
memmove(&vec->data[pos], &vec->data[pos + len], (vec->len - pos - len) * sizeof(void*));
vec->len -= len;
}
/**
* @brief This function removes a single element from
* the specified vector.
*
* @param vec The pointer to the vector to edit.
* @param pos The index of the element to remove.
*/
void vector_remove_idx(vector* vec, size_t pos)
{
vector_remove_range(vec, pos, 1);
}
/**
* @brief This function finds an element whose data
* matches the data specified and removes it if it
* exists.
*
* @param vec The pointer to the vector to edit.
* @param data The data to match.
*
* @return 1 if the element was removed successfully, 0 if the data was not found.
*/
dogecoin_bool vector_remove(vector* vec, void* data)
{
ssize_t idx = vector_find(vec, data);
if (idx < 0) {
return false;
}
vector_remove_idx(vec, idx);
return true;
}
/**
* @brief This function resizes the vector to be newsz
* elements long. If the new size is bigger, the vector
* is grown and the new elements are left empty. If the
* new size is smaller, vector elements will be truncated.
* If the new size is the same, do nothing.
*
* @param vec The pointer to the vector to resize.
* @param newsz The new desired size of the vector.
*
* @return 1 if the vector was resized successfully, 0 otherwise.
*/
dogecoin_bool vector_resize(vector* vec, size_t newsz)
{
unsigned int i;
/* same size */
if (newsz == vec->len) {
return true;
}
/* truncate */
else if (newsz < vec->len) {
size_t del_count = vec->len - newsz;
for (i = (vec->len - del_count); i < vec->len; i++) {
if (vec->elem_free_f) {
vec->elem_free_f(vec->data[i]);
}
vec->data[i] = NULL;
}
vec->len = newsz;
return true;
}
/* last possibility: grow */
if (!vector_grow(vec, newsz)) {
return false;
}
/* set new elements to NULL */
for (i = vec->len; i < newsz; i++) {
vec->data[i] = NULL;
}
return true;
}