Veritable Lasagna
An Allocator & Data Structure Library for C.
|
Go to the source code of this file.
Data Structures | |
struct | vl_linked_list |
A doubly-linked list. More... | |
Macros | |
#define | VL_LIST_ITER_INVALID VL_FIXEDPOOL_INVALID_IDX |
#define | VL_LIST_FOREACH(list, trackVar) for(vl_list_iter trackVar = (list)->head; (trackVar) != VL_LIST_ITER_INVALID; (trackVar) = vlListNext(list, trackVar)) |
#define | VL_LIST_FOREACH_REVERSE(list, trackVar) for(vl_list_iter trackVar = (list)->tail; (trackVar) != VL_LIST_ITER_INVALID; (trackVar) = vlListPrev(list, trackVar)) |
#define | vlListFree(listPtr) vlFixedPoolFree(&((listPtr)->nodePool)) |
Frees the specified list instance. | |
#define | vlListSize(listPtr) (vl_dsidx_t)((listPtr)->length) |
Returns the total number of elements in the specified list. | |
#define | vlListReserve(listPtr, n) vlFixedPoolReserve(&((listPtr)->nodePool)) |
Reserves space for n-many elements in the specified list. | |
#define | vlListClear(listPtr) |
Clears the specified list so it can be used as if it was just initialized. | |
Typedefs | |
typedef vl_fixedpool_idx | vl_list_iter |
List iterator type. Represents a location within a vl_linked_list. | |
Functions | |
void | vlListInit (vl_linked_list *list, vl_memsize_t elementSize) |
Initializes the specified list instance. | |
vl_linked_list * | vlListNew (vl_memsize_t elementSize) |
Allocates on the heap, initializes, and returns a list instance. | |
void | vlListDelete (vl_linked_list *list) |
Deletes the specified list instance. | |
vl_list_iter | vlListPushFront (vl_linked_list *list, const void *elem) |
Adds a new element to the front of the list. | |
void | vlListPopFront (vl_linked_list *list) |
Removes whatever element is at the front of the list. | |
vl_list_iter | vlListPushBack (vl_linked_list *list, const void *elem) |
Adds a new element to the end of the list. | |
void | vlListPopBack (vl_linked_list *list) |
Removes whatever element is at the end of the list. | |
vl_list_iter | vlListInsertAfter (vl_linked_list *list, vl_list_iter target, const void *elem) |
Inserts an element immediately after the specified target. | |
vl_list_iter | vlListInsertBefore (vl_linked_list *list, vl_list_iter target, const void *elem) |
Inserts an element immediately before the specified target. | |
vl_linked_list * | vlListClone (const vl_linked_list *src, vl_linked_list *dest) |
Clones the specified list to another. | |
int | vlListCopy (vl_linked_list *src, vl_list_iter begin, vl_list_iter end, vl_linked_list *dest, vl_list_iter after) |
Copies a range of elements from one list to another. | |
void | vlListSort (vl_linked_list *src, vl_compare_function cmp) |
Sorts the specified list in-place using the given comparator. | |
vl_list_iter | vlListFind (vl_linked_list *src, const void *element) |
Performs an iterative search on the specified list. | |
void | vlListRemove (vl_linked_list *list, vl_list_iter iter) |
Removes the specified element from the list. | |
vl_list_iter | vlListNext (vl_linked_list *list, vl_list_iter iter) |
Returns next adjacent iterator, or VL_LIST_ITER_INVALID if no such element exists. | |
vl_list_iter | vlListPrev (vl_linked_list *list, vl_list_iter iter) |
Returns the previous adjacent iterator, or VL_LIST_ITER_INVALID if no such element exists. | |
vl_transient * | vlListSample (vl_linked_list *list, vl_list_iter iter) |
Returns a pointer to the element data for the specified iterator. | |
struct vl_linked_list |
A doubly-linked list.
The vl_linked_list structure represents a doubly linked list with elements of a fixed size. This is implemented on top of vl_fixedpool, which is used to manage underlying memory. Due to using a pool, allocation should not occur frequently due to the larger pool of memory which is sliced into smaller pieces for individual nodes.
The vl_list_iter are simply vl_fixedpool indices, and thus, surrounding iterators are not invalidated when removing or inserting elements.
Pointers to list elements may never be invalidated due to using a fixed pool.
Data Fields | ||
---|---|---|
vl_memsize_t | elementSize | |
vl_list_iter | head | |
vl_dsidx_t | length | |
vl_fixedpool | nodePool | |
vl_list_iter | tail |
#define VL_LIST_FOREACH | ( | list, | |
trackVar ) for(vl_list_iter trackVar = (list)->head; (trackVar) != VL_LIST_ITER_INVALID; (trackVar) = vlListNext(list, trackVar)) |
This is a simple macro for iterating a list.
list | pointer vl_list pointer |
trackVar | identifier of the iterator. Always of type vl_list_iter. |
#define VL_LIST_FOREACH_REVERSE | ( | list, | |
trackVar ) for(vl_list_iter trackVar = (list)->tail; (trackVar) != VL_LIST_ITER_INVALID; (trackVar) = vlListPrev(list, trackVar)) |
This is a simple macro for iterating a list, in reverse.
list | pointer vl_list pointer |
trackVar | identifier of the iterator. Always of type vl_list_iter. |
#define VL_LIST_ITER_INVALID VL_FIXEDPOOL_INVALID_IDX |
#define vlListClear | ( | listPtr | ) |
Clears the specified list so it can be used as if it was just initialized.
This function does not touch any information in the underlying buffer, but rather resets some book-keeping variables.
list | pointer |
#define vlListFree | ( | listPtr | ) | vlFixedPoolFree(&((listPtr)->nodePool)) |
Frees the specified list instance.
The specified list should be initialized via vlListInit.
list | pointer |
#define vlListReserve | ( | listPtr, | |
n ) vlFixedPoolReserve(&((listPtr)->nodePool)) |
Reserves space for n-many elements in the specified list.
list | pointer |
n | number of elements to reserve space for. |
#define vlListSize | ( | listPtr | ) | (vl_dsidx_t)((listPtr)->length) |
Returns the total number of elements in the specified list.
list | pointer |
typedef vl_fixedpool_idx vl_list_iter |
List iterator type. Represents a location within a vl_linked_list.
vl_linked_list * vlListClone | ( | const vl_linked_list * | src, |
vl_linked_list * | dest ) |
Clones the specified list to another.
Clones the entirety of the src list to the dest list.
The 'src' list pointer must be non-null and initialized. The 'dest' list pointer may be null, but if it is not null it must be initialized.
If the 'dest' list pointer is null, a new list is created via vlListNew. Otherwise, its element size is set to the source's and all of its existing data is replaced.
src | |
dest |
int vlListCopy | ( | vl_linked_list * | src, |
vl_list_iter | begin, | ||
vl_list_iter | end, | ||
vl_linked_list * | dest, | ||
vl_list_iter | after ) |
Copies a range of elements from one list to another.
Both the src list and the dest list must have equivalent element sizes, otherwise this is a no-op.
This is an inclusive range, and as such, the elements referred to by the begin and end iterators are also copied to the target list.
The begin and end iterators are expected to be in logical iterative order, meaning that if iterating through the entire src list, begin would be found before end.
src | source list pointer |
begin | iterator to start the copy at, or VL_LIST_ITER_INVALID for the beginning of the list. |
end | iterator to end the copy after, or VL_LIST_ITER_INVALID for the end of the list. |
dest | destination buffer pointer |
after | the iterator to insert elements after, or VL_LIST_ITER_INVALID for the end of the destination. |
void vlListDelete | ( | vl_linked_list * | list | ) |
Deletes the specified list instance.
The specified list should be created via vlListNew.
list | pointer |
vl_list_iter vlListFind | ( | vl_linked_list * | src, |
const void * | element ) |
Performs an iterative search on the specified list.
This will return an iterator to the first instance of the specified element that was found.
Returns VL_LIST_ITER_INVALID if the element could not be found.
src | source list pointer |
element | pointer to the element to compare against |
void vlListInit | ( | vl_linked_list * | list, |
vl_memsize_t | elementSize ) |
Initializes the specified list instance.
The initialized list should be freed via vlListFree.
list | pointer |
elementSize | size of a single list element, in bytes. |
vl_list_iter vlListInsertAfter | ( | vl_linked_list * | list, |
vl_list_iter | target, | ||
const void * | elem ) |
Inserts an element immediately after the specified target.
Element data is copied to the internal pool allocator.
list | pointer |
target | iterator to element that will have something inserted after it. |
elem | pointer |
vl_list_iter vlListInsertBefore | ( | vl_linked_list * | list, |
vl_list_iter | target, | ||
const void * | elem ) |
Inserts an element immediately before the specified target.
Element data is copied to the internal pool allocator.
list | pointer |
target | iterator to element that will have something inserted before it. |
elem |
vl_linked_list * vlListNew | ( | vl_memsize_t | elementSize | ) |
Allocates on the heap, initializes, and returns a list instance.
elementSize | size of a single list element, in bytes. |
vl_list_iter vlListNext | ( | vl_linked_list * | list, |
vl_list_iter | iter ) |
Returns next adjacent iterator, or VL_LIST_ITER_INVALID if no such element exists.
list | pointer |
iter | node iterator |
void vlListPopBack | ( | vl_linked_list * | list | ) |
Removes whatever element is at the end of the list.
This is a no-op if the list is empty.
list | pointer |
void vlListPopFront | ( | vl_linked_list * | list | ) |
Removes whatever element is at the front of the list.
This is a no-op if the list is empty.
list | pointer |
vl_list_iter vlListPrev | ( | vl_linked_list * | list, |
vl_list_iter | iter ) |
Returns the previous adjacent iterator, or VL_LIST_ITER_INVALID if no such element exists.
list | pointer |
iter | node iterator |
vl_list_iter vlListPushBack | ( | vl_linked_list * | list, |
const void * | elem ) |
Adds a new element to the end of the list.
Element data is copied to the internal pool allocator.
list | pointer |
elem | pointer to element data |
vl_list_iter vlListPushFront | ( | vl_linked_list * | list, |
const void * | elem ) |
Adds a new element to the front of the list.
Element data is copied to the internal pool allocator.
list | pointer |
elem | pointer to element data |
void vlListRemove | ( | vl_linked_list * | list, |
vl_list_iter | iter ) |
Removes the specified element from the list.
The underlying node is returned to the underlying node pool for reuse, without modifying its discarded data.
list | pointer |
iter | node iterator. |
vl_transient * vlListSample | ( | vl_linked_list * | list, |
vl_list_iter | iter ) |
Returns a pointer to the element data for the specified iterator.
list | pointer |
iter | node iterator |
void vlListSort | ( | vl_linked_list * | src, |
vl_compare_function | cmp ) |
Sorts the specified list in-place using the given comparator.
This function implements an iterative merge sort. Elements are not copied in this operation, but rather the links between them are modified.
The list is split into a "forest" in the underlying node pool, briefly holding a variety of sub-lists which are later merged back together.
src | source list pointer |
cmp | comparator function |