Wed, 27 Dec 2023 14:54:04 +0100
change default make target from 'compile test-compile' to 'check'
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 | * \copyright 2-Clause BSD License |
universe@453 | 34 | */ |
universe@390 | 35 | |
universe@390 | 36 | #ifndef UCX_LIST_H |
universe@390 | 37 | #define UCX_LIST_H |
universe@390 | 38 | |
universe@681 | 39 | #include "common.h" |
universe@677 | 40 | #include "collection.h" |
universe@398 | 41 | |
universe@415 | 42 | #ifdef __cplusplus |
universe@415 | 43 | extern "C" { |
universe@415 | 44 | #endif |
universe@415 | 45 | |
universe@464 | 46 | /** |
universe@500 | 47 | * List class type. |
universe@464 | 48 | */ |
universe@500 | 49 | typedef struct cx_list_class_s cx_list_class; |
universe@435 | 50 | |
universe@464 | 51 | /** |
universe@464 | 52 | * Structure for holding the base data of a list. |
universe@464 | 53 | */ |
universe@435 | 54 | struct cx_list_s { |
universe@677 | 55 | CX_COLLECTION_MEMBERS |
universe@464 | 56 | /** |
universe@464 | 57 | * The list class definition. |
universe@464 | 58 | */ |
universe@641 | 59 | cx_list_class const *cl; |
universe@641 | 60 | /** |
universe@641 | 61 | * The actual implementation in case the list class is delegating. |
universe@641 | 62 | */ |
universe@641 | 63 | cx_list_class const *climpl; |
universe@435 | 64 | }; |
universe@398 | 65 | |
universe@464 | 66 | /** |
universe@500 | 67 | * The class definition for arbitrary lists. |
universe@500 | 68 | */ |
universe@500 | 69 | struct cx_list_class_s { |
universe@500 | 70 | /** |
universe@524 | 71 | * Destructor function. |
universe@708 | 72 | * |
universe@708 | 73 | * Implementations SHALL invoke the content destructor functions if provided |
universe@708 | 74 | * and SHALL deallocate the list memory, if an allocator is provided. |
universe@524 | 75 | */ |
universe@524 | 76 | void (*destructor)(struct cx_list_s *list); |
universe@524 | 77 | |
universe@524 | 78 | /** |
olaf@739 | 79 | * Member function for inserting a single element. |
universe@641 | 80 | * Implementors SHOULD see to performant implementations for corner cases. |
universe@641 | 81 | */ |
universe@641 | 82 | int (*insert_element)( |
universe@641 | 83 | struct cx_list_s *list, |
universe@641 | 84 | size_t index, |
universe@641 | 85 | void const *data |
universe@641 | 86 | ); |
universe@641 | 87 | |
universe@641 | 88 | /** |
universe@638 | 89 | * Member function for inserting multiple elements. |
universe@640 | 90 | * Implementors SHOULD see to performant implementations for corner cases. |
universe@638 | 91 | */ |
universe@638 | 92 | size_t (*insert_array)( |
universe@638 | 93 | struct cx_list_s *list, |
universe@638 | 94 | size_t index, |
universe@638 | 95 | void const *data, |
universe@638 | 96 | size_t n |
universe@638 | 97 | ); |
universe@638 | 98 | |
universe@638 | 99 | /** |
universe@500 | 100 | * Member function for inserting an element relative to an iterator position. |
universe@500 | 101 | */ |
universe@500 | 102 | int (*insert_iter)( |
universe@630 | 103 | struct cx_mut_iterator_s *iter, |
universe@500 | 104 | void const *elem, |
universe@500 | 105 | int prepend |
universe@500 | 106 | ); |
universe@500 | 107 | |
universe@500 | 108 | /** |
universe@500 | 109 | * Member function for removing an element. |
universe@500 | 110 | */ |
universe@500 | 111 | int (*remove)( |
universe@500 | 112 | struct cx_list_s *list, |
universe@500 | 113 | size_t index |
universe@500 | 114 | ); |
universe@500 | 115 | |
universe@500 | 116 | /** |
universe@664 | 117 | * Member function for removing all elements. |
universe@664 | 118 | */ |
universe@664 | 119 | void (*clear)(struct cx_list_s *list); |
universe@664 | 120 | |
universe@664 | 121 | /** |
universe@647 | 122 | * Member function for swapping two elements. |
universe@647 | 123 | */ |
universe@647 | 124 | int (*swap)( |
universe@647 | 125 | struct cx_list_s *list, |
universe@647 | 126 | size_t i, |
universe@647 | 127 | size_t j |
universe@647 | 128 | ); |
universe@647 | 129 | |
universe@647 | 130 | /** |
universe@500 | 131 | * Member function for element lookup. |
universe@500 | 132 | */ |
universe@500 | 133 | void *(*at)( |
universe@500 | 134 | struct cx_list_s const *list, |
universe@500 | 135 | size_t index |
universe@500 | 136 | ); |
universe@500 | 137 | |
universe@500 | 138 | /** |
universe@764 | 139 | * Member function for finding and optionally removing an element. |
universe@500 | 140 | */ |
universe@764 | 141 | ssize_t (*find_remove)( |
universe@764 | 142 | struct cx_list_s *list, |
universe@764 | 143 | void const *elem, |
universe@764 | 144 | bool remove |
universe@500 | 145 | ); |
universe@500 | 146 | |
universe@500 | 147 | /** |
olaf@741 | 148 | * Member function for sorting the list in-place. |
universe@500 | 149 | */ |
universe@500 | 150 | void (*sort)(struct cx_list_s *list); |
universe@500 | 151 | |
universe@500 | 152 | /** |
universe@500 | 153 | * Member function for comparing this list to another list of the same type. |
universe@500 | 154 | */ |
universe@500 | 155 | int (*compare)( |
universe@500 | 156 | struct cx_list_s const *list, |
universe@500 | 157 | struct cx_list_s const *other |
universe@500 | 158 | ); |
universe@500 | 159 | |
universe@500 | 160 | /** |
universe@500 | 161 | * Member function for reversing the order of the items. |
universe@500 | 162 | */ |
universe@500 | 163 | void (*reverse)(struct cx_list_s *list); |
universe@500 | 164 | |
universe@500 | 165 | /** |
universe@640 | 166 | * Member function for returning an iterator pointing to the specified index. |
universe@500 | 167 | */ |
universe@500 | 168 | struct cx_iterator_s (*iterator)( |
universe@630 | 169 | struct cx_list_s const *list, |
universe@655 | 170 | size_t index, |
universe@655 | 171 | bool backward |
universe@630 | 172 | ); |
universe@500 | 173 | }; |
universe@500 | 174 | |
universe@500 | 175 | /** |
universe@464 | 176 | * Common type for all list implementations. |
universe@464 | 177 | */ |
universe@500 | 178 | typedef struct cx_list_s CxList; |
universe@398 | 179 | |
universe@464 | 180 | /** |
universe@641 | 181 | * Advises the list to store copies of the objects (default mode of operation). |
universe@641 | 182 | * |
universe@641 | 183 | * Retrieving objects from this list will yield pointers to the copies stored |
universe@641 | 184 | * within this list. |
universe@641 | 185 | * |
universe@641 | 186 | * @param list the list |
universe@641 | 187 | * @see cxListStorePointers() |
universe@641 | 188 | */ |
universe@641 | 189 | __attribute__((__nonnull__)) |
universe@641 | 190 | void cxListStoreObjects(CxList *list); |
universe@641 | 191 | |
universe@641 | 192 | /** |
universe@641 | 193 | * Advises the list to only store pointers to the objects. |
universe@641 | 194 | * |
universe@641 | 195 | * Retrieving objects from this list will yield the original pointers stored. |
universe@641 | 196 | * |
universe@641 | 197 | * @note This function forcibly sets the element size to the size of a pointer. |
universe@641 | 198 | * Invoking this function on a non-empty list that already stores copies of |
universe@641 | 199 | * objects is undefined. |
universe@641 | 200 | * |
universe@641 | 201 | * @param list the list |
universe@641 | 202 | * @see cxListStoreObjects() |
universe@641 | 203 | */ |
universe@641 | 204 | __attribute__((__nonnull__)) |
universe@641 | 205 | void cxListStorePointers(CxList *list); |
universe@641 | 206 | |
universe@641 | 207 | /** |
universe@641 | 208 | * Returns true, if this list is storing pointers instead of the actual data. |
universe@641 | 209 | * |
universe@641 | 210 | * @param list |
universe@677 | 211 | * @return true, if this list is storing pointers |
universe@641 | 212 | * @see cxListStorePointers() |
universe@641 | 213 | */ |
universe@641 | 214 | __attribute__((__nonnull__)) |
universe@677 | 215 | static inline bool cxListIsStoringPointers(CxList const *list) { |
universe@677 | 216 | return list->store_pointer; |
universe@677 | 217 | } |
universe@677 | 218 | |
universe@677 | 219 | /** |
universe@677 | 220 | * Returns the number of elements currently stored in the list. |
universe@677 | 221 | * |
universe@677 | 222 | * @param list the list |
universe@677 | 223 | * @return the number of currently stored elements |
universe@677 | 224 | */ |
universe@677 | 225 | __attribute__((__nonnull__)) |
universe@677 | 226 | static inline size_t cxListSize(CxList const *list) { |
universe@677 | 227 | return list->size; |
universe@677 | 228 | } |
universe@641 | 229 | |
universe@641 | 230 | /** |
universe@464 | 231 | * Adds an item to the end of the list. |
universe@464 | 232 | * |
universe@464 | 233 | * @param list the list |
universe@464 | 234 | * @param elem a pointer to the element to add |
universe@464 | 235 | * @return zero on success, non-zero on memory allocation failure |
universe@629 | 236 | * @see cxListAddArray() |
universe@464 | 237 | */ |
universe@508 | 238 | __attribute__((__nonnull__)) |
universe@489 | 239 | static inline int cxListAdd( |
universe@500 | 240 | CxList *list, |
universe@489 | 241 | void const *elem |
universe@489 | 242 | ) { |
universe@641 | 243 | return list->cl->insert_element(list, list->size, elem); |
universe@469 | 244 | } |
universe@398 | 245 | |
universe@464 | 246 | /** |
universe@629 | 247 | * Adds multiple items to the end of the list. |
universe@629 | 248 | * |
universe@629 | 249 | * This method is more efficient than invoking cxListAdd() multiple times. |
universe@629 | 250 | * |
universe@629 | 251 | * If there is not enough memory to add all elements, the returned value is |
universe@629 | 252 | * less than \p n. |
universe@629 | 253 | * |
universe@641 | 254 | * If this list is storing pointers instead of objects \p array is expected to |
universe@641 | 255 | * be an array of pointers. |
universe@641 | 256 | * |
universe@629 | 257 | * @param list the list |
universe@629 | 258 | * @param array a pointer to the elements to add |
universe@629 | 259 | * @param n the number of elements to add |
universe@629 | 260 | * @return the number of added elements |
universe@629 | 261 | */ |
universe@629 | 262 | __attribute__((__nonnull__)) |
universe@629 | 263 | static inline size_t cxListAddArray( |
universe@629 | 264 | CxList *list, |
universe@629 | 265 | void const *array, |
universe@629 | 266 | size_t n |
universe@629 | 267 | ) { |
universe@640 | 268 | return list->cl->insert_array(list, list->size, array, n); |
universe@629 | 269 | } |
universe@629 | 270 | |
universe@629 | 271 | /** |
universe@464 | 272 | * Inserts an item at the specified index. |
universe@464 | 273 | * |
universe@464 | 274 | * If \p index equals the list \c size, this is effectively cxListAdd(). |
universe@464 | 275 | * |
universe@464 | 276 | * @param list the list |
universe@464 | 277 | * @param index the index the element shall have |
universe@464 | 278 | * @param elem a pointer to the element to add |
universe@464 | 279 | * @return zero on success, non-zero on memory allocation failure |
universe@464 | 280 | * or when the index is out of bounds |
universe@499 | 281 | * @see cxListInsertAfter() |
universe@499 | 282 | * @see cxListInsertBefore() |
universe@464 | 283 | */ |
universe@508 | 284 | __attribute__((__nonnull__)) |
universe@489 | 285 | static inline int cxListInsert( |
universe@500 | 286 | CxList *list, |
universe@489 | 287 | size_t index, |
universe@489 | 288 | void const *elem |
universe@489 | 289 | ) { |
universe@641 | 290 | return list->cl->insert_element(list, index, elem); |
universe@469 | 291 | } |
universe@398 | 292 | |
universe@464 | 293 | /** |
universe@638 | 294 | * Inserts multiple items to the list at the specified index. |
universe@638 | 295 | * If \p index equals the list size, this is effectively cxListAddArray(). |
universe@638 | 296 | * |
universe@638 | 297 | * This method is usually more efficient than invoking cxListInsert() |
universe@638 | 298 | * multiple times. |
universe@638 | 299 | * |
universe@638 | 300 | * If there is not enough memory to add all elements, the returned value is |
universe@638 | 301 | * less than \p n. |
universe@638 | 302 | * |
universe@641 | 303 | * If this list is storing pointers instead of objects \p array is expected to |
universe@641 | 304 | * be an array of pointers. |
universe@641 | 305 | * |
universe@638 | 306 | * @param list the list |
universe@638 | 307 | * @param index the index where to add the elements |
universe@638 | 308 | * @param array a pointer to the elements to add |
universe@638 | 309 | * @param n the number of elements to add |
universe@638 | 310 | * @return the number of added elements |
universe@638 | 311 | */ |
universe@638 | 312 | __attribute__((__nonnull__)) |
universe@638 | 313 | static inline size_t cxListInsertArray( |
universe@638 | 314 | CxList *list, |
universe@638 | 315 | size_t index, |
universe@638 | 316 | void const *array, |
universe@638 | 317 | size_t n |
universe@638 | 318 | ) { |
universe@638 | 319 | return list->cl->insert_array(list, index, array, n); |
universe@638 | 320 | } |
universe@638 | 321 | |
universe@638 | 322 | /** |
universe@499 | 323 | * Inserts an element after the current location of the specified iterator. |
universe@499 | 324 | * |
universe@499 | 325 | * The used iterator remains operational, but all other active iterators should |
universe@499 | 326 | * be considered invalidated. |
universe@499 | 327 | * |
universe@499 | 328 | * If \p iter is not a list iterator, the behavior is undefined. |
universe@499 | 329 | * If \p iter is a past-the-end iterator, the new element gets appended to the list. |
universe@499 | 330 | * |
universe@499 | 331 | * @param iter an iterator |
universe@499 | 332 | * @param elem the element to insert |
universe@499 | 333 | * @return zero on success, non-zero on memory allocation failure |
universe@499 | 334 | * @see cxListInsert() |
universe@499 | 335 | * @see cxListInsertBefore() |
universe@499 | 336 | */ |
universe@508 | 337 | __attribute__((__nonnull__)) |
universe@499 | 338 | static inline int cxListInsertAfter( |
universe@630 | 339 | CxMutIterator *iter, |
universe@499 | 340 | void const *elem |
universe@499 | 341 | ) { |
universe@500 | 342 | return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 0); |
universe@499 | 343 | } |
universe@499 | 344 | |
universe@499 | 345 | /** |
universe@499 | 346 | * Inserts an element before the current location of the specified iterator. |
universe@499 | 347 | * |
universe@499 | 348 | * The used iterator remains operational, but all other active iterators should |
universe@499 | 349 | * be considered invalidated. |
universe@499 | 350 | * |
universe@499 | 351 | * If \p iter is not a list iterator, the behavior is undefined. |
universe@499 | 352 | * If \p iter is a past-the-end iterator, the new element gets appended to the list. |
universe@499 | 353 | * |
universe@499 | 354 | * @param iter an iterator |
universe@499 | 355 | * @param elem the element to insert |
universe@499 | 356 | * @return zero on success, non-zero on memory allocation failure |
universe@499 | 357 | * @see cxListInsert() |
universe@499 | 358 | * @see cxListInsertAfter() |
universe@499 | 359 | */ |
universe@508 | 360 | __attribute__((__nonnull__)) |
universe@499 | 361 | static inline int cxListInsertBefore( |
universe@630 | 362 | CxMutIterator *iter, |
universe@499 | 363 | void const *elem |
universe@499 | 364 | ) { |
universe@500 | 365 | return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 1); |
universe@499 | 366 | } |
universe@499 | 367 | |
universe@499 | 368 | /** |
universe@464 | 369 | * Removes the element at the specified index. |
universe@664 | 370 | * |
universe@664 | 371 | * If an element destructor function is specified, it is called before |
universe@664 | 372 | * removing the element. |
universe@664 | 373 | * |
universe@464 | 374 | * @param list the list |
universe@464 | 375 | * @param index the index of the element |
universe@464 | 376 | * @return zero on success, non-zero if the index is out of bounds |
universe@464 | 377 | */ |
universe@508 | 378 | __attribute__((__nonnull__)) |
universe@489 | 379 | static inline int cxListRemove( |
universe@500 | 380 | CxList *list, |
universe@489 | 381 | size_t index |
universe@489 | 382 | ) { |
universe@469 | 383 | return list->cl->remove(list, index); |
universe@469 | 384 | } |
universe@398 | 385 | |
universe@464 | 386 | /** |
universe@664 | 387 | * Removes all elements from this list. |
universe@664 | 388 | * |
universe@664 | 389 | * If an element destructor function is specified, it is called for each |
universe@664 | 390 | * element before removing them. |
universe@664 | 391 | * |
universe@664 | 392 | * @param list the list |
universe@664 | 393 | */ |
universe@664 | 394 | __attribute__((__nonnull__)) |
universe@664 | 395 | static inline void cxListClear(CxList *list) { |
universe@664 | 396 | list->cl->clear(list); |
universe@664 | 397 | } |
universe@664 | 398 | |
universe@664 | 399 | /** |
universe@647 | 400 | * Swaps two items in the list. |
universe@647 | 401 | * |
universe@647 | 402 | * Implementations should only allocate temporary memory for the swap, if |
universe@647 | 403 | * it is necessary. |
universe@647 | 404 | * |
universe@647 | 405 | * @param list the list |
universe@647 | 406 | * @param i the index of the first element |
universe@647 | 407 | * @param j the index of the second element |
universe@647 | 408 | * @return zero on success, non-zero if one of the indices is out of bounds |
universe@647 | 409 | */ |
universe@647 | 410 | __attribute__((__nonnull__)) |
universe@647 | 411 | static inline int cxListSwap( |
universe@647 | 412 | CxList *list, |
universe@647 | 413 | size_t i, |
universe@647 | 414 | size_t j |
universe@647 | 415 | ) { |
universe@647 | 416 | return list->cl->swap(list, i, j); |
universe@647 | 417 | } |
universe@647 | 418 | |
universe@647 | 419 | /** |
universe@464 | 420 | * Returns a pointer to the element at the specified index. |
universe@464 | 421 | * |
universe@464 | 422 | * @param list the list |
universe@464 | 423 | * @param index the index of the element |
universe@464 | 424 | * @return a pointer to the element or \c NULL if the index is out of bounds |
universe@464 | 425 | */ |
universe@508 | 426 | __attribute__((__nonnull__)) |
universe@489 | 427 | static inline void *cxListAt( |
universe@500 | 428 | CxList *list, |
universe@489 | 429 | size_t index |
universe@489 | 430 | ) { |
universe@469 | 431 | return list->cl->at(list, index); |
universe@469 | 432 | } |
universe@439 | 433 | |
universe@464 | 434 | /** |
universe@494 | 435 | * Returns an iterator pointing to the item at the specified index. |
universe@494 | 436 | * |
universe@494 | 437 | * The returned iterator is position-aware. |
universe@494 | 438 | * |
universe@494 | 439 | * If the index is out of range, a past-the-end iterator will be returned. |
universe@494 | 440 | * |
universe@494 | 441 | * @param list the list |
universe@494 | 442 | * @param index the index where the iterator shall point at |
universe@494 | 443 | * @return a new iterator |
universe@494 | 444 | */ |
universe@508 | 445 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@655 | 446 | static inline CxIterator cxListIteratorAt( |
universe@630 | 447 | CxList const *list, |
universe@630 | 448 | size_t index |
universe@630 | 449 | ) { |
universe@655 | 450 | return list->cl->iterator(list, index, false); |
universe@655 | 451 | } |
universe@655 | 452 | |
universe@655 | 453 | /** |
universe@655 | 454 | * Returns a backwards iterator pointing to the item at the specified index. |
universe@655 | 455 | * |
universe@655 | 456 | * The returned iterator is position-aware. |
universe@655 | 457 | * |
universe@655 | 458 | * If the index is out of range, a past-the-end iterator will be returned. |
universe@655 | 459 | * |
universe@655 | 460 | * @param list the list |
universe@655 | 461 | * @param index the index where the iterator shall point at |
universe@655 | 462 | * @return a new iterator |
universe@655 | 463 | */ |
universe@655 | 464 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@655 | 465 | static inline CxIterator cxListBackwardsIteratorAt( |
universe@655 | 466 | CxList const *list, |
universe@655 | 467 | size_t index |
universe@655 | 468 | ) { |
universe@655 | 469 | return list->cl->iterator(list, index, true); |
universe@630 | 470 | } |
universe@630 | 471 | |
universe@630 | 472 | /** |
universe@630 | 473 | * Returns a mutating iterator pointing to the item at the specified index. |
universe@630 | 474 | * |
universe@630 | 475 | * The returned iterator is position-aware. |
universe@630 | 476 | * |
universe@630 | 477 | * If the index is out of range, a past-the-end iterator will be returned. |
universe@630 | 478 | * |
universe@630 | 479 | * @param list the list |
universe@630 | 480 | * @param index the index where the iterator shall point at |
universe@630 | 481 | * @return a new iterator |
universe@630 | 482 | */ |
universe@630 | 483 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@655 | 484 | CxMutIterator cxListMutIteratorAt( |
universe@655 | 485 | CxList *list, |
universe@655 | 486 | size_t index |
universe@655 | 487 | ); |
universe@655 | 488 | |
universe@655 | 489 | /** |
universe@655 | 490 | * Returns a mutating backwards iterator pointing to the item at the |
universe@655 | 491 | * specified index. |
universe@655 | 492 | * |
universe@655 | 493 | * The returned iterator is position-aware. |
universe@655 | 494 | * |
universe@655 | 495 | * If the index is out of range, a past-the-end iterator will be returned. |
universe@655 | 496 | * |
universe@655 | 497 | * @param list the list |
universe@655 | 498 | * @param index the index where the iterator shall point at |
universe@655 | 499 | * @return a new iterator |
universe@655 | 500 | */ |
universe@655 | 501 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@655 | 502 | CxMutIterator cxListMutBackwardsIteratorAt( |
universe@500 | 503 | CxList *list, |
universe@494 | 504 | size_t index |
universe@640 | 505 | ); |
universe@494 | 506 | |
universe@494 | 507 | /** |
universe@494 | 508 | * Returns an iterator pointing to the first item of the list. |
universe@494 | 509 | * |
universe@494 | 510 | * The returned iterator is position-aware. |
universe@494 | 511 | * |
universe@494 | 512 | * If the list is empty, a past-the-end iterator will be returned. |
universe@494 | 513 | * |
universe@494 | 514 | * @param list the list |
universe@494 | 515 | * @return a new iterator |
universe@494 | 516 | */ |
universe@508 | 517 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@655 | 518 | static inline CxIterator cxListIterator(CxList const *list) { |
universe@655 | 519 | return list->cl->iterator(list, 0, false); |
universe@494 | 520 | } |
universe@494 | 521 | |
universe@494 | 522 | /** |
universe@630 | 523 | * Returns a mutating iterator pointing to the first item of the list. |
universe@630 | 524 | * |
universe@630 | 525 | * The returned iterator is position-aware. |
universe@630 | 526 | * |
universe@630 | 527 | * If the list is empty, a past-the-end iterator will be returned. |
universe@630 | 528 | * |
universe@630 | 529 | * @param list the list |
universe@630 | 530 | * @return a new iterator |
universe@630 | 531 | */ |
universe@630 | 532 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@655 | 533 | static inline CxMutIterator cxListMutIterator(CxList *list) { |
universe@655 | 534 | return cxListMutIteratorAt(list, 0); |
universe@655 | 535 | } |
universe@655 | 536 | |
universe@655 | 537 | |
universe@655 | 538 | /** |
universe@655 | 539 | * Returns a backwards iterator pointing to the last item of the list. |
universe@655 | 540 | * |
universe@655 | 541 | * The returned iterator is position-aware. |
universe@655 | 542 | * |
universe@655 | 543 | * If the list is empty, a past-the-end iterator will be returned. |
universe@655 | 544 | * |
universe@655 | 545 | * @param list the list |
universe@655 | 546 | * @return a new iterator |
universe@655 | 547 | */ |
universe@655 | 548 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@655 | 549 | static inline CxIterator cxListBackwardsIterator(CxList const *list) { |
universe@655 | 550 | return list->cl->iterator(list, list->size - 1, true); |
universe@655 | 551 | } |
universe@655 | 552 | |
universe@655 | 553 | /** |
universe@655 | 554 | * Returns a mutating backwards iterator pointing to the last item of the list. |
universe@655 | 555 | * |
universe@655 | 556 | * The returned iterator is position-aware. |
universe@655 | 557 | * |
universe@655 | 558 | * If the list is empty, a past-the-end iterator will be returned. |
universe@655 | 559 | * |
universe@655 | 560 | * @param list the list |
universe@655 | 561 | * @return a new iterator |
universe@655 | 562 | */ |
universe@655 | 563 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@655 | 564 | static inline CxMutIterator cxListMutBackwardsIterator(CxList *list) { |
universe@655 | 565 | return cxListMutBackwardsIteratorAt(list, list->size - 1); |
universe@630 | 566 | } |
universe@630 | 567 | |
universe@630 | 568 | /** |
universe@464 | 569 | * Returns the index of the first element that equals \p elem. |
universe@464 | 570 | * |
universe@464 | 571 | * Determining equality is performed by the list's comparator function. |
universe@464 | 572 | * |
universe@464 | 573 | * @param list the list |
universe@464 | 574 | * @param elem the element to find |
universe@699 | 575 | * @return the index of the element or a negative |
universe@699 | 576 | * value when the element is not found |
universe@464 | 577 | */ |
universe@508 | 578 | __attribute__((__nonnull__)) |
universe@699 | 579 | static inline ssize_t cxListFind( |
universe@621 | 580 | CxList const *list, |
universe@489 | 581 | void const *elem |
universe@489 | 582 | ) { |
universe@764 | 583 | return list->cl->find_remove((CxList*)list, elem, false); |
universe@764 | 584 | } |
universe@764 | 585 | |
universe@764 | 586 | /** |
universe@764 | 587 | * Removes and returns the index of the first element that equals \p elem. |
universe@764 | 588 | * |
universe@764 | 589 | * Determining equality is performed by the list's comparator function. |
universe@764 | 590 | * |
universe@764 | 591 | * @param list the list |
universe@764 | 592 | * @param elem the element to find and remove |
universe@764 | 593 | * @return the index of the now removed element or a negative |
universe@764 | 594 | * value when the element is not found or could not be removed |
universe@764 | 595 | */ |
universe@764 | 596 | __attribute__((__nonnull__)) |
universe@764 | 597 | static inline ssize_t cxListFindRemove( |
universe@764 | 598 | CxList *list, |
universe@764 | 599 | void const *elem |
universe@764 | 600 | ) { |
universe@764 | 601 | return list->cl->find_remove(list, elem, true); |
universe@469 | 602 | } |
universe@398 | 603 | |
universe@464 | 604 | /** |
olaf@741 | 605 | * Sorts the list in-place. |
universe@469 | 606 | * |
universe@469 | 607 | * \remark The underlying sort algorithm is implementation defined. |
universe@469 | 608 | * |
universe@469 | 609 | * @param list the list |
universe@469 | 610 | */ |
universe@508 | 611 | __attribute__((__nonnull__)) |
universe@500 | 612 | static inline void cxListSort(CxList *list) { |
universe@469 | 613 | list->cl->sort(list); |
universe@469 | 614 | } |
universe@404 | 615 | |
universe@488 | 616 | /** |
universe@490 | 617 | * Reverses the order of the items. |
universe@490 | 618 | * |
universe@490 | 619 | * @param list the list |
universe@490 | 620 | */ |
universe@508 | 621 | __attribute__((__nonnull__)) |
universe@500 | 622 | static inline void cxListReverse(CxList *list) { |
universe@490 | 623 | list->cl->reverse(list); |
universe@490 | 624 | } |
universe@490 | 625 | |
universe@490 | 626 | /** |
universe@488 | 627 | * Compares a list to another list of the same type. |
universe@488 | 628 | * |
universe@618 | 629 | * First, the list sizes are compared. |
universe@618 | 630 | * If they match, the lists are compared element-wise. |
universe@488 | 631 | * |
universe@488 | 632 | * @param list the list |
universe@488 | 633 | * @param other the list to compare to |
universe@618 | 634 | * @return zero, if both lists are equal element wise, |
universe@618 | 635 | * negative if the first list is smaller, positive if the first list is larger |
universe@488 | 636 | */ |
universe@508 | 637 | __attribute__((__nonnull__)) |
universe@618 | 638 | int cxListCompare( |
universe@618 | 639 | CxList const *list, |
universe@618 | 640 | CxList const *other |
universe@618 | 641 | ); |
universe@488 | 642 | |
universe@503 | 643 | /** |
universe@528 | 644 | * Deallocates the memory of the specified list structure. |
universe@528 | 645 | * |
universe@528 | 646 | * Also calls content a destructor function, depending on the configuration |
universe@528 | 647 | * in CxList.content_destructor_type. |
universe@503 | 648 | * |
universe@503 | 649 | * This function itself is a destructor function for the CxList. |
universe@503 | 650 | * |
universe@528 | 651 | * @param list the list which shall be destroyed |
universe@503 | 652 | */ |
universe@503 | 653 | __attribute__((__nonnull__)) |
universe@528 | 654 | void cxListDestroy(CxList *list); |
universe@503 | 655 | |
universe@704 | 656 | /** |
universe@704 | 657 | * A shared instance of an empty list. |
universe@704 | 658 | * |
universe@704 | 659 | * Writing to that list is undefined. |
universe@704 | 660 | */ |
universe@704 | 661 | extern CxList * const cxEmptyList; |
universe@704 | 662 | |
universe@704 | 663 | |
universe@415 | 664 | #ifdef __cplusplus |
universe@628 | 665 | } // extern "C" |
universe@415 | 666 | #endif |
universe@415 | 667 | |
universe@628 | 668 | #endif // UCX_LIST_H |