CVector  4.1.0
A C++ style vector library in strict ANSI C (C89)
cvector_short.h
Go to the documentation of this file.
1 /*
2 
3 CVector 4.1.0 MIT Licensed vector (dynamic array) library in strict C89
4 http://www.robertwinkler.com/projects/cvector.html
5 http://www.robertwinkler.com/projects/cvector/
6 
7 Besides the docs and all the Doxygen comments, see cvector_tests.c for
8 examples of how to use it or look at any of these other projects for
9 more practical examples:
10 
11 https://github.com/rswinkle/C_Interpreter
12 https://github.com/rswinkle/CPIM2
13 https://github.com/rswinkle/spelling_game
14 https://github.com/rswinkle/c_bigint
15 http://portablegl.com/
16 
17 The MIT License (MIT)
18 
19 Copyright (c) 2011-2023 Robert Winkler
20 
21 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
22 documentation files (the "Software"), to deal in the Software without restriction, including without limitation
23 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
24 to permit persons to whom the Software is furnished to do so, subject to the following conditions:
25 
26 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
27 
28 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
29 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
31 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32 IN THE SOFTWARE.
33 */
34 
35 #ifndef CVECTOR_short_H
36 #define CVECTOR_short_H
37 
38 #include <stdlib.h>
39 
40 #ifndef CVEC_SIZE_T
41 #define CVEC_SIZE_T size_t
42 #endif
43 
44 #ifndef CVEC_SZ
45 #define CVEC_SZ
47 #endif
48 
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
54 typedef struct cvector_short
55 {
56  short* a;
60 
61 
62 
63 extern cvec_sz CVEC_short_SZ;
64 
65 int cvec_short(cvector_short* vec, cvec_sz size, cvec_sz capacity);
66 int cvec_init_short(cvector_short* vec, short* vals, cvec_sz num);
67 
70 int cvec_copyc_short(void* dest, void* src);
72 
73 int cvec_push_short(cvector_short* vec, short a);
75 
77 int cvec_insert_short(cvector_short* vec, cvec_sz i, short a);
79 short cvec_replace_short(cvector_short* vec, cvec_sz i, short a);
83 void cvec_set_val_sz_short(cvector_short* vec, short val);
84 void cvec_set_val_cap_short(cvector_short* vec, short val);
85 
87 
89 void cvec_free_short_heap(void* vec);
90 void cvec_free_short(void* vec);
91 
92 #ifdef __cplusplus
93 }
94 #endif
95 
96 /* CVECTOR_short_H */
97 #endif
98 
99 
100 #ifdef CVECTOR_short_IMPLEMENTATION
101 
103 
104 #define CVEC_short_ALLOCATOR(x) ((x+1) * 2)
105 
106 #if defined(CVEC_MALLOC) && defined(CVEC_FREE) && defined(CVEC_REALLOC)
107 /* ok */
108 #elif !defined(CVEC_MALLOC) && !defined(CVEC_FREE) && !defined(CVEC_REALLOC)
109 /* ok */
110 #else
111 #error "Must define all or none of CVEC_MALLOC, CVEC_FREE, and CVEC_REALLOC."
112 #endif
113 
114 #ifndef CVEC_MALLOC
115 #define CVEC_MALLOC(sz) malloc(sz)
116 #define CVEC_REALLOC(p, sz) realloc(p, sz)
117 #define CVEC_FREE(p) free(p)
118 #endif
119 
120 #ifndef CVEC_MEMMOVE
121 #include <string.h>
122 #define CVEC_MEMMOVE(dst, src, sz) memmove(dst, src, sz)
123 #endif
124 
125 #ifndef CVEC_ASSERT
126 #include <assert.h>
127 #define CVEC_ASSERT(x) assert(x)
128 #endif
129 
131 {
132  cvector_short* vec;
133  if (!(vec = (cvector_short*)CVEC_MALLOC(sizeof(cvector_short)))) {
134  CVEC_ASSERT(vec != NULL);
135  return NULL;
136  }
137 
138  vec->size = size;
139  vec->capacity = (capacity > vec->size || (vec->size && capacity == vec->size)) ? capacity : vec->size + CVEC_short_SZ;
140 
141  if (!(vec->a = (short*)CVEC_MALLOC(vec->capacity*sizeof(short)))) {
142  CVEC_ASSERT(vec->a != NULL);
143  CVEC_FREE(vec);
144  return NULL;
145  }
146 
147  return vec;
148 }
149 
150 cvector_short* cvec_init_short_heap(short* vals, cvec_sz num)
151 {
152  cvector_short* vec;
153 
154  if (!(vec = (cvector_short*)CVEC_MALLOC(sizeof(cvector_short)))) {
155  CVEC_ASSERT(vec != NULL);
156  return NULL;
157  }
158 
159  vec->capacity = num + CVEC_short_SZ;
160  vec->size = num;
161  if (!(vec->a = (short*)CVEC_MALLOC(vec->capacity*sizeof(short)))) {
162  CVEC_ASSERT(vec->a != NULL);
163  CVEC_FREE(vec);
164  return NULL;
165  }
166 
167  CVEC_MEMMOVE(vec->a, vals, sizeof(short)*num);
168 
169  return vec;
170 }
171 
172 int cvec_short(cvector_short* vec, cvec_sz size, cvec_sz capacity)
173 {
174  vec->size = size;
175  vec->capacity = (capacity > vec->size || (vec->size && capacity == vec->size)) ? capacity : vec->size + CVEC_short_SZ;
176 
177  if (!(vec->a = (short*)CVEC_MALLOC(vec->capacity*sizeof(short)))) {
178  CVEC_ASSERT(vec->a != NULL);
179  vec->size = vec->capacity = 0;
180  return 0;
181  }
182 
183  return 1;
184 }
185 
186 int cvec_init_short(cvector_short* vec, short* vals, cvec_sz num)
187 {
188  vec->capacity = num + CVEC_short_SZ;
189  vec->size = num;
190  if (!(vec->a = (short*)CVEC_MALLOC(vec->capacity*sizeof(short)))) {
191  CVEC_ASSERT(vec->a != NULL);
192  vec->size = vec->capacity = 0;
193  return 0;
194  }
195 
196  CVEC_MEMMOVE(vec->a, vals, sizeof(short)*num);
197 
198  return 1;
199 }
200 
201 int cvec_copyc_short(void* dest, void* src)
202 {
203  cvector_short* vec1 = (cvector_short*)dest;
204  cvector_short* vec2 = (cvector_short*)src;
205 
206  vec1->a = NULL;
207  vec1->size = 0;
208  vec1->capacity = 0;
209 
210  return cvec_copy_short(vec1, vec2);
211 }
212 
214 {
215  short* tmp = NULL;
216  if (!(tmp = (short*)CVEC_REALLOC(dest->a, src->capacity*sizeof(short)))) {
217  CVEC_ASSERT(tmp != NULL);
218  return 0;
219  }
220  dest->a = tmp;
221 
222  CVEC_MEMMOVE(dest->a, src->a, src->size*sizeof(short));
223  dest->size = src->size;
224  dest->capacity = src->capacity;
225  return 1;
226 }
227 
228 
229 int cvec_push_short(cvector_short* vec, short a)
230 {
231  short* tmp;
232  cvec_sz tmp_sz;
233  if (vec->capacity > vec->size) {
234  vec->a[vec->size++] = a;
235  } else {
236  tmp_sz = CVEC_short_ALLOCATOR(vec->capacity);
237  if (!(tmp = (short*)CVEC_REALLOC(vec->a, sizeof(short)*tmp_sz))) {
238  CVEC_ASSERT(tmp != NULL);
239  return 0;
240  }
241  vec->a = tmp;
242  vec->a[vec->size++] = a;
243  vec->capacity = tmp_sz;
244  }
245  return 1;
246 }
247 
248 short cvec_pop_short(cvector_short* vec)
249 {
250  return vec->a[--vec->size];
251 }
252 
253 short* cvec_back_short(cvector_short* vec)
254 {
255  return &vec->a[vec->size-1];
256 }
257 
259 {
260  short* tmp;
261  cvec_sz tmp_sz;
262  if (vec->capacity < vec->size + num) {
263  tmp_sz = vec->capacity + num + CVEC_short_SZ;
264  if (!(tmp = (short*)CVEC_REALLOC(vec->a, sizeof(short)*tmp_sz))) {
265  CVEC_ASSERT(tmp != NULL);
266  return 0;
267  }
268  vec->a = tmp;
269  vec->capacity = tmp_sz;
270  }
271 
272  vec->size += num;
273  return 1;
274 }
275 
276 int cvec_insert_short(cvector_short* vec, cvec_sz i, short a)
277 {
278  short* tmp;
279  cvec_sz tmp_sz;
280  if (vec->capacity > vec->size) {
281  CVEC_MEMMOVE(&vec->a[i+1], &vec->a[i], (vec->size-i)*sizeof(short));
282  vec->a[i] = a;
283  } else {
284  tmp_sz = CVEC_short_ALLOCATOR(vec->capacity);
285  if (!(tmp = (short*)CVEC_REALLOC(vec->a, sizeof(short)*tmp_sz))) {
286  CVEC_ASSERT(tmp != NULL);
287  return 0;
288  }
289  vec->a = tmp;
290  CVEC_MEMMOVE(&vec->a[i+1], &vec->a[i], (vec->size-i)*sizeof(short));
291  vec->a[i] = a;
292  vec->capacity = tmp_sz;
293  }
294 
295  vec->size++;
296  return 1;
297 }
298 
299 int cvec_insert_array_short(cvector_short* vec, cvec_sz i, short* a, cvec_sz num)
300 {
301  short* tmp;
302  cvec_sz tmp_sz;
303  if (vec->capacity < vec->size + num) {
304  tmp_sz = vec->capacity + num + CVEC_short_SZ;
305  if (!(tmp = (short*)CVEC_REALLOC(vec->a, sizeof(short)*tmp_sz))) {
306  CVEC_ASSERT(tmp != NULL);
307  return 0;
308  }
309  vec->a = tmp;
310  vec->capacity = tmp_sz;
311  }
312 
313  CVEC_MEMMOVE(&vec->a[i+num], &vec->a[i], (vec->size-i)*sizeof(short));
314  CVEC_MEMMOVE(&vec->a[i], a, num*sizeof(short));
315  vec->size += num;
316  return 1;
317 }
318 
319 short cvec_replace_short(cvector_short* vec, cvec_sz i, short a)
320 {
321  short tmp = vec->a[i];
322  vec->a[i] = a;
323  return tmp;
324 }
325 
326 void cvec_erase_short(cvector_short* vec, cvec_sz start, cvec_sz end)
327 {
328  cvec_sz d = end - start + 1;
329  CVEC_MEMMOVE(&vec->a[start], &vec->a[end+1], (vec->size-1-end)*sizeof(short));
330  vec->size -= d;
331 }
332 
333 
335 {
336  short* tmp;
337  if (vec->capacity < size) {
338  if (!(tmp = (short*)CVEC_REALLOC(vec->a, sizeof(short)*(size+CVEC_short_SZ)))) {
339  CVEC_ASSERT(tmp != NULL);
340  return 0;
341  }
342  vec->a = tmp;
343  vec->capacity = size + CVEC_short_SZ;
344  }
345  return 1;
346 }
347 
349 {
350  short* tmp;
351  if (size < vec->size) {
352  vec->size = size;
353  }
354 
355  if (!(tmp = (short*)CVEC_REALLOC(vec->a, sizeof(short)*size))) {
356  CVEC_ASSERT(tmp != NULL);
357  return 0;
358  }
359  vec->a = tmp;
360  vec->capacity = size;
361  return 1;
362 }
363 
364 void cvec_set_val_sz_short(cvector_short* vec, short val)
365 {
366  cvec_sz i;
367  for (i=0; i<vec->size; i++) {
368  vec->a[i] = val;
369  }
370 }
371 
372 void cvec_set_val_cap_short(cvector_short* vec, short val)
373 {
374  cvec_sz i;
375  for (i=0; i<vec->capacity; i++) {
376  vec->a[i] = val;
377  }
378 }
379 
380 void cvec_clear_short(cvector_short* vec) { vec->size = 0; }
381 
382 void cvec_free_short_heap(void* vec)
383 {
384  cvector_short* tmp = (cvector_short*)vec;
385  if (!tmp) return;
386  CVEC_FREE(tmp->a);
387  CVEC_FREE(tmp);
388 }
389 
390 void cvec_free_short(void* vec)
391 {
392  cvector_short* tmp = (cvector_short*)vec;
393  CVEC_FREE(tmp->a);
394  tmp->size = 0;
395  tmp->capacity = 0;
396 }
397 
398 #endif
#define CVEC_FREE(p)
Definition: cvector.h:71
#define CVEC_REALLOC(p, sz)
Definition: cvector.h:70
#define CVEC_MEMMOVE(dst, src, sz)
Definition: cvector.h:76
#define CVEC_MALLOC(sz)
Definition: cvector.h:69
#define CVEC_ASSERT(x)
Definition: cvector.h:81
CVEC_SIZE_T cvec_sz
Definition: cvector.h:88
int cvec_set_cap_short(cvector_short *vec, cvec_sz size)
void cvec_set_val_cap_short(cvector_short *vec, short val)
int cvec_short(cvector_short *vec, cvec_sz size, cvec_sz capacity)
int cvec_copy_short(cvector_short *dest, cvector_short *src)
int cvec_push_short(cvector_short *vec, short a)
int cvec_init_short(cvector_short *vec, short *vals, cvec_sz num)
short * cvec_back_short(cvector_short *vec)
cvec_sz CVEC_short_SZ
int cvec_extend_short(cvector_short *vec, cvec_sz num)
void cvec_erase_short(cvector_short *vec, cvec_sz start, cvec_sz end)
cvector_short * cvec_short_heap(cvec_sz size, cvec_sz capacity)
short cvec_replace_short(cvector_short *vec, cvec_sz i, short a)
int cvec_insert_short(cvector_short *vec, cvec_sz i, short a)
int cvec_copyc_short(void *dest, void *src)
void cvec_clear_short(cvector_short *vec)
void cvec_free_short(void *vec)
CVEC_SIZE_T cvec_sz
Definition: cvector_short.h:46
short cvec_pop_short(cvector_short *vec)
int cvec_reserve_short(cvector_short *vec, cvec_sz size)
cvector_short * cvec_init_short_heap(short *vals, cvec_sz num)
#define CVEC_SIZE_T
Definition: cvector_short.h:41
int cvec_insert_array_short(cvector_short *vec, cvec_sz i, short *a, cvec_sz num)
void cvec_free_short_heap(void *vec)
void cvec_set_val_sz_short(cvector_short *vec, short val)
Data structure for short vector.
Definition: cvector_short.h:55
cvec_sz capacity
Allocated size of array; always >= size.
Definition: cvector_short.h:58
short * a
Array.
Definition: cvector_short.h:56
cvec_sz size
Current size (amount you use when manipulating array directly).
Definition: cvector_short.h:57