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

Functions

cvector_dcvec_d_heap (cvec_sz size, cvec_sz capacity)
 Creates a new cvector_d on the heap. More...
 
cvector_dcvec_init_d_heap (double *vals, cvec_sz num)
 Create (on the heap) and initialize cvector_d with num elements of vals. More...
 
int cvec_d (cvector_d *vec, cvec_sz size, cvec_sz capacity)
 Same as cvec_d_heap() except the vector passed in was declared on the stack so it isn't allocated in this function. More...
 
int cvec_init_d (cvector_d *vec, double *vals, cvec_sz num)
 Same as cvec_init_d_heap() except the vector passed in was declared on the stack so it isn't allocated in this function. More...
 
int cvec_copyc_d (void *dest, void *src)
 Makes dest a copy of src. More...
 
int cvec_copy_d (cvector_d *dest, cvector_d *src)
 Makes dest a copy of src. More...
 
int cvec_push_d (cvector_d *vec, double a)
 Append a to end of vector (size increased 1). More...
 
double cvec_pop_d (cvector_d *vec)
 Remove and return the last element (size decreased 1). More...
 
double * cvec_back_d (cvector_d *vec)
 Return pointer to last element. More...
 
int cvec_extend_d (cvector_d *vec, cvec_sz num)
 Increase the size of the array num items. More...
 
int cvec_insert_d (cvector_d *vec, cvec_sz i, double a)
 Insert a at index i (0 based). More...
 
int cvec_insert_array_d (cvector_d *vec, cvec_sz i, double *a, cvec_sz num)
 Insert the first num elements of array a at index i. More...
 
double cvec_replace_d (cvector_d *vec, cvec_sz i, double a)
 Replace value at index i with a, return original value. More...
 
void cvec_erase_d (cvector_d *vec, cvec_sz start, cvec_sz end)
 Erases elements from start to end inclusive. More...
 
int cvec_reserve_d (cvector_d *vec, cvec_sz size)
 Make sure capacity is at least size(parameter not member). More...
 
int cvec_set_cap_d (cvector_d *vec, cvec_sz size)
 Set capacity to size. More...
 
void cvec_set_val_sz_d (cvector_d *vec, double val)
 Set all size elements to val. More...
 
void cvec_set_val_cap_d (cvector_d *vec, double val)
 Fills entire allocated array (capacity) with val. More...
 
void cvec_clear_d (cvector_d *vec)
 Sets size to 0 (does not clear contents). More...
 
void cvec_free_d_heap (void *vec)
 Frees everything so don't use vec after calling this. More...
 
void cvec_free_d (void *vec)
 Frees the internal array and sets size and capacity to 0. More...
 

Variables

cvec_sz CVEC_D_START_SZ = 50
 

Macro Definition Documentation

◆ CVEC_ASSERT

#define CVEC_ASSERT (   x)    assert(x)

Definition at line 26 of file cvector_d.c.

◆ CVEC_D_ALLOCATOR

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

Definition at line 31 of file cvector_d.c.

◆ CVEC_FREE

#define CVEC_FREE (   p)    free(p)

Definition at line 16 of file cvector_d.c.

◆ CVEC_MALLOC

#define CVEC_MALLOC (   sz)    malloc(sz)

Definition at line 14 of file cvector_d.c.

◆ CVEC_MEMMOVE

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

Definition at line 21 of file cvector_d.c.

◆ CVEC_REALLOC

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

Definition at line 15 of file cvector_d.c.

Function Documentation

◆ cvec_back_d()

double* cvec_back_d ( cvector_d vec)

Return pointer to last element.

Definition at line 195 of file cvector_d.c.

◆ cvec_clear_d()

void cvec_clear_d ( cvector_d vec)

Sets size to 0 (does not clear contents).

Definition at line 343 of file cvector_d.c.

◆ cvec_copy_d()

int cvec_copy_d ( cvector_d dest,
cvector_d 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_D_START_SZ

Definition at line 152 of file cvector_d.c.

◆ cvec_copyc_d()

int cvec_copyc_d ( 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_d'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 131 of file cvector_d.c.

◆ cvec_d()

int cvec_d ( cvector_d vec,
cvec_sz  size,
cvec_sz  capacity 
)

Same as cvec_d_heap() except the vector passed in was declared on the stack so it isn't allocated in this function.

Use the cvec_free_d in this case. This and cvec_init_d should be preferred over the heap versions.

Definition at line 89 of file cvector_d.c.

◆ cvec_d_heap()

cvector_d* cvec_d_heap ( cvec_sz  size,
cvec_sz  capacity 
)

Creates a new cvector_d 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_D_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_d.c.

◆ cvec_erase_d()

void cvec_erase_d ( cvector_d vec,
cvec_sz  start,
cvec_sz  end 
)

Erases elements from start to end inclusive.

Example cvec_erase_d(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_d.c.

◆ cvec_extend_d()

int cvec_extend_d ( cvector_d 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_d.c.

◆ cvec_free_d()

void cvec_free_d ( void *  vec)

Frees the internal array and sets size and capacity to 0.

Definition at line 356 of file cvector_d.c.

◆ cvec_free_d_heap()

void cvec_free_d_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 347 of file cvector_d.c.

◆ cvec_init_d()

int cvec_init_d ( cvector_d vec,
double *  vals,
cvec_sz  num 
)

Same as cvec_init_d_heap() except the vector passed in was declared on the stack so it isn't allocated in this function.

Use the cvec_free_d in this case.

Definition at line 106 of file cvector_d.c.

◆ cvec_init_d_heap()

cvector_d* cvec_init_d_heap ( double *  vals,
cvec_sz  num 
)

Create (on the heap) and initialize cvector_d with num elements of vals.

Capacity is set to num + CVEC_D_START_SZ.

Definition at line 63 of file cvector_d.c.

◆ cvec_insert_array_d()

int cvec_insert_array_d ( cvector_d vec,
cvec_sz  i,
double *  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_d.c.

◆ cvec_insert_d()

int cvec_insert_d ( cvector_d vec,
cvec_sz  i,
double  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_d.c.

◆ cvec_pop_d()

double cvec_pop_d ( cvector_d vec)

Remove and return the last element (size decreased 1).

Definition at line 189 of file cvector_d.c.

◆ cvec_push_d()

int cvec_push_d ( cvector_d vec,
double  a 
)

Append a to end of vector (size increased 1).

Capacity is increased by doubling when necessary.

Definition at line 171 of file cvector_d.c.

◆ cvec_replace_d()

double cvec_replace_d ( cvector_d vec,
cvec_sz  i,
double  a 
)

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

Definition at line 271 of file cvector_d.c.

◆ cvec_reserve_d()

int cvec_reserve_d ( cvector_d vec,
cvec_sz  size 
)

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

Definition at line 291 of file cvector_d.c.

◆ cvec_set_cap_d()

int cvec_set_cap_d ( cvector_d 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_d.c.

◆ cvec_set_val_cap_d()

void cvec_set_val_cap_d ( cvector_d vec,
double  val 
)

Fills entire allocated array (capacity) with val.

Definition at line 334 of file cvector_d.c.

◆ cvec_set_val_sz_d()

void cvec_set_val_sz_d ( cvector_d vec,
double  val 
)

Set all size elements to val.

Definition at line 325 of file cvector_d.c.

Variable Documentation

◆ CVEC_D_START_SZ

cvec_sz CVEC_D_START_SZ = 50

Definition at line 29 of file cvector_d.c.