CVector
4.1.0
A C++ style vector library in strict ANSI C (C89)
|
#include <stdlib.h>
#include <string.h>
#include <assert.h>
Go to the source code of this file.
Data Structures | |
struct | cvector_i |
Data structure for int vector. More... | |
struct | cvector_d |
Data structure for double vector. More... | |
struct | cvector_str |
Data structure for string vector. More... | |
struct | cvector_void |
Data structure for generic type (cast to void) vectors. More... | |
Macros | |
#define | CVEC_MALLOC(sz) malloc(sz) |
#define | CVEC_REALLOC(p, sz) realloc(p, sz) |
#define | CVEC_FREE(p) free(p) |
#define | CVEC_MEMMOVE(dst, src, sz) memmove(dst, src, sz) |
#define | CVEC_ASSERT(x) assert(x) |
#define | CVEC_SIZE_T size_t |
#define | CVEC_STRDUP cvec_strdup |
#define | cvec_popm_str(vec) (vec).a[--(vec).size] |
#define | cvec_replacem_str(vec, i, s, ret) ((ret) = (vec).a[i], (vec).a[i] = (s)) |
#define | CVEC_GET_VOID(VEC, TYPE, I) ((TYPE*)&(VEC)->a[(I)*(VEC)->elem_size]) |
#define | CVEC_NEW_DECLS(TYPE) |
#define | CVEC_NEW_DEFS(TYPE, RESIZE_MACRO) |
#define | CVEC_NEW_DECLS2(TYPE) |
#define | CVEC_NEW_DEFS2(TYPE, RESIZE_MACRO) |
Typedefs | |
typedef CVEC_SIZE_T | cvec_sz |
typedef unsigned char | cvec_u8 |
Functions | |
int | cvec_i (cvector_i *vec, cvec_sz size, cvec_sz capacity) |
Same as cvec_i_heap() except the vector passed in was declared on the stack so it isn't allocated in this function. More... | |
int | cvec_init_i (cvector_i *vec, int *vals, cvec_sz num) |
Same as cvec_init_i_heap() except the vector passed in was declared on the stack so it isn't allocated in this function. More... | |
cvector_i * | cvec_i_heap (cvec_sz size, cvec_sz capacity) |
Creates a new cvector_i on the heap. More... | |
cvector_i * | cvec_init_i_heap (int *vals, cvec_sz num) |
Create (on the heap) and initialize cvector_i with num elements of vals. More... | |
int | cvec_copyc_i (void *dest, void *src) |
Makes dest a copy of src. More... | |
int | cvec_copy_i (cvector_i *dest, cvector_i *src) |
Makes dest a copy of src. More... | |
int | cvec_push_i (cvector_i *vec, int a) |
Append a to end of vector (size increased 1). More... | |
int | cvec_pop_i (cvector_i *vec) |
Remove and return the last element (size decreased 1). More... | |
int | cvec_extend_i (cvector_i *vec, cvec_sz num) |
Increase the size of the array num items. More... | |
int | cvec_insert_i (cvector_i *vec, cvec_sz i, int a) |
Insert a at index i (0 based). More... | |
int | cvec_insert_array_i (cvector_i *vec, cvec_sz i, int *a, cvec_sz num) |
Insert the first num elements of array a at index i. More... | |
int | cvec_replace_i (cvector_i *vec, cvec_sz i, int a) |
Replace value at index i with a, return original value. More... | |
void | cvec_erase_i (cvector_i *vec, cvec_sz start, cvec_sz end) |
Erases elements from start to end inclusive. More... | |
int | cvec_reserve_i (cvector_i *vec, cvec_sz size) |
Make sure capacity is at least size(parameter not member). More... | |
int | cvec_set_cap_i (cvector_i *vec, cvec_sz size) |
Set capacity to size. More... | |
void | cvec_set_val_sz_i (cvector_i *vec, int val) |
Set all size elements to val. More... | |
void | cvec_set_val_cap_i (cvector_i *vec, int val) |
Fills entire allocated array (capacity) with val. More... | |
int * | cvec_back_i (cvector_i *vec) |
Return pointer to last element. More... | |
void | cvec_clear_i (cvector_i *vec) |
Sets size to 0 (does not clear contents). More... | |
void | cvec_free_i_heap (void *vec) |
Frees everything so don't use vec after calling this. More... | |
void | cvec_free_i (void *vec) |
Frees the internal array and sets size and capacity to 0. More... | |
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 this function. More... | |
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 allocated in this function. More... | |
cvector_d * | cvec_d_heap (cvec_sz size, cvec_sz capacity) |
Creates a new cvector_d on the heap. More... | |
cvector_d * | cvec_init_d_heap (double *vals, cvec_sz num) |
Create (on the heap) and initialize cvector_d with num elements of vals. More... | |
int | cvec_copyc_d (void *dest, void *src) |
Makes dest a copy of src. More... | |
int | cvec_copy_d (cvector_d *dest, cvector_d *src) |
Makes dest a copy of src. More... | |
int | cvec_push_d (cvector_d *vec, double a) |
Append a to end of vector (size increased 1). More... | |
double | cvec_pop_d (cvector_d *vec) |
Remove and return the last element (size decreased 1). More... | |
int | cvec_extend_d (cvector_d *vec, cvec_sz num) |
Increase the size of the array num items. More... | |
int | cvec_insert_d (cvector_d *vec, cvec_sz i, double a) |
Insert a at index i (0 based). More... | |
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. More... | |
double | cvec_replace_d (cvector_d *vec, cvec_sz i, double a) |
Replace value at index i with a, return original value. More... | |
void | cvec_erase_d (cvector_d *vec, cvec_sz start, cvec_sz end) |
Erases elements from start to end inclusive. More... | |
int | cvec_reserve_d (cvector_d *vec, cvec_sz size) |
Make sure capacity is at least size(parameter not member). More... | |
int | cvec_set_cap_d (cvector_d *vec, cvec_sz size) |
Set capacity to size. More... | |
void | cvec_set_val_sz_d (cvector_d *vec, double val) |
Set all size elements to val. More... | |
void | cvec_set_val_cap_d (cvector_d *vec, double val) |
Fills entire allocated array (capacity) with val. More... | |
double * | cvec_back_d (cvector_d *vec) |
Return pointer to last element. More... | |
void | cvec_clear_d (cvector_d *vec) |
Sets size to 0 (does not clear contents). More... | |
void | cvec_free_d_heap (void *vec) |
Frees everything so don't use vec after calling this. More... | |
void | cvec_free_d (void *vec) |
Frees the internal array and sets size and capacity to 0. More... | |
char * | cvec_strdup (const char *str) |
Useful utility function since strdup isn't in standard C. More... | |
int | cvec_str (cvector_str *vec, cvec_sz size, cvec_sz capacity) |
Same as cvec_str_heap() except the vector passed in was declared on the stack so it isn't allocated in this function. More... | |
int | cvec_init_str (cvector_str *vec, char **vals, cvec_sz num) |
Same as cvec_init_str_heap() except the vector passed in was declared on the stack so it isn't allocated in this function. More... | |
cvector_str * | cvec_str_heap (cvec_sz size, cvec_sz capacity) |
Create a new cvector_str on the heap. More... | |
cvector_str * | cvec_init_str_heap (char **vals, cvec_sz num) |
Create (on the heap) and initialize cvector_str with num elements of vals. More... | |
int | cvec_copyc_str (void *dest, void *src) |
Makes dest a copy of src. More... | |
int | cvec_copy_str (cvector_str *dest, cvector_str *src) |
Makes dest a copy of src. More... | |
int | cvec_push_str (cvector_str *vec, char *a) |
Append a to end of vector (size increased 1). More... | |
void | cvec_pop_str (cvector_str *vec, char *ret) |
Remove the last element (size decreased 1). More... | |
int | cvec_pushm_str (cvector_str *vec, char *a) |
same as push but without calling CVEC_STRDUP(a), m suffix is for "move" More... | |
int | cvec_insertm_str (cvector_str *vec, cvec_sz i, char *a) |
Same as insert except no CVEC_STRDUP. More... | |
int | cvec_insert_arraym_str (cvector_str *vec, cvec_sz i, char **a, cvec_sz num) |
Same as insert_array except no CVEC_STRDUP. More... | |
int | cvec_extend_str (cvector_str *vec, cvec_sz num) |
Increase the size of the array num items. More... | |
int | cvec_insert_str (cvector_str *vec, cvec_sz i, char *a) |
Insert a at index i (0 based). More... | |
int | cvec_insert_array_str (cvector_str *vec, cvec_sz i, char **a, cvec_sz num) |
Insert the first num elements of array a at index i. More... | |
void | cvec_replace_str (cvector_str *vec, cvec_sz i, char *a, char *ret) |
Replace string at i with a. More... | |
void | cvec_erase_str (cvector_str *vec, cvec_sz start, cvec_sz end) |
Erases strings from start to end inclusive. More... | |
void | cvec_remove_str (cvector_str *vec, cvec_sz start, cvec_sz end) |
Same as erase except it does not call CVEC_FREE. More... | |
int | cvec_reserve_str (cvector_str *vec, cvec_sz size) |
Makes sure the vector capacity is >= size (parameter not member). More... | |
int | cvec_set_cap_str (cvector_str *vec, cvec_sz size) |
Set capacity to size. More... | |
void | cvec_set_val_sz_str (cvector_str *vec, char *val) |
Sets all size elements to val. More... | |
void | cvec_set_val_cap_str (cvector_str *vec, char *val) |
Fills entire allocated array (capacity) with val. More... | |
char ** | cvec_back_str (cvector_str *vec) |
Return pointer to last element. More... | |
void | cvec_clear_str (cvector_str *vec) |
Clears the contents of vector (frees all strings) and sets size to 0. More... | |
void | cvec_free_str_heap (void *vec) |
Frees contents (individual strings and array) and frees vector so don't use after calling this. More... | |
void | cvec_free_str (void *vec) |
Frees the internal array and sets size and capacity to 0. More... | |
int | cvec_void (cvector_void *vec, cvec_sz size, cvec_sz capacity, cvec_sz elem_sz, void(*elem_free)(void *), int(*elem_init)(void *, void *)) |
Same as cvec_void_heap() except the vector passed in was declared on the stack so it isn't allocated in this function. More... | |
int | cvec_init_void (cvector_void *vec, void *vals, cvec_sz num, cvec_sz elem_sz, void(*elem_free)(void *), int(*elem_init)(void *, void *)) |
Same as init_vec_heap() except the vector passed in was declared on the stack so it isn't allocated in this function. More... | |
cvector_void * | cvec_void_heap (cvec_sz size, cvec_sz capacity, cvec_sz elem_sz, void(*elem_free)(void *), int(*elem_init)(void *, void *)) |
Creates a new vector on the heap. More... | |
cvector_void * | cvec_init_void_heap (void *vals, cvec_sz num, cvec_sz elem_sz, void(*elem_free)(void *), int(*elem_init)(void *, void *)) |
Create (on the heap) and initialize vector with num elements of vals. More... | |
int | cvec_copyc_void (void *dest, void *src) |
Makes dest a copy of src. More... | |
int | cvec_copy_void (cvector_void *dest, cvector_void *src) |
Makes dest a copy of src. More... | |
int | cvec_push_void (cvector_void *vec, void *a) |
Append a to end of vector (size increased 1). More... | |
void | cvec_pop_void (cvector_void *vec, void *ret) |
Remove the last element (size decreased 1). More... | |
void * | cvec_get_void (cvector_void *vec, cvec_sz i) |
Return a void pointer to the ith element. More... | |
int | cvec_pushm_void (cvector_void *vec, void *a) |
Same as push except no elem_init even if it's set. More... | |
void | cvec_popm_void (cvector_void *vec, void *ret) |
Same as pop except no elem_free even if it's set. More... | |
int | cvec_insertm_void (cvector_void *vec, cvec_sz i, void *a) |
Same as insert but no elem_init even if defined. More... | |
int | cvec_insert_arraym_void (cvector_void *vec, cvec_sz i, void *a, cvec_sz num) |
Same as insert_array but no elem_init even if defined. More... | |
void | cvec_replacem_void (cvector_void *vec, cvec_sz i, void *a, void *ret) |
Same as replace but no elem_free or elem_init even if they're defined. More... | |
int | cvec_extend_void (cvector_void *vec, cvec_sz num) |
Increase the size of the array num items. More... | |
int | cvec_insert_void (cvector_void *vec, cvec_sz i, void *a) |
Insert a at index i (0 based). More... | |
int | cvec_insert_array_void (cvector_void *vec, cvec_sz i, void *a, cvec_sz num) |
Insert the first num elements of array a at index i. More... | |
int | cvec_replace_void (cvector_void *vec, cvec_sz i, void *a, void *ret) |
Replace value at i with a, return old value in ret if non-NULL. More... | |
void | cvec_erase_void (cvector_void *vec, cvec_sz start, cvec_sz end) |
Erases elements from start to end inclusive. More... | |
void | cvec_remove_void (cvector_void *vec, cvec_sz start, cvec_sz end) |
Same as erase except it does not call elem_free. More... | |
int | cvec_reserve_void (cvector_void *vec, cvec_sz size) |
Makes sure capacity >= size (the parameter not the member). More... | |
int | cvec_set_cap_void (cvector_void *vec, cvec_sz size) |
Set capacity to size. More... | |
int | cvec_set_val_sz_void (cvector_void *vec, void *val) |
Set all size elements to val. More... | |
int | cvec_set_val_cap_void (cvector_void *vec, void *val) |
Fills entire allocated array (capacity) with val. More... | |
void * | cvec_back_void (cvector_void *vec) |
Return pointer to last element. More... | |
void | cvec_clear_void (cvector_void *vec) |
Sets size to 0 (does not change contents unless elem_free is set then it will elem_free all size elements as in cvector_str). More... | |
void | cvec_free_void_heap (void *vec) |
Frees everything so don't use vec after calling this. More... | |
void | cvec_free_void (void *vec) |
Frees the internal array and sets size and capacity to 0. More... | |
Variables | |
cvec_sz | CVEC_I_START_SZ |
cvec_sz | CVEC_D_START_SZ |
cvec_sz | CVEC_STR_START_SZ |
cvec_sz | CVEC_VOID_START_SZ |
#define CVEC_GET_VOID | ( | VEC, | |
TYPE, | |||
I | |||
) | ((TYPE*)&(VEC)->a[(I)*(VEC)->elem_size]) |
#define CVEC_MEMMOVE | ( | dst, | |
src, | |||
sz | |||
) | memmove(dst, src, sz) |
#define cvec_replacem_str | ( | vec, | |
i, | |||
s, | |||
ret | |||
) | ((ret) = (vec).a[i], (vec).a[i] = (s)) |
#define CVEC_STRDUP cvec_strdup |
typedef CVEC_SIZE_T cvec_sz |
double* cvec_back_d | ( | cvector_d * | vec | ) |
Return pointer to last element.
Definition at line 195 of file cvector_d.c.
int* cvec_back_i | ( | cvector_i * | vec | ) |
Return pointer to last element.
Definition at line 195 of file cvector_i.c.
char** cvec_back_str | ( | cvector_str * | vec | ) |
Return pointer to last element.
Definition at line 262 of file cvector_str.c.
void* cvec_back_void | ( | cvector_void * | vec | ) |
Return pointer to last element.
Definition at line 327 of file cvector_void.c.
void cvec_clear_d | ( | cvector_d * | vec | ) |
Sets size to 0 (does not clear contents).
Definition at line 343 of file cvector_d.c.
void cvec_clear_i | ( | cvector_i * | vec | ) |
Sets size to 0 (does not clear contents).
Definition at line 344 of file cvector_i.c.
void cvec_clear_str | ( | cvector_str * | vec | ) |
Clears the contents of vector (frees all strings) and sets size to 0.
Definition at line 495 of file cvector_str.c.
void cvec_clear_void | ( | cvector_void * | vec | ) |
Sets size to 0 (does not change contents unless elem_free is set then it will elem_free all size elements as in cvector_str).
Definition at line 638 of file cvector_void.c.
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
Definition at line 152 of file cvector_d.c.
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_I_START_SZ
Definition at line 151 of file cvector_i.c.
int cvec_copy_str | ( | cvector_str * | dest, |
cvector_str * | src | ||
) |
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_STR_START_SZ
Definition at line 190 of file cvector_str.c.
int cvec_copy_void | ( | cvector_void * | dest, |
cvector_void * | src | ||
) |
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_VOID_START_SZ
Definition at line 218 of file cvector_void.c.
int cvec_copyc_d | ( | void * | dest, |
void * | src | ||
) |
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.
Definition at line 131 of file cvector_d.c.
int cvec_copyc_i | ( | void * | dest, |
void * | src | ||
) |
Makes dest a copy of src.
The parameters are void so it can be used as the constructor when making a vector of cvector_i'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.
Definition at line 130 of file cvector_i.c.
int cvec_copyc_str | ( | void * | dest, |
void * | src | ||
) |
Makes dest a copy of src.
The parameters are void so it can be used as the constructor when making a vector of cvector_str'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.
Definition at line 169 of file cvector_str.c.
int cvec_copyc_void | ( | void * | dest, |
void * | src | ||
) |
Makes dest a copy of src.
The parameters are void so it can be used as the constructor when making a vector of cvector_void'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.
Definition at line 197 of file cvector_void.c.
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.
Definition at line 89 of file cvector_d.c.
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.
Definition at line 39 of file cvector_d.c.
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.
Definition at line 283 of file cvector_d.c.
Erases elements from start to end inclusive.
Example cvec_erase_i(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.
Definition at line 283 of file cvector_i.c.
void cvec_erase_str | ( | cvector_str * | vec, |
cvec_sz | start, | ||
cvec_sz | end | ||
) |
Erases strings from start to end inclusive.
Example erases(myvec, 1, 3) would CVEC_FREE and remove strings at 1, 2, and 3 and the string that was at index 4 would now be at 1 etc.
Definition at line 405 of file cvector_str.c.
void cvec_erase_void | ( | cvector_void * | vec, |
cvec_sz | start, | ||
cvec_sz | end | ||
) |
Erases elements from start to end inclusive.
Example cvec_erase_void(myvec, 1, 3) would call elem_free (if an elem_free function was provided) and remove elements at 1, 2, and 3 and the element that was at index 4 would now be at 1 etc.
Definition at line 517 of file cvector_void.c.
Increase the size of the array num items.
Items are not initialized to anything
Definition at line 202 of file cvector_d.c.
Increase the size of the array num items.
Items are not initialized to anything
Definition at line 202 of file cvector_i.c.
int cvec_extend_str | ( | cvector_str * | vec, |
cvec_sz | num | ||
) |
Increase the size of the array num items.
Items are memset to NULL since they will be freed when popped or the vector is freed.
Definition at line 270 of file cvector_str.c.
int cvec_extend_void | ( | cvector_void * | vec, |
cvec_sz | num | ||
) |
Increase the size of the array num items.
Items are not initialized to anything!
Definition at line 334 of file cvector_void.c.
void cvec_free_d | ( | void * | vec | ) |
Frees the internal array and sets size and capacity to 0.
Definition at line 356 of file cvector_d.c.
void cvec_free_d_heap | ( | void * | vec | ) |
Frees everything so don't use vec after calling this.
Passing NULL is a NO-OP, matching the behavior of free().
Definition at line 347 of file cvector_d.c.
void cvec_free_i | ( | void * | vec | ) |
Frees the internal array and sets size and capacity to 0.
Definition at line 357 of file cvector_i.c.
void cvec_free_i_heap | ( | void * | vec | ) |
Frees everything so don't use vec after calling this.
Passing NULL is a NO-OP, matching the behavior of free().
Definition at line 348 of file cvector_i.c.
void cvec_free_str | ( | void * | vec | ) |
Frees the internal array and sets size and capacity to 0.
Definition at line 521 of file cvector_str.c.
void cvec_free_str_heap | ( | void * | vec | ) |
Frees contents (individual strings and array) and frees vector so don't use after calling this.
Passing NULL is a NO-OP, matching the behavior of free().
Definition at line 507 of file cvector_str.c.
void cvec_free_void | ( | void * | vec | ) |
Frees the internal array and sets size and capacity to 0.
Definition at line 667 of file cvector_void.c.
void cvec_free_void_heap | ( | void * | vec | ) |
Frees everything so don't use vec after calling this.
If you set an elem_free function it will be called on all size elements of course. Passing NULL is a NO-OP, matching the behavior of free().
Definition at line 652 of file cvector_void.c.
void* cvec_get_void | ( | cvector_void * | vec, |
cvec_sz | i | ||
) |
Return a void pointer to the ith element.
Another way to get elements from vector that is used in vector_tests.c is a macro like this one #define GET_ELEMENT(VEC,I,TYPE) ((TYPE*)&VEC.a[(I)*VEC.elem_size])
Definition at line 357 of file cvector_void.c.
Same as cvec_i_heap() except the vector passed in was declared on the stack so it isn't allocated in this function.
Use the cvec_free_i in this case. This and cvec_init_i should be preferred over the heap versions.
Definition at line 88 of file cvector_i.c.
Creates a new cvector_i 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_I_START_SZ in other words capacity has to be at least 1 and >= to vec->size of course.
Definition at line 39 of file cvector_i.c.
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.
Definition at line 106 of file cvector_d.c.
Create (on the heap) and initialize cvector_d with num elements of vals.
Capacity is set to num + CVEC_D_START_SZ.
Definition at line 63 of file cvector_d.c.
Same as cvec_init_i_heap() except the vector passed in was declared on the stack so it isn't allocated in this function.
Definition at line 105 of file cvector_i.c.
Create (on the heap) and initialize cvector_i with num elements of vals.
Capacity is set to num + CVEC_I_START_SZ.
Definition at line 62 of file cvector_i.c.
int cvec_init_str | ( | cvector_str * | vec, |
char ** | vals, | ||
cvec_sz | num | ||
) |
Same as cvec_init_str_heap() except the vector passed in was declared on the stack so it isn't allocated in this function.
Use the cvec_free_str in this case
Definition at line 140 of file cvector_str.c.
cvector_str* cvec_init_str_heap | ( | char ** | vals, |
cvec_sz | num | ||
) |
Create (on the heap) and initialize cvector_str with num elements of vals.
Definition at line 91 of file cvector_str.c.
int cvec_init_void | ( | cvector_void * | vec, |
void * | vals, | ||
cvec_sz | num, | ||
cvec_sz | elem_sz, | ||
void(*)(void *) | elem_free, | ||
int(*)(void *, void *) | elem_init | ||
) |
Same as init_vec_heap() except the vector passed in was declared on the stack so it isn't allocated in this function.
Use the cvec_free_void in this case
Definition at line 156 of file cvector_void.c.
cvector_void* cvec_init_void_heap | ( | void * | vals, |
cvec_sz | num, | ||
cvec_sz | elem_sz, | ||
void(*)(void *) | elem_free, | ||
int(*)(void *, void *) | elem_init | ||
) |
Create (on the heap) and initialize vector with num elements of vals.
elem_sz is the size of the type you want to store ( ie sizeof(T) where T is your type ). See cvec_void_heap() for more information about the elem_free and elem_init parameters.
Definition at line 92 of file cvector_void.c.
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)
Definition at line 250 of file cvector_d.c.
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)
Definition at line 250 of file cvector_i.c.
int cvec_insert_array_str | ( | cvector_str * | vec, |
cvec_sz | i, | ||
char ** | a, | ||
cvec_sz | num | ||
) |
Insert the first num elements of array a at index i.
Note that it is the user's responsibility to pass in valid arguments.
Definition at line 341 of file cvector_str.c.
int cvec_insert_array_void | ( | cvector_void * | vec, |
cvec_sz | i, | ||
void * | a, | ||
cvec_sz | num | ||
) |
Insert the first num elements of array a at index i.
Note that it is the user's responsibility to pass in val_id arguments. Also CVEC_MEMMOVE is used (when there is no elem_init function) so don't try to insert part of the vector array into itself (that would require CVEC_MEMMOVE)
Definition at line 425 of file cvector_void.c.
int cvec_insert_arraym_str | ( | cvector_str * | vec, |
cvec_sz | i, | ||
char ** | a, | ||
cvec_sz | num | ||
) |
Same as insert_array except no CVEC_STRDUP.
Definition at line 367 of file cvector_str.c.
int cvec_insert_arraym_void | ( | cvector_void * | vec, |
cvec_sz | i, | ||
void * | a, | ||
cvec_sz | num | ||
) |
Same as insert_array but no elem_init even if defined.
Definition at line 455 of file cvector_void.c.
Insert a at index i (0 based).
Everything from that index and right is shifted one to the right.
Definition at line 224 of file cvector_d.c.
Insert a at index i (0 based).
Everything from that index and right is shifted one to the right.
Definition at line 224 of file cvector_i.c.
int cvec_insert_str | ( | cvector_str * | vec, |
cvec_sz | i, | ||
char * | a | ||
) |
Insert a at index i (0 based).
Everything from that index and right is shifted one to the right.
Definition at line 293 of file cvector_str.c.
int cvec_insert_void | ( | cvector_void * | vec, |
cvec_sz | i, | ||
void * | a | ||
) |
Insert a at index i (0 based).
Everything from that index and right is shifted one to the right.
Definition at line 366 of file cvector_void.c.
int cvec_insertm_str | ( | cvector_str * | vec, |
cvec_sz | i, | ||
char * | a | ||
) |
Same as insert except no CVEC_STRDUP.
Definition at line 316 of file cvector_str.c.
int cvec_insertm_void | ( | cvector_void * | vec, |
cvec_sz | i, | ||
void * | a | ||
) |
Same as insert but no elem_init even if defined.
Definition at line 396 of file cvector_void.c.
double cvec_pop_d | ( | cvector_d * | vec | ) |
Remove and return the last element (size decreased 1).
Definition at line 189 of file cvector_d.c.
int cvec_pop_i | ( | cvector_i * | vec | ) |
Remove and return the last element (size decreased 1).
Definition at line 189 of file cvector_i.c.
void cvec_pop_str | ( | cvector_str * | vec, |
char * | ret | ||
) |
Remove the last element (size decreased 1).
String is freed. If ret != NULL strcpy the last element into ret. It is the user's responsibility to make sure ret can receive it without error (ie ret has adequate space.)
Definition at line 253 of file cvector_str.c.
void cvec_pop_void | ( | cvector_void * | vec, |
void * | ret | ||
) |
Remove the last element (size decreased 1).
Copy the element into ret if ret is not NULL. This function assumes that ret is large accept the element and just CVEC_MEMMOVE's it in. Similar to pop_backs it is users responsibility.
Definition at line 306 of file cvector_void.c.
void cvec_popm_void | ( | cvector_void * | vec, |
void * | ret | ||
) |
Same as pop except no elem_free even if it's set.
Definition at line 318 of file cvector_void.c.
int cvec_push_d | ( | cvector_d * | vec, |
double | a | ||
) |
Append a to end of vector (size increased 1).
Capacity is increased by doubling when necessary.
Definition at line 171 of file cvector_d.c.
int cvec_push_i | ( | cvector_i * | vec, |
int | a | ||
) |
Append a to end of vector (size increased 1).
Capacity is increased by doubling when necessary.
Definition at line 170 of file cvector_i.c.
int cvec_push_str | ( | cvector_str * | vec, |
char * | a | ||
) |
Append a to end of vector (size increased 1).
Capacity is increased by doubling when necessary.
Definition at line 212 of file cvector_str.c.
int cvec_push_void | ( | cvector_void * | vec, |
void * | a | ||
) |
Append a to end of vector (size increased 1).
Capacity is increased by doubling when necessary.
TODO For all of cvector_void, now that elem_init returns int, is it worth the extra code and overhead of checking it and asserting/returning 0?
Definition at line 254 of file cvector_void.c.
int cvec_pushm_str | ( | cvector_str * | vec, |
char * | a | ||
) |
same as push but without calling CVEC_STRDUP(a), m suffix is for "move"
Definition at line 231 of file cvector_str.c.
int cvec_pushm_void | ( | cvector_void * | vec, |
void * | a | ||
) |
Same as push except no elem_init even if it's set.
Definition at line 281 of file cvector_void.c.
void cvec_remove_str | ( | cvector_str * | vec, |
cvec_sz | start, | ||
cvec_sz | end | ||
) |
Same as erase except it does not call CVEC_FREE.
Definition at line 418 of file cvector_str.c.
void cvec_remove_void | ( | cvector_void * | vec, |
cvec_sz | start, | ||
cvec_sz | end | ||
) |
Same as erase except it does not call elem_free.
Definition at line 531 of file cvector_void.c.
Replace value at index i with a, return original value.
Definition at line 271 of file cvector_d.c.
Replace value at index i with a, return original value.
Definition at line 271 of file cvector_i.c.
void cvec_replace_str | ( | cvector_str * | vec, |
cvec_sz | i, | ||
char * | a, | ||
char * | ret | ||
) |
Replace string at i with a.
If ret != NULL, strcpy the old str to it. See cvec_pop_str warning
Definition at line 392 of file cvector_str.c.
int cvec_replace_void | ( | cvector_void * | vec, |
cvec_sz | i, | ||
void * | a, | ||
void * | ret | ||
) |
Replace value at i with a, return old value in ret if non-NULL.
Definition at line 479 of file cvector_void.c.
void cvec_replacem_void | ( | cvector_void * | vec, |
cvec_sz | i, | ||
void * | a, | ||
void * | ret | ||
) |
Same as replace but no elem_free or elem_init even if they're defined.
Because it doesn't call elem_init, there's no chance of failure so there's no return value.
Definition at line 503 of file cvector_void.c.
Make sure capacity is at least size(parameter not member).
Definition at line 291 of file cvector_d.c.
Make sure capacity is at least size(parameter not member).
Definition at line 291 of file cvector_i.c.
int cvec_reserve_str | ( | cvector_str * | vec, |
cvec_sz | size | ||
) |
Makes sure the vector capacity is >= size (parameter not member).
Definition at line 426 of file cvector_str.c.
int cvec_reserve_void | ( | cvector_void * | vec, |
cvec_sz | size | ||
) |
Makes sure capacity >= size (the parameter not the member).
Definition at line 539 of file cvector_void.c.
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.
Definition at line 309 of file cvector_d.c.
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.
Definition at line 309 of file cvector_i.c.
int cvec_set_cap_str | ( | cvector_str * | vec, |
cvec_sz | size | ||
) |
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.
Definition at line 444 of file cvector_str.c.
int cvec_set_cap_void | ( | cvector_void * | vec, |
cvec_sz | size | ||
) |
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.
Definition at line 557 of file cvector_void.c.
void cvec_set_val_cap_d | ( | cvector_d * | vec, |
double | val | ||
) |
Fills entire allocated array (capacity) with val.
Definition at line 334 of file cvector_d.c.
void cvec_set_val_cap_i | ( | cvector_i * | vec, |
int | val | ||
) |
Fills entire allocated array (capacity) with val.
Definition at line 335 of file cvector_i.c.
void cvec_set_val_cap_str | ( | cvector_str * | vec, |
char * | val | ||
) |
Fills entire allocated array (capacity) with val.
Size is set to capacity in this case because strings are individually dynamically allocated. This is different from cvector_i, cvector_d and cvector_void (without a CVEC_FREE function) where the size stays the same. TODO Remove this function? even more unnecessary than for cvector_i and cvector_d and different behavior
Definition at line 481 of file cvector_str.c.
int cvec_set_val_cap_void | ( | cvector_void * | vec, |
void * | val | ||
) |
Fills entire allocated array (capacity) with val.
If you set an elem_free function then size is set to capacity like cvector_str for the same reason, ie I need to know that the elem_free function needs to be called on those elements. TODO Remove this function? Same reason as set_val_cap_str.
Definition at line 611 of file cvector_void.c.
void cvec_set_val_sz_d | ( | cvector_d * | vec, |
double | val | ||
) |
Set all size elements to val.
Definition at line 325 of file cvector_d.c.
void cvec_set_val_sz_i | ( | cvector_i * | vec, |
int | val | ||
) |
Set all size elements to val.
Definition at line 326 of file cvector_i.c.
void cvec_set_val_sz_str | ( | cvector_str * | vec, |
char * | val | ||
) |
Sets all size elements to val.
Definition at line 466 of file cvector_str.c.
int cvec_set_val_sz_void | ( | cvector_void * | vec, |
void * | val | ||
) |
Set all size elements to val.
Definition at line 581 of file cvector_void.c.
int cvec_str | ( | cvector_str * | vec, |
cvec_sz | size, | ||
cvec_sz | capacity | ||
) |
Same as cvec_str_heap() except the vector passed in was declared on the stack so it isn't allocated in this function.
Use the cvec_free_str in this case This and cvec_init_str should be preferred over the heap versions.
Definition at line 120 of file cvector_str.c.
cvector_str* cvec_str_heap | ( | cvec_sz | size, |
cvec_sz | capacity | ||
) |
Create a new cvector_str 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_STR_START_SZ in other words capacity has to be at least 1 and >= to vec->size of course. Note: cvector_str does not copy pointers passed in but duplicates the strings they point to (using CVEC_STRDUP()) so you don't have to worry about freeing or changing the contents of variables that you've pushed or inserted; it won't affect the values in the vector.
Definition at line 64 of file cvector_str.c.
char* cvec_strdup | ( | const char * | str | ) |
Useful utility function since strdup isn't in standard C.
Definition at line 35 of file cvector_str.c.
int cvec_void | ( | cvector_void * | vec, |
cvec_sz | size, | ||
cvec_sz | capacity, | ||
cvec_sz | elem_sz, | ||
void(*)(void *) | elem_free, | ||
int(*)(void *, void *) | elem_init | ||
) |
Same as cvec_void_heap() except the vector passed in was declared on the stack so it isn't allocated in this function.
Use the cvec_free_void in that case
Definition at line 134 of file cvector_void.c.
cvector_void* cvec_void_heap | ( | cvec_sz | size, |
cvec_sz | capacity, | ||
cvec_sz | elem_sz, | ||
void(*)(void *) | elem_free, | ||
int(*)(void *, void *) | elem_init | ||
) |
Creates a new vector 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_VOID_START_SZ in other words capacity has to be at least 1 and >= to vec->size of course. elem_sz is the size of the type you want to store ( ie sizeof(T) where T is your type ). You can pass in an optional function, elem_free, to be called on every element before it is erased from the vector to free any dynamically allocated memory. Likewise you can pass in elem_init to be as a sort of copy constructor for any insertions if you needed some kind of deep copy/allocations.
For example if you passed in sizeof(char*) for elem_sz, and wrappers around the standard free(void*) function for elem_free and CVEC_STRDUP for elem_init you could make vector work almost exactly like cvector_str. The main difference is cvector_str does not check for failure of CVEC_STRDUP while cvector_void does check for failure of elem_init. The other minor differences are popm and replacem are macros in cvector_str (and the latter returns the result rather than using a double pointer return parameter) and depending on how you defined elem_init and whether you're using the 'move' functions, you have to pass in char**'s instead of char*'s because cvector_void has to use memmove rather than straight assignment.
Pass in NULL, to not use the function parameters.
The function remove and the 'move' functions (with the m suffix) do not call elem_init or elem_free even if they are set. This gives you some flexibility and performance when you already have things allocated or want to keep things after removing them from the vector but only some of the time (otherwise you wouldn't have defined elem_free/elem_init in the first place).
See the other functions and the tests for more behavioral/usage details.
Definition at line 61 of file cvector_void.c.
|
extern |
Definition at line 29 of file cvector_d.c.
|
extern |
Definition at line 29 of file cvector_i.c.
|
extern |
Definition at line 29 of file cvector_str.c.
|
extern |
Definition at line 29 of file cvector_void.c.