C Container Collection (CCC)
|
The Buffer Interface. More...
Go to the source code of this file.
Initialization Interface | |
Initialize the container with memory, callbacks, and permissions. | |
#define | ccc_buf_init(mem_ptr, alloc_fn, aux_data, capacity, optional_size...) ccc_impl_buf_init(mem_ptr, alloc_fn, aux_data, capacity, optional_size) |
Initialize a contiguous buffer of user a specified type, allocation policy, capacity, and optional starting size. | |
Container Types | |
Types available in the container interface. | |
typedef struct ccc_buf_ | ccc_buffer |
A contiguous block of storage for elements of the same type. | |
Insert and Remove Interface | |
These functions assume contiguity of elements in the buffer and increase or decrease size accordingly. | |
ccc_result | ccc_buf_alloc (ccc_buffer *buf, size_t capacity, ccc_alloc_fn *fn) |
allocates the buffer to the specified size according to the user defined allocation function. | |
void * | ccc_buf_alloc_back (ccc_buffer *buf) |
allocates a new slot from the buffer at the end of the contiguous array. A slot is equivalent to one of the element type specified when the buffer is initialized. | |
void * | ccc_buf_push_back (ccc_buffer *buf, void const *data) |
return the newly pushed data into the last slot of the buffer according to size. | |
void * | ccc_buf_insert (ccc_buffer *buf, size_t i, void const *data) |
insert data at slot i according to size of the buffer maintaining contiguous storage of elements between 0 and size. | |
ccc_result | ccc_buf_pop_back (ccc_buffer *buf) |
pop the back element from the buffer according to size. | |
ccc_result | ccc_buf_pop_back_n (ccc_buffer *buf, size_t n) |
pop n elements from the back of the buffer according to size. | |
ccc_result | ccc_buf_erase (ccc_buffer *buf, size_t i) |
erase element at slot i according to size of the buffer maintaining contiguous storage of elements between 0 and size. | |
Slot Management Interface | |
These functions interact with slots in the buffer directly and do not modify the size of the buffer. These are best used for custom container implementations operating at a higher level of abstraction. | |
void * | ccc_buf_at (ccc_buffer const *buf, size_t i) |
return the element at slot i in buf. | |
ptrdiff_t | ccc_buf_i (ccc_buffer const *buf, void const *slot) |
return the index of an element known to be in the buffer. | |
void * | ccc_buf_back (ccc_buffer const *buf) |
return the final element in the buffer according the current size. | |
void * | ccc_buf_front (ccc_buffer const *buf) |
return the first element in the buffer at index 0. | |
void * | ccc_buf_copy (ccc_buffer *buf, size_t dst, size_t src) |
copy data at index src to dst according to capacity. | |
ccc_result | ccc_buf_write (ccc_buffer *buf, size_t i, void const *data) |
write data to buffer at slot at index i according to capacity. | |
ccc_result | ccc_buf_swap (ccc_buffer *buf, char tmp[], size_t i, size_t j) |
swap elements at i and j according to capacity of the bufer. | |
Iteration Interface | |
The following functions implement iterators over the buffer. | |
void * | ccc_buf_begin (ccc_buffer const *buf) |
obtain the base address of the buffer in preparation for iteration. | |
void * | ccc_buf_next (ccc_buffer const *buf, void const *iter) |
advance the iter to the next slot in the buffer according to size. | |
void * | ccc_buf_end (ccc_buffer const *buf) |
return the end position of the buffer according to size. | |
void * | ccc_buf_capacity_end (ccc_buffer const *buf) |
return the end position of the buffer according to capacity. | |
void * | ccc_buf_rbegin (ccc_buffer const *buf) |
obtain the address of the last element in the buffer in preparation for iteration according to size. | |
void * | ccc_buf_rnext (ccc_buffer const *buf, void const *iter) |
advance the iter to the next slot in the buffer according to size and in reverse order. | |
void * | ccc_buf_rend (ccc_buffer const *buf) |
return the rend position of the buffer. | |
State Interface | |
These functions help manage or obtain state of the buffer. | |
ccc_result | ccc_buf_size_plus (ccc_buffer *buf, size_t n) |
add n to the size of the buffer. | |
ccc_result | ccc_buf_size_minus (ccc_buffer *buf, size_t n) |
subtract n from the size of the buffer. | |
ccc_result | ccc_buf_size_set (ccc_buffer *buf, size_t n) |
set the buffer size to n. | |
size_t | ccc_buf_capacity (ccc_buffer const *buf) |
return the current capacity of the buffer. | |
size_t | ccc_buf_elem_size (ccc_buffer const *buf) |
the size of the type being stored contiguously in the buffer. | |
size_t | ccc_buf_size (ccc_buffer const *buf) |
obtain the size of the buffer. | |
bool | ccc_buf_is_empty (ccc_buffer const *buf) |
return true if the size of the buffer is 0. | |
bool | ccc_buf_is_full (ccc_buffer const *buf) |
return true if the size of the buffer equals capacity. | |
The Buffer Interface.
Buffer usage is similar to a C++ vector, with more flexible functions provided to support higher level containers and abstractions. While useful on its own–a stack could be implemented with the provided functions–a buffer is often used as the lower level abstraction for the flat data structures in this library that provide more specialized operations. A buffer does not require the user accommodate any intrusive elements.
A buffer with allocation permission will re-size when a new element is inserted in a contiguous fashion. Interface in the allocation management section assume elements are stored contiguously and adjust size accordingly.
Interface in the slot management section offer data movement and writing operations that do not affect the size of the container. If writing a more complex higher level container that does not need size management these functions offer more custom control over the buffer.
If allocation is not permitted, resizing will not occur and the insertion function will fail when capacity is reached, returning some value to indicate failure.
If shorter names are desired, define the following preprocessor directive.
Then, the ccc_
prefix can be dropped from all types and functions.
#define ccc_buf_init | ( | mem_ptr, | |
alloc_fn, | |||
aux_data, | |||
capacity, | |||
optional_size... | |||
) | ccc_impl_buf_init(mem_ptr, alloc_fn, aux_data, capacity, optional_size) |
Initialize a contiguous buffer of user a specified type, allocation policy, capacity, and optional starting size.
[in] | mem_ptr | the pointer to existing memory or ((Type *)NULL). |
[in] | alloc_fn | ccc_alloc_fn or NULL if no allocation is permitted. |
[in] | aux_data | any auxiliary data needed for managing buffer memory. |
[in] | capacity | the capacity of memory at mem_ptr. |
[in] | optional_size | optional starting size of the buffer <= capacity. |
Initialization of a buffer can occur at compile time or run time depending on the arguments. The memory pointer should be of the same type one intends to store in the buffer. Therefore, if one desires a dynamic buffer with a starting capacity of 0 and mem_ptr of NULL, casting to a type is required (e.g. (int*)NULL).
This initializer determines memory control for the lifetime of the buffer. If the buffer points to memory of a predetermined and fixed capacity do not provide an allocation function. If a dynamic buffer is preferred, provide the allocation function as defined by the signature in types.h. If resizing is desired on memory that has already been allocated, ensure allocation has occurred with the provided allocation function.
typedef struct ccc_buf_ ccc_buffer |
A contiguous block of storage for elements of the same type.
A buffer may be initialized on the stack, heap, or data segment at compile time or runtime.
ccc_result ccc_buf_alloc | ( | ccc_buffer * | buf, |
size_t | capacity, | ||
ccc_alloc_fn * | fn | ||
) |
allocates the buffer to the specified size according to the user defined allocation function.
[in] | buf | a pointer to the buffer. |
[in] | capacity | the newly desired capacity. |
[in] | fn | the allocation function defined by the user. |
This function takes the allocation function as an argument in case no allocation function has been provided upon initialization and the user is managing allocations and resizing directly. If an allocation function has been provided than the use of this function should be rare as the buffer will reallocate more memory when necessary.
void * ccc_buf_alloc_back | ( | ccc_buffer * | buf | ) |
allocates a new slot from the buffer at the end of the contiguous array. A slot is equivalent to one of the element type specified when the buffer is initialized.
[in] | buf | a pointer to the buffer. |
A buffer can be used as the backing for more complex data structures. Requesting new space from a buffer as an allocator can be helpful for these higher level organizations.
void * ccc_buf_at | ( | ccc_buffer const * | buf, |
size_t | i | ||
) |
return the element at slot i in buf.
[in] | buf | the pointer to the buffer. |
[in] | i | the index within capacity range of the buffer. |
Note that as long as the index is valid within the capacity of the buffer a valid pointer is returned, which may result in a slot of old or uninitialized data. It is up to the user to ensure the index provided is within the current size of the buffer.
void * ccc_buf_back | ( | ccc_buffer const * | buf | ) |
return the final element in the buffer according the current size.
[in] | buf | the pointer to the buffer. |
void * ccc_buf_begin | ( | ccc_buffer const * | buf | ) |
obtain the base address of the buffer in preparation for iteration.
[in] | buf | the pointer to the buffer. |
size_t ccc_buf_capacity | ( | ccc_buffer const * | buf | ) |
return the current capacity of the buffer.
[in] | buf | the pointer to the buffer. |
void * ccc_buf_capacity_end | ( | ccc_buffer const * | buf | ) |
return the end position of the buffer according to capacity.
[in] | buf | the pointer to the buffer. |
Note that end is determined by the capcity of the buffer and will not change until a resize has occured, if permitted.
void * ccc_buf_copy | ( | ccc_buffer * | buf, |
size_t | dst, | ||
size_t | src | ||
) |
copy data at index src to dst according to capacity.
[in] | buf | the pointer to the buffer. |
[in] | dst | the index of destination within bounds of capacity. |
[in] | src | the index of source within bounds of capacity. |
Note that destination and source are only required to be valid within bounds of capacity of the buffer. It is up to the user to ensure destination and source are within the size bounds of the buffer.
size_t ccc_buf_elem_size | ( | ccc_buffer const * | buf | ) |
the size of the type being stored contiguously in the buffer.
[in] | buf | the pointer to the buffer. |
void * ccc_buf_end | ( | ccc_buffer const * | buf | ) |
return the end position of the buffer according to size.
[in] | buf | the pointer to the buffer. |
Note that end is determined by the size of the buffer dynamically.
ccc_result ccc_buf_erase | ( | ccc_buffer * | buf, |
size_t | i | ||
) |
erase element at slot i according to size of the buffer maintaining contiguous storage of elements between 0 and size.
[in] | buf | the pointer to the buffer. |
[in] | i | the index of the element to be erased. |
Note that this function assumes elements must be maintained contiguously according to size meaning a bulk copy of elements sliding down to fill the space left by i will occur.
void * ccc_buf_front | ( | ccc_buffer const * | buf | ) |
return the first element in the buffer at index 0.
[in] | buf | the pointer to the buffer. |
ptrdiff_t ccc_buf_i | ( | ccc_buffer const * | buf, |
void const * | slot | ||
) |
return the index of an element known to be in the buffer.
[in] | buf | the pointer to the buffer. |
[in] | slot | the pointer to the element stored in the buffer. |
void * ccc_buf_insert | ( | ccc_buffer * | buf, |
size_t | i, | ||
void const * | data | ||
) |
insert data at slot i according to size of the buffer maintaining contiguous storage of elements between 0 and size.
[in] | buf | the pointer to the buffer. |
[in] | i | the index at which to insert data. |
[in] | data | the data copied into the buffer at index i of the same size as elements stored in the buffer. |
Note that this function assumes elements must be maintained contiguously according to size of the buffer meaning a bulk move of elements sliding down to accommodate i will occur.
bool ccc_buf_is_empty | ( | ccc_buffer const * | buf | ) |
return true if the size of the buffer is 0.
[in] | buf | the pointer to the buffer. |
bool ccc_buf_is_full | ( | ccc_buffer const * | buf | ) |
return true if the size of the buffer equals capacity.
[in] | buf | the pointer to the buffer. |
void * ccc_buf_next | ( | ccc_buffer const * | buf, |
void const * | iter | ||
) |
advance the iter to the next slot in the buffer according to size.
[in] | buf | the pointer to the buffer. |
[in] | iter | the pointer to the current slot of the buffer. |
ccc_result ccc_buf_pop_back | ( | ccc_buffer * | buf | ) |
pop the back element from the buffer according to size.
[in] | buf | the pointer to the buffer. |
ccc_result ccc_buf_pop_back_n | ( | ccc_buffer * | buf, |
size_t | n | ||
) |
pop n elements from the back of the buffer according to size.
[in] | buf | the pointer to the buffer. |
[in] | n | the number of elements to pop. |
void * ccc_buf_push_back | ( | ccc_buffer * | buf, |
void const * | data | ||
) |
return the newly pushed data into the last slot of the buffer according to size.
[in] | buf | the pointer to the buffer. |
[in] | data | the pointer to the data of element size. |
The data is copied into the buffer at the final slot if there is remaining capacity. If size is equal to capacity resizing will be attempted but may fail if no allocation function is provided or the allocator provided is exhausted.
void * ccc_buf_rbegin | ( | ccc_buffer const * | buf | ) |
obtain the address of the last element in the buffer in preparation for iteration according to size.
[in] | buf | the pointer to the buffer. |
void * ccc_buf_rend | ( | ccc_buffer const * | buf | ) |
return the rend position of the buffer.
[in] | buf | the pointer to the buffer. |
void * ccc_buf_rnext | ( | ccc_buffer const * | buf, |
void const * | iter | ||
) |
advance the iter to the next slot in the buffer according to size and in reverse order.
[in] | buf | the pointer to the buffer. |
[in] | iter | the pointer to the current slot of the buffer. |
size_t ccc_buf_size | ( | ccc_buffer const * | buf | ) |
obtain the size of the buffer.
[in] | buf | the pointer to the buffer. |
Note that size must be less than or equal to capacity.
ccc_result ccc_buf_size_minus | ( | ccc_buffer * | buf, |
size_t | n | ||
) |
subtract n from the size of the buffer.
[in] | buf | the pointer to the buffer. |
[in] | n | the quantity to subtract from the current buffer size. |
If n would reduce the size to less than 0, the buffer size is set to 0 and the input error status is returned.
ccc_result ccc_buf_size_plus | ( | ccc_buffer * | buf, |
size_t | n | ||
) |
add n to the size of the buffer.
[in] | buf | the pointer to the buffer. |
[in] | n | the quantity to add to the current buffer size. |
If n would exceed the current capacity of the buffer the size is set to capacity and the input error status is returned.
ccc_result ccc_buf_size_set | ( | ccc_buffer * | buf, |
size_t | n | ||
) |
set the buffer size to n.
[in] | buf | the pointer to the buffer. |
[in] | n | the new size of the buffer. |
If n is larger than the capacity of the buffer the size is set equal to the capacity and an error is returned.
ccc_result ccc_buf_swap | ( | ccc_buffer * | buf, |
char | tmp[], | ||
size_t | i, | ||
size_t | j | ||
) |
swap elements at i and j according to capacity of the bufer.
[in] | buf | the pointer to the buffer. |
[in] | tmp | the pointer to the temporary buffer of the same size as an element stored in the buffer. |
[in] | i | the index of an element in the buffer. |
[in] | j | the index of an element in the buffer. |
Note that i and j are only checked to be within capacity range of the buffer. It is the user's responsibility to check for i and j within bounds of size if such behavior is needed.
ccc_result ccc_buf_write | ( | ccc_buffer * | buf, |
size_t | i, | ||
void const * | data | ||
) |
write data to buffer at slot at index i according to capacity.
[in] | buf | the pointer to the buffer. |
[in] | i | the index within bounds of capacity of the buffer. |
[in] | data | the data that will be written to slot at i. |
Note that data will be written to the slot at index i, according to the capacity of the buffer. It is up to the user to ensure i is within size of the buffer if such behavior is desired. No elements are moved to be preserved meaning any data at i is overwritten.