Tue, 28 Mar 2023 21:00:33 +0200
allow NULL for allocator and comparator
universe@390 | 1 | /* |
universe@390 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@390 | 3 | * |
universe@390 | 4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
universe@390 | 5 | * |
universe@390 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@390 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@390 | 8 | * |
universe@390 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@390 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@390 | 11 | * |
universe@390 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@390 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@390 | 14 | * documentation and/or other materials provided with the distribution. |
universe@390 | 15 | * |
universe@390 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@390 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@390 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@390 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@390 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@390 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@390 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@390 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@390 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@390 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@390 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@390 | 27 | */ |
universe@453 | 28 | /** |
universe@453 | 29 | * \file list.h |
universe@453 | 30 | * \brief Interface for list implementations. |
universe@453 | 31 | * \author Mike Becker |
universe@453 | 32 | * \author Olaf Wintermann |
universe@453 | 33 | * \version 3.0 |
universe@453 | 34 | * \copyright 2-Clause BSD License |
universe@453 | 35 | */ |
universe@390 | 36 | |
universe@390 | 37 | #ifndef UCX_LIST_H |
universe@390 | 38 | #define UCX_LIST_H |
universe@390 | 39 | |
universe@484 | 40 | #include "common.h" |
universe@398 | 41 | #include "allocator.h" |
universe@494 | 42 | #include "iterator.h" |
universe@398 | 43 | |
universe@415 | 44 | #ifdef __cplusplus |
universe@415 | 45 | extern "C" { |
universe@415 | 46 | #endif |
universe@415 | 47 | |
universe@667 | 48 | #ifndef CX_STORE_POINTERS |
universe@669 | 49 | /** |
universe@669 | 50 | * Special constant used for creating collections that are storing pointers. |
universe@669 | 51 | */ |
universe@667 | 52 | #define CX_STORE_POINTERS 0 |
universe@667 | 53 | #endif |
universe@667 | 54 | |
universe@464 | 55 | /** |
universe@464 | 56 | * A comparator function comparing two list elements. |
universe@464 | 57 | */ |
universe@489 | 58 | typedef int(*CxListComparator)( |
universe@489 | 59 | void const *left, |
universe@489 | 60 | void const *right |
universe@489 | 61 | ); |
universe@398 | 62 | |
universe@464 | 63 | /** |
universe@500 | 64 | * List class type. |
universe@464 | 65 | */ |
universe@500 | 66 | typedef struct cx_list_class_s cx_list_class; |
universe@435 | 67 | |
universe@464 | 68 | /** |
universe@464 | 69 | * Structure for holding the base data of a list. |
universe@464 | 70 | */ |
universe@435 | 71 | struct cx_list_s { |
universe@464 | 72 | /** |
universe@464 | 73 | * The list class definition. |
universe@464 | 74 | */ |
universe@641 | 75 | cx_list_class const *cl; |
universe@641 | 76 | /** |
universe@641 | 77 | * The actual implementation in case the list class is delegating. |
universe@641 | 78 | */ |
universe@641 | 79 | cx_list_class const *climpl; |
universe@464 | 80 | /** |
universe@464 | 81 | * The allocator to use. |
universe@464 | 82 | */ |
universe@508 | 83 | CxAllocator const *allocator; |
universe@464 | 84 | /** |
universe@464 | 85 | * The comparator function for the elements. |
universe@464 | 86 | */ |
universe@398 | 87 | CxListComparator cmpfunc; |
universe@464 | 88 | /** |
universe@464 | 89 | * The size of each element (payload only). |
universe@464 | 90 | */ |
universe@401 | 91 | size_t itemsize; |
universe@464 | 92 | /** |
universe@464 | 93 | * The size of the list (number of currently stored elements). |
universe@464 | 94 | */ |
universe@401 | 95 | size_t size; |
universe@464 | 96 | /** |
universe@464 | 97 | * The capacity of the list (maximum number of elements). |
universe@464 | 98 | */ |
universe@401 | 99 | size_t capacity; |
universe@528 | 100 | union { |
universe@528 | 101 | /** |
universe@528 | 102 | * An optional simple destructor for the list contents that admits the free() interface. |
universe@528 | 103 | * |
universe@528 | 104 | * @remark Set content_destructor_type to #CX_DESTRUCTOR_SIMPLE. |
universe@528 | 105 | * |
universe@528 | 106 | * @attention Read the documentation of the particular list implementation |
universe@528 | 107 | * whether this destructor shall only destroy the contents or also free the memory. |
universe@528 | 108 | */ |
universe@528 | 109 | cx_destructor_func simple_destructor; |
universe@528 | 110 | /** |
universe@528 | 111 | * An optional advanced destructor for the list contents providing additional data. |
universe@528 | 112 | * |
universe@528 | 113 | * @remark Set content_destructor_type to #CX_DESTRUCTOR_ADVANCED. |
universe@528 | 114 | * |
universe@528 | 115 | * @attention Read the documentation of the particular list implementation |
universe@528 | 116 | * whether this destructor shall only destroy the contents or also free the memory. |
universe@528 | 117 | */ |
universe@528 | 118 | cx_advanced_destructor advanced_destructor; |
universe@528 | 119 | }; |
universe@528 | 120 | /** |
universe@528 | 121 | * The type of destructor to use. |
universe@528 | 122 | */ |
universe@528 | 123 | enum cx_destructor_type content_destructor_type; |
universe@435 | 124 | }; |
universe@398 | 125 | |
universe@464 | 126 | /** |
universe@500 | 127 | * The class definition for arbitrary lists. |
universe@500 | 128 | */ |
universe@500 | 129 | struct cx_list_class_s { |
universe@500 | 130 | /** |
universe@524 | 131 | * Destructor function. |
universe@524 | 132 | */ |
universe@524 | 133 | void (*destructor)(struct cx_list_s *list); |
universe@524 | 134 | |
universe@524 | 135 | /** |
universe@641 | 136 | * Member function for inserting a single elements. |
universe@641 | 137 | * Implementors SHOULD see to performant implementations for corner cases. |
universe@641 | 138 | */ |
universe@641 | 139 | int (*insert_element)( |
universe@641 | 140 | struct cx_list_s *list, |
universe@641 | 141 | size_t index, |
universe@641 | 142 | void const *data |
universe@641 | 143 | ); |
universe@641 | 144 | |
universe@641 | 145 | /** |
universe@638 | 146 | * Member function for inserting multiple elements. |
universe@640 | 147 | * Implementors SHOULD see to performant implementations for corner cases. |
universe@638 | 148 | */ |
universe@638 | 149 | size_t (*insert_array)( |
universe@638 | 150 | struct cx_list_s *list, |
universe@638 | 151 | size_t index, |
universe@638 | 152 | void const *data, |
universe@638 | 153 | size_t n |
universe@638 | 154 | ); |
universe@638 | 155 | |
universe@638 | 156 | /** |
universe@500 | 157 | * Member function for inserting an element relative to an iterator position. |
universe@500 | 158 | */ |
universe@500 | 159 | int (*insert_iter)( |
universe@630 | 160 | struct cx_mut_iterator_s *iter, |
universe@500 | 161 | void const *elem, |
universe@500 | 162 | int prepend |
universe@500 | 163 | ); |
universe@500 | 164 | |
universe@500 | 165 | /** |
universe@500 | 166 | * Member function for removing an element. |
universe@500 | 167 | */ |
universe@500 | 168 | int (*remove)( |
universe@500 | 169 | struct cx_list_s *list, |
universe@500 | 170 | size_t index |
universe@500 | 171 | ); |
universe@500 | 172 | |
universe@500 | 173 | /** |
universe@664 | 174 | * Member function for removing all elements. |
universe@664 | 175 | */ |
universe@664 | 176 | void (*clear)(struct cx_list_s *list); |
universe@664 | 177 | |
universe@664 | 178 | /** |
universe@647 | 179 | * Member function for swapping two elements. |
universe@647 | 180 | */ |
universe@647 | 181 | int (*swap)( |
universe@647 | 182 | struct cx_list_s *list, |
universe@647 | 183 | size_t i, |
universe@647 | 184 | size_t j |
universe@647 | 185 | ); |
universe@647 | 186 | |
universe@647 | 187 | /** |
universe@500 | 188 | * Member function for element lookup. |
universe@500 | 189 | */ |
universe@500 | 190 | void *(*at)( |
universe@500 | 191 | struct cx_list_s const *list, |
universe@500 | 192 | size_t index |
universe@500 | 193 | ); |
universe@500 | 194 | |
universe@500 | 195 | /** |
universe@500 | 196 | * Member function for finding an element. |
universe@500 | 197 | */ |
universe@500 | 198 | size_t (*find)( |
universe@500 | 199 | struct cx_list_s const *list, |
universe@500 | 200 | void const *elem |
universe@500 | 201 | ); |
universe@500 | 202 | |
universe@500 | 203 | /** |
universe@500 | 204 | * Member function for sorting the list in place. |
universe@500 | 205 | */ |
universe@500 | 206 | void (*sort)(struct cx_list_s *list); |
universe@500 | 207 | |
universe@500 | 208 | /** |
universe@500 | 209 | * Member function for comparing this list to another list of the same type. |
universe@500 | 210 | */ |
universe@500 | 211 | int (*compare)( |
universe@500 | 212 | struct cx_list_s const *list, |
universe@500 | 213 | struct cx_list_s const *other |
universe@500 | 214 | ); |
universe@500 | 215 | |
universe@500 | 216 | /** |
universe@500 | 217 | * Member function for reversing the order of the items. |
universe@500 | 218 | */ |
universe@500 | 219 | void (*reverse)(struct cx_list_s *list); |
universe@500 | 220 | |
universe@500 | 221 | /** |
universe@640 | 222 | * Member function for returning an iterator pointing to the specified index. |
universe@500 | 223 | */ |
universe@500 | 224 | struct cx_iterator_s (*iterator)( |
universe@630 | 225 | struct cx_list_s const *list, |
universe@655 | 226 | size_t index, |
universe@655 | 227 | bool backward |
universe@630 | 228 | ); |
universe@500 | 229 | }; |
universe@500 | 230 | |
universe@500 | 231 | /** |
universe@464 | 232 | * Common type for all list implementations. |
universe@464 | 233 | */ |
universe@500 | 234 | typedef struct cx_list_s CxList; |
universe@398 | 235 | |
universe@464 | 236 | /** |
universe@666 | 237 | * Invokes the configured destructor function for a specific element. |
universe@664 | 238 | * |
universe@664 | 239 | * Usually only used by list implementations. There should be no need |
universe@664 | 240 | * to invoke this function manually. |
universe@664 | 241 | * |
universe@664 | 242 | * @param list the list |
universe@664 | 243 | * @param elem the element |
universe@664 | 244 | */ |
universe@664 | 245 | __attribute__((__nonnull__)) |
universe@664 | 246 | void cx_list_invoke_destructor( |
universe@664 | 247 | struct cx_list_s const *list, |
universe@664 | 248 | void *elem |
universe@664 | 249 | ); |
universe@664 | 250 | |
universe@664 | 251 | /** |
universe@666 | 252 | * Invokes the simple destructor function for a specific element. |
universe@666 | 253 | * |
universe@666 | 254 | * Usually only used by list implementations. There should be no need |
universe@666 | 255 | * to invoke this function manually. |
universe@666 | 256 | * |
universe@666 | 257 | * @param list the list |
universe@666 | 258 | * @param elem the element |
universe@666 | 259 | */ |
universe@666 | 260 | __attribute__((__nonnull__)) |
universe@666 | 261 | void cx_list_invoke_simple_destructor( |
universe@666 | 262 | struct cx_list_s const *list, |
universe@666 | 263 | void *elem |
universe@666 | 264 | ); |
universe@666 | 265 | |
universe@666 | 266 | /** |
universe@666 | 267 | * Invokes the advanced destructor function for a specific element. |
universe@666 | 268 | * |
universe@666 | 269 | * Usually only used by list implementations. There should be no need |
universe@666 | 270 | * to invoke this function manually. |
universe@666 | 271 | * |
universe@666 | 272 | * @param list the list |
universe@666 | 273 | * @param elem the element |
universe@666 | 274 | */ |
universe@666 | 275 | __attribute__((__nonnull__)) |
universe@666 | 276 | void cx_list_invoke_advanced_destructor( |
universe@666 | 277 | struct cx_list_s const *list, |
universe@666 | 278 | void *elem |
universe@666 | 279 | ); |
universe@666 | 280 | |
universe@666 | 281 | /** |
universe@641 | 282 | * Advises the list to store copies of the objects (default mode of operation). |
universe@641 | 283 | * |
universe@641 | 284 | * Retrieving objects from this list will yield pointers to the copies stored |
universe@641 | 285 | * within this list. |
universe@641 | 286 | * |
universe@641 | 287 | * @param list the list |
universe@641 | 288 | * @see cxListStorePointers() |
universe@641 | 289 | */ |
universe@641 | 290 | __attribute__((__nonnull__)) |
universe@641 | 291 | void cxListStoreObjects(CxList *list); |
universe@641 | 292 | |
universe@641 | 293 | /** |
universe@641 | 294 | * Advises the list to only store pointers to the objects. |
universe@641 | 295 | * |
universe@641 | 296 | * Retrieving objects from this list will yield the original pointers stored. |
universe@641 | 297 | * |
universe@641 | 298 | * @note This function forcibly sets the element size to the size of a pointer. |
universe@641 | 299 | * Invoking this function on a non-empty list that already stores copies of |
universe@641 | 300 | * objects is undefined. |
universe@641 | 301 | * |
universe@641 | 302 | * @param list the list |
universe@641 | 303 | * @see cxListStoreObjects() |
universe@641 | 304 | */ |
universe@641 | 305 | __attribute__((__nonnull__)) |
universe@641 | 306 | void cxListStorePointers(CxList *list); |
universe@641 | 307 | |
universe@641 | 308 | /** |
universe@641 | 309 | * Returns true, if this list is storing pointers instead of the actual data. |
universe@641 | 310 | * |
universe@641 | 311 | * @param list |
universe@641 | 312 | * @return |
universe@641 | 313 | * @see cxListStorePointers() |
universe@641 | 314 | */ |
universe@641 | 315 | __attribute__((__nonnull__)) |
universe@666 | 316 | bool cxListIsStoringPointers(CxList const *list); |
universe@641 | 317 | |
universe@641 | 318 | /** |
universe@464 | 319 | * Adds an item to the end of the list. |
universe@464 | 320 | * |
universe@464 | 321 | * @param list the list |
universe@464 | 322 | * @param elem a pointer to the element to add |
universe@464 | 323 | * @return zero on success, non-zero on memory allocation failure |
universe@629 | 324 | * @see cxListAddArray() |
universe@464 | 325 | */ |
universe@508 | 326 | __attribute__((__nonnull__)) |
universe@489 | 327 | static inline int cxListAdd( |
universe@500 | 328 | CxList *list, |
universe@489 | 329 | void const *elem |
universe@489 | 330 | ) { |
universe@641 | 331 | return list->cl->insert_element(list, list->size, elem); |
universe@469 | 332 | } |
universe@398 | 333 | |
universe@464 | 334 | /** |
universe@629 | 335 | * Adds multiple items to the end of the list. |
universe@629 | 336 | * |
universe@629 | 337 | * This method is more efficient than invoking cxListAdd() multiple times. |
universe@629 | 338 | * |
universe@629 | 339 | * If there is not enough memory to add all elements, the returned value is |
universe@629 | 340 | * less than \p n. |
universe@629 | 341 | * |
universe@641 | 342 | * If this list is storing pointers instead of objects \p array is expected to |
universe@641 | 343 | * be an array of pointers. |
universe@641 | 344 | * |
universe@629 | 345 | * @param list the list |
universe@629 | 346 | * @param array a pointer to the elements to add |
universe@629 | 347 | * @param n the number of elements to add |
universe@629 | 348 | * @return the number of added elements |
universe@629 | 349 | */ |
universe@629 | 350 | __attribute__((__nonnull__)) |
universe@629 | 351 | static inline size_t cxListAddArray( |
universe@629 | 352 | CxList *list, |
universe@629 | 353 | void const *array, |
universe@629 | 354 | size_t n |
universe@629 | 355 | ) { |
universe@640 | 356 | return list->cl->insert_array(list, list->size, array, n); |
universe@629 | 357 | } |
universe@629 | 358 | |
universe@629 | 359 | /** |
universe@464 | 360 | * Inserts an item at the specified index. |
universe@464 | 361 | * |
universe@464 | 362 | * If \p index equals the list \c size, this is effectively cxListAdd(). |
universe@464 | 363 | * |
universe@464 | 364 | * @param list the list |
universe@464 | 365 | * @param index the index the element shall have |
universe@464 | 366 | * @param elem a pointer to the element to add |
universe@464 | 367 | * @return zero on success, non-zero on memory allocation failure |
universe@464 | 368 | * or when the index is out of bounds |
universe@499 | 369 | * @see cxListInsertAfter() |
universe@499 | 370 | * @see cxListInsertBefore() |
universe@464 | 371 | */ |
universe@508 | 372 | __attribute__((__nonnull__)) |
universe@489 | 373 | static inline int cxListInsert( |
universe@500 | 374 | CxList *list, |
universe@489 | 375 | size_t index, |
universe@489 | 376 | void const *elem |
universe@489 | 377 | ) { |
universe@641 | 378 | return list->cl->insert_element(list, index, elem); |
universe@469 | 379 | } |
universe@398 | 380 | |
universe@464 | 381 | /** |
universe@638 | 382 | * Inserts multiple items to the list at the specified index. |
universe@638 | 383 | * If \p index equals the list size, this is effectively cxListAddArray(). |
universe@638 | 384 | * |
universe@638 | 385 | * This method is usually more efficient than invoking cxListInsert() |
universe@638 | 386 | * multiple times. |
universe@638 | 387 | * |
universe@638 | 388 | * If there is not enough memory to add all elements, the returned value is |
universe@638 | 389 | * less than \p n. |
universe@638 | 390 | * |
universe@641 | 391 | * If this list is storing pointers instead of objects \p array is expected to |
universe@641 | 392 | * be an array of pointers. |
universe@641 | 393 | * |
universe@638 | 394 | * @param list the list |
universe@638 | 395 | * @param index the index where to add the elements |
universe@638 | 396 | * @param array a pointer to the elements to add |
universe@638 | 397 | * @param n the number of elements to add |
universe@638 | 398 | * @return the number of added elements |
universe@638 | 399 | */ |
universe@638 | 400 | __attribute__((__nonnull__)) |
universe@638 | 401 | static inline size_t cxListInsertArray( |
universe@638 | 402 | CxList *list, |
universe@638 | 403 | size_t index, |
universe@638 | 404 | void const *array, |
universe@638 | 405 | size_t n |
universe@638 | 406 | ) { |
universe@638 | 407 | return list->cl->insert_array(list, index, array, n); |
universe@638 | 408 | } |
universe@638 | 409 | |
universe@638 | 410 | /** |
universe@499 | 411 | * Inserts an element after the current location of the specified iterator. |
universe@499 | 412 | * |
universe@499 | 413 | * The used iterator remains operational, but all other active iterators should |
universe@499 | 414 | * be considered invalidated. |
universe@499 | 415 | * |
universe@499 | 416 | * If \p iter is not a list iterator, the behavior is undefined. |
universe@499 | 417 | * If \p iter is a past-the-end iterator, the new element gets appended to the list. |
universe@499 | 418 | * |
universe@499 | 419 | * @param iter an iterator |
universe@499 | 420 | * @param elem the element to insert |
universe@499 | 421 | * @return zero on success, non-zero on memory allocation failure |
universe@499 | 422 | * @see cxListInsert() |
universe@499 | 423 | * @see cxListInsertBefore() |
universe@499 | 424 | */ |
universe@508 | 425 | __attribute__((__nonnull__)) |
universe@499 | 426 | static inline int cxListInsertAfter( |
universe@630 | 427 | CxMutIterator *iter, |
universe@499 | 428 | void const *elem |
universe@499 | 429 | ) { |
universe@500 | 430 | return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 0); |
universe@499 | 431 | } |
universe@499 | 432 | |
universe@499 | 433 | /** |
universe@499 | 434 | * Inserts an element before the current location of the specified iterator. |
universe@499 | 435 | * |
universe@499 | 436 | * The used iterator remains operational, but all other active iterators should |
universe@499 | 437 | * be considered invalidated. |
universe@499 | 438 | * |
universe@499 | 439 | * If \p iter is not a list iterator, the behavior is undefined. |
universe@499 | 440 | * If \p iter is a past-the-end iterator, the new element gets appended to the list. |
universe@499 | 441 | * |
universe@499 | 442 | * @param iter an iterator |
universe@499 | 443 | * @param elem the element to insert |
universe@499 | 444 | * @return zero on success, non-zero on memory allocation failure |
universe@499 | 445 | * @see cxListInsert() |
universe@499 | 446 | * @see cxListInsertAfter() |
universe@499 | 447 | */ |
universe@508 | 448 | __attribute__((__nonnull__)) |
universe@499 | 449 | static inline int cxListInsertBefore( |
universe@630 | 450 | CxMutIterator *iter, |
universe@499 | 451 | void const *elem |
universe@499 | 452 | ) { |
universe@500 | 453 | return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 1); |
universe@499 | 454 | } |
universe@499 | 455 | |
universe@499 | 456 | /** |
universe@464 | 457 | * Removes the element at the specified index. |
universe@664 | 458 | * |
universe@664 | 459 | * If an element destructor function is specified, it is called before |
universe@664 | 460 | * removing the element. |
universe@664 | 461 | * |
universe@464 | 462 | * @param list the list |
universe@464 | 463 | * @param index the index of the element |
universe@464 | 464 | * @return zero on success, non-zero if the index is out of bounds |
universe@464 | 465 | */ |
universe@508 | 466 | __attribute__((__nonnull__)) |
universe@489 | 467 | static inline int cxListRemove( |
universe@500 | 468 | CxList *list, |
universe@489 | 469 | size_t index |
universe@489 | 470 | ) { |
universe@469 | 471 | return list->cl->remove(list, index); |
universe@469 | 472 | } |
universe@398 | 473 | |
universe@464 | 474 | /** |
universe@664 | 475 | * Removes all elements from this list. |
universe@664 | 476 | * |
universe@664 | 477 | * If an element destructor function is specified, it is called for each |
universe@664 | 478 | * element before removing them. |
universe@664 | 479 | * |
universe@664 | 480 | * @param list the list |
universe@664 | 481 | */ |
universe@664 | 482 | __attribute__((__nonnull__)) |
universe@664 | 483 | static inline void cxListClear(CxList *list) { |
universe@664 | 484 | list->cl->clear(list); |
universe@664 | 485 | } |
universe@664 | 486 | |
universe@664 | 487 | /** |
universe@647 | 488 | * Swaps two items in the list. |
universe@647 | 489 | * |
universe@647 | 490 | * Implementations should only allocate temporary memory for the swap, if |
universe@647 | 491 | * it is necessary. |
universe@647 | 492 | * |
universe@647 | 493 | * @param list the list |
universe@647 | 494 | * @param i the index of the first element |
universe@647 | 495 | * @param j the index of the second element |
universe@647 | 496 | * @return zero on success, non-zero if one of the indices is out of bounds |
universe@647 | 497 | */ |
universe@647 | 498 | __attribute__((__nonnull__)) |
universe@647 | 499 | static inline int cxListSwap( |
universe@647 | 500 | CxList *list, |
universe@647 | 501 | size_t i, |
universe@647 | 502 | size_t j |
universe@647 | 503 | ) { |
universe@647 | 504 | return list->cl->swap(list, i, j); |
universe@647 | 505 | } |
universe@647 | 506 | |
universe@647 | 507 | /** |
universe@464 | 508 | * Returns a pointer to the element at the specified index. |
universe@464 | 509 | * |
universe@464 | 510 | * @param list the list |
universe@464 | 511 | * @param index the index of the element |
universe@464 | 512 | * @return a pointer to the element or \c NULL if the index is out of bounds |
universe@464 | 513 | */ |
universe@508 | 514 | __attribute__((__nonnull__)) |
universe@489 | 515 | static inline void *cxListAt( |
universe@500 | 516 | CxList *list, |
universe@489 | 517 | size_t index |
universe@489 | 518 | ) { |
universe@469 | 519 | return list->cl->at(list, index); |
universe@469 | 520 | } |
universe@439 | 521 | |
universe@464 | 522 | /** |
universe@494 | 523 | * Returns an iterator pointing to the item at the specified index. |
universe@494 | 524 | * |
universe@494 | 525 | * The returned iterator is position-aware. |
universe@494 | 526 | * |
universe@494 | 527 | * If the index is out of range, a past-the-end iterator will be returned. |
universe@494 | 528 | * |
universe@494 | 529 | * @param list the list |
universe@494 | 530 | * @param index the index where the iterator shall point at |
universe@494 | 531 | * @return a new iterator |
universe@494 | 532 | */ |
universe@508 | 533 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@655 | 534 | static inline CxIterator cxListIteratorAt( |
universe@630 | 535 | CxList const *list, |
universe@630 | 536 | size_t index |
universe@630 | 537 | ) { |
universe@655 | 538 | return list->cl->iterator(list, index, false); |
universe@655 | 539 | } |
universe@655 | 540 | |
universe@655 | 541 | /** |
universe@655 | 542 | * Returns a backwards iterator pointing to the item at the specified index. |
universe@655 | 543 | * |
universe@655 | 544 | * The returned iterator is position-aware. |
universe@655 | 545 | * |
universe@655 | 546 | * If the index is out of range, a past-the-end iterator will be returned. |
universe@655 | 547 | * |
universe@655 | 548 | * @param list the list |
universe@655 | 549 | * @param index the index where the iterator shall point at |
universe@655 | 550 | * @return a new iterator |
universe@655 | 551 | */ |
universe@655 | 552 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@655 | 553 | static inline CxIterator cxListBackwardsIteratorAt( |
universe@655 | 554 | CxList const *list, |
universe@655 | 555 | size_t index |
universe@655 | 556 | ) { |
universe@655 | 557 | return list->cl->iterator(list, index, true); |
universe@630 | 558 | } |
universe@630 | 559 | |
universe@630 | 560 | /** |
universe@630 | 561 | * Returns a mutating iterator pointing to the item at the specified index. |
universe@630 | 562 | * |
universe@630 | 563 | * The returned iterator is position-aware. |
universe@630 | 564 | * |
universe@630 | 565 | * If the index is out of range, a past-the-end iterator will be returned. |
universe@630 | 566 | * |
universe@630 | 567 | * @param list the list |
universe@630 | 568 | * @param index the index where the iterator shall point at |
universe@630 | 569 | * @return a new iterator |
universe@630 | 570 | */ |
universe@630 | 571 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@655 | 572 | CxMutIterator cxListMutIteratorAt( |
universe@655 | 573 | CxList *list, |
universe@655 | 574 | size_t index |
universe@655 | 575 | ); |
universe@655 | 576 | |
universe@655 | 577 | /** |
universe@655 | 578 | * Returns a mutating backwards iterator pointing to the item at the |
universe@655 | 579 | * specified index. |
universe@655 | 580 | * |
universe@655 | 581 | * The returned iterator is position-aware. |
universe@655 | 582 | * |
universe@655 | 583 | * If the index is out of range, a past-the-end iterator will be returned. |
universe@655 | 584 | * |
universe@655 | 585 | * @param list the list |
universe@655 | 586 | * @param index the index where the iterator shall point at |
universe@655 | 587 | * @return a new iterator |
universe@655 | 588 | */ |
universe@655 | 589 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@655 | 590 | CxMutIterator cxListMutBackwardsIteratorAt( |
universe@500 | 591 | CxList *list, |
universe@494 | 592 | size_t index |
universe@640 | 593 | ); |
universe@494 | 594 | |
universe@494 | 595 | /** |
universe@494 | 596 | * Returns an iterator pointing to the first item of the list. |
universe@494 | 597 | * |
universe@494 | 598 | * The returned iterator is position-aware. |
universe@494 | 599 | * |
universe@494 | 600 | * If the list is empty, a past-the-end iterator will be returned. |
universe@494 | 601 | * |
universe@494 | 602 | * @param list the list |
universe@494 | 603 | * @return a new iterator |
universe@494 | 604 | */ |
universe@508 | 605 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@655 | 606 | static inline CxIterator cxListIterator(CxList const *list) { |
universe@655 | 607 | return list->cl->iterator(list, 0, false); |
universe@494 | 608 | } |
universe@494 | 609 | |
universe@494 | 610 | /** |
universe@630 | 611 | * Returns a mutating iterator pointing to the first item of the list. |
universe@630 | 612 | * |
universe@630 | 613 | * The returned iterator is position-aware. |
universe@630 | 614 | * |
universe@630 | 615 | * If the list is empty, a past-the-end iterator will be returned. |
universe@630 | 616 | * |
universe@630 | 617 | * @param list the list |
universe@630 | 618 | * @return a new iterator |
universe@630 | 619 | */ |
universe@630 | 620 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@655 | 621 | static inline CxMutIterator cxListMutIterator(CxList *list) { |
universe@655 | 622 | return cxListMutIteratorAt(list, 0); |
universe@655 | 623 | } |
universe@655 | 624 | |
universe@655 | 625 | |
universe@655 | 626 | /** |
universe@655 | 627 | * Returns a backwards iterator pointing to the last item of the list. |
universe@655 | 628 | * |
universe@655 | 629 | * The returned iterator is position-aware. |
universe@655 | 630 | * |
universe@655 | 631 | * If the list is empty, a past-the-end iterator will be returned. |
universe@655 | 632 | * |
universe@655 | 633 | * @param list the list |
universe@655 | 634 | * @return a new iterator |
universe@655 | 635 | */ |
universe@655 | 636 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@655 | 637 | static inline CxIterator cxListBackwardsIterator(CxList const *list) { |
universe@655 | 638 | return list->cl->iterator(list, list->size - 1, true); |
universe@655 | 639 | } |
universe@655 | 640 | |
universe@655 | 641 | /** |
universe@655 | 642 | * Returns a mutating backwards iterator pointing to the last item of the list. |
universe@655 | 643 | * |
universe@655 | 644 | * The returned iterator is position-aware. |
universe@655 | 645 | * |
universe@655 | 646 | * If the list is empty, a past-the-end iterator will be returned. |
universe@655 | 647 | * |
universe@655 | 648 | * @param list the list |
universe@655 | 649 | * @return a new iterator |
universe@655 | 650 | */ |
universe@655 | 651 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@655 | 652 | static inline CxMutIterator cxListMutBackwardsIterator(CxList *list) { |
universe@655 | 653 | return cxListMutBackwardsIteratorAt(list, list->size - 1); |
universe@630 | 654 | } |
universe@630 | 655 | |
universe@630 | 656 | /** |
universe@464 | 657 | * Returns the index of the first element that equals \p elem. |
universe@464 | 658 | * |
universe@464 | 659 | * Determining equality is performed by the list's comparator function. |
universe@464 | 660 | * |
universe@464 | 661 | * @param list the list |
universe@464 | 662 | * @param elem the element to find |
universe@464 | 663 | * @return the index of the element or \c (size+1) if the element is not found |
universe@464 | 664 | */ |
universe@508 | 665 | __attribute__((__nonnull__)) |
universe@489 | 666 | static inline size_t cxListFind( |
universe@621 | 667 | CxList const *list, |
universe@489 | 668 | void const *elem |
universe@489 | 669 | ) { |
universe@469 | 670 | return list->cl->find(list, elem); |
universe@469 | 671 | } |
universe@398 | 672 | |
universe@464 | 673 | /** |
universe@469 | 674 | * Sorts the list in place. |
universe@469 | 675 | * |
universe@469 | 676 | * \remark The underlying sort algorithm is implementation defined. |
universe@469 | 677 | * |
universe@469 | 678 | * @param list the list |
universe@469 | 679 | */ |
universe@508 | 680 | __attribute__((__nonnull__)) |
universe@500 | 681 | static inline void cxListSort(CxList *list) { |
universe@469 | 682 | list->cl->sort(list); |
universe@469 | 683 | } |
universe@404 | 684 | |
universe@488 | 685 | /** |
universe@490 | 686 | * Reverses the order of the items. |
universe@490 | 687 | * |
universe@490 | 688 | * @param list the list |
universe@490 | 689 | */ |
universe@508 | 690 | __attribute__((__nonnull__)) |
universe@500 | 691 | static inline void cxListReverse(CxList *list) { |
universe@490 | 692 | list->cl->reverse(list); |
universe@490 | 693 | } |
universe@490 | 694 | |
universe@490 | 695 | /** |
universe@488 | 696 | * Compares a list to another list of the same type. |
universe@488 | 697 | * |
universe@618 | 698 | * First, the list sizes are compared. |
universe@618 | 699 | * If they match, the lists are compared element-wise. |
universe@488 | 700 | * |
universe@488 | 701 | * @param list the list |
universe@488 | 702 | * @param other the list to compare to |
universe@618 | 703 | * @return zero, if both lists are equal element wise, |
universe@618 | 704 | * negative if the first list is smaller, positive if the first list is larger |
universe@488 | 705 | */ |
universe@508 | 706 | __attribute__((__nonnull__)) |
universe@618 | 707 | int cxListCompare( |
universe@618 | 708 | CxList const *list, |
universe@618 | 709 | CxList const *other |
universe@618 | 710 | ); |
universe@488 | 711 | |
universe@503 | 712 | /** |
universe@528 | 713 | * Deallocates the memory of the specified list structure. |
universe@528 | 714 | * |
universe@528 | 715 | * Also calls content a destructor function, depending on the configuration |
universe@528 | 716 | * in CxList.content_destructor_type. |
universe@503 | 717 | * |
universe@503 | 718 | * This function itself is a destructor function for the CxList. |
universe@503 | 719 | * |
universe@528 | 720 | * @param list the list which shall be destroyed |
universe@503 | 721 | */ |
universe@503 | 722 | __attribute__((__nonnull__)) |
universe@528 | 723 | void cxListDestroy(CxList *list); |
universe@503 | 724 | |
universe@415 | 725 | #ifdef __cplusplus |
universe@628 | 726 | } // extern "C" |
universe@415 | 727 | #endif |
universe@415 | 728 | |
universe@628 | 729 | #endif // UCX_LIST_H |