CVector  4.1.0
A C++ style vector library in strict ANSI C (C89)
Macros | Functions | Variables
cvector_void.c File Reference
#include "cvector_void.h"
#include <string.h>
#include <assert.h>

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_VOID_ALLOCATOR(x)   ((x+1) * 2)
 

Functions

cvector_voidcvec_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_voidcvec_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_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...
 
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...
 
int cvec_pushm_void (cvector_void *vec, void *a)
 Same as push except no elem_init even if it's set. More...
 
void cvec_pop_void (cvector_void *vec, void *ret)
 Remove the last element (size decreased 1). More...
 
void cvec_popm_void (cvector_void *vec, void *ret)
 Same as pop except no elem_free even if it's set. More...
 
void * cvec_back_void (cvector_void *vec)
 Return pointer to last element. More...
 
int cvec_extend_void (cvector_void *vec, cvec_sz num)
 Increase the size of the array num items. More...
 
void * cvec_get_void (cvector_void *vec, cvec_sz i)
 Return a void pointer to the ith element. More...
 
int cvec_insert_void (cvector_void *vec, cvec_sz i, void *a)
 Insert a at index i (0 based). 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_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_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...
 
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_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...
 
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_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_VOID_START_SZ = 20
 

Macro Definition Documentation

◆ CVEC_ASSERT

#define CVEC_ASSERT (   x)    assert(x)

Definition at line 26 of file cvector_void.c.

◆ CVEC_FREE

#define CVEC_FREE (   p)    free(p)

Definition at line 16 of file cvector_void.c.

◆ CVEC_MALLOC

#define CVEC_MALLOC (   sz)    malloc(sz)

Definition at line 14 of file cvector_void.c.

◆ CVEC_MEMMOVE

#define CVEC_MEMMOVE (   dst,
  src,
  sz 
)    memmove(dst, src, sz)

Definition at line 21 of file cvector_void.c.

◆ CVEC_REALLOC

#define CVEC_REALLOC (   p,
  sz 
)    realloc(p, sz)

Definition at line 15 of file cvector_void.c.

◆ CVEC_VOID_ALLOCATOR

#define CVEC_VOID_ALLOCATOR (   x)    ((x+1) * 2)

Definition at line 31 of file cvector_void.c.

Function Documentation

◆ cvec_back_void()

void* cvec_back_void ( cvector_void vec)

Return pointer to last element.

Definition at line 327 of file cvector_void.c.

◆ cvec_clear_void()

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.

◆ cvec_copy_void()

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.

◆ cvec_copyc_void()

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.

◆ cvec_erase_void()

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.

◆ cvec_extend_void()

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.

◆ cvec_free_void()

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.

◆ cvec_free_void_heap()

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.

◆ cvec_get_void()

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.

◆ cvec_init_void()

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.

◆ cvec_init_void_heap()

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.

◆ cvec_insert_array_void()

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.

◆ cvec_insert_arraym_void()

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.

◆ cvec_insert_void()

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.

◆ cvec_insertm_void()

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.

◆ cvec_pop_void()

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.

◆ cvec_popm_void()

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.

◆ cvec_push_void()

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.

◆ cvec_pushm_void()

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.

◆ cvec_remove_void()

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.

◆ cvec_replace_void()

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.

◆ cvec_replacem_void()

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.

◆ cvec_reserve_void()

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.

◆ cvec_set_cap_void()

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.

◆ cvec_set_val_cap_void()

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.

◆ cvec_set_val_sz_void()

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.

◆ cvec_void()

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.

◆ cvec_void_heap()

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.

Variable Documentation

◆ CVEC_VOID_START_SZ

cvec_sz CVEC_VOID_START_SZ = 20

Definition at line 29 of file cvector_void.c.