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