Mon, 26 May 2025 21:06:17 +0200
update ucx
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | /** |
| 579 | 29 | * @file list.h |
| 30 | * @brief Interface for list implementations. | |
| 31 | * @author Mike Becker | |
| 32 | * @author Olaf Wintermann | |
| 33 | * @copyright 2-Clause BSD License | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
34 | */ |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
35 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
36 | #ifndef UCX_LIST_H |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
37 | #define UCX_LIST_H |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
38 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
39 | #include "common.h" |
| 490 | 40 | #include "collection.h" |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
41 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
42 | #ifdef __cplusplus |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
43 | extern "C" { |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
44 | #endif |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
45 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
46 | /** |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
47 | * List class type. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
48 | */ |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
49 | typedef struct cx_list_class_s cx_list_class; |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
50 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
51 | /** |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
52 | * Structure for holding the base data of a list. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
53 | */ |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
54 | struct cx_list_s { |
| 579 | 55 | /** |
| 56 | * Common members for collections. | |
| 57 | */ | |
| 58 | CX_COLLECTION_BASE; | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
59 | /** |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
60 | * The list class definition. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
61 | */ |
| 579 | 62 | const cx_list_class *cl; |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
63 | /** |
| 490 | 64 | * The actual implementation in case the list class is delegating. |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
65 | */ |
| 579 | 66 | const cx_list_class *climpl; |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
67 | }; |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
68 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
69 | /** |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
70 | * The class definition for arbitrary lists. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
71 | */ |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
72 | struct cx_list_class_s { |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
73 | /** |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
74 | * Destructor function. |
| 504 | 75 | * |
| 76 | * Implementations SHALL invoke the content destructor functions if provided | |
| 579 | 77 | * and SHALL deallocate the entire list memory. |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
78 | */ |
| 579 | 79 | void (*deallocate)(struct cx_list_s *list); |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
80 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
81 | /** |
| 504 | 82 | * Member function for inserting a single element. |
| 582 | 83 | * The data pointer may be @c NULL in which case the function shall only allocate memory. |
| 84 | * Returns a pointer to the data of the inserted element. | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
85 | */ |
| 582 | 86 | void *(*insert_element)( |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
87 | struct cx_list_s *list, |
| 490 | 88 | size_t index, |
| 579 | 89 | const void *data |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
90 | ); |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
91 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
92 | /** |
| 490 | 93 | * Member function for inserting multiple elements. |
| 579 | 94 | * |
| 95 | * @see cx_list_default_insert_array() | |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
96 | */ |
| 490 | 97 | size_t (*insert_array)( |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
98 | struct cx_list_s *list, |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
99 | size_t index, |
| 579 | 100 | const void *data, |
| 101 | size_t n | |
| 102 | ); | |
| 103 | ||
| 104 | /** | |
| 105 | * Member function for inserting sorted elements into a sorted list. | |
| 106 | * | |
| 107 | * @see cx_list_default_insert_sorted() | |
| 108 | */ | |
| 109 | size_t (*insert_sorted)( | |
| 110 | struct cx_list_s *list, | |
| 111 | const void *sorted_data, | |
| 490 | 112 | size_t n |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
113 | ); |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
114 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
115 | /** |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
116 | * Member function for inserting an element relative to an iterator position. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
117 | */ |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
118 | int (*insert_iter)( |
| 579 | 119 | struct cx_iterator_s *iter, |
| 120 | const void *elem, | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
121 | int prepend |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
122 | ); |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
123 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
124 | /** |
| 579 | 125 | * Member function for removing elements. |
| 126 | * | |
| 127 | * Implementations SHALL check if @p targetbuf is set and copy the elements | |
| 128 | * to the buffer without invoking any destructor. | |
| 129 | * When @p targetbuf is not set, the destructors SHALL be invoked. | |
| 130 | * | |
| 131 | * The function SHALL return the actual number of elements removed, which | |
| 132 | * might be lower than @p num when going out of bounds. | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
133 | */ |
| 579 | 134 | size_t (*remove)( |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
135 | struct cx_list_s *list, |
| 579 | 136 | size_t index, |
| 137 | size_t num, | |
| 138 | void *targetbuf | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
139 | ); |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
140 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
141 | /** |
| 490 | 142 | * Member function for removing all elements. |
| 143 | */ | |
| 144 | void (*clear)(struct cx_list_s *list); | |
| 145 | ||
| 146 | /** | |
| 147 | * Member function for swapping two elements. | |
| 579 | 148 | * |
| 149 | * @see cx_list_default_swap() | |
| 490 | 150 | */ |
| 151 | int (*swap)( | |
| 152 | struct cx_list_s *list, | |
| 153 | size_t i, | |
| 154 | size_t j | |
| 155 | ); | |
| 156 | ||
| 157 | /** | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
158 | * Member function for element lookup. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
159 | */ |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
160 | void *(*at)( |
| 579 | 161 | const struct cx_list_s *list, |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
162 | size_t index |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
163 | ); |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
164 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
165 | /** |
| 579 | 166 | * Member function for finding and optionally removing an element. |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
167 | */ |
| 579 | 168 | size_t (*find_remove)( |
| 169 | struct cx_list_s *list, | |
| 170 | const void *elem, | |
| 171 | bool remove | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
172 | ); |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
173 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
174 | /** |
| 579 | 175 | * Member function for sorting the list. |
| 176 | * | |
| 177 | * @see cx_list_default_sort() | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
178 | */ |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
179 | void (*sort)(struct cx_list_s *list); |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
180 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
181 | /** |
| 579 | 182 | * Optional member function for comparing this list |
| 183 | * to another list of the same type. | |
| 184 | * If set to @c NULL, comparison won't be optimized. | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
185 | */ |
| 579 | 186 | cx_attr_nonnull |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
187 | int (*compare)( |
| 579 | 188 | const struct cx_list_s *list, |
| 189 | const struct cx_list_s *other | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
190 | ); |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
191 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
192 | /** |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
193 | * Member function for reversing the order of the items. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
194 | */ |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
195 | void (*reverse)(struct cx_list_s *list); |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
196 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
197 | /** |
| 490 | 198 | * Member function for returning an iterator pointing to the specified index. |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
199 | */ |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
200 | struct cx_iterator_s (*iterator)( |
| 579 | 201 | const struct cx_list_s *list, |
| 490 | 202 | size_t index, |
| 203 | bool backward | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
204 | ); |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
205 | }; |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
206 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
207 | /** |
| 582 | 208 | * Common type for all list implementations. |
| 209 | */ | |
| 210 | typedef struct cx_list_s CxList; | |
| 211 | ||
| 212 | /** | |
| 213 | * A shared instance of an empty list. | |
| 214 | * | |
| 215 | * Writing to that list is not allowed. | |
| 216 | * | |
| 217 | * You can use this is a placeholder for initializing CxList pointers | |
| 218 | * for which you do not want to reserve memory right from the beginning. | |
| 219 | */ | |
| 220 | cx_attr_export | |
| 221 | extern CxList *const cxEmptyList; | |
| 222 | ||
| 223 | /** | |
| 579 | 224 | * Default implementation of an array insert. |
| 225 | * | |
| 226 | * This function uses the element insert function for each element of the array. | |
| 227 | * | |
| 228 | * Use this in your own list class if you do not want to implement an optimized | |
| 229 | * version for your list. | |
| 230 | * | |
| 231 | * @param list the list | |
| 232 | * @param index the index where to insert the data | |
| 233 | * @param data a pointer to the array of data to insert | |
| 234 | * @param n the number of elements to insert | |
| 235 | * @return the number of elements actually inserted | |
| 236 | */ | |
| 237 | cx_attr_nonnull | |
| 238 | cx_attr_export | |
| 239 | size_t cx_list_default_insert_array( | |
| 240 | struct cx_list_s *list, | |
| 241 | size_t index, | |
| 242 | const void *data, | |
| 243 | size_t n | |
| 244 | ); | |
| 245 | ||
| 246 | /** | |
| 247 | * Default implementation of a sorted insert. | |
| 248 | * | |
| 249 | * This function uses the array insert function to insert consecutive groups | |
| 250 | * of sorted data. | |
| 251 | * | |
| 252 | * The source data @em must already be sorted wrt. the list's compare function. | |
| 253 | * | |
| 254 | * Use this in your own list class if you do not want to implement an optimized | |
| 255 | * version for your list. | |
| 256 | * | |
| 257 | * @param list the list | |
| 258 | * @param sorted_data a pointer to the array of pre-sorted data to insert | |
| 259 | * @param n the number of elements to insert | |
| 260 | * @return the number of elements actually inserted | |
| 261 | */ | |
| 262 | cx_attr_nonnull | |
| 263 | cx_attr_export | |
| 264 | size_t cx_list_default_insert_sorted( | |
| 265 | struct cx_list_s *list, | |
| 266 | const void *sorted_data, | |
| 267 | size_t n | |
| 268 | ); | |
| 269 | ||
| 270 | /** | |
| 271 | * Default unoptimized sort implementation. | |
| 272 | * | |
| 273 | * This function will copy all data to an array, sort the array with standard | |
| 274 | * qsort, and then copy the data back to the list memory. | |
| 275 | * | |
| 276 | * Use this in your own list class if you do not want to implement an optimized | |
| 277 | * version for your list. | |
| 278 | * | |
| 279 | * @param list the list that shall be sorted | |
| 280 | */ | |
| 281 | cx_attr_nonnull | |
| 282 | cx_attr_export | |
| 283 | void cx_list_default_sort(struct cx_list_s *list); | |
| 284 | ||
| 285 | /** | |
| 286 | * Default unoptimized swap implementation. | |
| 287 | * | |
| 288 | * Use this in your own list class if you do not want to implement an optimized | |
| 289 | * version for your list. | |
| 290 | * | |
| 291 | * @param list the list in which to swap | |
| 292 | * @param i index of one element | |
| 293 | * @param j index of the other element | |
| 294 | * @retval zero success | |
| 295 | * @retval non-zero when indices are out of bounds or memory | |
| 296 | * allocation for the temporary buffer fails | |
| 297 | */ | |
| 298 | cx_attr_nonnull | |
| 299 | cx_attr_export | |
| 300 | int cx_list_default_swap(struct cx_list_s *list, size_t i, size_t j); | |
| 301 | ||
| 302 | /** | |
| 303 | * Initializes a list struct. | |
| 304 | * | |
| 305 | * Only use this function if you are creating your own list implementation. | |
| 306 | * The purpose of this function is to be called in the initialization code | |
| 307 | * of your list, to set certain members correctly. | |
| 308 | * | |
| 309 | * This is particularly important when you want your list to support | |
| 310 | * #CX_STORE_POINTERS as @p elem_size. This function will wrap the list | |
| 311 | * class accordingly and make sure that you can implement your list as if | |
| 312 | * it was only storing objects and the wrapper will automatically enable | |
| 313 | * the feature of storing pointers. | |
| 314 | * | |
| 315 | * @par Example | |
| 316 | * | |
| 317 | * @code | |
| 318 | * CxList *myCustomListCreate( | |
| 319 | * const CxAllocator *allocator, | |
| 320 | * cx_compare_func comparator, | |
| 321 | * size_t elem_size | |
| 322 | * ) { | |
| 323 | * if (allocator == NULL) { | |
| 324 | * allocator = cxDefaultAllocator; | |
| 325 | * } | |
| 326 | * | |
| 327 | * MyCustomList *list = cxCalloc(allocator, 1, sizeof(MyCustomList)); | |
| 328 | * if (list == NULL) return NULL; | |
| 329 | * | |
| 330 | * // initialize | |
| 331 | * cx_list_init((CxList*)list, &my_custom_list_class, | |
| 332 | * allocator, comparator, elem_size); | |
| 333 | * | |
| 334 | * // ... some more custom stuff ... | |
| 335 | * | |
| 336 | * return (CxList *) list; | |
| 337 | * } | |
| 338 | * @endcode | |
| 339 | * | |
| 340 | * @param list the list to initialize | |
| 341 | * @param cl the list class | |
| 342 | * @param allocator the allocator for the elements | |
| 343 | * @param comparator a compare function for the elements | |
| 344 | * @param elem_size the size of one element | |
| 345 | */ | |
| 346 | cx_attr_nonnull_arg(1, 2, 3) | |
| 347 | cx_attr_export | |
| 348 | void cx_list_init( | |
| 349 | struct cx_list_s *list, | |
| 350 | struct cx_list_class_s *cl, | |
| 351 | const struct cx_allocator_s *allocator, | |
| 352 | cx_compare_func comparator, | |
| 353 | size_t elem_size | |
| 354 | ); | |
| 355 | ||
| 356 | /** | |
| 490 | 357 | * Returns the number of elements currently stored in the list. |
| 358 | * | |
| 359 | * @param list the list | |
| 360 | * @return the number of currently stored elements | |
| 361 | */ | |
| 579 | 362 | cx_attr_nonnull |
| 363 | static inline size_t cxListSize(const CxList *list) { | |
| 364 | return list->collection.size; | |
| 490 | 365 | } |
| 366 | ||
| 367 | /** | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
368 | * Adds an item to the end of the list. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
369 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
370 | * @param list the list |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
371 | * @param elem a pointer to the element to add |
| 579 | 372 | * @retval zero success |
| 373 | * @retval non-zero memory allocation failure | |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
374 | * @see cxListAddArray() |
| 582 | 375 | * @see cxListEmplace() |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
376 | */ |
| 579 | 377 | cx_attr_nonnull |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
378 | static inline int cxListAdd( |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
379 | CxList *list, |
| 579 | 380 | const void *elem |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
381 | ) { |
| 579 | 382 | list->collection.sorted = false; |
| 582 | 383 | return list->cl->insert_element(list, list->collection.size, elem) == NULL; |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
384 | } |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
385 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
386 | /** |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
387 | * Adds multiple items to the end of the list. |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
388 | * |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
389 | * This method is more efficient than invoking cxListAdd() multiple times. |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
390 | * |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
391 | * If there is not enough memory to add all elements, the returned value is |
| 579 | 392 | * less than @p n. |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
393 | * |
| 579 | 394 | * If this list is storing pointers instead of objects @p array is expected to |
| 490 | 395 | * be an array of pointers. |
| 396 | * | |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
397 | * @param list the list |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
398 | * @param array a pointer to the elements to add |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
399 | * @param n the number of elements to add |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
400 | * @return the number of added elements |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
401 | */ |
| 579 | 402 | cx_attr_nonnull |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
403 | static inline size_t cxListAddArray( |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
404 | CxList *list, |
| 579 | 405 | const void *array, |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
406 | size_t n |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
407 | ) { |
| 579 | 408 | list->collection.sorted = false; |
| 409 | return list->cl->insert_array(list, list->collection.size, array, n); | |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
410 | } |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
411 | |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
412 | /** |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
413 | * Inserts an item at the specified index. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
414 | * |
| 579 | 415 | * If @p index equals the list @c size, this is effectively cxListAdd(). |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
416 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
417 | * @param list the list |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
418 | * @param index the index the element shall have |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
419 | * @param elem a pointer to the element to add |
| 579 | 420 | * @retval zero success |
| 421 | * @retval non-zero memory allocation failure or the index is out of bounds | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
422 | * @see cxListInsertAfter() |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
423 | * @see cxListInsertBefore() |
| 582 | 424 | * @see cxListEmplaceAt() |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
425 | */ |
| 579 | 426 | cx_attr_nonnull |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
427 | static inline int cxListInsert( |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
428 | CxList *list, |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
429 | size_t index, |
| 579 | 430 | const void *elem |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
431 | ) { |
| 579 | 432 | list->collection.sorted = false; |
| 582 | 433 | return list->cl->insert_element(list, index, elem) == NULL; |
| 434 | } | |
| 435 | ||
| 436 | /** | |
| 437 | * Allocates memory for an element at the specified index and returns a pointer to that memory. | |
| 438 | * | |
| 439 | * @remark When the list is storing pointers, this will return a @c void**. | |
| 440 | * | |
| 441 | * @param list the list | |
| 442 | * @param index the index where to emplace the element | |
| 443 | * @return a pointer to the allocated memory; @c NULL when the operation fails, or the index is out-of-bounds | |
| 444 | * @see cxListEmplace() | |
| 445 | * @see cxListInsert() | |
| 446 | */ | |
| 447 | cx_attr_nonnull | |
| 448 | static inline void *cxListEmplaceAt(CxList *list, size_t index) { | |
| 449 | list->collection.sorted = false; | |
| 450 | return list->cl->insert_element(list, index, NULL); | |
| 451 | } | |
| 452 | ||
| 453 | ||
| 454 | /** | |
| 455 | * Allocates memory for an element at the end of the list and returns a pointer to that memory. | |
| 456 | * | |
| 457 | * @remark When the list is storing pointers, this will return a @c void**. | |
| 458 | * | |
| 459 | * @param list the list | |
| 460 | * @return a pointer to the allocated memory; @c NULL when the operation fails, or the index is out-of-bounds | |
| 461 | * @see cxListEmplaceAt() | |
| 462 | * @see cxListAdd() | |
| 463 | */ | |
| 464 | cx_attr_nonnull | |
| 465 | static inline void *cxListEmplace(CxList *list) { | |
| 466 | list->collection.sorted = false; | |
| 467 | return list->cl->insert_element(list, list->collection.size, NULL); | |
| 490 | 468 | } |
| 469 | ||
| 470 | /** | |
| 579 | 471 | * Inserts an item into a sorted list. |
| 472 | * | |
| 473 | * If the list is not sorted already, the behavior is undefined. | |
| 474 | * | |
| 475 | * @param list the list | |
| 476 | * @param elem a pointer to the element to add | |
| 477 | * @retval zero success | |
| 478 | * @retval non-zero memory allocation failure | |
| 479 | */ | |
| 480 | cx_attr_nonnull | |
| 481 | static inline int cxListInsertSorted( | |
| 482 | CxList *list, | |
| 483 | const void *elem | |
| 484 | ) { | |
| 485 | list->collection.sorted = true; // guaranteed by definition | |
| 486 | const void *data = list->collection.store_pointer ? &elem : elem; | |
| 487 | return list->cl->insert_sorted(list, data, 1) == 0; | |
| 488 | } | |
| 489 | ||
| 490 | /** | |
| 490 | 491 | * Inserts multiple items to the list at the specified index. |
| 579 | 492 | * If @p index equals the list size, this is effectively cxListAddArray(). |
| 490 | 493 | * |
| 494 | * This method is usually more efficient than invoking cxListInsert() | |
| 495 | * multiple times. | |
| 496 | * | |
| 497 | * If there is not enough memory to add all elements, the returned value is | |
| 579 | 498 | * less than @p n. |
| 490 | 499 | * |
| 579 | 500 | * If this list is storing pointers instead of objects @p array is expected to |
| 490 | 501 | * be an array of pointers. |
| 502 | * | |
| 503 | * @param list the list | |
| 504 | * @param index the index where to add the elements | |
| 505 | * @param array a pointer to the elements to add | |
| 506 | * @param n the number of elements to add | |
| 507 | * @return the number of added elements | |
| 508 | */ | |
| 579 | 509 | cx_attr_nonnull |
| 490 | 510 | static inline size_t cxListInsertArray( |
| 511 | CxList *list, | |
| 512 | size_t index, | |
| 579 | 513 | const void *array, |
| 490 | 514 | size_t n |
| 515 | ) { | |
| 579 | 516 | list->collection.sorted = false; |
| 490 | 517 | return list->cl->insert_array(list, index, array, n); |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
518 | } |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
519 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
520 | /** |
| 579 | 521 | * Inserts a sorted array into a sorted list. |
| 522 | * | |
| 523 | * This method is usually more efficient than inserting each element separately, | |
| 524 | * because consecutive chunks of sorted data are inserted in one pass. | |
| 525 | * | |
| 526 | * If there is not enough memory to add all elements, the returned value is | |
| 527 | * less than @p n. | |
| 528 | * | |
| 529 | * If this list is storing pointers instead of objects @p array is expected to | |
| 530 | * be an array of pointers. | |
| 531 | * | |
| 532 | * If the list is not sorted already, the behavior is undefined. | |
| 533 | * | |
| 534 | * @param list the list | |
| 535 | * @param array a pointer to the elements to add | |
| 536 | * @param n the number of elements to add | |
| 537 | * @return the number of added elements | |
| 538 | */ | |
| 539 | cx_attr_nonnull | |
| 540 | static inline size_t cxListInsertSortedArray( | |
| 541 | CxList *list, | |
| 542 | const void *array, | |
| 543 | size_t n | |
| 544 | ) { | |
| 545 | list->collection.sorted = true; // guaranteed by definition | |
| 546 | return list->cl->insert_sorted(list, array, n); | |
| 547 | } | |
| 548 | ||
| 549 | /** | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
550 | * Inserts an element after the current location of the specified iterator. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
551 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
552 | * The used iterator remains operational, but all other active iterators should |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
553 | * be considered invalidated. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
554 | * |
| 579 | 555 | * If @p iter is not a list iterator, the behavior is undefined. |
| 556 | * If @p iter is a past-the-end iterator, the new element gets appended to the list. | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
557 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
558 | * @param iter an iterator |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
559 | * @param elem the element to insert |
| 579 | 560 | * @retval zero success |
| 561 | * @retval non-zero memory allocation failure | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
562 | * @see cxListInsert() |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
563 | * @see cxListInsertBefore() |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
564 | */ |
| 579 | 565 | cx_attr_nonnull |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
566 | static inline int cxListInsertAfter( |
| 579 | 567 | CxIterator *iter, |
| 568 | const void *elem | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
569 | ) { |
| 579 | 570 | CxList* list = (CxList*)iter->src_handle.m; |
| 571 | list->collection.sorted = false; | |
| 572 | return list->cl->insert_iter(iter, elem, 0); | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
573 | } |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
574 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
575 | /** |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
576 | * Inserts an element before the current location of the specified iterator. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
577 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
578 | * The used iterator remains operational, but all other active iterators should |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
579 | * be considered invalidated. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
580 | * |
| 579 | 581 | * If @p iter is not a list iterator, the behavior is undefined. |
| 582 | * If @p iter is a past-the-end iterator, the new element gets appended to the list. | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
583 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
584 | * @param iter an iterator |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
585 | * @param elem the element to insert |
| 579 | 586 | * @retval zero success |
| 587 | * @retval non-zero memory allocation failure | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
588 | * @see cxListInsert() |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
589 | * @see cxListInsertAfter() |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
590 | */ |
| 579 | 591 | cx_attr_nonnull |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
592 | static inline int cxListInsertBefore( |
| 579 | 593 | CxIterator *iter, |
| 594 | const void *elem | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
595 | ) { |
| 579 | 596 | CxList* list = (CxList*)iter->src_handle.m; |
| 597 | list->collection.sorted = false; | |
| 598 | return list->cl->insert_iter(iter, elem, 1); | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
599 | } |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
600 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
601 | /** |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
602 | * Removes the element at the specified index. |
| 490 | 603 | * |
| 604 | * If an element destructor function is specified, it is called before | |
| 605 | * removing the element. | |
| 606 | * | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
607 | * @param list the list |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
608 | * @param index the index of the element |
| 579 | 609 | * @retval zero success |
| 610 | * @retval non-zero index out of bounds | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
611 | */ |
| 579 | 612 | cx_attr_nonnull |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
613 | static inline int cxListRemove( |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
614 | CxList *list, |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
615 | size_t index |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
616 | ) { |
| 579 | 617 | return list->cl->remove(list, index, 1, NULL) == 0; |
| 618 | } | |
| 619 | ||
| 620 | /** | |
| 621 | * Removes and returns the element at the specified index. | |
| 622 | * | |
| 582 | 623 | * No destructor is called, and instead the element is copied to the |
| 579 | 624 | * @p targetbuf which MUST be large enough to hold the removed element. |
| 582 | 625 | * If the list is storing pointers, only the pointer is copied to @p targetbuf. |
| 579 | 626 | * |
| 627 | * @param list the list | |
| 628 | * @param index the index of the element | |
| 629 | * @param targetbuf a buffer where to copy the element | |
| 630 | * @retval zero success | |
| 631 | * @retval non-zero index out of bounds | |
| 632 | */ | |
| 633 | cx_attr_nonnull | |
| 634 | cx_attr_access_w(3) | |
| 635 | static inline int cxListRemoveAndGet( | |
| 636 | CxList *list, | |
| 637 | size_t index, | |
| 638 | void *targetbuf | |
| 639 | ) { | |
| 640 | return list->cl->remove(list, index, 1, targetbuf) == 0; | |
| 641 | } | |
| 642 | ||
| 643 | /** | |
| 582 | 644 | * Removes and returns the first element of the list. |
| 645 | * | |
| 646 | * No destructor is called, and instead the element is copied to the | |
| 647 | * @p targetbuf which MUST be large enough to hold the removed element. | |
| 648 | * If the list is storing pointers, only the pointer is copied to @p targetbuf. | |
| 649 | * | |
| 650 | * @param list the list | |
| 651 | * @param targetbuf a buffer where to copy the element | |
| 652 | * @retval zero success | |
| 653 | * @retval non-zero list is empty | |
| 654 | * @see cxListPopFront() | |
| 655 | * @see cxListRemoveAndGetLast() | |
| 656 | */ | |
| 657 | cx_attr_nonnull | |
| 658 | cx_attr_access_w(2) | |
| 659 | static inline int cxListRemoveAndGetFirst( | |
| 660 | CxList *list, | |
| 661 | void *targetbuf | |
| 662 | ) { | |
| 663 | return list->cl->remove(list, 0, 1, targetbuf) == 0; | |
| 664 | } | |
| 665 | ||
| 666 | /** | |
| 667 | * Removes and returns the first element of the list. | |
| 668 | * | |
| 669 | * Alias for cxListRemoveAndGetFirst(). | |
| 670 | * | |
| 671 | * No destructor is called, and instead the element is copied to the | |
| 672 | * @p targetbuf which MUST be large enough to hold the removed element. | |
| 673 | * If the list is storing pointers, only the pointer is copied to @p targetbuf. | |
| 674 | * | |
| 675 | * @param list (@c CxList*) the list | |
| 676 | * @param targetbuf (@c void*) a buffer where to copy the element | |
| 677 | * @retval zero success | |
| 678 | * @retval non-zero list is empty | |
| 679 | * @see cxListRemoveAndGetFirst() | |
| 680 | * @see cxListPop() | |
| 681 | */ | |
| 682 | #define cxListPopFront(list, targetbuf) cxListRemoveAndGetFirst((list), (targetbuf)) | |
| 683 | ||
| 684 | ||
| 685 | /** | |
| 686 | * Removes and returns the last element of the list. | |
| 687 | * | |
| 688 | * No destructor is called, and instead the element is copied to the | |
| 689 | * @p targetbuf which MUST be large enough to hold the removed element. | |
| 690 | * If the list is storing pointers, only the pointer is copied to @p targetbuf. | |
| 691 | * | |
| 692 | * @param list the list | |
| 693 | * @param targetbuf a buffer where to copy the element | |
| 694 | * @retval zero success | |
| 695 | * @retval non-zero list is empty | |
| 696 | */ | |
| 697 | cx_attr_nonnull | |
| 698 | cx_attr_access_w(2) | |
| 699 | static inline int cxListRemoveAndGetLast( | |
| 700 | CxList *list, | |
| 701 | void *targetbuf | |
| 702 | ) { | |
| 703 | // note: index may wrap - member function will catch that | |
| 704 | return list->cl->remove(list, list->collection.size - 1, 1, targetbuf) == 0; | |
| 705 | } | |
| 706 | ||
| 707 | /** | |
| 708 | * Removes and returns the last element of the list. | |
| 709 | * | |
| 710 | * Alias for cxListRemoveAndGetLast(). | |
| 711 | * | |
| 712 | * No destructor is called, and instead the element is copied to the | |
| 713 | * @p targetbuf which MUST be large enough to hold the removed element. | |
| 714 | * If the list is storing pointers, only the pointer is copied to @p targetbuf. | |
| 715 | * | |
| 716 | * @param list (@c CxList*) the list | |
| 717 | * @param targetbuf (@c void*) a buffer where to copy the element | |
| 718 | * @retval zero success | |
| 719 | * @retval non-zero list is empty | |
| 720 | * @see cxListRemoveAndGetLast() | |
| 721 | * @see cxListPopFront() | |
| 722 | */ | |
| 723 | #define cxListPop(list, targetbuf) cxListRemoveAndGetLast((list), (targetbuf)) | |
| 724 | ||
| 725 | /** | |
| 579 | 726 | * Removes multiple element starting at the specified index. |
| 727 | * | |
| 728 | * If an element destructor function is specified, it is called for each | |
| 729 | * element. It is guaranteed that the destructor is called before removing | |
| 582 | 730 | * the element. However, due to possible optimizations, it is neither guaranteed |
| 579 | 731 | * that the destructors are invoked for all elements before starting to remove |
| 732 | * them, nor that the element is removed immediately after the destructor call | |
| 733 | * before proceeding to the next element. | |
| 734 | * | |
| 735 | * @param list the list | |
| 736 | * @param index the index of the element | |
| 737 | * @param num the number of elements to remove | |
| 738 | * @return the actual number of removed elements | |
| 739 | */ | |
| 740 | cx_attr_nonnull | |
| 741 | static inline size_t cxListRemoveArray( | |
| 742 | CxList *list, | |
| 743 | size_t index, | |
| 744 | size_t num | |
| 745 | ) { | |
| 746 | return list->cl->remove(list, index, num, NULL); | |
| 747 | } | |
| 748 | ||
| 749 | /** | |
| 582 | 750 | * Removes and returns multiple elements starting at the specified index. |
| 579 | 751 | * |
| 582 | 752 | * No destructor is called, and instead the elements are copied to the |
| 579 | 753 | * @p targetbuf which MUST be large enough to hold all removed elements. |
| 582 | 754 | * If the list is storing pointers, @p targetbuf is expected to be an array of pointers. |
| 579 | 755 | * |
| 756 | * @param list the list | |
| 757 | * @param index the index of the element | |
| 758 | * @param num the number of elements to remove | |
| 759 | * @param targetbuf a buffer where to copy the elements | |
| 760 | * @return the actual number of removed elements | |
| 761 | */ | |
| 762 | cx_attr_nonnull | |
| 763 | cx_attr_access_w(4) | |
| 764 | static inline size_t cxListRemoveArrayAndGet( | |
| 765 | CxList *list, | |
| 766 | size_t index, | |
| 767 | size_t num, | |
| 768 | void *targetbuf | |
| 769 | ) { | |
| 770 | return list->cl->remove(list, index, num, targetbuf); | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
771 | } |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
772 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
773 | /** |
| 490 | 774 | * Removes all elements from this list. |
| 775 | * | |
| 579 | 776 | * If element destructor functions are specified, they are called for each |
| 490 | 777 | * element before removing them. |
| 778 | * | |
| 779 | * @param list the list | |
| 780 | */ | |
| 579 | 781 | cx_attr_nonnull |
| 490 | 782 | static inline void cxListClear(CxList *list) { |
| 579 | 783 | list->collection.sorted = true; // empty lists are always sorted |
| 490 | 784 | list->cl->clear(list); |
| 785 | } | |
| 786 | ||
| 787 | /** | |
| 788 | * Swaps two items in the list. | |
| 789 | * | |
| 582 | 790 | * Implementations should only allocate temporary memory for the swap if |
| 490 | 791 | * it is necessary. |
| 792 | * | |
| 793 | * @param list the list | |
| 794 | * @param i the index of the first element | |
| 795 | * @param j the index of the second element | |
| 579 | 796 | * @retval zero success |
| 582 | 797 | * @retval non-zero one of the indices is out of bounds, |
| 798 | * or the swap needed extra memory, but allocation failed | |
| 490 | 799 | */ |
| 579 | 800 | cx_attr_nonnull |
| 490 | 801 | static inline int cxListSwap( |
| 802 | CxList *list, | |
| 803 | size_t i, | |
| 804 | size_t j | |
| 805 | ) { | |
| 579 | 806 | list->collection.sorted = false; |
| 490 | 807 | return list->cl->swap(list, i, j); |
| 808 | } | |
| 809 | ||
| 810 | /** | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
811 | * Returns a pointer to the element at the specified index. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
812 | * |
| 582 | 813 | * If the list is storing pointers, returns the pointer stored at the specified index. |
| 814 | * | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
815 | * @param list the list |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
816 | * @param index the index of the element |
| 579 | 817 | * @return a pointer to the element or @c NULL if the index is out of bounds |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
818 | */ |
| 579 | 819 | cx_attr_nonnull |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
820 | static inline void *cxListAt( |
| 579 | 821 | const CxList *list, |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
822 | size_t index |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
823 | ) { |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
824 | return list->cl->at(list, index); |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
825 | } |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
826 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
827 | /** |
| 582 | 828 | * Returns a pointer to the first element. |
| 829 | * | |
| 830 | * If the list is storing pointers, returns the first pointer stored in the list. | |
| 831 | * | |
| 832 | * @param list the list | |
| 833 | * @return a pointer to the first element or @c NULL if the list is empty | |
| 834 | */ | |
| 835 | cx_attr_nonnull | |
| 836 | static inline void *cxListFirst(const CxList *list) { | |
| 837 | return list->cl->at(list, 0); | |
| 838 | } | |
| 839 | ||
| 840 | /** | |
| 841 | * Returns a pointer to the last element. | |
| 842 | * | |
| 843 | * If the list is storing pointers, returns the last pointer stored in the list. | |
| 844 | * | |
| 845 | * @param list the list | |
| 846 | * @return a pointer to the last element or @c NULL if the list is empty | |
| 847 | */ | |
| 848 | cx_attr_nonnull | |
| 849 | static inline void *cxListLast(const CxList *list) { | |
| 850 | return list->cl->at(list, list->collection.size - 1); | |
| 851 | } | |
| 852 | ||
| 853 | /** | |
| 854 | * Sets the element at the specified index in the list | |
| 855 | * | |
| 856 | * @param list the list to set the element in | |
| 857 | * @param index the index to set the element at | |
| 858 | * @param elem element to set | |
| 859 | * @retval zero on success | |
| 860 | * @retval non-zero when index is out of bounds | |
| 861 | */ | |
| 862 | cx_attr_nonnull | |
| 863 | cx_attr_export | |
| 864 | int cxListSet( | |
| 865 | CxList *list, | |
| 866 | size_t index, | |
| 867 | const void *elem | |
| 868 | ); | |
| 869 | ||
| 870 | /** | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
871 | * Returns an iterator pointing to the item at the specified index. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
872 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
873 | * The returned iterator is position-aware. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
874 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
875 | * If the index is out of range, a past-the-end iterator will be returned. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
876 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
877 | * @param list the list |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
878 | * @param index the index where the iterator shall point at |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
879 | * @return a new iterator |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
880 | */ |
| 579 | 881 | cx_attr_nonnull |
| 882 | cx_attr_nodiscard | |
| 490 | 883 | static inline CxIterator cxListIteratorAt( |
| 579 | 884 | const CxList *list, |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
885 | size_t index |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
886 | ) { |
| 490 | 887 | return list->cl->iterator(list, index, false); |
| 888 | } | |
| 889 | ||
| 890 | /** | |
| 891 | * Returns a backwards iterator pointing to the item at the specified index. | |
| 892 | * | |
| 893 | * The returned iterator is position-aware. | |
| 894 | * | |
| 895 | * If the index is out of range, a past-the-end iterator will be returned. | |
| 896 | * | |
| 897 | * @param list the list | |
| 898 | * @param index the index where the iterator shall point at | |
| 899 | * @return a new iterator | |
| 900 | */ | |
| 579 | 901 | cx_attr_nonnull |
| 902 | cx_attr_nodiscard | |
| 490 | 903 | static inline CxIterator cxListBackwardsIteratorAt( |
| 579 | 904 | const CxList *list, |
| 490 | 905 | size_t index |
| 906 | ) { | |
| 907 | return list->cl->iterator(list, index, true); | |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
908 | } |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
909 | |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
910 | /** |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
911 | * Returns a mutating iterator pointing to the item at the specified index. |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
912 | * |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
913 | * The returned iterator is position-aware. |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
914 | * |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
915 | * If the index is out of range, a past-the-end iterator will be returned. |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
916 | * |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
917 | * @param list the list |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
918 | * @param index the index where the iterator shall point at |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
919 | * @return a new iterator |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
920 | */ |
| 579 | 921 | cx_attr_nonnull |
| 922 | cx_attr_nodiscard | |
| 923 | cx_attr_export | |
| 924 | CxIterator cxListMutIteratorAt( | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
925 | CxList *list, |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
926 | size_t index |
| 490 | 927 | ); |
| 928 | ||
| 929 | /** | |
| 930 | * Returns a mutating backwards iterator pointing to the item at the | |
| 931 | * specified index. | |
| 932 | * | |
| 933 | * The returned iterator is position-aware. | |
| 934 | * | |
| 935 | * If the index is out of range, a past-the-end iterator will be returned. | |
| 936 | * | |
| 937 | * @param list the list | |
| 938 | * @param index the index where the iterator shall point at | |
| 939 | * @return a new iterator | |
| 940 | */ | |
| 579 | 941 | cx_attr_nonnull |
| 942 | cx_attr_nodiscard | |
| 943 | cx_attr_export | |
| 944 | CxIterator cxListMutBackwardsIteratorAt( | |
| 490 | 945 | CxList *list, |
| 946 | size_t index | |
| 947 | ); | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
948 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
949 | /** |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
950 | * Returns an iterator pointing to the first item of the list. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
951 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
952 | * The returned iterator is position-aware. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
953 | * |
| 582 | 954 | * If the list is empty or @c NULL, a past-the-end iterator will be returned. |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
955 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
956 | * @param list the list |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
957 | * @return a new iterator |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
958 | */ |
| 579 | 959 | cx_attr_nodiscard |
| 960 | static inline CxIterator cxListIterator(const CxList *list) { | |
| 582 | 961 | if (list == NULL) list = cxEmptyList; |
| 490 | 962 | return list->cl->iterator(list, 0, false); |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
963 | } |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
964 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
965 | /** |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
966 | * Returns a mutating iterator pointing to the first item of the list. |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
967 | * |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
968 | * The returned iterator is position-aware. |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
969 | * |
| 582 | 970 | * If the list is empty or @c NULL, a past-the-end iterator will be returned. |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
971 | * |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
972 | * @param list the list |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
973 | * @return a new iterator |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
974 | */ |
| 579 | 975 | cx_attr_nodiscard |
| 976 | static inline CxIterator cxListMutIterator(CxList *list) { | |
| 582 | 977 | if (list == NULL) list = cxEmptyList; |
| 490 | 978 | return cxListMutIteratorAt(list, 0); |
| 979 | } | |
| 980 | ||
| 981 | ||
| 982 | /** | |
| 983 | * Returns a backwards iterator pointing to the last item of the list. | |
| 984 | * | |
| 985 | * The returned iterator is position-aware. | |
| 986 | * | |
| 582 | 987 | * If the list is empty or @c NULL, a past-the-end iterator will be returned. |
| 490 | 988 | * |
| 989 | * @param list the list | |
| 990 | * @return a new iterator | |
| 991 | */ | |
| 579 | 992 | cx_attr_nodiscard |
| 993 | static inline CxIterator cxListBackwardsIterator(const CxList *list) { | |
| 582 | 994 | if (list == NULL) list = cxEmptyList; |
| 579 | 995 | return list->cl->iterator(list, list->collection.size - 1, true); |
| 490 | 996 | } |
| 997 | ||
| 998 | /** | |
| 999 | * Returns a mutating backwards iterator pointing to the last item of the list. | |
| 1000 | * | |
| 1001 | * The returned iterator is position-aware. | |
| 1002 | * | |
| 582 | 1003 | * If the list is empty or @c NULL, a past-the-end iterator will be returned. |
| 490 | 1004 | * |
| 1005 | * @param list the list | |
| 1006 | * @return a new iterator | |
| 1007 | */ | |
| 579 | 1008 | cx_attr_nodiscard |
| 1009 | static inline CxIterator cxListMutBackwardsIterator(CxList *list) { | |
| 582 | 1010 | if (list == NULL) list = cxEmptyList; |
| 579 | 1011 | return cxListMutBackwardsIteratorAt(list, list->collection.size - 1); |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
1012 | } |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
1013 | |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
1014 | /** |
| 579 | 1015 | * Returns the index of the first element that equals @p elem. |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1016 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1017 | * Determining equality is performed by the list's comparator function. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1018 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1019 | * @param list the list |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1020 | * @param elem the element to find |
| 579 | 1021 | * @return the index of the element or the size of the list when the element is not found |
| 1022 | * @see cxListIndexValid() | |
| 582 | 1023 | * @see cxListContains() |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1024 | */ |
| 579 | 1025 | cx_attr_nonnull |
| 1026 | cx_attr_nodiscard | |
| 1027 | static inline size_t cxListFind( | |
| 1028 | const CxList *list, | |
| 1029 | const void *elem | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1030 | ) { |
| 579 | 1031 | return list->cl->find_remove((CxList*)list, elem, false); |
| 1032 | } | |
| 1033 | ||
| 1034 | /** | |
| 582 | 1035 | * Checks, if the list contains the specified element. |
| 1036 | * | |
| 1037 | * The elements are compared with the list's comparator function. | |
| 1038 | * | |
| 1039 | * @param list the list | |
| 1040 | * @param elem the element to find | |
| 1041 | * @retval true if the element is contained | |
| 1042 | * @retval false if the element is not contained | |
| 1043 | * @see cxListFind() | |
| 1044 | */ | |
| 1045 | cx_attr_nonnull | |
| 1046 | cx_attr_nodiscard | |
| 1047 | static inline bool cxListContains( | |
| 1048 | const CxList* list, | |
| 1049 | const void* elem | |
| 1050 | ) { | |
| 1051 | return list->cl->find_remove((CxList*)list, elem, false) < list->collection.size; | |
| 1052 | } | |
| 1053 | ||
| 1054 | /** | |
| 579 | 1055 | * Checks if the specified index is within bounds. |
| 1056 | * | |
| 1057 | * @param list the list | |
| 1058 | * @param index the index | |
| 1059 | * @retval true if the index is within bounds | |
| 1060 | * @retval false if the index is out of bounds | |
| 1061 | */ | |
| 1062 | cx_attr_nonnull | |
| 1063 | cx_attr_nodiscard | |
| 1064 | static inline bool cxListIndexValid(const CxList *list, size_t index) { | |
| 1065 | return index < list->collection.size; | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1066 | } |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1067 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1068 | /** |
| 579 | 1069 | * Removes and returns the index of the first element that equals @p elem. |
| 1070 | * | |
| 1071 | * Determining equality is performed by the list's comparator function. | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1072 | * |
| 579 | 1073 | * @param list the list |
| 1074 | * @param elem the element to find and remove | |
| 1075 | * @return the index of the now removed element or the list size | |
| 1076 | * when the element is not found or could not be removed | |
| 1077 | * @see cxListIndexValid() | |
| 1078 | */ | |
| 1079 | cx_attr_nonnull | |
| 1080 | static inline size_t cxListFindRemove( | |
| 1081 | CxList *list, | |
| 1082 | const void *elem | |
| 1083 | ) { | |
| 1084 | return list->cl->find_remove(list, elem, true); | |
| 1085 | } | |
| 1086 | ||
| 1087 | /** | |
| 1088 | * Sorts the list. | |
| 1089 | * | |
| 1090 | * @remark The underlying sort algorithm is implementation defined. | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1091 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1092 | * @param list the list |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1093 | */ |
| 579 | 1094 | cx_attr_nonnull |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1095 | static inline void cxListSort(CxList *list) { |
| 582 | 1096 | if (list->collection.sorted) return; |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1097 | list->cl->sort(list); |
| 579 | 1098 | list->collection.sorted = true; |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1099 | } |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1100 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1101 | /** |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1102 | * Reverses the order of the items. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1103 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1104 | * @param list the list |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1105 | */ |
| 579 | 1106 | cx_attr_nonnull |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1107 | static inline void cxListReverse(CxList *list) { |
| 579 | 1108 | // still sorted, but not according to the cmp_func |
| 1109 | list->collection.sorted = false; | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1110 | list->cl->reverse(list); |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1111 | } |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1112 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1113 | /** |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1114 | * Compares a list to another list of the same type. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1115 | * |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
1116 | * First, the list sizes are compared. |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
1117 | * If they match, the lists are compared element-wise. |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1118 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1119 | * @param list the list |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1120 | * @param other the list to compare to |
| 579 | 1121 | * @retval zero both lists are equal element wise |
| 1122 | * @retval negative the first list is smaller | |
| 1123 | * or the first non-equal element in the first list is smaller | |
| 1124 | * @retval positive the first list is larger | |
| 1125 | * or the first non-equal element in the first list is larger | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1126 | */ |
| 579 | 1127 | cx_attr_nonnull |
| 1128 | cx_attr_nodiscard | |
| 1129 | cx_attr_export | |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
1130 | int cxListCompare( |
| 579 | 1131 | const CxList *list, |
| 1132 | const CxList *other | |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
1133 | ); |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1134 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1135 | /** |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1136 | * Deallocates the memory of the specified list structure. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1137 | * |
| 579 | 1138 | * Also calls the content destructor functions for each element, if specified. |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1139 | * |
| 579 | 1140 | * @param list the list which shall be freed |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1141 | */ |
| 579 | 1142 | cx_attr_export |
| 1143 | void cxListFree(CxList *list); | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1144 | |
| 504 | 1145 | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1146 | #ifdef __cplusplus |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
1147 | } // extern "C" |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1148 | #endif |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1149 | |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
1150 | #endif // UCX_LIST_H |