5#if defined(CVEC_MALLOC) && defined(CVEC_FREE) && defined(CVEC_REALLOC)
7#elif !defined(CVEC_MALLOC) && !defined(CVEC_FREE) && !defined(CVEC_REALLOC)
10#error "Must define all or none of CVEC_MALLOC, CVEC_FREE, and CVEC_REALLOC."
15#define CVEC_MALLOC(sz) malloc(sz)
16#define CVEC_REALLOC(p, sz) realloc(p, sz)
17#define CVEC_FREE(p) free(p)
22#define CVEC_MEMMOVE(dst, src, sz) memmove(dst, src, sz)
27#define CVEC_ASSERT(x) assert(x)
32#define CVEC_STR_ALLOCATOR(x) ((x+1) * 2)
34#if CVEC_STRDUP == cvec_strdup
85 memset(vec->
a, 0, vec->
capacity*
sizeof(
char*));
110 for(i=0; i<num; i++) {
133 memset(vec->
a, 0, vec->
capacity*
sizeof(
char*));
153 for(i=0; i<num; i++) {
201 for (i=0; i<src->
size; ++i) {
219 if (!(tmp = (
char**)
CVEC_REALLOC(vec->
a,
sizeof(
char*)*tmp_sz))) {
238 if (!(tmp = (
char**)
CVEC_REALLOC(vec->
a,
sizeof(
char*)*tmp_sz))) {
246 vec->
a[vec->
size++] = a;
258 strcpy(ret, vec->
a[vec->
size]);
265 return &vec->
a[vec->
size-1];
277 if (!(tmp = (
char**)
CVEC_REALLOC(vec->
a,
sizeof(
char*)*tmp_sz))) {
285 memset(&vec->
a[vec->
size], 0, num*
sizeof(
char*));
300 if (!(tmp = (
char**)
CVEC_REALLOC(vec->
a,
sizeof(
char*)*tmp_sz))) {
323 if (!(tmp = (
char**)
CVEC_REALLOC(vec->
a,
sizeof(
char*)*tmp_sz))) {
348 if (!(tmp = (
char**)
CVEC_REALLOC(vec->
a,
sizeof(
char*)*tmp_sz))) {
357 for (j=0; j<num; ++j) {
374 if (!(tmp = (
char**)
CVEC_REALLOC(vec->
a,
sizeof(
char*)*tmp_sz))) {
396 strcpy(ret, vec->
a[i]);
410 for (i=start; i<=end; i++) {
449 if (size < vec->size) {
450 for(i=vec->
size-1; i>size-1; i--) {
457 if (!(tmp = (
char**)
CVEC_REALLOC(vec->
a,
sizeof(
char*)*size))) {
470 for(i=0; i<vec->
size; i++) {
499 for (i=0; i<vec->
size; i++) {
513 for (i=0; i<tmp->
size; i++) {
527 for (i=0; i<tmp->
size; i++) {
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 alloca...
void cvec_clear_str(cvector_str *vec)
Clears the contents of vector (frees all strings) and sets size to 0.
char ** cvec_back_str(cvector_str *vec)
Return pointer to last element.
int cvec_copy_str(cvector_str *dest, cvector_str *src)
Makes dest a copy of src.
int cvec_set_cap_str(cvector_str *vec, cvec_sz size)
Set capacity to size.
#define CVEC_REALLOC(p, sz)
int cvec_extend_str(cvector_str *vec, cvec_sz num)
Increase the size of the array num items.
void cvec_set_val_cap_str(cvector_str *vec, char *val)
Fills entire allocated array (capacity) with val.
cvector_str * cvec_init_str_heap(char **vals, cvec_sz num)
Create (on the heap) and initialize cvector_str with num elements of vals.
int cvec_copyc_str(void *dest, void *src)
Makes dest a copy of src.
#define CVEC_MEMMOVE(dst, src, sz)
void cvec_set_val_sz_str(cvector_str *vec, char *val)
Sets all size elements to val.
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.
int cvec_insertm_str(cvector_str *vec, cvec_sz i, char *a)
Same as insert except no CVEC_STRDUP.
char * cvec_strdup(const char *str)
Useful utility function since strdup isn't in standard C.
void cvec_erase_str(cvector_str *vec, cvec_sz start, cvec_sz end)
Erases strings from start to end inclusive.
void cvec_free_str(void *vec)
Frees the internal array and zeros out the members to maintain a consistent state.
cvector_str * cvec_str_heap(cvec_sz size, cvec_sz capacity)
Create a new cvector_str on the heap.
int cvec_push_str(cvector_str *vec, char *a)
Append a to end of vector (size increased 1).
int cvec_insert_arraym_str(cvector_str *vec, cvec_sz i, char **a, cvec_sz num)
Same as insert_array except no CVEC_STRDUP.
int cvec_pushm_str(cvector_str *vec, char *a)
same as push but without calling CVEC_STRDUP(a), m suffix is for "move"
void cvec_replace_str(cvector_str *vec, cvec_sz i, char *a, char *ret)
Replace string at i with a.
void cvec_pop_str(cvector_str *vec, char *ret)
Remove the last element (size decreased 1).
void cvec_free_str_heap(void *vec)
Frees contents (individual strings and array) and frees vector so don't use after calling this.
#define CVEC_STR_ALLOCATOR(x)
int cvec_reserve_str(cvector_str *vec, cvec_sz size)
Makes sure the vector capacity is >= size (parameter not member).
int cvec_insert_str(cvector_str *vec, cvec_sz i, char *a)
Insert a at index i (0 based).
void cvec_remove_str(cvector_str *vec, cvec_sz start, cvec_sz end)
Same as erase except it does not call CVEC_FREE.
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 i...
cvec_sz CVEC_STR_START_SZ
Data structure for string vector.
cvec_sz capacity
Allocated size of array; always >= size.
cvec_sz size
Current size (amount you use when manipulating array directly).