![]() |
ucx
UAP Common Extensions
|
Interface for list implementations. More...
Go to the source code of this file.
Data Structures | |
struct | cx_list_s |
Structure for holding the base data of a list. More... | |
struct | cx_list_class_s |
The class definition for arbitrary lists. More... | |
Typedefs | |
typedef struct cx_list_class_s | cx_list_class |
List class type. | |
typedef struct cx_list_s | CxList |
Common type for all list implementations. | |
Functions | |
size_t | cx_list_default_insert_array (struct cx_list_s *list, size_t index, const void *data, size_t n) |
Default implementation of an array insert. | |
size_t | cx_list_default_insert_sorted (struct cx_list_s *list, const void *sorted_data, size_t n) |
Default implementation of a sorted insert. | |
void | cx_list_default_sort (struct cx_list_s *list) |
Default unoptimized sort implementation. | |
int | cx_list_default_swap (struct cx_list_s *list, size_t i, size_t j) |
Default unoptimized swap implementation. | |
void | cx_list_init (struct cx_list_s *list, struct cx_list_class_s *cl, const struct cx_allocator_s *allocator, cx_compare_func comparator, size_t elem_size) |
Initializes a list struct. | |
static size_t | cxListSize (const CxList *list) |
Returns the number of elements currently stored in the list. | |
static int | cxListAdd (CxList *list, const void *elem) |
Adds an item to the end of the list. | |
static size_t | cxListAddArray (CxList *list, const void *array, size_t n) |
Adds multiple items to the end of the list. | |
static int | cxListInsert (CxList *list, size_t index, const void *elem) |
Inserts an item at the specified index. | |
static int | cxListInsertSorted (CxList *list, const void *elem) |
Inserts an item into a sorted list. | |
static size_t | cxListInsertArray (CxList *list, size_t index, const void *array, size_t n) |
Inserts multiple items to the list at the specified index. | |
static size_t | cxListInsertSortedArray (CxList *list, const void *array, size_t n) |
Inserts a sorted array into a sorted list. | |
static int | cxListInsertAfter (CxIterator *iter, const void *elem) |
Inserts an element after the current location of the specified iterator. | |
static int | cxListInsertBefore (CxIterator *iter, const void *elem) |
Inserts an element before the current location of the specified iterator. | |
static int | cxListRemove (CxList *list, size_t index) |
Removes the element at the specified index. | |
static int | cxListRemoveAndGet (CxList *list, size_t index, void *targetbuf) |
Removes and returns the element at the specified index. | |
static size_t | cxListRemoveArray (CxList *list, size_t index, size_t num) |
Removes multiple element starting at the specified index. | |
static size_t | cxListRemoveArrayAndGet (CxList *list, size_t index, size_t num, void *targetbuf) |
Removes and returns multiple element starting at the specified index. | |
static void | cxListClear (CxList *list) |
Removes all elements from this list. | |
static int | cxListSwap (CxList *list, size_t i, size_t j) |
Swaps two items in the list. | |
static void * | cxListAt (const CxList *list, size_t index) |
Returns a pointer to the element at the specified index. | |
static CxIterator | cxListIteratorAt (const CxList *list, size_t index) |
Returns an iterator pointing to the item at the specified index. | |
static CxIterator | cxListBackwardsIteratorAt (const CxList *list, size_t index) |
Returns a backwards iterator pointing to the item at the specified index. | |
CxIterator | cxListMutIteratorAt (CxList *list, size_t index) |
Returns a mutating iterator pointing to the item at the specified index. | |
CxIterator | cxListMutBackwardsIteratorAt (CxList *list, size_t index) |
Returns a mutating backwards iterator pointing to the item at the specified index. | |
static CxIterator | cxListIterator (const CxList *list) |
Returns an iterator pointing to the first item of the list. | |
static CxIterator | cxListMutIterator (CxList *list) |
Returns a mutating iterator pointing to the first item of the list. | |
static CxIterator | cxListBackwardsIterator (const CxList *list) |
Returns a backwards iterator pointing to the last item of the list. | |
static CxIterator | cxListMutBackwardsIterator (CxList *list) |
Returns a mutating backwards iterator pointing to the last item of the list. | |
static size_t | cxListFind (const CxList *list, const void *elem) |
Returns the index of the first element that equals elem . | |
static bool | cxListIndexValid (const CxList *list, size_t index) |
Checks if the specified index is within bounds. | |
static size_t | cxListFindRemove (CxList *list, const void *elem) |
Removes and returns the index of the first element that equals elem . | |
static void | cxListSort (CxList *list) |
Sorts the list. | |
static void | cxListReverse (CxList *list) |
Reverses the order of the items. | |
int | cxListCompare (const CxList *list, const CxList *other) |
Compares a list to another list of the same type. | |
void | cxListFree (CxList *list) |
Deallocates the memory of the specified list structure. | |
Variables | |
CxList *const | cxEmptyList |
A shared instance of an empty list. | |
Interface for list implementations.
size_t cx_list_default_insert_array | ( | struct cx_list_s * | list, |
size_t | index, | ||
const void * | data, | ||
size_t | n ) |
Default implementation of an array insert.
This function uses the element insert function for each element of the array.
Use this in your own list class if you do not want to implement an optimized version for your list.
list | the list |
index | the index where to insert the data |
data | a pointer to the array of data to insert |
n | the number of elements to insert |
size_t cx_list_default_insert_sorted | ( | struct cx_list_s * | list, |
const void * | sorted_data, | ||
size_t | n ) |
Default implementation of a sorted insert.
This function uses the array insert function to insert consecutive groups of sorted data.
The source data must already be sorted wrt. the list's compare function.
Use this in your own list class if you do not want to implement an optimized version for your list.
list | the list |
sorted_data | a pointer to the array of pre-sorted data to insert |
n | the number of elements to insert |
void cx_list_default_sort | ( | struct cx_list_s * | list | ) |
Default unoptimized sort implementation.
This function will copy all data to an array, sort the array with standard qsort, and then copy the data back to the list memory.
Use this in your own list class if you do not want to implement an optimized version for your list.
list | the list that shall be sorted |
int cx_list_default_swap | ( | struct cx_list_s * | list, |
size_t | i, | ||
size_t | j ) |
Default unoptimized swap implementation.
Use this in your own list class if you do not want to implement an optimized version for your list.
list | the list in which to swap |
i | index of one element |
j | index of the other element |
zero | success |
non-zero | when indices are out of bounds or memory allocation for the temporary buffer fails |
void cx_list_init | ( | struct cx_list_s * | list, |
struct cx_list_class_s * | cl, | ||
const struct cx_allocator_s * | allocator, | ||
cx_compare_func | comparator, | ||
size_t | elem_size ) |
Initializes a list struct.
Only use this function if you are creating your own list implementation. The purpose of this function is to be called in the initialization code of your list, to set certain members correctly.
This is particularly important when you want your list to support CX_STORE_POINTERS as elem_size
. This function will wrap the list class accordingly and make sure that you can implement your list as if it was only storing objects and the wrapper will automatically enable the feature of storing pointers.
list | the list to initialize |
cl | the list class |
allocator | the allocator for the elements |
comparator | a compare function for the elements |
elem_size | the size of one element |
|
inlinestatic |
Adds an item to the end of the list.
list | the list |
elem | a pointer to the element to add |
zero | success |
non-zero | memory allocation failure |
|
inlinestatic |
Adds multiple items to the end of the list.
This method is more efficient than invoking cxListAdd() multiple times.
If there is not enough memory to add all elements, the returned value is less than n
.
If this list is storing pointers instead of objects array
is expected to be an array of pointers.
list | the list |
array | a pointer to the elements to add |
n | the number of elements to add |
|
inlinestatic |
Returns a pointer to the element at the specified index.
list | the list |
index | the index of the element |
NULL
if the index is out of bounds
|
inlinestatic |
Returns a backwards iterator pointing to the last item of the list.
The returned iterator is position-aware.
If the list is empty, a past-the-end iterator will be returned.
list | the list |
|
inlinestatic |
Returns a backwards iterator pointing to the item at the specified index.
The returned iterator is position-aware.
If the index is out of range, a past-the-end iterator will be returned.
list | the list |
index | the index where the iterator shall point at |
|
inlinestatic |
Removes all elements from this list.
If element destructor functions are specified, they are called for each element before removing them.
list | the list |
Compares a list to another list of the same type.
First, the list sizes are compared. If they match, the lists are compared element-wise.
list | the list |
other | the list to compare to |
zero | both lists are equal element wise |
negative | the first list is smaller or the first non-equal element in the first list is smaller |
positive | the first list is larger or the first non-equal element in the first list is larger |
|
inlinestatic |
Returns the index of the first element that equals elem
.
Determining equality is performed by the list's comparator function.
list | the list |
elem | the element to find |
|
inlinestatic |
Removes and returns the index of the first element that equals elem
.
Determining equality is performed by the list's comparator function.
list | the list |
elem | the element to find and remove |
void cxListFree | ( | CxList * | list | ) |
Deallocates the memory of the specified list structure.
Also calls the content destructor functions for each element, if specified.
list | the list which shall be freed |
|
inlinestatic |
Checks if the specified index is within bounds.
list | the list |
index | the index |
true | if the index is within bounds |
false | if the index is out of bounds |
|
inlinestatic |
Inserts an item at the specified index.
If index
equals the list size
, this is effectively cxListAdd().
list | the list |
index | the index the element shall have |
elem | a pointer to the element to add |
zero | success |
non-zero | memory allocation failure or the index is out of bounds |
|
inlinestatic |
Inserts an element after the current location of the specified iterator.
The used iterator remains operational, but all other active iterators should be considered invalidated.
If iter
is not a list iterator, the behavior is undefined. If iter
is a past-the-end iterator, the new element gets appended to the list.
iter | an iterator |
elem | the element to insert |
zero | success |
non-zero | memory allocation failure |
|
inlinestatic |
Inserts multiple items to the list at the specified index.
If index
equals the list size, this is effectively cxListAddArray().
This method is usually more efficient than invoking cxListInsert() multiple times.
If there is not enough memory to add all elements, the returned value is less than n
.
If this list is storing pointers instead of objects array
is expected to be an array of pointers.
list | the list |
index | the index where to add the elements |
array | a pointer to the elements to add |
n | the number of elements to add |
|
inlinestatic |
Inserts an element before the current location of the specified iterator.
The used iterator remains operational, but all other active iterators should be considered invalidated.
If iter
is not a list iterator, the behavior is undefined. If iter
is a past-the-end iterator, the new element gets appended to the list.
iter | an iterator |
elem | the element to insert |
zero | success |
non-zero | memory allocation failure |
|
inlinestatic |
Inserts an item into a sorted list.
If the list is not sorted already, the behavior is undefined.
list | the list |
elem | a pointer to the element to add |
zero | success |
non-zero | memory allocation failure |
|
inlinestatic |
Inserts a sorted array into a sorted list.
This method is usually more efficient than inserting each element separately, because consecutive chunks of sorted data are inserted in one pass.
If there is not enough memory to add all elements, the returned value is less than n
.
If this list is storing pointers instead of objects array
is expected to be an array of pointers.
If the list is not sorted already, the behavior is undefined.
list | the list |
array | a pointer to the elements to add |
n | the number of elements to add |
|
inlinestatic |
Returns an iterator pointing to the first item of the list.
The returned iterator is position-aware.
If the list is empty, a past-the-end iterator will be returned.
list | the list |
|
inlinestatic |
Returns an iterator pointing to the item at the specified index.
The returned iterator is position-aware.
If the index is out of range, a past-the-end iterator will be returned.
list | the list |
index | the index where the iterator shall point at |
|
inlinestatic |
Returns a mutating backwards iterator pointing to the last item of the list.
The returned iterator is position-aware.
If the list is empty, a past-the-end iterator will be returned.
list | the list |
CxIterator cxListMutBackwardsIteratorAt | ( | CxList * | list, |
size_t | index ) |
Returns a mutating backwards iterator pointing to the item at the specified index.
The returned iterator is position-aware.
If the index is out of range, a past-the-end iterator will be returned.
list | the list |
index | the index where the iterator shall point at |
|
inlinestatic |
Returns a mutating iterator pointing to the first item of the list.
The returned iterator is position-aware.
If the list is empty, a past-the-end iterator will be returned.
list | the list |
CxIterator cxListMutIteratorAt | ( | CxList * | list, |
size_t | index ) |
Returns a mutating iterator pointing to the item at the specified index.
The returned iterator is position-aware.
If the index is out of range, a past-the-end iterator will be returned.
list | the list |
index | the index where the iterator shall point at |
|
inlinestatic |
Removes the element at the specified index.
If an element destructor function is specified, it is called before removing the element.
list | the list |
index | the index of the element |
zero | success |
non-zero | index out of bounds |
|
inlinestatic |
Removes and returns the element at the specified index.
No destructor is called and instead the element is copied to the targetbuf
which MUST be large enough to hold the removed element.
list | the list |
index | the index of the element |
targetbuf | a buffer where to copy the element |
zero | success |
non-zero | index out of bounds |
|
inlinestatic |
Removes multiple element starting at the specified index.
If an element destructor function is specified, it is called for each element. It is guaranteed that the destructor is called before removing the element, however, due to possible optimizations it is neither guaranteed that the destructors are invoked for all elements before starting to remove them, nor that the element is removed immediately after the destructor call before proceeding to the next element.
list | the list |
index | the index of the element |
num | the number of elements to remove |
|
inlinestatic |
Removes and returns multiple element starting at the specified index.
No destructor is called and instead the elements are copied to the targetbuf
which MUST be large enough to hold all removed elements.
list | the list |
index | the index of the element |
num | the number of elements to remove |
targetbuf | a buffer where to copy the elements |
|
inlinestatic |
Reverses the order of the items.
list | the list |
|
inlinestatic |
Returns the number of elements currently stored in the list.
list | the list |
|
inlinestatic |
Sorts the list.
list | the list |
|
inlinestatic |
Swaps two items in the list.
Implementations should only allocate temporary memory for the swap, if it is necessary.
list | the list |
i | the index of the first element |
j | the index of the second element |
zero | success |
non-zero | one of the indices is out of bounds or the swap needed extra memory but allocation failed |
|
extern |
A shared instance of an empty list.
Writing to that list is not allowed.
You can use this is a placeholder for initializing CxList pointers for which you do not want to reserve memory right from the beginning.