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 */
37#ifndef UCX_LIST_H
38#define UCX_LIST_H
39
40#include "common.h"
41#include "collection.h"
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
51
55struct cx_list_s {
65};
66
77 void (*destructor)(struct cx_list_s *list);
78
84 struct cx_list_s *list,
85 size_t index,
86 void const *data
87 );
88
93 size_t (*insert_array)(
94 struct cx_list_s *list,
95 size_t index,
96 void const *data,
97 size_t n
98 );
99
104 struct cx_mut_iterator_s *iter,
105 void const *elem,
106 int prepend
107 );
108
112 int (*remove)(
113 struct cx_list_s *list,
114 size_t index
115 );
116
120 void (*clear)(struct cx_list_s *list);
121
125 int (*swap)(
126 struct cx_list_s *list,
127 size_t i,
128 size_t j
129 );
130
134 void *(*at)(
135 struct cx_list_s const *list,
136 size_t index
137 );
138
142 ssize_t (*find)(
143 struct cx_list_s const *list,
144 void const *elem
145 );
146
150 void (*sort)(struct cx_list_s *list);
151
155 int (*compare)(
156 struct cx_list_s const *list,
157 struct cx_list_s const *other
158 );
159
163 void (*reverse)(struct cx_list_s *list);
164
168 struct cx_iterator_s (*iterator)(
169 struct cx_list_s const *list,
170 size_t index,
171 bool backward
172 );
173};
174
178typedef struct cx_list_s CxList;
179
189__attribute__((__nonnull__))
191
204__attribute__((__nonnull__))
206
214__attribute__((__nonnull__))
215static inline bool cxListIsStoringPointers(CxList const *list) {
216 return list->store_pointer;
217}
218
225__attribute__((__nonnull__))
226static inline size_t cxListSize(CxList const *list) {
227 return list->size;
228}
229
238__attribute__((__nonnull__))
239static inline int cxListAdd(
240 CxList *list,
241 void const *elem
242) {
243 return list->cl->insert_element(list, list->size, elem);
244}
245
262__attribute__((__nonnull__))
263static inline size_t cxListAddArray(
264 CxList *list,
265 void const *array,
266 size_t n
267) {
268 return list->cl->insert_array(list, list->size, array, n);
269}
270
284__attribute__((__nonnull__))
285static inline int cxListInsert(
286 CxList *list,
287 size_t index,
288 void const *elem
289) {
290 return list->cl->insert_element(list, index, elem);
291}
292
312__attribute__((__nonnull__))
313static inline size_t cxListInsertArray(
314 CxList *list,
315 size_t index,
316 void const *array,
317 size_t n
318) {
319 return list->cl->insert_array(list, index, array, n);
320}
321
337__attribute__((__nonnull__))
338static inline int cxListInsertAfter(
339 CxMutIterator *iter,
340 void const *elem
341) {
342 return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 0);
343}
344
360__attribute__((__nonnull__))
361static inline int cxListInsertBefore(
362 CxMutIterator *iter,
363 void const *elem
364) {
365 return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 1);
366}
367
378__attribute__((__nonnull__))
379static inline int cxListRemove(
380 CxList *list,
381 size_t index
382) {
383 return list->cl->remove(list, index);
384}
385
394__attribute__((__nonnull__))
395static inline void cxListClear(CxList *list) {
396 list->cl->clear(list);
397}
398
410__attribute__((__nonnull__))
411static inline int cxListSwap(
412 CxList *list,
413 size_t i,
414 size_t j
415) {
416 return list->cl->swap(list, i, j);
417}
418
426__attribute__((__nonnull__))
427static inline void *cxListAt(
428 CxList *list,
429 size_t index
430) {
431 return list->cl->at(list, index);
432}
433
445__attribute__((__nonnull__, __warn_unused_result__))
447 CxList const *list,
448 size_t index
449) {
450 return list->cl->iterator(list, index, false);
451}
452
464__attribute__((__nonnull__, __warn_unused_result__))
466 CxList const *list,
467 size_t index
468) {
469 return list->cl->iterator(list, index, true);
470}
471
483__attribute__((__nonnull__, __warn_unused_result__))
485 CxList *list,
486 size_t index
487);
488
501__attribute__((__nonnull__, __warn_unused_result__))
503 CxList *list,
504 size_t index
505);
506
517__attribute__((__nonnull__, __warn_unused_result__))
518static inline CxIterator cxListIterator(CxList const *list) {
519 return list->cl->iterator(list, 0, false);
520}
521
532__attribute__((__nonnull__, __warn_unused_result__))
534 return cxListMutIteratorAt(list, 0);
535}
536
537
548__attribute__((__nonnull__, __warn_unused_result__))
549static inline CxIterator cxListBackwardsIterator(CxList const *list) {
550 return list->cl->iterator(list, list->size - 1, true);
551}
552
563__attribute__((__nonnull__, __warn_unused_result__))
565 return cxListMutBackwardsIteratorAt(list, list->size - 1);
566}
567
578__attribute__((__nonnull__))
579static inline ssize_t cxListFind(
580 CxList const *list,
581 void const *elem
582) {
583 return list->cl->find(list, elem);
584}
585
593__attribute__((__nonnull__))
594static inline void cxListSort(CxList *list) {
595 list->cl->sort(list);
596}
597
603__attribute__((__nonnull__))
604static inline void cxListReverse(CxList *list) {
605 list->cl->reverse(list);
606}
607
619__attribute__((__nonnull__))
621 CxList const *list,
622 CxList const *other
623);
624
635__attribute__((__nonnull__))
637
643extern CxList * const cxEmptyList;
644
645
646#ifdef __cplusplus
647} // extern "C"
648#endif
649
650#endif // UCX_LIST_H
Common definitions for various collection implementations.
#define CX_COLLECTION_MEMBERS
Use this macro to declare common members for a collection structure.
Definition: collection.h:63
Common definitions and feature checks.
#define __attribute__(x)
Removes GNU C attributes where they are not supported.
Definition: common.h:127
CxList *const cxEmptyList
A shared instance of an empty list.
static CxMutIterator cxListMutIterator(CxList *list)
Returns a mutating iterator pointing to the first item of the list.
Definition: list.h:533
static CxIterator cxListIterator(CxList const *list)
Returns an iterator pointing to the first item of the list.
Definition: list.h:518
static bool cxListIsStoringPointers(CxList const *list)
Returns true, if this list is storing pointers instead of the actual data.
Definition: list.h:215
static int cxListInsert(CxList *list, size_t index, void const *elem)
Inserts an item at the specified index.
Definition: list.h:285
static void cxListReverse(CxList *list)
Reverses the order of the items.
Definition: list.h:604
static size_t cxListInsertArray(CxList *list, size_t index, void const *array, size_t n)
Inserts multiple items to the list at the specified index.
Definition: list.h:313
void cxListStorePointers(CxList *list)
Advises the list to only store pointers to the objects.
static int cxListAdd(CxList *list, void const *elem)
Adds an item to the end of the list.
Definition: list.h:239
int cxListCompare(CxList const *list, CxList const *other)
Compares a list to another list of the same type.
static CxMutIterator cxListMutBackwardsIterator(CxList *list)
Returns a mutating backwards iterator pointing to the last item of the list.
Definition: list.h:564
static void * cxListAt(CxList *list, size_t index)
Returns a pointer to the element at the specified index.
Definition: list.h:427
void cxListStoreObjects(CxList *list)
Advises the list to store copies of the objects (default mode of operation).
static int cxListSwap(CxList *list, size_t i, size_t j)
Swaps two items in the list.
Definition: list.h:411
static size_t cxListSize(CxList const *list)
Returns the number of elements currently stored in the list.
Definition: list.h:226
static void cxListSort(CxList *list)
Sorts the list in-place.
Definition: list.h:594
static CxIterator cxListBackwardsIteratorAt(CxList const *list, size_t index)
Returns a backwards iterator pointing to the item at the specified index.
Definition: list.h:465
static ssize_t cxListFind(CxList const *list, void const *elem)
Returns the index of the first element that equals elem.
Definition: list.h:579
CxMutIterator cxListMutBackwardsIteratorAt(CxList *list, size_t index)
Returns a mutating backwards iterator pointing to the item at the specified index.
static CxIterator cxListBackwardsIterator(CxList const *list)
Returns a backwards iterator pointing to the last item of the list.
Definition: list.h:549
CxMutIterator cxListMutIteratorAt(CxList *list, size_t index)
Returns a mutating iterator pointing to the item at the specified index.
static int cxListRemove(CxList *list, size_t index)
Removes the element at the specified index.
Definition: list.h:379
static size_t cxListAddArray(CxList *list, void const *array, size_t n)
Adds multiple items to the end of the list.
Definition: list.h:263
static int cxListInsertAfter(CxMutIterator *iter, void const *elem)
Inserts an element after the current location of the specified iterator.
Definition: list.h:338
static CxIterator cxListIteratorAt(CxList const *list, size_t index)
Returns an iterator pointing to the item at the specified index.
Definition: list.h:446
void cxListDestroy(CxList *list)
Deallocates the memory of the specified list structure.
static int cxListInsertBefore(CxMutIterator *iter, void const *elem)
Inserts an element before the current location of the specified iterator.
Definition: list.h:361
static void cxListClear(CxList *list)
Removes all elements from this list.
Definition: list.h:395
Internal iterator struct - use CxIterator.
Definition: iterator.h:160
The class definition for arbitrary lists.
Definition: list.h:70
void(* sort)(struct cx_list_s *list)
Member function for sorting the list in-place.
Definition: list.h:150
ssize_t(* find)(struct cx_list_s const *list, void const *elem)
Member function for finding an element.
Definition: list.h:142
int(* swap)(struct cx_list_s *list, size_t i, size_t j)
Member function for swapping two elements.
Definition: list.h:125
void(* destructor)(struct cx_list_s *list)
Destructor function.
Definition: list.h:77
void(* clear)(struct cx_list_s *list)
Member function for removing all elements.
Definition: list.h:120
int(* remove)(struct cx_list_s *list, size_t index)
Member function for removing an element.
Definition: list.h:112
int(* compare)(struct cx_list_s const *list, struct cx_list_s const *other)
Member function for comparing this list to another list of the same type.
Definition: list.h:155
int(* insert_element)(struct cx_list_s *list, size_t index, void const *data)
Member function for inserting a single element.
Definition: list.h:83
struct cx_iterator_s(* iterator)(struct cx_list_s const *list, size_t index, bool backward)
Member function for returning an iterator pointing to the specified index.
Definition: list.h:168
void(* reverse)(struct cx_list_s *list)
Member function for reversing the order of the items.
Definition: list.h:163
size_t(* insert_array)(struct cx_list_s *list, size_t index, void const *data, size_t n)
Member function for inserting multiple elements.
Definition: list.h:93
int(* insert_iter)(struct cx_mut_iterator_s *iter, void const *elem, int prepend)
Member function for inserting an element relative to an iterator position.
Definition: list.h:103
Structure for holding the base data of a list.
Definition: list.h:55
CX_COLLECTION_MEMBERS cx_list_class const * cl
The list class definition.
Definition: list.h:60
cx_list_class const * climpl
The actual implementation in case the list class is delegating.
Definition: list.h:64
Internal iterator struct - use CxMutIterator.
Definition: iterator.h:96