src/cx/list.h

Wed, 08 Feb 2023 20:26:09 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 08 Feb 2023 20:26:09 +0100
changeset 647
2e6e9d9f2159
parent 641
d402fead3386
child 655
7340c4255f1f
permissions
-rw-r--r--

implement swap function for list elements - fixes #218

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@647 167 * Member function for swapping two elements.
universe@647 168 */
universe@647 169 int (*swap)(
universe@647 170 struct cx_list_s *list,
universe@647 171 size_t i,
universe@647 172 size_t j
universe@647 173 );
universe@647 174
universe@647 175 /**
universe@500 176 * Member function for element lookup.
universe@500 177 */
universe@500 178 void *(*at)(
universe@500 179 struct cx_list_s const *list,
universe@500 180 size_t index
universe@500 181 );
universe@500 182
universe@500 183 /**
universe@500 184 * Member function for finding an element.
universe@500 185 */
universe@500 186 size_t (*find)(
universe@500 187 struct cx_list_s const *list,
universe@500 188 void const *elem
universe@500 189 );
universe@500 190
universe@500 191 /**
universe@500 192 * Member function for sorting the list in place.
universe@500 193 */
universe@500 194 void (*sort)(struct cx_list_s *list);
universe@500 195
universe@500 196 /**
universe@500 197 * Member function for comparing this list to another list of the same type.
universe@500 198 */
universe@500 199 int (*compare)(
universe@500 200 struct cx_list_s const *list,
universe@500 201 struct cx_list_s const *other
universe@500 202 );
universe@500 203
universe@500 204 /**
universe@500 205 * Member function for reversing the order of the items.
universe@500 206 */
universe@500 207 void (*reverse)(struct cx_list_s *list);
universe@500 208
universe@500 209 /**
universe@640 210 * Member function for returning an iterator pointing to the specified index.
universe@500 211 */
universe@500 212 struct cx_iterator_s (*iterator)(
universe@630 213 struct cx_list_s const *list,
universe@630 214 size_t index
universe@630 215 );
universe@500 216 };
universe@500 217
universe@500 218 /**
universe@464 219 * Common type for all list implementations.
universe@464 220 */
universe@500 221 typedef struct cx_list_s CxList;
universe@398 222
universe@464 223 /**
universe@641 224 * Advises the list to store copies of the objects (default mode of operation).
universe@641 225 *
universe@641 226 * Retrieving objects from this list will yield pointers to the copies stored
universe@641 227 * within this list.
universe@641 228 *
universe@641 229 * @param list the list
universe@641 230 * @see cxListStorePointers()
universe@641 231 */
universe@641 232 __attribute__((__nonnull__))
universe@641 233 void cxListStoreObjects(CxList *list);
universe@641 234
universe@641 235 /**
universe@641 236 * Advises the list to only store pointers to the objects.
universe@641 237 *
universe@641 238 * Retrieving objects from this list will yield the original pointers stored.
universe@641 239 *
universe@641 240 * @note This function forcibly sets the element size to the size of a pointer.
universe@641 241 * Invoking this function on a non-empty list that already stores copies of
universe@641 242 * objects is undefined.
universe@641 243 *
universe@641 244 * @param list the list
universe@641 245 * @see cxListStoreObjects()
universe@641 246 */
universe@641 247 __attribute__((__nonnull__))
universe@641 248 void cxListStorePointers(CxList *list);
universe@641 249
universe@641 250 /**
universe@641 251 * Returns true, if this list is storing pointers instead of the actual data.
universe@641 252 *
universe@641 253 * @param list
universe@641 254 * @return
universe@641 255 * @see cxListStorePointers()
universe@641 256 */
universe@641 257 __attribute__((__nonnull__))
universe@641 258 bool cxListIsStoringPointers(CxList *list);
universe@641 259
universe@641 260 /**
universe@464 261 * Adds an item to the end of the list.
universe@464 262 *
universe@464 263 * @param list the list
universe@464 264 * @param elem a pointer to the element to add
universe@464 265 * @return zero on success, non-zero on memory allocation failure
universe@629 266 * @see cxListAddArray()
universe@464 267 */
universe@508 268 __attribute__((__nonnull__))
universe@489 269 static inline int cxListAdd(
universe@500 270 CxList *list,
universe@489 271 void const *elem
universe@489 272 ) {
universe@641 273 return list->cl->insert_element(list, list->size, elem);
universe@469 274 }
universe@398 275
universe@464 276 /**
universe@629 277 * Adds multiple items to the end of the list.
universe@629 278 *
universe@629 279 * This method is more efficient than invoking cxListAdd() multiple times.
universe@629 280 *
universe@629 281 * If there is not enough memory to add all elements, the returned value is
universe@629 282 * less than \p n.
universe@629 283 *
universe@641 284 * If this list is storing pointers instead of objects \p array is expected to
universe@641 285 * be an array of pointers.
universe@641 286 *
universe@629 287 * @param list the list
universe@629 288 * @param array a pointer to the elements to add
universe@629 289 * @param n the number of elements to add
universe@629 290 * @return the number of added elements
universe@629 291 */
universe@629 292 __attribute__((__nonnull__))
universe@629 293 static inline size_t cxListAddArray(
universe@629 294 CxList *list,
universe@629 295 void const *array,
universe@629 296 size_t n
universe@629 297 ) {
universe@640 298 return list->cl->insert_array(list, list->size, array, n);
universe@629 299 }
universe@629 300
universe@629 301 /**
universe@464 302 * Inserts an item at the specified index.
universe@464 303 *
universe@464 304 * If \p index equals the list \c size, this is effectively cxListAdd().
universe@464 305 *
universe@464 306 * @param list the list
universe@464 307 * @param index the index the element shall have
universe@464 308 * @param elem a pointer to the element to add
universe@464 309 * @return zero on success, non-zero on memory allocation failure
universe@464 310 * or when the index is out of bounds
universe@499 311 * @see cxListInsertAfter()
universe@499 312 * @see cxListInsertBefore()
universe@464 313 */
universe@508 314 __attribute__((__nonnull__))
universe@489 315 static inline int cxListInsert(
universe@500 316 CxList *list,
universe@489 317 size_t index,
universe@489 318 void const *elem
universe@489 319 ) {
universe@641 320 return list->cl->insert_element(list, index, elem);
universe@469 321 }
universe@398 322
universe@464 323 /**
universe@638 324 * Inserts multiple items to the list at the specified index.
universe@638 325 * If \p index equals the list size, this is effectively cxListAddArray().
universe@638 326 *
universe@638 327 * This method is usually more efficient than invoking cxListInsert()
universe@638 328 * multiple times.
universe@638 329 *
universe@638 330 * If there is not enough memory to add all elements, the returned value is
universe@638 331 * less than \p n.
universe@638 332 *
universe@641 333 * If this list is storing pointers instead of objects \p array is expected to
universe@641 334 * be an array of pointers.
universe@641 335 *
universe@638 336 * @param list the list
universe@638 337 * @param index the index where to add the elements
universe@638 338 * @param array a pointer to the elements to add
universe@638 339 * @param n the number of elements to add
universe@638 340 * @return the number of added elements
universe@638 341 */
universe@638 342 __attribute__((__nonnull__))
universe@638 343 static inline size_t cxListInsertArray(
universe@638 344 CxList *list,
universe@638 345 size_t index,
universe@638 346 void const *array,
universe@638 347 size_t n
universe@638 348 ) {
universe@638 349 return list->cl->insert_array(list, index, array, n);
universe@638 350 }
universe@638 351
universe@638 352 /**
universe@499 353 * Inserts an element after the current location of the specified iterator.
universe@499 354 *
universe@499 355 * The used iterator remains operational, but all other active iterators should
universe@499 356 * be considered invalidated.
universe@499 357 *
universe@499 358 * If \p iter is not a list iterator, the behavior is undefined.
universe@499 359 * If \p iter is a past-the-end iterator, the new element gets appended to the list.
universe@499 360 *
universe@499 361 * @param iter an iterator
universe@499 362 * @param elem the element to insert
universe@499 363 * @return zero on success, non-zero on memory allocation failure
universe@499 364 * @see cxListInsert()
universe@499 365 * @see cxListInsertBefore()
universe@499 366 */
universe@508 367 __attribute__((__nonnull__))
universe@499 368 static inline int cxListInsertAfter(
universe@630 369 CxMutIterator *iter,
universe@499 370 void const *elem
universe@499 371 ) {
universe@500 372 return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 0);
universe@499 373 }
universe@499 374
universe@499 375 /**
universe@499 376 * Inserts an element before the current location of the specified iterator.
universe@499 377 *
universe@499 378 * The used iterator remains operational, but all other active iterators should
universe@499 379 * be considered invalidated.
universe@499 380 *
universe@499 381 * If \p iter is not a list iterator, the behavior is undefined.
universe@499 382 * If \p iter is a past-the-end iterator, the new element gets appended to the list.
universe@499 383 *
universe@499 384 * @param iter an iterator
universe@499 385 * @param elem the element to insert
universe@499 386 * @return zero on success, non-zero on memory allocation failure
universe@499 387 * @see cxListInsert()
universe@499 388 * @see cxListInsertAfter()
universe@499 389 */
universe@508 390 __attribute__((__nonnull__))
universe@499 391 static inline int cxListInsertBefore(
universe@630 392 CxMutIterator *iter,
universe@499 393 void const *elem
universe@499 394 ) {
universe@500 395 return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 1);
universe@499 396 }
universe@499 397
universe@499 398 /**
universe@464 399 * Removes the element at the specified index.
universe@464 400 * @param list the list
universe@464 401 * @param index the index of the element
universe@464 402 * @return zero on success, non-zero if the index is out of bounds
universe@464 403 */
universe@508 404 __attribute__((__nonnull__))
universe@489 405 static inline int cxListRemove(
universe@500 406 CxList *list,
universe@489 407 size_t index
universe@489 408 ) {
universe@469 409 return list->cl->remove(list, index);
universe@469 410 }
universe@398 411
universe@464 412 /**
universe@647 413 * Swaps two items in the list.
universe@647 414 *
universe@647 415 * Implementations should only allocate temporary memory for the swap, if
universe@647 416 * it is necessary.
universe@647 417 *
universe@647 418 * @param list the list
universe@647 419 * @param i the index of the first element
universe@647 420 * @param j the index of the second element
universe@647 421 * @return zero on success, non-zero if one of the indices is out of bounds
universe@647 422 */
universe@647 423 __attribute__((__nonnull__))
universe@647 424 static inline int cxListSwap(
universe@647 425 CxList *list,
universe@647 426 size_t i,
universe@647 427 size_t j
universe@647 428 ) {
universe@647 429 return list->cl->swap(list, i, j);
universe@647 430 }
universe@647 431
universe@647 432 /**
universe@464 433 * Returns a pointer to the element at the specified index.
universe@464 434 *
universe@464 435 * @param list the list
universe@464 436 * @param index the index of the element
universe@464 437 * @return a pointer to the element or \c NULL if the index is out of bounds
universe@464 438 */
universe@508 439 __attribute__((__nonnull__))
universe@489 440 static inline void *cxListAt(
universe@500 441 CxList *list,
universe@489 442 size_t index
universe@489 443 ) {
universe@469 444 return list->cl->at(list, index);
universe@469 445 }
universe@439 446
universe@464 447 /**
universe@494 448 * Returns an iterator pointing to the item at the specified index.
universe@494 449 *
universe@494 450 * The returned iterator is position-aware.
universe@494 451 *
universe@494 452 * If the index is out of range, a past-the-end iterator will be returned.
universe@494 453 *
universe@494 454 * @param list the list
universe@494 455 * @param index the index where the iterator shall point at
universe@494 456 * @return a new iterator
universe@494 457 */
universe@508 458 __attribute__((__nonnull__, __warn_unused_result__))
universe@494 459 static inline CxIterator cxListIterator(
universe@630 460 CxList const *list,
universe@630 461 size_t index
universe@630 462 ) {
universe@630 463 return list->cl->iterator(list, index);
universe@630 464 }
universe@630 465
universe@630 466 /**
universe@630 467 * Returns a mutating iterator pointing to the item at the specified index.
universe@630 468 *
universe@630 469 * The returned iterator is position-aware.
universe@630 470 *
universe@630 471 * If the index is out of range, a past-the-end iterator will be returned.
universe@630 472 *
universe@630 473 * @param list the list
universe@630 474 * @param index the index where the iterator shall point at
universe@630 475 * @return a new iterator
universe@630 476 */
universe@630 477 __attribute__((__nonnull__, __warn_unused_result__))
universe@640 478 CxMutIterator cxListMutIterator(
universe@500 479 CxList *list,
universe@494 480 size_t index
universe@640 481 );
universe@494 482
universe@494 483 /**
universe@494 484 * Returns an iterator pointing to the first item of the list.
universe@494 485 *
universe@494 486 * The returned iterator is position-aware.
universe@494 487 *
universe@494 488 * If the list is empty, a past-the-end iterator will be returned.
universe@494 489 *
universe@494 490 * @param list the list
universe@494 491 * @return a new iterator
universe@494 492 */
universe@508 493 __attribute__((__nonnull__, __warn_unused_result__))
universe@630 494 static inline CxIterator cxListBegin(CxList const *list) {
universe@494 495 return list->cl->iterator(list, 0);
universe@494 496 }
universe@494 497
universe@494 498 /**
universe@630 499 * Returns a mutating iterator pointing to the first item of the list.
universe@630 500 *
universe@630 501 * The returned iterator is position-aware.
universe@630 502 *
universe@630 503 * If the list is empty, a past-the-end iterator will be returned.
universe@630 504 *
universe@630 505 * @param list the list
universe@630 506 * @return a new iterator
universe@630 507 */
universe@630 508 __attribute__((__nonnull__, __warn_unused_result__))
universe@630 509 static inline CxMutIterator cxListBeginMut(CxList *list) {
universe@640 510 return cxListMutIterator(list, 0);
universe@630 511 }
universe@630 512
universe@630 513 /**
universe@464 514 * Returns the index of the first element that equals \p elem.
universe@464 515 *
universe@464 516 * Determining equality is performed by the list's comparator function.
universe@464 517 *
universe@464 518 * @param list the list
universe@464 519 * @param elem the element to find
universe@464 520 * @return the index of the element or \c (size+1) if the element is not found
universe@464 521 */
universe@508 522 __attribute__((__nonnull__))
universe@489 523 static inline size_t cxListFind(
universe@621 524 CxList const *list,
universe@489 525 void const *elem
universe@489 526 ) {
universe@469 527 return list->cl->find(list, elem);
universe@469 528 }
universe@398 529
universe@464 530 /**
universe@469 531 * Sorts the list in place.
universe@469 532 *
universe@469 533 * \remark The underlying sort algorithm is implementation defined.
universe@469 534 *
universe@469 535 * @param list the list
universe@469 536 */
universe@508 537 __attribute__((__nonnull__))
universe@500 538 static inline void cxListSort(CxList *list) {
universe@469 539 list->cl->sort(list);
universe@469 540 }
universe@404 541
universe@488 542 /**
universe@490 543 * Reverses the order of the items.
universe@490 544 *
universe@490 545 * @param list the list
universe@490 546 */
universe@508 547 __attribute__((__nonnull__))
universe@500 548 static inline void cxListReverse(CxList *list) {
universe@490 549 list->cl->reverse(list);
universe@490 550 }
universe@490 551
universe@490 552 /**
universe@488 553 * Compares a list to another list of the same type.
universe@488 554 *
universe@618 555 * First, the list sizes are compared.
universe@618 556 * If they match, the lists are compared element-wise.
universe@488 557 *
universe@488 558 * @param list the list
universe@488 559 * @param other the list to compare to
universe@618 560 * @return zero, if both lists are equal element wise,
universe@618 561 * negative if the first list is smaller, positive if the first list is larger
universe@488 562 */
universe@508 563 __attribute__((__nonnull__))
universe@618 564 int cxListCompare(
universe@618 565 CxList const *list,
universe@618 566 CxList const *other
universe@618 567 );
universe@488 568
universe@503 569 /**
universe@528 570 * Deallocates the memory of the specified list structure.
universe@528 571 *
universe@528 572 * Also calls content a destructor function, depending on the configuration
universe@528 573 * in CxList.content_destructor_type.
universe@503 574 *
universe@503 575 * This function itself is a destructor function for the CxList.
universe@503 576 *
universe@528 577 * @param list the list which shall be destroyed
universe@503 578 */
universe@503 579 __attribute__((__nonnull__))
universe@528 580 void cxListDestroy(CxList *list);
universe@503 581
universe@415 582 #ifdef __cplusplus
universe@628 583 } // extern "C"
universe@415 584 #endif
universe@415 585
universe@628 586 #endif // UCX_LIST_H

mercurial