ucx
UAP Common Extensions
Loading...
Searching...
No Matches
list.h
Go to the documentation of this file.
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
35
36#ifndef UCX_LIST_H
37#define UCX_LIST_H
38
39#include "common.h"
40#include "collection.h"
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
50
68
79 void (*deallocate)(struct cx_list_s *list);
80
86 void *(*insert_element)(struct cx_list_s *list, size_t index, const void *data);
87
96 size_t (*insert_array)(struct cx_list_s *list, size_t index, const void *data, size_t n);
97
104 size_t (*insert_sorted)(struct cx_list_s *list, const void *sorted_data, size_t n);
105
112 size_t (*insert_unique)(struct cx_list_s *list, const void *sorted_data, size_t n);
113
117 int (*insert_iter)(struct cx_iterator_s *iter, const void *elem, int prepend);
118
129 size_t (*remove)(struct cx_list_s *list, size_t index, size_t num, void *targetbuf);
130
134 void (*clear)(struct cx_list_s *list);
135
141 int (*swap)(struct cx_list_s *list, size_t i, size_t j);
142
146 void *(*at)(const struct cx_list_s *list, size_t index);
147
151 size_t (*find_remove)(struct cx_list_s *list, const void *elem, bool remove);
152
158 void (*sort)(struct cx_list_s *list);
159
165 int (*compare)(const struct cx_list_s *list, const struct cx_list_s *other);
166
170 void (*reverse)(struct cx_list_s *list);
171
176 int (*change_capacity)(struct cx_list_s *list, size_t new_capacity);
177
181 struct cx_iterator_s (*iterator)(const struct cx_list_s *list, size_t index, bool backward);
182};
183
187typedef struct cx_list_s CxList;
188
197CX_EXPORT extern CxList *const cxEmptyList;
198
215 size_t index, const void *data, size_t n);
216
235 const void *sorted_data, size_t n);
236
255 const void *sorted_data, size_t n);
256
270
285CX_EXPORT int cx_list_default_swap(struct cx_list_s *list, size_t i, size_t j);
286
331cx_attr_nonnull_arg(1, 2, 3)
333 struct cx_list_class_s *cl, const struct cx_allocator_s *allocator,
334 cx_compare_func comparator, size_t elem_size);
335
343CX_EXPORT size_t cxListSize(const CxList *list);
344
356CX_EXPORT int cxListAdd(CxList *list, const void *elem);
357
376CX_EXPORT size_t cxListAddArray(CxList *list, const void *array, size_t n);
377
393CX_EXPORT int cxListInsert(CxList *list, size_t index, const void *elem);
394
408CX_EXPORT void *cxListEmplaceAt(CxList *list, size_t index);
409
422
441CX_EXPORT CxIterator cxListEmplaceArrayAt(CxList *list, size_t index, size_t n);
442
461
473CX_EXPORT int cxListInsertSorted(CxList *list, const void *elem);
474
489CX_EXPORT int cxListInsertUnique(CxList *list, const void *elem);
490
512CX_EXPORT size_t cxListInsertArray(CxList *list, size_t index, const void *array, size_t n);
513
534CX_EXPORT size_t cxListInsertSortedArray(CxList *list, const void *array, size_t n);
535
569CX_EXPORT size_t cxListInsertUniqueArray(CxList *list, const void *array, size_t n);
570
588CX_EXPORT int cxListInsertAfter(CxIterator *iter, const void *elem);
589
607CX_EXPORT int cxListInsertBefore(CxIterator *iter, const void *elem);
608
621CX_EXPORT int cxListRemove(CxList *list, size_t index);
622
637CX_EXPORT int cxListRemoveAndGet(CxList *list, size_t index, void *targetbuf);
638
654CX_EXPORT int cxListRemoveAndGetFirst(CxList *list, void *targetbuf);
655
672#define cxListPopFront(list, targetbuf) cxListRemoveAndGetFirst((list), (targetbuf))
673
674
688CX_EXPORT int cxListRemoveAndGetLast(CxList *list, void *targetbuf);
689
706#define cxListPop(list, targetbuf) cxListRemoveAndGetLast((list), (targetbuf))
707
724CX_EXPORT size_t cxListRemoveArray(CxList *list, size_t index, size_t num);
725
740CX_EXPORT size_t cxListRemoveArrayAndGet(CxList *list, size_t index, size_t num, void *targetbuf);
741
752
767CX_EXPORT int cxListSwap(CxList *list, size_t i, size_t j);
768
779CX_EXPORT void *cxListAt(const CxList *list, size_t index);
780
790CX_EXPORT void *cxListFirst(const CxList *list);
791
801CX_EXPORT void *cxListLast(const CxList *list);
802
816CX_EXPORT int cxListSet(CxList *list, size_t index, const void *elem);
817
830CX_EXPORT CxIterator cxListIteratorAt(const CxList *list, size_t index);
831
845
858
871
884CX_EXPORT size_t cxListFind(const CxList *list, const void *elem);
885
898CX_EXPORT bool cxListContains(const CxList* list, const void* elem);
899
909CX_EXPORT bool cxListIndexValid(const CxList *list, size_t index);
910
923CX_EXPORT size_t cxListFindRemove(CxList *list, const void *elem);
924
934
942
958CX_EXPORT int cxListCompare(const CxList *list, const CxList *other);
959
968
969
989cx_attr_nonnull_arg(1, 2, 3)
990CX_EXPORT int cxListClone(CxList *dst, const CxList *src,
991 cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data);
992
1012cx_attr_nonnull_arg(1, 2, 3, 4)
1014 const CxList *minuend, const CxList *subtrahend,
1015 cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data);
1016
1036cx_attr_nonnull_arg(1, 2, 3, 4)
1037CX_EXPORT int cxListIntersection(CxList *dst, const CxList *src, const CxList *other,
1038 cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data);
1039
1061cx_attr_nonnull_arg(1, 2, 3, 4)
1062CX_EXPORT int cxListUnion(CxList *dst, const CxList *src, const CxList *other,
1063 cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data);
1064
1086
1108 const CxList *minuend, const CxList *subtrahend);
1109
1130CX_EXPORT int cxListIntersectionSimple(CxList *dst, const CxList *src, const CxList *other);
1131
1154CX_EXPORT int cxListUnionSimple(CxList *dst, const CxList *src, const CxList *other);
1155
1174CX_EXPORT int cxListReserve(CxList *list, size_t capacity);
1175
1189
1190#ifdef __cplusplus
1191} // extern "C"
1192#endif
1193
1194#endif // UCX_LIST_H
struct cx_allocator_s CxAllocator
High-Level type alias for the allocator type.
Definition allocator.h:84
void * cx_clone_func(void *target, const void *source, const CxAllocator *allocator, void *data)
Function pointer type for clone functions.
Definition allocator.h:145
Common definitions for various collection implementations.
#define CX_COLLECTION_BASE
Use this macro to declare common members for a collection structure.
Definition collection.h:113
Common definitions and feature checks.
#define cx_attr_nonnull
All pointer arguments must be non-NULL.
Definition common.h:141
#define CX_EXPORT
Only used for building Windows DLLs.
Definition common.h:278
#define cx_attr_nodiscard
Warn about discarded return value.
Definition common.h:256
#define cx_attr_access_w(...)
Specifies that the function will only write through the given pointer.
Definition common.h:246
#define cx_attr_nonnull_arg(...)
The specified pointer arguments must be non-NULL.
Definition common.h:146
int(* cx_compare_func)(const void *left, const void *right)
A comparator function comparing two arbitrary values.
Definition compare.h:57
struct cx_iterator_s CxIterator
Iterator type.
Definition iterator.h:148
CxList *const cxEmptyList
A shared instance of an empty list.
bool cxListContains(const CxList *list, const void *elem)
Checks if the list contains the specified element.
int cxListIntersection(CxList *dst, const CxList *src, const CxList *other, cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data)
Clones elements from a list only if they are also present in another list.
int cxListSet(CxList *list, size_t index, const void *elem)
Sets the element at the specified index in the list.
struct cx_list_s CxList
Common type for all list implementations.
Definition list.h:187
int cxListDifferenceSimple(CxList *dst, const CxList *minuend, const CxList *subtrahend)
Clones elements from a list only if they are not present in another list.
int cxListCloneSimple(CxList *dst, const CxList *src)
Performs a shallow clone of one list into another.
CxIterator cxListBackwardsIterator(const CxList *list)
Returns a backwards iterator pointing to the last item of the list.
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 cxListSort(CxList *list)
Sorts the list.
size_t cxListInsertSortedArray(CxList *list, const void *array, size_t n)
Inserts a sorted array into a sorted list.
CxIterator cxListIteratorAt(const CxList *list, size_t index)
Returns an iterator pointing to the item at the specified index.
CxIterator cxListBackwardsIteratorAt(const CxList *list, size_t index)
Returns a backwards iterator pointing to the item at the specified index.
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 cxListSize(const CxList *list)
Returns the number of elements currently stored in the list.
int cxListRemoveAndGetLast(CxList *list, void *targetbuf)
Removes and returns the last element of the list.
void * cxListEmplace(CxList *list)
Allocates memory for an element at the end of the list and returns a pointer to that memory.
int cxListCompare(const CxList *list, const CxList *other)
Compares a list to another list of the same type.
int cxListInsertAfter(CxIterator *iter, const void *elem)
Inserts an element after the current location of the specified iterator.
size_t cxListInsertArray(CxList *list, size_t index, const void *array, size_t n)
Inserts multiple items to the list at the specified index.
CxIterator cxListEmplaceArrayAt(CxList *list, size_t index, size_t n)
Allocates memory for multiple elements and returns an iterator.
size_t cxListFind(const CxList *list, const void *elem)
Returns the index of the first element that equals elem.
size_t cxListRemoveArray(CxList *list, size_t index, size_t num)
Removes multiple element starting at the specified index.
int cxListIntersectionSimple(CxList *dst, const CxList *src, const CxList *other)
Clones elements from a list only if they are also present in another list.
size_t cxListAddArray(CxList *list, const void *array, size_t n)
Adds multiple items to the end of the list.
int cxListRemove(CxList *list, size_t index)
Removes the element at the specified index.
int cxListUnionSimple(CxList *dst, const CxList *src, const CxList *other)
Performs a deep clone of one list into another, skipping duplicates.
int cxListReserve(CxList *list, size_t capacity)
Asks the list to reserve enough memory for a given total number of elements.
struct cx_list_class_s cx_list_class
List class type.
Definition list.h:49
size_t cxListFindRemove(CxList *list, const void *elem)
Removes and returns the index of the first element that equals elem.
int cxListShrink(CxList *list)
Advises the list to free any overallocated memory.
CxIterator cxListEmplaceArray(CxList *list, size_t n)
Allocates memory for multiple elements and returns an iterator.
void cxListReverse(CxList *list)
Reverses the order of the items.
void * cxListEmplaceAt(CxList *list, size_t index)
Allocates memory for an element at the specified index and returns a pointer to that memory.
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.
int cxListClone(CxList *dst, const CxList *src, cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data)
Performs a deep clone of one list into another.
int cxListInsert(CxList *list, size_t index, const void *elem)
Inserts an item at the specified index.
void * cxListAt(const CxList *list, size_t index)
Returns a pointer to the element at the specified index.
void cx_list_default_sort(struct cx_list_s *list)
Default unoptimized sort implementation.
int cxListRemoveAndGet(CxList *list, size_t index, void *targetbuf)
Removes and returns the element at the specified index.
int cxListUnion(CxList *dst, const CxList *src, const CxList *other, cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data)
Performs a deep clone of one list into another, skipping duplicates.
void cxListFree(CxList *list)
Deallocates the memory of the specified list structure.
int cxListInsertBefore(CxIterator *iter, const void *elem)
Inserts an element before the current location of the specified iterator.
int cxListAdd(CxList *list, const void *elem)
Adds an item to the end of the list.
int cx_list_default_swap(struct cx_list_s *list, size_t i, size_t j)
Default unoptimized swap implementation.
CxIterator cxListIterator(const CxList *list)
Returns an iterator pointing to the first item of the list.
int cxListDifference(CxList *dst, const CxList *minuend, const CxList *subtrahend, cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data)
Clones elements from a list only if they are not present in another list.
bool cxListIndexValid(const CxList *list, size_t index)
Checks if the specified index is within bounds.
size_t cx_list_default_insert_unique(struct cx_list_s *list, const void *sorted_data, size_t n)
Default implementation of an array insert where only elements are inserted when they don't exist in t...
int cxListInsertUnique(CxList *list, const void *elem)
Inserts an item into a list if it does not exist.
int cxListRemoveAndGetFirst(CxList *list, void *targetbuf)
Removes and returns the first element of the list.
size_t cxListRemoveArrayAndGet(CxList *list, size_t index, size_t num, void *targetbuf)
Removes and returns multiple elements starting at the specified index.
size_t cxListInsertUniqueArray(CxList *list, const void *array, size_t n)
Inserts an array into a list, skipping duplicates.
int cxListInsertSorted(CxList *list, const void *elem)
Inserts an item into a sorted list.
void * cxListLast(const CxList *list)
Returns a pointer to the last element.
void cxListClear(CxList *list)
Removes all elements from this list.
void * cxListFirst(const CxList *list)
Returns a pointer to the first element.
int cxListSwap(CxList *list, size_t i, size_t j)
Swaps two items in the list.
Structure holding the data for an allocator.
Definition allocator.h:70
Internal iterator struct - use CxIterator.
Definition iterator.h:103
The class definition for arbitrary lists.
Definition list.h:72
void(* sort)(struct cx_list_s *list)
Member function for sorting the list.
Definition list.h:158
size_t(* find_remove)(struct cx_list_s *list, const void *elem, bool remove)
Member function for finding and optionally removing an element.
Definition list.h:151
int(* swap)(struct cx_list_s *list, size_t i, size_t j)
Member function for swapping two elements.
Definition list.h:141
size_t(* insert_sorted)(struct cx_list_s *list, const void *sorted_data, size_t n)
Member function for inserting sorted elements into a sorted list.
Definition list.h:104
void(* clear)(struct cx_list_s *list)
Member function for removing all elements.
Definition list.h:134
int(* compare)(const struct cx_list_s *list, const struct cx_list_s *other)
Optional member function for comparing this list to another list of the same type.
Definition list.h:165
struct cx_iterator_s(* iterator)(const struct cx_list_s *list, size_t index, bool backward)
Member function for returning an iterator pointing to the specified index.
Definition list.h:181
int(* change_capacity)(struct cx_list_s *list, size_t new_capacity)
Optional member function for changing the capacity.
Definition list.h:176
void(* deallocate)(struct cx_list_s *list)
Destructor function.
Definition list.h:79
size_t(* remove)(struct cx_list_s *list, size_t index, size_t num, void *targetbuf)
Member function for removing elements.
Definition list.h:129
size_t(* insert_array)(struct cx_list_s *list, size_t index, const void *data, size_t n)
Member function for inserting multiple elements.
Definition list.h:96
size_t(* insert_unique)(struct cx_list_s *list, const void *sorted_data, size_t n)
Member function for inserting multiple elements if they do not exist.
Definition list.h:112
int(* insert_iter)(struct cx_iterator_s *iter, const void *elem, int prepend)
Member function for inserting an element relative to an iterator position.
Definition list.h:117
void(* reverse)(struct cx_list_s *list)
Member function for reversing the order of the items.
Definition list.h:170
Structure for holding the base data of a list.
Definition list.h:54
const cx_list_class * climpl
The actual implementation in case the list class is delegating.
Definition list.h:66
const cx_list_class * cl
The list class definition.
Definition list.h:62