cr8r_vec_ft Struct Reference

Function table for vector. More...

Data Fields

cr8r_base_ft base
 Base function table values (data and size)
 
uint64_t(* new_size )(cr8r_base_ft *, uint64_t cap)
 determines the capacity to grow to if the vector is full but needs to have additional elements added. More...
 
void *(* resize )(cr8r_base_ft *, void *p, uint64_t cap)
 resize the backing array to a new size, possibly copying it to a new buffer. More...
 
void(* del )(cr8r_base_ft *, void *p)
 delete an element of the vector. More...
 
void(* copy )(cr8r_base_ft *, void *dest, const void *src)
 copy an element of the vector. More...
 
int(* cmp )(const cr8r_base_ft *, const void *a, const void *b)
 compare two elements of the vector. More...
 
void(* swap )(cr8r_base_ft *, void *a, void *b)
 swap two elements. More...
 

Detailed Description

Function table for vector.

Imagine this struct as the class of the vector. This struct specifies the size of the elements in a vector in bytes, as well as how to perform necessary operations (compare, copy, etc). Remember that function tables must be initialized manually. In particular, the cr8r_vec_default_* functions can be used as decent defaults, but must be explicitly set. Also look at complete default tables provided, cr8r_vecft_*.

Definition at line 40 of file vec.h.

Field Documentation

◆ new_size

uint64_t(* cr8r_vec_ft::new_size) (cr8r_base_ft *, uint64_t cap)

determines the capacity to grow to if the vector is full but needs to have additional elements added.

Both "cap" and the return value are counted in terms of number of elements, not number of bytes. cr8r_default_new_size is a good choice generally.

Definition at line 46 of file vec.h.

◆ resize

void*(* cr8r_vec_ft::resize) (cr8r_base_ft *, void *p, uint64_t cap)

resize the backing array to a new size, possibly copying it to a new buffer.

"cap" is counted in terms of number of elements, not number of bytes. Must return NULL and "free" buf if the new cap is 0, must "free" NULL (by doing nothing), and must be able to allocate a new buffer if the current buffer is NULL. cr8r_default_resize wraps realloc and free to do this. If it is not possible to safely move elements, then this function should probably not be allowed to move the allocated buffer (in the manner realloc does) and should fail instead so that external logic can manage copying to a new vector.

Definition at line 53 of file vec.h.

◆ del

void(* cr8r_vec_ft::del) (cr8r_base_ft *, void *p)

delete an element of the vector.

This only needs to clean up data owned by the element, not the element itself because it lives in the buffer. Can also be used to scramble memory if desired. Useful for strings, file descriptors, etc. Can be NULL if no operation is needed.

Definition at line 57 of file vec.h.

◆ copy

void(* cr8r_vec_ft::copy) (cr8r_base_ft *, void *dest, const void *src)

copy an element of the vector.

Can be NULL if memcpy is sufficient, otherwise this function must perform the role of memcpy plus whatever else it needs to do.

Definition at line 60 of file vec.h.

◆ cmp

int(* cr8r_vec_ft::cmp) (const cr8r_base_ft *, const void *a, const void *b)

compare two elements of the vector.

cr8r_default_cmp is a suitable default if needed. should return <0 if a < b, 0 if a == b, and >0 if a > b. should not return INT_MIN.

Definition at line 63 of file vec.h.

◆ swap

void(* cr8r_vec_ft::swap) (cr8r_base_ft *, void *a, void *b)

swap two elements.

The default cr8r_default_swap simply swaps the two elements using memcpy and a temporary buffer. more optimized versions can be used if relevant. Should work even if a == b, when it should do nothing.

Definition at line 66 of file vec.h.


The documentation for this struct was generated from the following file: