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