CVector
4.1.0
A C++ style vector library in strict ANSI C (C89)
|
#include <stdlib.h>
Go to the source code of this file.
Data Structures | |
struct | cvector_i |
Data structure for int vector. More... | |
Macros | |
#define | CVEC_SIZE_T size_t |
#define | CVEC_SZ |
Typedefs | |
typedef CVEC_SIZE_T | cvec_sz |
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... | |
Variables | |
cvec_sz | CVEC_I_START_SZ |
#define CVEC_SIZE_T size_t |
Definition at line 7 of file cvector_i.h.
#define CVEC_SZ |
Definition at line 11 of file cvector_i.h.
typedef CVEC_SIZE_T cvec_sz |
Definition at line 12 of file cvector_i.h.
int* cvec_back_i | ( | cvector_i * | vec | ) |
Return pointer to last element.
Definition at line 195 of file cvector_i.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.
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_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.
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.
Increase the size of the array num items.
Items are not initialized to anything
Definition at line 202 of file cvector_i.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.
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_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.
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.
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_pop_i | ( | cvector_i * | vec | ) |
Remove and return the last element (size decreased 1).
Definition at line 189 of file cvector_i.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.
Replace value at index i with a, return original value.
Definition at line 271 of file cvector_i.c.
Make sure capacity is at least size(parameter not member).
Definition at line 291 of file cvector_i.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.
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_sz_i | ( | cvector_i * | vec, |
int | val | ||
) |
Set all size elements to val.
Definition at line 326 of file cvector_i.c.
|
extern |
Definition at line 29 of file cvector_i.c.