-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cvector_d.c
362 lines (313 loc) · 9.08 KB
/
cvector_d.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include "cvector_d.h"
#if defined(CVEC_MALLOC) && defined(CVEC_FREE) && defined(CVEC_REALLOC)
/* ok */
#elif !defined(CVEC_MALLOC) && !defined(CVEC_FREE) && !defined(CVEC_REALLOC)
/* ok */
#else
#error "Must define all or none of CVEC_MALLOC, CVEC_FREE, and CVEC_REALLOC."
#endif
#ifndef CVEC_MALLOC
#include <stdlib.h>
#define CVEC_MALLOC(sz) malloc(sz)
#define CVEC_REALLOC(p, sz) realloc(p, sz)
#define CVEC_FREE(p) free(p)
#endif
#ifndef CVEC_MEMMOVE
#include <string.h>
#define CVEC_MEMMOVE(dst, src, sz) memmove(dst, src, sz)
#endif
#ifndef CVEC_ASSERT
#include <assert.h>
#define CVEC_ASSERT(x) assert(x)
#endif
cvec_sz CVEC_D_START_SZ = 50;
#define CVEC_D_ALLOCATOR(x) ((x+1) * 2)
/**
* Creates a new cvector_d on the heap.
* Vector size set to (size > 0) ? size : 0;
* Capacity to (capacity > vec->size || (vec->size && capacity == vec->size)) ? capacity : vec->size + CVEC_D_START_SZ
* in other words capacity has to be at least 1 and >= to vec->size of course.
*/
cvector_d* cvec_d_heap(cvec_sz size, cvec_sz capacity)
{
cvector_d* vec;
if (!(vec = (cvector_d*)CVEC_MALLOC(sizeof(cvector_d)))) {
CVEC_ASSERT(vec != NULL);
return NULL;
}
vec->size = size;
vec->capacity = (capacity > vec->size || (vec->size && capacity == vec->size)) ? capacity : vec->size + CVEC_D_START_SZ;
if (!(vec->a = (double*)CVEC_MALLOC(vec->capacity*sizeof(double)))) {
CVEC_ASSERT(vec->a != NULL);
CVEC_FREE(vec);
return NULL;
}
return vec;
}
/** Create (on the heap) and initialize cvector_d with num elements of vals.
* Capacity is set to num + CVEC_D_START_SZ.
*/
cvector_d* cvec_init_d_heap(double* vals, cvec_sz num)
{
cvector_d* vec;
if (!(vec = (cvector_d*)CVEC_MALLOC(sizeof(cvector_d)))) {
CVEC_ASSERT(vec != NULL);
return NULL;
}
vec->capacity = num + CVEC_D_START_SZ;
vec->size = num;
if (!(vec->a = (double*)CVEC_MALLOC(vec->capacity*sizeof(double)))) {
CVEC_ASSERT(vec->a != NULL);
CVEC_FREE(vec);
return NULL;
}
CVEC_MEMMOVE(vec->a, vals, sizeof(double)*num);
return vec;
}
/** Same as cvec_d_heap() except the vector passed in was declared on the stack so
* it isn't allocated in this function. Use the cvec_free_d in this case.
* This and cvec_init_d should be preferred over the heap versions.
*/
int cvec_d(cvector_d* vec, cvec_sz size, cvec_sz capacity)
{
vec->size = size;
vec->capacity = (capacity > vec->size || (vec->size && capacity == vec->size)) ? capacity : vec->size + CVEC_D_START_SZ;
if (!(vec->a = (double*)CVEC_MALLOC(vec->capacity*sizeof(double)))) {
CVEC_ASSERT(vec->a != NULL);
vec->size = vec->capacity = 0;
return 0;
}
return 1;
}
/** Same as cvec_init_d_heap() except the vector passed in was declared on the stack so
* it isn't allocated in this function. Use the cvec_free_d in this case.
*/
int cvec_init_d(cvector_d* vec, double* vals, cvec_sz num)
{
vec->capacity = num + CVEC_D_START_SZ;
vec->size = num;
if (!(vec->a = (double*)CVEC_MALLOC(vec->capacity*sizeof(double)))) {
CVEC_ASSERT(vec->a != NULL);
vec->size = vec->capacity = 0;
return 0;
}
CVEC_MEMMOVE(vec->a, vals, sizeof(double)*num);
return 1;
}
/** Makes dest a copy of src. The parameters
* are void so it can be used as the constructor when making
* a vector of cvector_d's. Assumes dest (the structure)
* is already allocated (probably on the stack) and that
* capacity is 0 (ie the array doesn't need to be freed).
*
* Really just a wrapper around copy, that initializes dest/vec1's
* members to NULL/0. If you pre-initialized dest to 0, you could
* just use copy.
*/
int cvec_copyc_d(void* dest, void* src)
{
cvector_d* vec1 = (cvector_d*)dest;
cvector_d* vec2 = (cvector_d*)src;
vec1->a = NULL;
vec1->size = 0;
vec1->capacity = 0;
return cvec_copy_d(vec1, vec2);
}
/** Makes dest a copy of src. Assumes dest
* (the structure) is already allocated (probably on the stack) and
* is in a valid state (ie array is either NULL or allocated with
* size and capacity set appropriately).
*
* TODO Should I copy capacity, so dest is truly identical or do
* I only care about the actual contents, and let dest->cap = src->size
* maybe plus CVEC_D_START_SZ
*/
int cvec_copy_d(cvector_d* dest, cvector_d* src)
{
double* tmp = NULL;
if (!(tmp = (double*)CVEC_REALLOC(dest->a, src->capacity*sizeof(double)))) {
CVEC_ASSERT(tmp != NULL);
return 0;
}
dest->a = tmp;
CVEC_MEMMOVE(dest->a, src->a, src->size*sizeof(double));
dest->size = src->size;
dest->capacity = src->capacity;
return 1;
}
/** Append a to end of vector (size increased 1).
* Capacity is increased by doubling when necessary.
*/
int cvec_push_d(cvector_d* vec, double a)
{
double* tmp;
cvec_sz tmp_sz;
if (vec->capacity == vec->size) {
tmp_sz = CVEC_D_ALLOCATOR(vec->capacity);
if (!(tmp = (double*)CVEC_REALLOC(vec->a, sizeof(double)*tmp_sz))) {
CVEC_ASSERT(tmp != NULL);
return 0;
}
vec->a = tmp;
vec->capacity = tmp_sz;
}
vec->a[vec->size++] = a;
return 1;
}
/** Remove and return the last element (size decreased 1).*/
double cvec_pop_d(cvector_d* vec)
{
return vec->a[--vec->size];
}
/** Return pointer to last element */
double* cvec_back_d(cvector_d* vec)
{
return &vec->a[vec->size-1];
}
/** Increase the size of the array num items. Items
* are not initialized to anything */
int cvec_extend_d(cvector_d* vec, cvec_sz num)
{
double* tmp;
cvec_sz tmp_sz;
if (vec->capacity < vec->size + num) {
tmp_sz = vec->capacity + num + CVEC_D_START_SZ;
if (!(tmp = (double*)CVEC_REALLOC(vec->a, sizeof(double)*tmp_sz))) {
CVEC_ASSERT(tmp != NULL);
return 0;
}
vec->a = tmp;
vec->capacity = tmp_sz;
}
vec->size += num;
return 1;
}
/**
* Insert a at index i (0 based).
* Everything from that index and right is shifted one to the right.
*/
int cvec_insert_d(cvector_d* vec, cvec_sz i, double a)
{
double* tmp;
cvec_sz tmp_sz;
if (vec->capacity == vec->size) {
tmp_sz = CVEC_D_ALLOCATOR(vec->capacity);
if (!(tmp = (double*)CVEC_REALLOC(vec->a, sizeof(double)*tmp_sz))) {
CVEC_ASSERT(tmp != NULL);
return 0;
}
vec->a = tmp;
vec->capacity = tmp_sz;
}
CVEC_MEMMOVE(&vec->a[i+1], &vec->a[i], (vec->size-i)*sizeof(double));
vec->a[i] = a;
vec->size++;
return 1;
}
/**
* Insert the first num elements of array a at index i.
* Note that it is the user's responsibility to pass in valid
* arguments. Also CVEC_MEMMOVE is used so don't try to insert
* part of the vector array into itself (that would require CVEC_MEMMOVE)
*/
int cvec_insert_array_d(cvector_d* vec, cvec_sz i, double* a, cvec_sz num)
{
double* tmp;
cvec_sz tmp_sz;
if (vec->capacity < vec->size + num) {
tmp_sz = vec->capacity + num + CVEC_D_START_SZ;
if (!(tmp = (double*)CVEC_REALLOC(vec->a, sizeof(double)*tmp_sz))) {
CVEC_ASSERT(tmp != NULL);
return 0;
}
vec->a = tmp;
vec->capacity = tmp_sz;
}
CVEC_MEMMOVE(&vec->a[i+num], &vec->a[i], (vec->size-i)*sizeof(double));
CVEC_MEMMOVE(&vec->a[i], a, num*sizeof(double));
vec->size += num;
return 1;
}
/** Replace value at index i with a, return original value. */
double cvec_replace_d(cvector_d* vec, cvec_sz i, double a)
{
double tmp = vec->a[i];
vec->a[i] = a;
return tmp;
}
/**
* Erases elements from start to end inclusive.
* Example cvec_erase_d(myvec, 1, 3) would remove elements at 1, 2, and 3 and the element
* that was at index 4 would now be at 1 etc.
*/
void cvec_erase_d(cvector_d* vec, cvec_sz start, cvec_sz end)
{
cvec_sz d = end - start + 1;
CVEC_MEMMOVE(&vec->a[start], &vec->a[end+1], (vec->size-1-end)*sizeof(double));
vec->size -= d;
}
/** Make sure capacity is at least size(parameter not member). */
int cvec_reserve_d(cvector_d* vec, cvec_sz size)
{
double* tmp;
if (vec->capacity < size) {
if (!(tmp = (double*)CVEC_REALLOC(vec->a, sizeof(double)*(size+CVEC_D_START_SZ)))) {
CVEC_ASSERT(tmp != NULL);
return 0;
}
vec->a = tmp;
vec->capacity = size + CVEC_D_START_SZ;
}
return 1;
}
/** Set capacity to size.
* You will lose data if you shrink the capacity below the current size.
* If you do, the size will be set to capacity of course.
*/
int cvec_set_cap_d(cvector_d* vec, cvec_sz size)
{
double* tmp;
if (size < vec->size)
vec->size = size;
if (!(tmp = (double*)CVEC_REALLOC(vec->a, sizeof(double)*size))) {
CVEC_ASSERT(tmp != NULL);
return 0;
}
vec->a = tmp;
vec->capacity = size;
return 1;
}
/** Set all size elements to val. */
void cvec_set_val_sz_d(cvector_d* vec, double val)
{
cvec_sz i;
for(i=0; i<vec->size; i++) {
vec->a[i] = val;
}
}
/** Fills entire allocated array (capacity) with val. */
void cvec_set_val_cap_d(cvector_d* vec, double val)
{
cvec_sz i;
for(i=0; i<vec->capacity; i++) {
vec->a[i] = val;
}
}
/** Sets size to 0 (does not clear contents).*/
void cvec_clear_d(cvector_d* vec) { vec->size = 0; }
/** Frees everything so don't use vec after calling this.
* Passing NULL is a NO-OP, matching the behavior of free(). */
void cvec_free_d_heap(void* vec)
{
cvector_d* tmp = (cvector_d*)vec;
if (!tmp) return;
CVEC_FREE(tmp->a);
CVEC_FREE(tmp);
}
/** Frees the internal array and sets size and capacity to 0 */
void cvec_free_d(void* vec)
{
cvector_d* tmp = (cvector_d*)vec;
CVEC_FREE(tmp->a); tmp->size = 0;
tmp->capacity = 0;
}