Sat, 19 Apr 2025 19:56:09 +0200
fix toolbar item signal
| 174 | 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 | */ | |
| 28 | /** | |
| 440 | 29 | * @file list.h |
| 30 | * @brief Interface for list implementations. | |
| 31 | * @author Mike Becker | |
| 32 | * @author Olaf Wintermann | |
| 33 | * @copyright 2-Clause BSD License | |
| 174 | 34 | */ |
| 35 | ||
| 36 | #ifndef UCX_LIST_H | |
| 37 | #define UCX_LIST_H | |
| 38 | ||
| 39 | #include "common.h" | |
| 40 | #include "collection.h" | |
| 41 | ||
| 42 | #ifdef __cplusplus | |
| 43 | extern "C" { | |
| 44 | #endif | |
| 45 | ||
| 46 | /** | |
| 47 | * List class type. | |
| 48 | */ | |
| 49 | typedef struct cx_list_class_s cx_list_class; | |
| 50 | ||
| 51 | /** | |
| 52 | * Structure for holding the base data of a list. | |
| 53 | */ | |
| 54 | struct cx_list_s { | |
| 440 | 55 | /** |
| 56 | * Common members for collections. | |
| 57 | */ | |
| 324 | 58 | CX_COLLECTION_BASE; |
| 174 | 59 | /** |
| 60 | * The list class definition. | |
| 61 | */ | |
| 324 | 62 | const cx_list_class *cl; |
| 174 | 63 | /** |
| 64 | * The actual implementation in case the list class is delegating. | |
| 65 | */ | |
| 324 | 66 | const cx_list_class *climpl; |
| 174 | 67 | }; |
| 68 | ||
| 69 | /** | |
| 70 | * The class definition for arbitrary lists. | |
| 71 | */ | |
| 72 | struct cx_list_class_s { | |
| 73 | /** | |
| 74 | * Destructor function. | |
| 75 | * | |
| 76 | * Implementations SHALL invoke the content destructor functions if provided | |
| 440 | 77 | * and SHALL deallocate the entire list memory. |
| 174 | 78 | */ |
| 440 | 79 | void (*deallocate)(struct cx_list_s *list); |
| 174 | 80 | |
| 81 | /** | |
|
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
82 | * Member function for inserting a single element. |
| 174 | 83 | */ |
| 84 | int (*insert_element)( | |
| 85 | struct cx_list_s *list, | |
| 86 | size_t index, | |
| 324 | 87 | const void *data |
| 174 | 88 | ); |
| 89 | ||
| 90 | /** | |
| 91 | * Member function for inserting multiple elements. | |
| 440 | 92 | * |
| 324 | 93 | * @see cx_list_default_insert_array() |
| 174 | 94 | */ |
| 95 | size_t (*insert_array)( | |
| 96 | struct cx_list_s *list, | |
| 97 | size_t index, | |
| 324 | 98 | const void *data, |
| 99 | size_t n | |
| 100 | ); | |
| 101 | ||
| 102 | /** | |
| 103 | * Member function for inserting sorted elements into a sorted list. | |
| 104 | * | |
| 105 | * @see cx_list_default_insert_sorted() | |
| 106 | */ | |
| 107 | size_t (*insert_sorted)( | |
| 108 | struct cx_list_s *list, | |
| 109 | const void *sorted_data, | |
| 174 | 110 | size_t n |
| 111 | ); | |
| 112 | ||
| 113 | /** | |
| 114 | * Member function for inserting an element relative to an iterator position. | |
| 115 | */ | |
| 116 | int (*insert_iter)( | |
| 324 | 117 | struct cx_iterator_s *iter, |
| 118 | const void *elem, | |
| 174 | 119 | int prepend |
| 120 | ); | |
| 121 | ||
| 122 | /** | |
| 440 | 123 | * Member function for removing elements. |
| 124 | * | |
| 125 | * Implementations SHALL check if @p targetbuf is set and copy the elements | |
| 126 | * to the buffer without invoking any destructor. | |
| 127 | * When @p targetbuf is not set, the destructors SHALL be invoked. | |
| 128 | * | |
| 129 | * The function SHALL return the actual number of elements removed, which | |
| 130 | * might be lower than @p num when going out of bounds. | |
| 174 | 131 | */ |
| 440 | 132 | size_t (*remove)( |
| 174 | 133 | struct cx_list_s *list, |
| 440 | 134 | size_t index, |
| 135 | size_t num, | |
| 136 | void *targetbuf | |
| 174 | 137 | ); |
| 138 | ||
| 139 | /** | |
| 140 | * Member function for removing all elements. | |
| 141 | */ | |
| 142 | void (*clear)(struct cx_list_s *list); | |
| 143 | ||
| 144 | /** | |
| 145 | * Member function for swapping two elements. | |
| 440 | 146 | * |
| 324 | 147 | * @see cx_list_default_swap() |
| 174 | 148 | */ |
| 149 | int (*swap)( | |
| 150 | struct cx_list_s *list, | |
| 151 | size_t i, | |
| 152 | size_t j | |
| 153 | ); | |
| 154 | ||
| 155 | /** | |
| 156 | * Member function for element lookup. | |
| 157 | */ | |
| 158 | void *(*at)( | |
| 324 | 159 | const struct cx_list_s *list, |
| 174 | 160 | size_t index |
| 161 | ); | |
| 162 | ||
| 163 | /** | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
164 | * Member function for finding and optionally removing an element. |
| 174 | 165 | */ |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
166 | size_t (*find_remove)( |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
167 | struct cx_list_s *list, |
| 324 | 168 | const void *elem, |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
169 | bool remove |
| 174 | 170 | ); |
| 171 | ||
| 172 | /** | |
| 440 | 173 | * Member function for sorting the list. |
| 174 | * | |
| 324 | 175 | * @see cx_list_default_sort() |
| 174 | 176 | */ |
| 177 | void (*sort)(struct cx_list_s *list); | |
| 178 | ||
| 179 | /** | |
| 324 | 180 | * Optional member function for comparing this list |
| 181 | * to another list of the same type. | |
| 440 | 182 | * If set to @c NULL, comparison won't be optimized. |
| 174 | 183 | */ |
| 440 | 184 | cx_attr_nonnull |
| 174 | 185 | int (*compare)( |
| 324 | 186 | const struct cx_list_s *list, |
| 187 | const struct cx_list_s *other | |
| 174 | 188 | ); |
| 189 | ||
| 190 | /** | |
| 191 | * Member function for reversing the order of the items. | |
| 192 | */ | |
| 193 | void (*reverse)(struct cx_list_s *list); | |
| 194 | ||
| 195 | /** | |
| 196 | * Member function for returning an iterator pointing to the specified index. | |
| 197 | */ | |
| 198 | struct cx_iterator_s (*iterator)( | |
| 324 | 199 | const struct cx_list_s *list, |
| 174 | 200 | size_t index, |
| 201 | bool backward | |
| 202 | ); | |
| 203 | }; | |
| 204 | ||
| 205 | /** | |
| 324 | 206 | * Default implementation of an array insert. |
| 207 | * | |
| 208 | * This function uses the element insert function for each element of the array. | |
| 209 | * | |
| 210 | * Use this in your own list class if you do not want to implement an optimized | |
| 211 | * version for your list. | |
| 212 | * | |
| 213 | * @param list the list | |
| 214 | * @param index the index where to insert the data | |
| 215 | * @param data a pointer to the array of data to insert | |
| 216 | * @param n the number of elements to insert | |
| 217 | * @return the number of elements actually inserted | |
| 218 | */ | |
| 440 | 219 | cx_attr_nonnull |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
220 | cx_attr_export |
| 324 | 221 | size_t cx_list_default_insert_array( |
| 222 | struct cx_list_s *list, | |
| 223 | size_t index, | |
| 224 | const void *data, | |
| 225 | size_t n | |
| 226 | ); | |
| 227 | ||
| 228 | /** | |
| 229 | * Default implementation of a sorted insert. | |
| 230 | * | |
| 231 | * This function uses the array insert function to insert consecutive groups | |
| 232 | * of sorted data. | |
| 233 | * | |
| 440 | 234 | * The source data @em must already be sorted wrt. the list's compare function. |
| 324 | 235 | * |
| 236 | * Use this in your own list class if you do not want to implement an optimized | |
| 237 | * version for your list. | |
| 238 | * | |
| 239 | * @param list the list | |
| 240 | * @param sorted_data a pointer to the array of pre-sorted data to insert | |
| 241 | * @param n the number of elements to insert | |
| 242 | * @return the number of elements actually inserted | |
| 243 | */ | |
| 440 | 244 | cx_attr_nonnull |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
245 | cx_attr_export |
| 324 | 246 | size_t cx_list_default_insert_sorted( |
| 247 | struct cx_list_s *list, | |
| 248 | const void *sorted_data, | |
| 249 | size_t n | |
| 250 | ); | |
| 251 | ||
| 252 | /** | |
| 253 | * Default unoptimized sort implementation. | |
| 254 | * | |
| 255 | * This function will copy all data to an array, sort the array with standard | |
| 256 | * qsort, and then copy the data back to the list memory. | |
| 257 | * | |
| 258 | * Use this in your own list class if you do not want to implement an optimized | |
| 259 | * version for your list. | |
| 260 | * | |
| 261 | * @param list the list that shall be sorted | |
| 262 | */ | |
| 440 | 263 | cx_attr_nonnull |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
264 | cx_attr_export |
| 324 | 265 | void cx_list_default_sort(struct cx_list_s *list); |
| 266 | ||
| 267 | /** | |
| 268 | * Default unoptimized swap implementation. | |
| 269 | * | |
| 270 | * Use this in your own list class if you do not want to implement an optimized | |
| 271 | * version for your list. | |
| 272 | * | |
| 273 | * @param list the list in which to swap | |
| 274 | * @param i index of one element | |
| 275 | * @param j index of the other element | |
| 440 | 276 | * @retval zero success |
| 277 | * @retval non-zero when indices are out of bounds or memory | |
| 324 | 278 | * allocation for the temporary buffer fails |
| 279 | */ | |
| 440 | 280 | cx_attr_nonnull |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
281 | cx_attr_export |
| 324 | 282 | int cx_list_default_swap(struct cx_list_s *list, size_t i, size_t j); |
| 283 | ||
| 284 | /** | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
285 | * Initializes a list struct. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
286 | * |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
287 | * Only use this function if you are creating your own list implementation. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
288 | * The purpose of this function is to be called in the initialization code |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
289 | * of your list, to set certain members correctly. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
290 | * |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
291 | * This is particularly important when you want your list to support |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
292 | * #CX_STORE_POINTERS as @p elem_size. This function will wrap the list |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
293 | * class accordingly and make sure that you can implement your list as if |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
294 | * it was only storing objects and the wrapper will automatically enable |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
295 | * the feature of storing pointers. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
296 | * |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
297 | * @par Example |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
298 | * |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
299 | * @code |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
300 | * CxList *myCustomListCreate( |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
301 | * const CxAllocator *allocator, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
302 | * cx_compare_func comparator, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
303 | * size_t elem_size |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
304 | * ) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
305 | * if (allocator == NULL) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
306 | * allocator = cxDefaultAllocator; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
307 | * } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
308 | * |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
309 | * MyCustomList *list = cxCalloc(allocator, 1, sizeof(MyCustomList)); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
310 | * if (list == NULL) return NULL; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
311 | * |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
312 | * // initialize |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
313 | * cx_list_init((CxList*)list, &my_custom_list_class, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
314 | * allocator, comparator, elem_size); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
315 | * |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
316 | * // ... some more custom stuff ... |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
317 | * |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
318 | * return (CxList *) list; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
319 | * } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
320 | * @endcode |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
321 | * |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
322 | * @param list the list to initialize |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
323 | * @param cl the list class |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
324 | * @param allocator the allocator for the elements |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
325 | * @param comparator a compare function for the elements |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
326 | * @param elem_size the size of one element |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
327 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
328 | cx_attr_nonnull_arg(1, 2, 3) |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
329 | cx_attr_export |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
330 | void cx_list_init( |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
331 | struct cx_list_s *list, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
332 | struct cx_list_class_s *cl, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
333 | const struct cx_allocator_s *allocator, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
334 | cx_compare_func comparator, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
335 | size_t elem_size |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
336 | ); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
337 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
338 | /** |
| 174 | 339 | * Common type for all list implementations. |
| 340 | */ | |
| 341 | typedef struct cx_list_s CxList; | |
| 342 | ||
| 343 | /** | |
| 344 | * Returns the number of elements currently stored in the list. | |
| 345 | * | |
| 346 | * @param list the list | |
| 347 | * @return the number of currently stored elements | |
| 348 | */ | |
| 440 | 349 | cx_attr_nonnull |
| 324 | 350 | static inline size_t cxListSize(const CxList *list) { |
| 351 | return list->collection.size; | |
| 174 | 352 | } |
| 353 | ||
| 354 | /** | |
| 355 | * Adds an item to the end of the list. | |
| 356 | * | |
| 357 | * @param list the list | |
| 358 | * @param elem a pointer to the element to add | |
| 440 | 359 | * @retval zero success |
| 360 | * @retval non-zero memory allocation failure | |
| 174 | 361 | * @see cxListAddArray() |
| 362 | */ | |
| 440 | 363 | cx_attr_nonnull |
| 174 | 364 | static inline int cxListAdd( |
| 365 | CxList *list, | |
| 324 | 366 | const void *elem |
| 174 | 367 | ) { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
368 | list->collection.sorted = false; |
| 324 | 369 | return list->cl->insert_element(list, list->collection.size, elem); |
| 174 | 370 | } |
| 371 | ||
| 372 | /** | |
| 373 | * Adds multiple items to the end of the list. | |
| 374 | * | |
| 375 | * This method is more efficient than invoking cxListAdd() multiple times. | |
| 376 | * | |
| 377 | * If there is not enough memory to add all elements, the returned value is | |
| 440 | 378 | * less than @p n. |
| 174 | 379 | * |
| 440 | 380 | * If this list is storing pointers instead of objects @p array is expected to |
| 174 | 381 | * be an array of pointers. |
| 382 | * | |
| 383 | * @param list the list | |
| 384 | * @param array a pointer to the elements to add | |
| 385 | * @param n the number of elements to add | |
| 386 | * @return the number of added elements | |
| 387 | */ | |
| 440 | 388 | cx_attr_nonnull |
| 174 | 389 | static inline size_t cxListAddArray( |
| 390 | CxList *list, | |
| 324 | 391 | const void *array, |
| 174 | 392 | size_t n |
| 393 | ) { | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
394 | list->collection.sorted = false; |
| 324 | 395 | return list->cl->insert_array(list, list->collection.size, array, n); |
| 174 | 396 | } |
| 397 | ||
| 398 | /** | |
| 399 | * Inserts an item at the specified index. | |
| 400 | * | |
| 440 | 401 | * If @p index equals the list @c size, this is effectively cxListAdd(). |
| 174 | 402 | * |
| 403 | * @param list the list | |
| 404 | * @param index the index the element shall have | |
| 405 | * @param elem a pointer to the element to add | |
| 440 | 406 | * @retval zero success |
| 407 | * @retval non-zero memory allocation failure or the index is out of bounds | |
| 174 | 408 | * @see cxListInsertAfter() |
| 409 | * @see cxListInsertBefore() | |
| 410 | */ | |
| 440 | 411 | cx_attr_nonnull |
| 174 | 412 | static inline int cxListInsert( |
| 413 | CxList *list, | |
| 414 | size_t index, | |
| 324 | 415 | const void *elem |
| 174 | 416 | ) { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
417 | list->collection.sorted = false; |
| 174 | 418 | return list->cl->insert_element(list, index, elem); |
| 419 | } | |
| 420 | ||
| 421 | /** | |
| 324 | 422 | * Inserts an item into a sorted list. |
| 423 | * | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
424 | * If the list is not sorted already, the behavior is undefined. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
425 | * |
| 324 | 426 | * @param list the list |
| 427 | * @param elem a pointer to the element to add | |
| 440 | 428 | * @retval zero success |
| 429 | * @retval non-zero memory allocation failure | |
| 324 | 430 | */ |
| 440 | 431 | cx_attr_nonnull |
| 324 | 432 | static inline int cxListInsertSorted( |
| 433 | CxList *list, | |
| 434 | const void *elem | |
| 435 | ) { | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
436 | list->collection.sorted = true; // guaranteed by definition |
| 324 | 437 | const void *data = list->collection.store_pointer ? &elem : elem; |
| 438 | return list->cl->insert_sorted(list, data, 1) == 0; | |
| 439 | } | |
| 440 | ||
| 441 | /** | |
| 174 | 442 | * Inserts multiple items to the list at the specified index. |
| 440 | 443 | * If @p index equals the list size, this is effectively cxListAddArray(). |
| 174 | 444 | * |
| 445 | * This method is usually more efficient than invoking cxListInsert() | |
| 446 | * multiple times. | |
| 447 | * | |
| 448 | * If there is not enough memory to add all elements, the returned value is | |
| 440 | 449 | * less than @p n. |
| 174 | 450 | * |
| 440 | 451 | * If this list is storing pointers instead of objects @p array is expected to |
| 174 | 452 | * be an array of pointers. |
| 453 | * | |
| 454 | * @param list the list | |
| 455 | * @param index the index where to add the elements | |
| 456 | * @param array a pointer to the elements to add | |
| 457 | * @param n the number of elements to add | |
| 458 | * @return the number of added elements | |
| 459 | */ | |
| 440 | 460 | cx_attr_nonnull |
| 174 | 461 | static inline size_t cxListInsertArray( |
| 462 | CxList *list, | |
| 463 | size_t index, | |
| 324 | 464 | const void *array, |
| 174 | 465 | size_t n |
| 466 | ) { | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
467 | list->collection.sorted = false; |
| 174 | 468 | return list->cl->insert_array(list, index, array, n); |
| 469 | } | |
| 470 | ||
| 471 | /** | |
| 324 | 472 | * Inserts a sorted array into a sorted list. |
| 473 | * | |
| 474 | * This method is usually more efficient than inserting each element separately, | |
| 475 | * because consecutive chunks of sorted data are inserted in one pass. | |
| 476 | * | |
| 477 | * If there is not enough memory to add all elements, the returned value is | |
| 440 | 478 | * less than @p n. |
| 324 | 479 | * |
| 440 | 480 | * If this list is storing pointers instead of objects @p array is expected to |
| 324 | 481 | * be an array of pointers. |
| 482 | * | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
483 | * If the list is not sorted already, the behavior is undefined. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
484 | * |
| 324 | 485 | * @param list the list |
| 486 | * @param array a pointer to the elements to add | |
| 487 | * @param n the number of elements to add | |
| 488 | * @return the number of added elements | |
| 489 | */ | |
| 440 | 490 | cx_attr_nonnull |
| 324 | 491 | static inline size_t cxListInsertSortedArray( |
| 492 | CxList *list, | |
| 493 | const void *array, | |
| 494 | size_t n | |
| 495 | ) { | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
496 | list->collection.sorted = true; // guaranteed by definition |
| 324 | 497 | return list->cl->insert_sorted(list, array, n); |
| 498 | } | |
| 499 | ||
| 500 | /** | |
| 174 | 501 | * Inserts an element after the current location of the specified iterator. |
| 502 | * | |
| 503 | * The used iterator remains operational, but all other active iterators should | |
| 504 | * be considered invalidated. | |
| 505 | * | |
| 440 | 506 | * If @p iter is not a list iterator, the behavior is undefined. |
| 507 | * If @p iter is a past-the-end iterator, the new element gets appended to the list. | |
| 174 | 508 | * |
| 509 | * @param iter an iterator | |
| 510 | * @param elem the element to insert | |
| 440 | 511 | * @retval zero success |
| 512 | * @retval non-zero memory allocation failure | |
| 174 | 513 | * @see cxListInsert() |
| 514 | * @see cxListInsertBefore() | |
| 515 | */ | |
| 440 | 516 | cx_attr_nonnull |
| 174 | 517 | static inline int cxListInsertAfter( |
| 324 | 518 | CxIterator *iter, |
| 519 | const void *elem | |
| 174 | 520 | ) { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
521 | CxList* list = (CxList*)iter->src_handle.m; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
522 | list->collection.sorted = false; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
523 | return list->cl->insert_iter(iter, elem, 0); |
| 174 | 524 | } |
| 525 | ||
| 526 | /** | |
| 527 | * Inserts an element before the current location of the specified iterator. | |
| 528 | * | |
| 529 | * The used iterator remains operational, but all other active iterators should | |
| 530 | * be considered invalidated. | |
| 531 | * | |
| 440 | 532 | * If @p iter is not a list iterator, the behavior is undefined. |
| 533 | * If @p iter is a past-the-end iterator, the new element gets appended to the list. | |
| 174 | 534 | * |
| 535 | * @param iter an iterator | |
| 536 | * @param elem the element to insert | |
| 440 | 537 | * @retval zero success |
| 538 | * @retval non-zero memory allocation failure | |
| 174 | 539 | * @see cxListInsert() |
| 540 | * @see cxListInsertAfter() | |
| 541 | */ | |
| 440 | 542 | cx_attr_nonnull |
| 174 | 543 | static inline int cxListInsertBefore( |
| 324 | 544 | CxIterator *iter, |
| 545 | const void *elem | |
| 174 | 546 | ) { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
547 | CxList* list = (CxList*)iter->src_handle.m; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
548 | list->collection.sorted = false; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
549 | return list->cl->insert_iter(iter, elem, 1); |
| 174 | 550 | } |
| 551 | ||
| 552 | /** | |
| 553 | * Removes the element at the specified index. | |
| 554 | * | |
| 555 | * If an element destructor function is specified, it is called before | |
| 556 | * removing the element. | |
| 557 | * | |
| 558 | * @param list the list | |
| 559 | * @param index the index of the element | |
| 440 | 560 | * @retval zero success |
| 561 | * @retval non-zero index out of bounds | |
| 174 | 562 | */ |
| 440 | 563 | cx_attr_nonnull |
| 174 | 564 | static inline int cxListRemove( |
| 565 | CxList *list, | |
| 566 | size_t index | |
| 567 | ) { | |
| 440 | 568 | return list->cl->remove(list, index, 1, NULL) == 0; |
| 569 | } | |
| 570 | ||
| 571 | /** | |
| 572 | * Removes and returns the element at the specified index. | |
| 573 | * | |
| 574 | * No destructor is called and instead the element is copied to the | |
| 575 | * @p targetbuf which MUST be large enough to hold the removed element. | |
| 576 | * | |
| 577 | * @param list the list | |
| 578 | * @param index the index of the element | |
| 579 | * @param targetbuf a buffer where to copy the element | |
| 580 | * @retval zero success | |
| 581 | * @retval non-zero index out of bounds | |
| 582 | */ | |
| 583 | cx_attr_nonnull | |
| 584 | cx_attr_access_w(3) | |
| 585 | static 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 | ||
| 593 | /** | |
| 594 | * Removes multiple element starting at the specified index. | |
| 595 | * | |
| 596 | * If an element destructor function is specified, it is called for each | |
| 597 | * element. It is guaranteed that the destructor is called before removing | |
| 598 | * the element, however, due to possible optimizations it is neither guaranteed | |
| 599 | * that the destructors are invoked for all elements before starting to remove | |
| 600 | * them, nor that the element is removed immediately after the destructor call | |
| 601 | * before proceeding to the next element. | |
| 602 | * | |
| 603 | * @param list the list | |
| 604 | * @param index the index of the element | |
| 605 | * @param num the number of elements to remove | |
| 606 | * @return the actual number of removed elements | |
| 607 | */ | |
| 608 | cx_attr_nonnull | |
| 609 | static 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 | ||
| 617 | /** | |
| 618 | * Removes and returns multiple element starting at the specified index. | |
| 619 | * | |
| 620 | * No destructor is called and instead the elements are copied to the | |
| 621 | * @p targetbuf which MUST be large enough to hold all removed elements. | |
| 622 | * | |
| 623 | * @param list the list | |
| 624 | * @param index the index of the element | |
| 625 | * @param num the number of elements to remove | |
| 626 | * @param targetbuf a buffer where to copy the elements | |
| 627 | * @return the actual number of removed elements | |
| 628 | */ | |
| 629 | cx_attr_nonnull | |
| 630 | cx_attr_access_w(4) | |
| 631 | static 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); | |
| 174 | 638 | } |
| 639 | ||
| 640 | /** | |
| 641 | * Removes all elements from this list. | |
| 642 | * | |
| 440 | 643 | * If element destructor functions are specified, they are called for each |
| 174 | 644 | * element before removing them. |
| 645 | * | |
| 646 | * @param list the list | |
| 647 | */ | |
| 440 | 648 | cx_attr_nonnull |
| 174 | 649 | static inline void cxListClear(CxList *list) { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
650 | list->collection.sorted = true; // empty lists are always sorted |
| 174 | 651 | list->cl->clear(list); |
| 652 | } | |
| 653 | ||
| 654 | /** | |
| 655 | * Swaps two items in the list. | |
| 656 | * | |
| 657 | * Implementations should only allocate temporary memory for the swap, if | |
| 658 | * it is necessary. | |
| 659 | * | |
| 660 | * @param list the list | |
| 661 | * @param i the index of the first element | |
| 662 | * @param j the index of the second element | |
| 440 | 663 | * @retval zero success |
| 664 | * @retval non-zero one of the indices is out of bounds | |
| 665 | * or the swap needed extra memory but allocation failed | |
| 174 | 666 | */ |
| 440 | 667 | cx_attr_nonnull |
| 174 | 668 | static inline int cxListSwap( |
| 669 | CxList *list, | |
| 670 | size_t i, | |
| 671 | size_t j | |
| 672 | ) { | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
673 | list->collection.sorted = false; |
| 174 | 674 | return list->cl->swap(list, i, j); |
| 675 | } | |
| 676 | ||
| 677 | /** | |
| 678 | * Returns a pointer to the element at the specified index. | |
| 679 | * | |
| 680 | * @param list the list | |
| 681 | * @param index the index of the element | |
| 440 | 682 | * @return a pointer to the element or @c NULL if the index is out of bounds |
| 174 | 683 | */ |
| 440 | 684 | cx_attr_nonnull |
| 174 | 685 | static inline void *cxListAt( |
| 440 | 686 | const CxList *list, |
| 174 | 687 | size_t index |
| 688 | ) { | |
| 689 | return list->cl->at(list, index); | |
| 690 | } | |
| 691 | ||
| 692 | /** | |
| 693 | * Returns an iterator pointing to the item at the specified index. | |
| 694 | * | |
| 695 | * The returned iterator is position-aware. | |
| 696 | * | |
| 697 | * If the index is out of range, a past-the-end iterator will be returned. | |
| 698 | * | |
| 699 | * @param list the list | |
| 700 | * @param index the index where the iterator shall point at | |
| 701 | * @return a new iterator | |
| 702 | */ | |
| 440 | 703 | cx_attr_nonnull |
| 704 | cx_attr_nodiscard | |
| 174 | 705 | static inline CxIterator cxListIteratorAt( |
| 324 | 706 | const CxList *list, |
| 174 | 707 | size_t index |
| 708 | ) { | |
| 709 | return list->cl->iterator(list, index, false); | |
| 710 | } | |
| 711 | ||
| 712 | /** | |
| 713 | * Returns a backwards iterator pointing to the item at the specified index. | |
| 714 | * | |
| 715 | * The returned iterator is position-aware. | |
| 716 | * | |
| 717 | * If the index is out of range, a past-the-end iterator will be returned. | |
| 718 | * | |
| 719 | * @param list the list | |
| 720 | * @param index the index where the iterator shall point at | |
| 721 | * @return a new iterator | |
| 722 | */ | |
| 440 | 723 | cx_attr_nonnull |
| 724 | cx_attr_nodiscard | |
| 174 | 725 | static inline CxIterator cxListBackwardsIteratorAt( |
| 324 | 726 | const CxList *list, |
| 174 | 727 | size_t index |
| 728 | ) { | |
| 729 | return list->cl->iterator(list, index, true); | |
| 730 | } | |
| 731 | ||
| 732 | /** | |
| 733 | * Returns a mutating iterator pointing to the item at the specified index. | |
| 734 | * | |
| 735 | * The returned iterator is position-aware. | |
| 736 | * | |
| 737 | * If the index is out of range, a past-the-end iterator will be returned. | |
| 738 | * | |
| 739 | * @param list the list | |
| 740 | * @param index the index where the iterator shall point at | |
| 741 | * @return a new iterator | |
| 742 | */ | |
| 440 | 743 | cx_attr_nonnull |
| 744 | cx_attr_nodiscard | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
745 | cx_attr_export |
| 324 | 746 | CxIterator cxListMutIteratorAt( |
| 174 | 747 | CxList *list, |
| 748 | size_t index | |
| 749 | ); | |
| 750 | ||
| 751 | /** | |
| 752 | * Returns a mutating backwards iterator pointing to the item at the | |
| 753 | * specified index. | |
| 754 | * | |
| 755 | * The returned iterator is position-aware. | |
| 756 | * | |
| 757 | * If the index is out of range, a past-the-end iterator will be returned. | |
| 758 | * | |
| 759 | * @param list the list | |
| 760 | * @param index the index where the iterator shall point at | |
| 761 | * @return a new iterator | |
| 762 | */ | |
| 440 | 763 | cx_attr_nonnull |
| 764 | cx_attr_nodiscard | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
765 | cx_attr_export |
| 324 | 766 | CxIterator cxListMutBackwardsIteratorAt( |
| 174 | 767 | CxList *list, |
| 768 | size_t index | |
| 769 | ); | |
| 770 | ||
| 771 | /** | |
| 772 | * Returns an iterator pointing to the first item of the list. | |
| 773 | * | |
| 774 | * The returned iterator is position-aware. | |
| 775 | * | |
| 776 | * If the list is empty, a past-the-end iterator will be returned. | |
| 777 | * | |
| 778 | * @param list the list | |
| 779 | * @return a new iterator | |
| 780 | */ | |
| 440 | 781 | cx_attr_nonnull |
| 782 | cx_attr_nodiscard | |
| 324 | 783 | static inline CxIterator cxListIterator(const CxList *list) { |
| 174 | 784 | return list->cl->iterator(list, 0, false); |
| 785 | } | |
| 786 | ||
| 787 | /** | |
| 788 | * Returns a mutating iterator pointing to the first item of the list. | |
| 789 | * | |
| 790 | * The returned iterator is position-aware. | |
| 791 | * | |
| 792 | * If the list is empty, a past-the-end iterator will be returned. | |
| 793 | * | |
| 794 | * @param list the list | |
| 795 | * @return a new iterator | |
| 796 | */ | |
| 440 | 797 | cx_attr_nonnull |
| 798 | cx_attr_nodiscard | |
| 324 | 799 | static inline CxIterator cxListMutIterator(CxList *list) { |
| 174 | 800 | return cxListMutIteratorAt(list, 0); |
| 801 | } | |
| 802 | ||
| 803 | ||
| 804 | /** | |
| 805 | * Returns a backwards iterator pointing to the last item of the list. | |
| 806 | * | |
| 807 | * The returned iterator is position-aware. | |
| 808 | * | |
| 809 | * If the list is empty, a past-the-end iterator will be returned. | |
| 810 | * | |
| 811 | * @param list the list | |
| 812 | * @return a new iterator | |
| 813 | */ | |
| 440 | 814 | cx_attr_nonnull |
| 815 | cx_attr_nodiscard | |
| 324 | 816 | static inline CxIterator cxListBackwardsIterator(const CxList *list) { |
| 817 | return list->cl->iterator(list, list->collection.size - 1, true); | |
| 174 | 818 | } |
| 819 | ||
| 820 | /** | |
| 821 | * Returns a mutating backwards iterator pointing to the last item of the list. | |
| 822 | * | |
| 823 | * The returned iterator is position-aware. | |
| 824 | * | |
| 825 | * If the list is empty, a past-the-end iterator will be returned. | |
| 826 | * | |
| 827 | * @param list the list | |
| 828 | * @return a new iterator | |
| 829 | */ | |
| 440 | 830 | cx_attr_nonnull |
| 831 | cx_attr_nodiscard | |
| 324 | 832 | static inline CxIterator cxListMutBackwardsIterator(CxList *list) { |
| 833 | return cxListMutBackwardsIteratorAt(list, list->collection.size - 1); | |
| 174 | 834 | } |
| 835 | ||
| 836 | /** | |
| 440 | 837 | * Returns the index of the first element that equals @p elem. |
| 174 | 838 | * |
| 839 | * Determining equality is performed by the list's comparator function. | |
| 840 | * | |
| 841 | * @param list the list | |
| 842 | * @param elem the element to find | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
843 | * @return the index of the element or the size of the list when the element is not found |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
844 | * @see cxListIndexValid() |
| 174 | 845 | */ |
| 440 | 846 | cx_attr_nonnull |
| 847 | cx_attr_nodiscard | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
848 | static inline size_t cxListFind( |
| 324 | 849 | const CxList *list, |
| 850 | const void *elem | |
| 174 | 851 | ) { |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
852 | return list->cl->find_remove((CxList*)list, elem, false); |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
853 | } |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
854 | |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
855 | /** |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
856 | * Checks if the specified index is within bounds. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
857 | * |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
858 | * @param list the list |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
859 | * @param index the index |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
860 | * @retval true if the index is within bounds |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
861 | * @retval false if the index is out of bounds |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
862 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
863 | cx_attr_nonnull |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
864 | cx_attr_nodiscard |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
865 | static inline bool cxListIndexValid(const CxList *list, size_t index) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
866 | return index < list->collection.size; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
867 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
868 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
869 | /** |
| 440 | 870 | * Removes and returns the index of the first element that equals @p elem. |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
871 | * |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
872 | * Determining equality is performed by the list's comparator function. |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
873 | * |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
874 | * @param list the list |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
875 | * @param elem the element to find and remove |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
876 | * @return the index of the now removed element or the list size |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
877 | * when the element is not found or could not be removed |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
878 | * @see cxListIndexValid() |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
879 | */ |
| 440 | 880 | cx_attr_nonnull |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
881 | static inline size_t cxListFindRemove( |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
882 | CxList *list, |
| 324 | 883 | const void *elem |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
884 | ) { |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
885 | return list->cl->find_remove(list, elem, true); |
| 174 | 886 | } |
| 887 | ||
| 888 | /** | |
| 440 | 889 | * Sorts the list. |
| 174 | 890 | * |
| 440 | 891 | * @remark The underlying sort algorithm is implementation defined. |
| 174 | 892 | * |
| 893 | * @param list the list | |
| 894 | */ | |
| 440 | 895 | cx_attr_nonnull |
| 174 | 896 | static inline void cxListSort(CxList *list) { |
| 897 | list->cl->sort(list); | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
898 | list->collection.sorted = true; |
| 174 | 899 | } |
| 900 | ||
| 901 | /** | |
| 902 | * Reverses the order of the items. | |
| 903 | * | |
| 904 | * @param list the list | |
| 905 | */ | |
| 440 | 906 | cx_attr_nonnull |
| 174 | 907 | static inline void cxListReverse(CxList *list) { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
908 | // still sorted, but not according to the cmp_func |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
909 | list->collection.sorted = false; |
| 174 | 910 | list->cl->reverse(list); |
| 911 | } | |
| 912 | ||
| 913 | /** | |
| 914 | * Compares a list to another list of the same type. | |
| 915 | * | |
| 916 | * First, the list sizes are compared. | |
| 917 | * If they match, the lists are compared element-wise. | |
| 918 | * | |
| 919 | * @param list the list | |
| 920 | * @param other the list to compare to | |
| 440 | 921 | * @retval zero both lists are equal element wise |
| 922 | * @retval negative the first list is smaller | |
| 923 | * or the first non-equal element in the first list is smaller | |
| 924 | * @retval positive the first list is larger | |
| 925 | * or the first non-equal element in the first list is larger | |
| 174 | 926 | */ |
| 440 | 927 | cx_attr_nonnull |
| 928 | cx_attr_nodiscard | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
929 | cx_attr_export |
| 174 | 930 | int cxListCompare( |
| 324 | 931 | const CxList *list, |
| 932 | const CxList *other | |
| 174 | 933 | ); |
| 934 | ||
| 935 | /** | |
| 936 | * Deallocates the memory of the specified list structure. | |
| 937 | * | |
| 440 | 938 | * Also calls the content destructor functions for each element, if specified. |
| 174 | 939 | * |
| 440 | 940 | * @param list the list which shall be freed |
| 174 | 941 | */ |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
942 | cx_attr_export |
| 440 | 943 | void cxListFree(CxList *list); |
| 174 | 944 | |
| 945 | /** | |
| 946 | * A shared instance of an empty list. | |
| 947 | * | |
| 440 | 948 | * Writing to that list is not allowed. |
| 949 | * | |
| 950 | * You can use this is a placeholder for initializing CxList pointers | |
| 951 | * for which you do not want to reserve memory right from the beginning. | |
| 174 | 952 | */ |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
953 | cx_attr_export |
| 440 | 954 | extern CxList *const cxEmptyList; |
| 174 | 955 | |
| 956 | ||
| 957 | #ifdef __cplusplus | |
| 958 | } // extern "C" | |
| 959 | #endif | |
| 960 | ||
| 961 | #endif // UCX_LIST_H |