CVector  4.1.0
A C++ style vector library in strict ANSI C (C89)
Macros | Functions | Variables
cvector_i.c File Reference
#include "cvector_i.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_I_ALLOCATOR(x)   ((x+1) * 2)
 

Functions

cvector_icvec_i_heap (cvec_sz size, cvec_sz capacity)
 Creates a new cvector_i on the heap. More...
 
cvector_icvec_init_i_heap (int *vals, cvec_sz num)
 Create (on the heap) and initialize cvector_i with num elements of vals. More...
 
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...
 
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_back_i (cvector_i *vec)
 Return pointer to last element. 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...
 
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...
 

Variables

cvec_sz CVEC_I_START_SZ = 50
 

Macro Definition Documentation

◆ CVEC_ASSERT

#define CVEC_ASSERT (   x)    assert(x)

Definition at line 26 of file cvector_i.c.

◆ CVEC_FREE

#define CVEC_FREE (   p)    free(p)

Definition at line 16 of file cvector_i.c.

◆ CVEC_I_ALLOCATOR

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

Definition at line 31 of file cvector_i.c.

◆ CVEC_MALLOC

#define CVEC_MALLOC (   sz)    malloc(sz)

Definition at line 14 of file cvector_i.c.

◆ CVEC_MEMMOVE

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

Definition at line 21 of file cvector_i.c.

◆ CVEC_REALLOC

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

Definition at line 15 of file cvector_i.c.

Function Documentation

◆ cvec_back_i()

int* cvec_back_i ( cvector_i vec)

Return pointer to last element.

Definition at line 195 of file cvector_i.c.

◆ cvec_clear_i()

void cvec_clear_i ( cvector_i vec)

Sets size to 0 (does not clear contents).

Definition at line 344 of file cvector_i.c.

◆ cvec_copy_i()

int cvec_copy_i ( cvector_i dest,
cvector_i 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_I_START_SZ

Definition at line 151 of file cvector_i.c.

◆ cvec_copyc_i()

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.

◆ cvec_erase_i()

void cvec_erase_i ( cvector_i vec,
cvec_sz  start,
cvec_sz  end 
)

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.

◆ cvec_extend_i()

int cvec_extend_i ( cvector_i vec,
cvec_sz  num 
)

Increase the size of the array num items.

Items are not initialized to anything

Definition at line 202 of file cvector_i.c.

◆ cvec_free_i()

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.

◆ cvec_free_i_heap()

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.

◆ cvec_i()

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.

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.

◆ cvec_i_heap()

cvector_i* cvec_i_heap ( cvec_sz  size,
cvec_sz  capacity 
)

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.

◆ cvec_init_i()

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.

Definition at line 105 of file cvector_i.c.

◆ cvec_init_i_heap()

cvector_i* cvec_init_i_heap ( int *  vals,
cvec_sz  num 
)

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.

◆ cvec_insert_array_i()

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.

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.

◆ cvec_insert_i()

int cvec_insert_i ( cvector_i vec,
cvec_sz  i,
int  a 
)

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.

◆ cvec_pop_i()

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.

◆ cvec_push_i()

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.

◆ cvec_replace_i()

int cvec_replace_i ( cvector_i vec,
cvec_sz  i,
int  a 
)

Replace value at index i with a, return original value.

Definition at line 271 of file cvector_i.c.

◆ cvec_reserve_i()

int cvec_reserve_i ( cvector_i vec,
cvec_sz  size 
)

Make sure capacity is at least size(parameter not member).

Definition at line 291 of file cvector_i.c.

◆ cvec_set_cap_i()

int cvec_set_cap_i ( cvector_i 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 309 of file cvector_i.c.

◆ cvec_set_val_cap_i()

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.

◆ cvec_set_val_sz_i()

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.

Variable Documentation

◆ CVEC_I_START_SZ

cvec_sz CVEC_I_START_SZ = 50

Definition at line 29 of file cvector_i.c.