CVector  4.1.0
A C++ style vector library in strict ANSI C (C89)
cvector_d.c
Go to the documentation of this file.
1 #include "cvector_d.h"
2 
3 
4 
5 #if defined(CVEC_MALLOC) && defined(CVEC_FREE) && defined(CVEC_REALLOC)
6 /* ok */
7 #elif !defined(CVEC_MALLOC) && !defined(CVEC_FREE) && !defined(CVEC_REALLOC)
8 /* ok */
9 #else
10 #error "Must define all or none of CVEC_MALLOC, CVEC_FREE, and CVEC_REALLOC."
11 #endif
12 
13 #ifndef CVEC_MALLOC
14 #define CVEC_MALLOC(sz) malloc(sz)
15 #define CVEC_REALLOC(p, sz) realloc(p, sz)
16 #define CVEC_FREE(p) free(p)
17 #endif
18 
19 #ifndef CVEC_MEMMOVE
20 #include <string.h>
21 #define CVEC_MEMMOVE(dst, src, sz) memmove(dst, src, sz)
22 #endif
23 
24 #ifndef CVEC_ASSERT
25 #include <assert.h>
26 #define CVEC_ASSERT(x) assert(x)
27 #endif
28 
30 
31 #define CVEC_D_ALLOCATOR(x) ((x+1) * 2)
32 
40 {
41  cvector_d* vec;
42 
43  if (!(vec = (cvector_d*)CVEC_MALLOC(sizeof(cvector_d)))) {
44  CVEC_ASSERT(vec != NULL);
45  return NULL;
46  }
47 
48  vec->size = size;
49  vec->capacity = (capacity > vec->size || (vec->size && capacity == vec->size)) ? capacity : vec->size + CVEC_D_START_SZ;
50 
51  if (!(vec->a = (double*)CVEC_MALLOC(vec->capacity*sizeof(double)))) {
52  CVEC_ASSERT(vec->a != NULL);
53  CVEC_FREE(vec);
54  return NULL;
55  }
56 
57  return vec;
58 }
59 
63 cvector_d* cvec_init_d_heap(double* vals, cvec_sz num)
64 {
65  cvector_d* vec;
66 
67  if (!(vec = (cvector_d*)CVEC_MALLOC(sizeof(cvector_d)))) {
68  CVEC_ASSERT(vec != NULL);
69  return NULL;
70  }
71 
72  vec->capacity = num + CVEC_D_START_SZ;
73  vec->size = num;
74  if (!(vec->a = (double*)CVEC_MALLOC(vec->capacity*sizeof(double)))) {
75  CVEC_ASSERT(vec->a != NULL);
76  CVEC_FREE(vec);
77  return NULL;
78  }
79 
80  CVEC_MEMMOVE(vec->a, vals, sizeof(double)*num);
81 
82  return vec;
83 }
84 
89 int cvec_d(cvector_d* vec, cvec_sz size, cvec_sz capacity)
90 {
91  vec->size = size;
92  vec->capacity = (capacity > vec->size || (vec->size && capacity == vec->size)) ? capacity : vec->size + CVEC_D_START_SZ;
93 
94  if (!(vec->a = (double*)CVEC_MALLOC(vec->capacity*sizeof(double)))) {
95  CVEC_ASSERT(vec->a != NULL);
96  vec->size = vec->capacity = 0;
97  return 0;
98  }
99 
100  return 1;
101 }
102 
106 int cvec_init_d(cvector_d* vec, double* vals, cvec_sz num)
107 {
108  vec->capacity = num + CVEC_D_START_SZ;
109  vec->size = num;
110  if (!(vec->a = (double*)CVEC_MALLOC(vec->capacity*sizeof(double)))) {
111  CVEC_ASSERT(vec->a != NULL);
112  vec->size = vec->capacity = 0;
113  return 0;
114  }
115 
116  CVEC_MEMMOVE(vec->a, vals, sizeof(double)*num);
117 
118  return 1;
119 }
120 
131 int cvec_copyc_d(void* dest, void* src)
132 {
133  cvector_d* vec1 = (cvector_d*)dest;
134  cvector_d* vec2 = (cvector_d*)src;
135 
136  vec1->a = NULL;
137  vec1->size = 0;
138  vec1->capacity = 0;
139 
140  return cvec_copy_d(vec1, vec2);
141 }
142 
153 {
154  double* tmp = NULL;
155  if (!(tmp = (double*)CVEC_REALLOC(dest->a, src->capacity*sizeof(double)))) {
156  CVEC_ASSERT(tmp != NULL);
157  return 0;
158  }
159  dest->a = tmp;
160 
161  CVEC_MEMMOVE(dest->a, src->a, src->size*sizeof(double));
162  dest->size = src->size;
163  dest->capacity = src->capacity;
164  return 1;
165 }
166 
167 
171 int cvec_push_d(cvector_d* vec, double a)
172 {
173  double* tmp;
174  cvec_sz tmp_sz;
175  if (vec->capacity == vec->size) {
176  tmp_sz = CVEC_D_ALLOCATOR(vec->capacity);
177  if (!(tmp = (double*)CVEC_REALLOC(vec->a, sizeof(double)*tmp_sz))) {
178  CVEC_ASSERT(tmp != NULL);
179  return 0;
180  }
181  vec->a = tmp;
182  vec->capacity = tmp_sz;
183  }
184  vec->a[vec->size++] = a;
185  return 1;
186 }
187 
189 double cvec_pop_d(cvector_d* vec)
190 {
191  return vec->a[--vec->size];
192 }
193 
195 double* cvec_back_d(cvector_d* vec)
196 {
197  return &vec->a[vec->size-1];
198 }
199 
203 {
204  double* tmp;
205  cvec_sz tmp_sz;
206  if (vec->capacity < vec->size + num) {
207  tmp_sz = vec->capacity + num + CVEC_D_START_SZ;
208  if (!(tmp = (double*)CVEC_REALLOC(vec->a, sizeof(double)*tmp_sz))) {
209  CVEC_ASSERT(tmp != NULL);
210  return 0;
211  }
212  vec->a = tmp;
213  vec->capacity = tmp_sz;
214  }
215 
216  vec->size += num;
217  return 1;
218 }
219 
224 int cvec_insert_d(cvector_d* vec, cvec_sz i, double a)
225 {
226  double* tmp;
227  cvec_sz tmp_sz;
228  if (vec->capacity == vec->size) {
229  tmp_sz = CVEC_D_ALLOCATOR(vec->capacity);
230  if (!(tmp = (double*)CVEC_REALLOC(vec->a, sizeof(double)*tmp_sz))) {
231  CVEC_ASSERT(tmp != NULL);
232  return 0;
233  }
234  vec->a = tmp;
235  vec->capacity = tmp_sz;
236  }
237 
238  CVEC_MEMMOVE(&vec->a[i+1], &vec->a[i], (vec->size-i)*sizeof(double));
239  vec->a[i] = a;
240  vec->size++;
241  return 1;
242 }
243 
250 int cvec_insert_array_d(cvector_d* vec, cvec_sz i, double* a, cvec_sz num)
251 {
252  double* tmp;
253  cvec_sz tmp_sz;
254  if (vec->capacity < vec->size + num) {
255  tmp_sz = vec->capacity + num + CVEC_D_START_SZ;
256  if (!(tmp = (double*)CVEC_REALLOC(vec->a, sizeof(double)*tmp_sz))) {
257  CVEC_ASSERT(tmp != NULL);
258  return 0;
259  }
260  vec->a = tmp;
261  vec->capacity = tmp_sz;
262  }
263 
264  CVEC_MEMMOVE(&vec->a[i+num], &vec->a[i], (vec->size-i)*sizeof(double));
265  CVEC_MEMMOVE(&vec->a[i], a, num*sizeof(double));
266  vec->size += num;
267  return 1;
268 }
269 
271 double cvec_replace_d(cvector_d* vec, cvec_sz i, double a)
272 {
273  double tmp = vec->a[i];
274  vec->a[i] = a;
275  return tmp;
276 }
277 
283 void cvec_erase_d(cvector_d* vec, cvec_sz start, cvec_sz end)
284 {
285  cvec_sz d = end - start + 1;
286  CVEC_MEMMOVE(&vec->a[start], &vec->a[end+1], (vec->size-1-end)*sizeof(double));
287  vec->size -= d;
288 }
289 
292 {
293  double* tmp;
294  if (vec->capacity < size) {
295  if (!(tmp = (double*)CVEC_REALLOC(vec->a, sizeof(double)*(size+CVEC_D_START_SZ)))) {
296  CVEC_ASSERT(tmp != NULL);
297  return 0;
298  }
299  vec->a = tmp;
300  vec->capacity = size + CVEC_D_START_SZ;
301  }
302  return 1;
303 }
304 
310 {
311  double* tmp;
312  if (size < vec->size)
313  vec->size = size;
314 
315  if (!(tmp = (double*)CVEC_REALLOC(vec->a, sizeof(double)*size))) {
316  CVEC_ASSERT(tmp != NULL);
317  return 0;
318  }
319  vec->a = tmp;
320  vec->capacity = size;
321  return 1;
322 }
323 
325 void cvec_set_val_sz_d(cvector_d* vec, double val)
326 {
327  cvec_sz i;
328  for(i=0; i<vec->size; i++) {
329  vec->a[i] = val;
330  }
331 }
332 
334 void cvec_set_val_cap_d(cvector_d* vec, double val)
335 {
336  cvec_sz i;
337  for(i=0; i<vec->capacity; i++) {
338  vec->a[i] = val;
339  }
340 }
341 
343 void cvec_clear_d(cvector_d* vec) { vec->size = 0; }
344 
347 void cvec_free_d_heap(void* vec)
348 {
349  cvector_d* tmp = (cvector_d*)vec;
350  if (!tmp) return;
351  CVEC_FREE(tmp->a);
352  CVEC_FREE(tmp);
353 }
354 
356 void cvec_free_d(void* vec)
357 {
358  cvector_d* tmp = (cvector_d*)vec;
359  CVEC_FREE(tmp->a); tmp->size = 0;
360  tmp->capacity = 0;
361 }
CVEC_SIZE_T cvec_sz
Definition: cvector.h:88
#define CVEC_FREE(p)
Definition: cvector_d.c:16
cvector_d * cvec_d_heap(cvec_sz size, cvec_sz capacity)
Creates a new cvector_d on the heap.
Definition: cvector_d.c:39
int cvec_insert_array_d(cvector_d *vec, cvec_sz i, double *a, cvec_sz num)
Insert the first num elements of array a at index i.
Definition: cvector_d.c:250
int cvec_insert_d(cvector_d *vec, cvec_sz i, double a)
Insert a at index i (0 based).
Definition: cvector_d.c:224
void cvec_free_d(void *vec)
Frees the internal array and sets size and capacity to 0.
Definition: cvector_d.c:356
#define CVEC_REALLOC(p, sz)
Definition: cvector_d.c:15
void cvec_erase_d(cvector_d *vec, cvec_sz start, cvec_sz end)
Erases elements from start to end inclusive.
Definition: cvector_d.c:283
cvector_d * cvec_init_d_heap(double *vals, cvec_sz num)
Create (on the heap) and initialize cvector_d with num elements of vals.
Definition: cvector_d.c:63
void cvec_set_val_cap_d(cvector_d *vec, double val)
Fills entire allocated array (capacity) with val.
Definition: cvector_d.c:334
int cvec_copy_d(cvector_d *dest, cvector_d *src)
Makes dest a copy of src.
Definition: cvector_d.c:152
int cvec_reserve_d(cvector_d *vec, cvec_sz size)
Make sure capacity is at least size(parameter not member).
Definition: cvector_d.c:291
#define CVEC_MEMMOVE(dst, src, sz)
Definition: cvector_d.c:21
int cvec_extend_d(cvector_d *vec, cvec_sz num)
Increase the size of the array num items.
Definition: cvector_d.c:202
int cvec_copyc_d(void *dest, void *src)
Makes dest a copy of src.
Definition: cvector_d.c:131
#define CVEC_MALLOC(sz)
Definition: cvector_d.c:14
double cvec_replace_d(cvector_d *vec, cvec_sz i, double a)
Replace value at index i with a, return original value.
Definition: cvector_d.c:271
#define CVEC_ASSERT(x)
Definition: cvector_d.c:26
void cvec_set_val_sz_d(cvector_d *vec, double val)
Set all size elements to val.
Definition: cvector_d.c:325
int cvec_d(cvector_d *vec, cvec_sz size, cvec_sz capacity)
Same as cvec_d_heap() except the vector passed in was declared on the stack so it isn't allocated in ...
Definition: cvector_d.c:89
int cvec_set_cap_d(cvector_d *vec, cvec_sz size)
Set capacity to size.
Definition: cvector_d.c:309
#define CVEC_D_ALLOCATOR(x)
Definition: cvector_d.c:31
double * cvec_back_d(cvector_d *vec)
Return pointer to last element.
Definition: cvector_d.c:195
int cvec_push_d(cvector_d *vec, double a)
Append a to end of vector (size increased 1).
Definition: cvector_d.c:171
void cvec_clear_d(cvector_d *vec)
Sets size to 0 (does not clear contents).
Definition: cvector_d.c:343
void cvec_free_d_heap(void *vec)
Frees everything so don't use vec after calling this.
Definition: cvector_d.c:347
int cvec_init_d(cvector_d *vec, double *vals, cvec_sz num)
Same as cvec_init_d_heap() except the vector passed in was declared on the stack so it isn't allocate...
Definition: cvector_d.c:106
double cvec_pop_d(cvector_d *vec)
Remove and return the last element (size decreased 1).
Definition: cvector_d.c:189
cvec_sz CVEC_D_START_SZ
Definition: cvector_d.c:29
Data structure for double vector.
Definition: cvector.h:143
cvec_sz size
Current size (amount you use when manipulating array directly).
Definition: cvector.h:145
cvec_sz capacity
Allocated size of array; always >= size.
Definition: cvector.h:146
double * a
Array.
Definition: cvector.h:144