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 */
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
85 struct cx_list_s *list,
86 size_t index,
87 const void *data
88 );
89
95 size_t (*insert_array)(
96 struct cx_list_s *list,
97 size_t index,
98 const void *data,
99 size_t n
100 );
101
107 size_t (*insert_sorted)(
108 struct cx_list_s *list,
109 const void *sorted_data,
110 size_t n
111 );
112
117 struct cx_iterator_s *iter,
118 const void *elem,
119 int prepend
120 );
121
132 size_t (*remove)(
133 struct cx_list_s *list,
134 size_t index,
135 size_t num,
136 void *targetbuf
137 );
138
142 void (*clear)(struct cx_list_s *list);
143
149 int (*swap)(
150 struct cx_list_s *list,
151 size_t i,
152 size_t j
153 );
154
158 void *(*at)(
159 const struct cx_list_s *list,
160 size_t index
161 );
162
166 size_t (*find_remove)(
167 struct cx_list_s *list,
168 const void *elem,
169 bool remove
170 );
171
177 void (*sort)(struct cx_list_s *list);
178
185 int (*compare)(
186 const struct cx_list_s *list,
187 const struct cx_list_s *other
188 );
189
193 void (*reverse)(struct cx_list_s *list);
194
198 struct cx_iterator_s (*iterator)(
199 const struct cx_list_s *list,
200 size_t index,
201 bool backward
202 );
203};
204
222 struct cx_list_s *list,
223 size_t index,
224 const void *data,
225 size_t n
226);
227
247 struct cx_list_s *list,
248 const void *sorted_data,
249 size_t n
250);
251
266
282int cx_list_default_swap(struct cx_list_s *list, size_t i, size_t j);
283
328cx_attr_nonnull_arg(1, 2, 3)
331 struct cx_list_s *list,
332 struct cx_list_class_s *cl,
333 const struct cx_allocator_s *allocator,
334 cx_compare_func comparator,
335 size_t elem_size
336);
337
341typedef struct cx_list_s CxList;
342
350static inline size_t cxListSize(const CxList *list) {
351 return list->collection.size;
352}
353
364static inline int cxListAdd(
365 CxList *list,
366 const void *elem
367) {
368 list->collection.sorted = false;
369 return list->cl->insert_element(list, list->collection.size, elem);
370}
371
389static inline size_t cxListAddArray(
390 CxList *list,
391 const void *array,
392 size_t n
393) {
394 list->collection.sorted = false;
395 return list->cl->insert_array(list, list->collection.size, array, n);
396}
397
412static inline int cxListInsert(
413 CxList *list,
414 size_t index,
415 const void *elem
416) {
417 list->collection.sorted = false;
418 return list->cl->insert_element(list, index, elem);
419}
420
432static inline int cxListInsertSorted(
433 CxList *list,
434 const void *elem
435) {
436 list->collection.sorted = true; // guaranteed by definition
437 const void *data = list->collection.store_pointer ? &elem : elem;
438 return list->cl->insert_sorted(list, data, 1) == 0;
439}
440
461static inline size_t cxListInsertArray(
462 CxList *list,
463 size_t index,
464 const void *array,
465 size_t n
466) {
467 list->collection.sorted = false;
468 return list->cl->insert_array(list, index, array, n);
469}
470
491static inline size_t cxListInsertSortedArray(
492 CxList *list,
493 const void *array,
494 size_t n
495) {
496 list->collection.sorted = true; // guaranteed by definition
497 return list->cl->insert_sorted(list, array, n);
498}
499
517static inline int cxListInsertAfter(
518 CxIterator *iter,
519 const void *elem
520) {
521 CxList* list = (CxList*)iter->src_handle.m;
522 list->collection.sorted = false;
523 return list->cl->insert_iter(iter, elem, 0);
524}
525
543static inline int cxListInsertBefore(
544 CxIterator *iter,
545 const void *elem
546) {
547 CxList* list = (CxList*)iter->src_handle.m;
548 list->collection.sorted = false;
549 return list->cl->insert_iter(iter, elem, 1);
550}
551
564static inline int cxListRemove(
565 CxList *list,
566 size_t index
567) {
568 return list->cl->remove(list, index, 1, NULL) == 0;
569}
570
585static inline int cxListRemoveAndGet(
586 CxList *list,
587 size_t index,
588 void *targetbuf
589) {
590 return list->cl->remove(list, index, 1, targetbuf) == 0;
591}
592
609static inline size_t cxListRemoveArray(
610 CxList *list,
611 size_t index,
612 size_t num
613) {
614 return list->cl->remove(list, index, num, NULL);
615}
616
631static inline size_t cxListRemoveArrayAndGet(
632 CxList *list,
633 size_t index,
634 size_t num,
635 void *targetbuf
636) {
637 return list->cl->remove(list, index, num, targetbuf);
638}
639
649static inline void cxListClear(CxList *list) {
650 list->collection.sorted = true; // empty lists are always sorted
651 list->cl->clear(list);
652}
653
668static inline int cxListSwap(
669 CxList *list,
670 size_t i,
671 size_t j
672) {
673 list->collection.sorted = false;
674 return list->cl->swap(list, i, j);
675}
676
685static inline void *cxListAt(
686 const CxList *list,
687 size_t index
688) {
689 return list->cl->at(list, index);
690}
691
706 const CxList *list,
707 size_t index
708) {
709 return list->cl->iterator(list, index, false);
710}
711
726 const CxList *list,
727 size_t index
728) {
729 return list->cl->iterator(list, index, true);
730}
731
747 CxList *list,
748 size_t index
749);
750
767 CxList *list,
768 size_t index
769);
770
783static inline CxIterator cxListIterator(const CxList *list) {
784 return list->cl->iterator(list, 0, false);
785}
786
799static inline CxIterator cxListMutIterator(CxList *list) {
800 return cxListMutIteratorAt(list, 0);
801}
802
803
816static inline CxIterator cxListBackwardsIterator(const CxList *list) {
817 return list->cl->iterator(list, list->collection.size - 1, true);
818}
819
833 return cxListMutBackwardsIteratorAt(list, list->collection.size - 1);
834}
835
848static inline size_t cxListFind(
849 const CxList *list,
850 const void *elem
851) {
852 return list->cl->find_remove((CxList*)list, elem, false);
853}
854
865static inline bool cxListIndexValid(const CxList *list, size_t index) {
866 return index < list->collection.size;
867}
868
881static inline size_t cxListFindRemove(
882 CxList *list,
883 const void *elem
884) {
885 return list->cl->find_remove(list, elem, true);
886}
887
896static inline void cxListSort(CxList *list) {
897 if (list->collection.sorted) return;
898 list->cl->sort(list);
899 list->collection.sorted = true;
900}
901
908static inline void cxListReverse(CxList *list) {
909 // still sorted, but not according to the cmp_func
910 list->collection.sorted = false;
911 list->cl->reverse(list);
912}
913
932 const CxList *list,
933 const CxList *other
934);
935
944void cxListFree(CxList *list);
945
955extern CxList *const cxEmptyList;
956
957
958#ifdef __cplusplus
959} // extern "C"
960#endif
961
962#endif // UCX_LIST_H
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_export
Only used for building Windows DLLs.
Definition common.h:285
#define cx_attr_nonnull
All pointer arguments must be non-NULL.
Definition common.h:136
#define cx_attr_nodiscard
Warn about discarded return value.
Definition common.h:265
#define cx_attr_access_w(...)
Specifies that the function will only write through the given pointer.
Definition common.h:241
#define cx_attr_nonnull_arg(...)
The specified pointer arguments must be non-NULL.
Definition common.h:141
int(* cx_compare_func)(const void *left, const void *right)
A comparator function comparing two arbitrary values.
Definition compare.h:60
CxList *const cxEmptyList
A shared instance of an empty list.
static CxIterator cxListIteratorAt(const CxList *list, size_t index)
Returns an iterator pointing to the item at the specified index.
Definition list.h:705
static int cxListRemoveAndGet(CxList *list, size_t index, void *targetbuf)
Removes and returns the element at the specified index.
Definition list.h:585
static CxIterator cxListMutBackwardsIterator(CxList *list)
Returns a mutating backwards iterator pointing to the last item of the list.
Definition list.h:832
static size_t cxListSize(const CxList *list)
Returns the number of elements currently stored in the list.
Definition list.h:350
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.
static size_t cxListInsertSortedArray(CxList *list, const void *array, size_t n)
Inserts a sorted array into a sorted list.
Definition list.h:491
static int cxListInsert(CxList *list, size_t index, const void *elem)
Inserts an item at the specified index.
Definition list.h:412
static void cxListReverse(CxList *list)
Reverses the order of the items.
Definition list.h:908
static CxIterator cxListBackwardsIterator(const CxList *list)
Returns a backwards iterator pointing to the last item of the list.
Definition list.h:816
static int cxListInsertSorted(CxList *list, const void *elem)
Inserts an item into a sorted list.
Definition list.h:432
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.
int cxListCompare(const CxList *list, const CxList *other)
Compares a list to another list of the same type.
static size_t cxListRemoveArrayAndGet(CxList *list, size_t index, size_t num, void *targetbuf)
Removes and returns multiple element starting at the specified index.
Definition list.h:631
static int cxListInsertAfter(CxIterator *iter, const void *elem)
Inserts an element after the current location of the specified iterator.
Definition list.h:517
static CxIterator cxListMutIterator(CxList *list)
Returns a mutating iterator pointing to the first item of the list.
Definition list.h:799
static int cxListSwap(CxList *list, size_t i, size_t j)
Swaps two items in the list.
Definition list.h:668
CxIterator cxListMutIteratorAt(CxList *list, size_t index)
Returns a mutating iterator pointing to the item at the specified index.
static void cxListSort(CxList *list)
Sorts the list.
Definition list.h:896
static size_t cxListAddArray(CxList *list, const void *array, size_t n)
Adds multiple items to the end of the list.
Definition list.h:389
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 int cxListInsertBefore(CxIterator *iter, const void *elem)
Inserts an element before the current location of the specified iterator.
Definition list.h:543
static bool cxListIndexValid(const CxList *list, size_t index)
Checks if the specified index is within bounds.
Definition list.h:865
static size_t cxListFindRemove(CxList *list, const void *elem)
Removes and returns the index of the first element that equals elem.
Definition list.h:881
static int cxListAdd(CxList *list, const void *elem)
Adds an item to the end of the list.
Definition list.h:364
void cx_list_default_sort(struct cx_list_s *list)
Default unoptimized sort implementation.
void cxListFree(CxList *list)
Deallocates the memory of the specified list structure.
static size_t cxListFind(const CxList *list, const void *elem)
Returns the index of the first element that equals elem.
Definition list.h:848
static CxIterator cxListIterator(const CxList *list)
Returns an iterator pointing to the first item of the list.
Definition list.h:783
static size_t cxListRemoveArray(CxList *list, size_t index, size_t num)
Removes multiple element starting at the specified index.
Definition list.h:609
int cx_list_default_swap(struct cx_list_s *list, size_t i, size_t j)
Default unoptimized swap implementation.
static CxIterator cxListBackwardsIteratorAt(const CxList *list, size_t index)
Returns a backwards iterator pointing to the item at the specified index.
Definition list.h:725
static void * cxListAt(const CxList *list, size_t index)
Returns a pointer to the element at the specified index.
Definition list.h:685
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.
Definition list.h:461
static int cxListRemove(CxList *list, size_t index)
Removes the element at the specified index.
Definition list.h:564
CxIterator cxListMutBackwardsIteratorAt(CxList *list, size_t index)
Returns a mutating backwards iterator pointing to the item at the specified index.
static void cxListClear(CxList *list)
Removes all elements from this list.
Definition list.h:649
Structure holding the data for an allocator.
Definition allocator.h:84
bool sorted
Indicates if this collection is guaranteed to be sorted.
Definition collection.h:99
bool store_pointer
Indicates if this list is supposed to store pointers instead of copies of the actual objects.
Definition collection.h:94
size_t size
The number of currently stored elements.
Definition collection.h:71
Internal iterator struct - use CxIterator.
Definition iterator.h:97
void * m
Access for mutating iterators.
Definition iterator.h:115
size_t index
If the iterator is position-aware, contains the index of the element in the underlying collection.
Definition iterator.h:126
union cx_iterator_s::@2 src_handle
Handle for the source collection, if any.
size_t elem_size
The size of an individual element.
Definition iterator.h:131
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:177
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:166
int(* swap)(struct cx_list_s *list, size_t i, size_t j)
Member function for swapping two elements.
Definition list.h:149
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:107
void(* clear)(struct cx_list_s *list)
Member function for removing all elements.
Definition list.h:142
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:185
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:198
int(* insert_element)(struct cx_list_s *list, size_t index, const void *data)
Member function for inserting a single element.
Definition list.h:84
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:132
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:95
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:116
void(* reverse)(struct cx_list_s *list)
Member function for reversing the order of the items.
Definition list.h:193
void *(* at)(const struct cx_list_s *list, size_t index)
Member function for element lookup.
Definition list.h:158
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
struct cx_collection_s collection
Common members for collections.
Definition list.h:58
const cx_list_class * cl
The list class definition.
Definition list.h:62