src/cx/list.h

Tue, 14 Mar 2023 20:25:24 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 14 Mar 2023 20:25:24 +0100
changeset 664
af5bf4603a5d
parent 655
7340c4255f1f
child 666
b5dd654deb3b
permissions
-rw-r--r--

add cxListClear and fix missing destructor invocations - #241 #246

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

mercurial