CVector
4.1.0
A C++ style vector library in strict ANSI C (C89)
|
Go to the source code of this file.
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_STR_ALLOCATOR(x) ((x+1) * 2) |
Functions | |
char * | cvec_strdup (const char *str) |
Useful utility function since strdup isn't in standard C. 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_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... | |
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... | |
int | cvec_pushm_str (cvector_str *vec, char *a) |
same as push but without calling CVEC_STRDUP(a), m suffix is for "move" More... | |
void | cvec_pop_str (cvector_str *vec, char *ret) |
Remove the last element (size decreased 1). More... | |
char ** | cvec_back_str (cvector_str *vec) |
Return pointer to last element. 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_insertm_str (cvector_str *vec, cvec_sz i, char *a) |
Same as insert except no CVEC_STRDUP. 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... | |
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... | |
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... | |
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... | |
Variables | |
cvec_sz | CVEC_STR_START_SZ = 20 |
#define CVEC_ASSERT | ( | x | ) | assert(x) |
Definition at line 26 of file cvector_str.c.
#define CVEC_FREE | ( | p | ) | free(p) |
Definition at line 16 of file cvector_str.c.
#define CVEC_MALLOC | ( | sz | ) | malloc(sz) |
Definition at line 14 of file cvector_str.c.
#define CVEC_MEMMOVE | ( | dst, | |
src, | |||
sz | |||
) | memmove(dst, src, sz) |
Definition at line 21 of file cvector_str.c.
#define CVEC_REALLOC | ( | p, | |
sz | |||
) | realloc(p, sz) |
Definition at line 15 of file cvector_str.c.
#define CVEC_STR_ALLOCATOR | ( | x | ) | ((x+1) * 2) |
Definition at line 31 of file cvector_str.c.
char** cvec_back_str | ( | cvector_str * | vec | ) |
Return pointer to last element.
Definition at line 262 of file cvector_str.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.
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_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.
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.
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.
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.
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_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_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_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_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.
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.
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_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.
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_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_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_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.
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.
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_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.
cvec_sz CVEC_STR_START_SZ = 20 |
Definition at line 29 of file cvector_str.c.