src/cx/list.h

Sat, 26 Nov 2022 16:58:41 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 26 Nov 2022 16:58:41 +0100
changeset 630
ac5e7f789048
parent 629
6c81ee4f11ad
child 638
eafb45eefc51
permissions
-rw-r--r--

separate iterators and mutating iterators

Trade tons of code duplication for const-correctness.

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@435 68 cx_list_class *cl;
universe@464 69 /**
universe@464 70 * The allocator to use.
universe@464 71 */
universe@508 72 CxAllocator const *allocator;
universe@464 73 /**
universe@464 74 * The comparator function for the elements.
universe@464 75 */
universe@398 76 CxListComparator cmpfunc;
universe@464 77 /**
universe@464 78 * The size of each element (payload only).
universe@464 79 */
universe@401 80 size_t itemsize;
universe@464 81 /**
universe@464 82 * The size of the list (number of currently stored elements).
universe@464 83 */
universe@401 84 size_t size;
universe@464 85 /**
universe@464 86 * The capacity of the list (maximum number of elements).
universe@464 87 */
universe@401 88 size_t capacity;
universe@528 89 union {
universe@528 90 /**
universe@528 91 * An optional simple destructor for the list contents that admits the free() interface.
universe@528 92 *
universe@528 93 * @remark Set content_destructor_type to #CX_DESTRUCTOR_SIMPLE.
universe@528 94 *
universe@528 95 * @attention Read the documentation of the particular list implementation
universe@528 96 * whether this destructor shall only destroy the contents or also free the memory.
universe@528 97 */
universe@528 98 cx_destructor_func simple_destructor;
universe@528 99 /**
universe@528 100 * An optional advanced destructor for the list contents providing additional data.
universe@528 101 *
universe@528 102 * @remark Set content_destructor_type to #CX_DESTRUCTOR_ADVANCED.
universe@528 103 *
universe@528 104 * @attention Read the documentation of the particular list implementation
universe@528 105 * whether this destructor shall only destroy the contents or also free the memory.
universe@528 106 */
universe@528 107 cx_advanced_destructor advanced_destructor;
universe@528 108 };
universe@528 109 /**
universe@528 110 * The type of destructor to use.
universe@528 111 */
universe@528 112 enum cx_destructor_type content_destructor_type;
universe@435 113 };
universe@398 114
universe@464 115 /**
universe@500 116 * The class definition for arbitrary lists.
universe@500 117 */
universe@500 118 struct cx_list_class_s {
universe@500 119 /**
universe@524 120 * Destructor function.
universe@524 121 */
universe@524 122 void (*destructor)(struct cx_list_s *list);
universe@524 123
universe@524 124 /**
universe@500 125 * Member function for adding an element.
universe@500 126 */
universe@500 127 int (*add)(
universe@500 128 struct cx_list_s *list,
universe@500 129 void const *elem
universe@500 130 );
universe@500 131
universe@500 132 /**
universe@629 133 * Member function for adding multiple elements.
universe@629 134 */
universe@629 135 size_t (*add_array)(
universe@629 136 struct cx_list_s *list,
universe@629 137 void const *array,
universe@629 138 size_t n
universe@629 139 );
universe@629 140
universe@629 141 /**
universe@500 142 * Member function for inserting an element.
universe@500 143 */
universe@500 144 int (*insert)(
universe@500 145 struct cx_list_s *list,
universe@500 146 size_t index,
universe@500 147 void const *elem
universe@500 148 );
universe@500 149
universe@500 150 /**
universe@500 151 * Member function for inserting an element relative to an iterator position.
universe@500 152 */
universe@500 153 int (*insert_iter)(
universe@630 154 struct cx_mut_iterator_s *iter,
universe@500 155 void const *elem,
universe@500 156 int prepend
universe@500 157 );
universe@500 158
universe@500 159 /**
universe@500 160 * Member function for removing an element.
universe@500 161 */
universe@500 162 int (*remove)(
universe@500 163 struct cx_list_s *list,
universe@500 164 size_t index
universe@500 165 );
universe@500 166
universe@500 167 /**
universe@500 168 * Member function for element lookup.
universe@500 169 */
universe@500 170 void *(*at)(
universe@500 171 struct cx_list_s const *list,
universe@500 172 size_t index
universe@500 173 );
universe@500 174
universe@500 175 /**
universe@500 176 * Member function for finding an element.
universe@500 177 */
universe@500 178 size_t (*find)(
universe@500 179 struct cx_list_s const *list,
universe@500 180 void const *elem
universe@500 181 );
universe@500 182
universe@500 183 /**
universe@500 184 * Member function for sorting the list in place.
universe@500 185 */
universe@500 186 void (*sort)(struct cx_list_s *list);
universe@500 187
universe@500 188 /**
universe@500 189 * Member function for comparing this list to another list of the same type.
universe@500 190 */
universe@500 191 int (*compare)(
universe@500 192 struct cx_list_s const *list,
universe@500 193 struct cx_list_s const *other
universe@500 194 );
universe@500 195
universe@500 196 /**
universe@500 197 * Member function for reversing the order of the items.
universe@500 198 */
universe@500 199 void (*reverse)(struct cx_list_s *list);
universe@500 200
universe@500 201 /**
universe@500 202 * Returns an iterator pointing to the specified index.
universe@500 203 */
universe@500 204 struct cx_iterator_s (*iterator)(
universe@630 205 struct cx_list_s const *list,
universe@630 206 size_t index
universe@630 207 );
universe@630 208
universe@630 209 /**
universe@630 210 * Returns a mutating iterator pointing to the specified index.
universe@630 211 */
universe@630 212 struct cx_mut_iterator_s (*mut_iterator)(
universe@500 213 struct cx_list_s *list,
universe@500 214 size_t index
universe@500 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@464 224 * Adds an item to the end of the list.
universe@464 225 *
universe@464 226 * @param list the list
universe@464 227 * @param elem a pointer to the element to add
universe@464 228 * @return zero on success, non-zero on memory allocation failure
universe@629 229 * @see cxListAddArray()
universe@464 230 */
universe@508 231 __attribute__((__nonnull__))
universe@489 232 static inline int cxListAdd(
universe@500 233 CxList *list,
universe@489 234 void const *elem
universe@489 235 ) {
universe@469 236 return list->cl->add(list, elem);
universe@469 237 }
universe@398 238
universe@464 239 /**
universe@629 240 * Adds multiple items to the end of the list.
universe@629 241 *
universe@629 242 * This method is more efficient than invoking cxListAdd() multiple times.
universe@629 243 *
universe@629 244 * If there is not enough memory to add all elements, the returned value is
universe@629 245 * less than \p n.
universe@629 246 *
universe@629 247 * @param list the list
universe@629 248 * @param array a pointer to the elements to add
universe@629 249 * @param n the number of elements to add
universe@629 250 * @return the number of added elements
universe@629 251 */
universe@629 252 __attribute__((__nonnull__))
universe@629 253 static inline size_t cxListAddArray(
universe@629 254 CxList *list,
universe@629 255 void const *array,
universe@629 256 size_t n
universe@629 257 ) {
universe@629 258 return list->cl->add_array(list, array, n);
universe@629 259 }
universe@629 260
universe@629 261 /**
universe@464 262 * Inserts an item at the specified index.
universe@464 263 *
universe@464 264 * If \p index equals the list \c size, this is effectively cxListAdd().
universe@464 265 *
universe@464 266 * @param list the list
universe@464 267 * @param index the index the element shall have
universe@464 268 * @param elem a pointer to the element to add
universe@464 269 * @return zero on success, non-zero on memory allocation failure
universe@464 270 * or when the index is out of bounds
universe@499 271 * @see cxListInsertAfter()
universe@499 272 * @see cxListInsertBefore()
universe@464 273 */
universe@508 274 __attribute__((__nonnull__))
universe@489 275 static inline int cxListInsert(
universe@500 276 CxList *list,
universe@489 277 size_t index,
universe@489 278 void const *elem
universe@489 279 ) {
universe@469 280 return list->cl->insert(list, index, elem);
universe@469 281 }
universe@398 282
universe@464 283 /**
universe@499 284 * Inserts an element after the current location of the specified iterator.
universe@499 285 *
universe@499 286 * The used iterator remains operational, but all other active iterators should
universe@499 287 * be considered invalidated.
universe@499 288 *
universe@499 289 * If \p iter is not a list iterator, the behavior is undefined.
universe@499 290 * If \p iter is a past-the-end iterator, the new element gets appended to the list.
universe@499 291 *
universe@499 292 * @param iter an iterator
universe@499 293 * @param elem the element to insert
universe@499 294 * @return zero on success, non-zero on memory allocation failure
universe@499 295 * @see cxListInsert()
universe@499 296 * @see cxListInsertBefore()
universe@499 297 */
universe@508 298 __attribute__((__nonnull__))
universe@499 299 static inline int cxListInsertAfter(
universe@630 300 CxMutIterator *iter,
universe@499 301 void const *elem
universe@499 302 ) {
universe@500 303 return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 0);
universe@499 304 }
universe@499 305
universe@499 306 /**
universe@499 307 * Inserts an element before the current location of the specified iterator.
universe@499 308 *
universe@499 309 * The used iterator remains operational, but all other active iterators should
universe@499 310 * be considered invalidated.
universe@499 311 *
universe@499 312 * If \p iter is not a list iterator, the behavior is undefined.
universe@499 313 * If \p iter is a past-the-end iterator, the new element gets appended to the list.
universe@499 314 *
universe@499 315 * @param iter an iterator
universe@499 316 * @param elem the element to insert
universe@499 317 * @return zero on success, non-zero on memory allocation failure
universe@499 318 * @see cxListInsert()
universe@499 319 * @see cxListInsertAfter()
universe@499 320 */
universe@508 321 __attribute__((__nonnull__))
universe@499 322 static inline int cxListInsertBefore(
universe@630 323 CxMutIterator *iter,
universe@499 324 void const *elem
universe@499 325 ) {
universe@500 326 return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 1);
universe@499 327 }
universe@499 328
universe@499 329 /**
universe@464 330 * Removes the element at the specified index.
universe@464 331 * @param list the list
universe@464 332 * @param index the index of the element
universe@464 333 * @return zero on success, non-zero if the index is out of bounds
universe@464 334 */
universe@508 335 __attribute__((__nonnull__))
universe@489 336 static inline int cxListRemove(
universe@500 337 CxList *list,
universe@489 338 size_t index
universe@489 339 ) {
universe@469 340 return list->cl->remove(list, index);
universe@469 341 }
universe@398 342
universe@464 343 /**
universe@464 344 * Returns a pointer to the element at the specified index.
universe@464 345 *
universe@464 346 * @param list the list
universe@464 347 * @param index the index of the element
universe@464 348 * @return a pointer to the element or \c NULL if the index is out of bounds
universe@464 349 */
universe@508 350 __attribute__((__nonnull__))
universe@489 351 static inline void *cxListAt(
universe@500 352 CxList *list,
universe@489 353 size_t index
universe@489 354 ) {
universe@469 355 return list->cl->at(list, index);
universe@469 356 }
universe@439 357
universe@464 358 /**
universe@494 359 * Returns an iterator pointing to the item at the specified index.
universe@494 360 *
universe@494 361 * The returned iterator is position-aware.
universe@494 362 *
universe@494 363 * If the index is out of range, a past-the-end iterator will be returned.
universe@494 364 *
universe@494 365 * @param list the list
universe@494 366 * @param index the index where the iterator shall point at
universe@494 367 * @return a new iterator
universe@494 368 */
universe@508 369 __attribute__((__nonnull__, __warn_unused_result__))
universe@494 370 static inline CxIterator cxListIterator(
universe@630 371 CxList const *list,
universe@630 372 size_t index
universe@630 373 ) {
universe@630 374 return list->cl->iterator(list, index);
universe@630 375 }
universe@630 376
universe@630 377 /**
universe@630 378 * Returns a mutating iterator pointing to the item at the specified index.
universe@630 379 *
universe@630 380 * The returned iterator is position-aware.
universe@630 381 *
universe@630 382 * If the index is out of range, a past-the-end iterator will be returned.
universe@630 383 *
universe@630 384 * @param list the list
universe@630 385 * @param index the index where the iterator shall point at
universe@630 386 * @return a new iterator
universe@630 387 */
universe@630 388 __attribute__((__nonnull__, __warn_unused_result__))
universe@630 389 static inline CxMutIterator cxListMutIterator(
universe@500 390 CxList *list,
universe@494 391 size_t index
universe@494 392 ) {
universe@630 393 return list->cl->mut_iterator(list, index);
universe@494 394 }
universe@494 395
universe@494 396 /**
universe@494 397 * Returns an iterator pointing to the first item of the list.
universe@494 398 *
universe@494 399 * The returned iterator is position-aware.
universe@494 400 *
universe@494 401 * If the list is empty, a past-the-end iterator will be returned.
universe@494 402 *
universe@494 403 * @param list the list
universe@494 404 * @return a new iterator
universe@494 405 */
universe@508 406 __attribute__((__nonnull__, __warn_unused_result__))
universe@630 407 static inline CxIterator cxListBegin(CxList const *list) {
universe@494 408 return list->cl->iterator(list, 0);
universe@494 409 }
universe@494 410
universe@494 411 /**
universe@630 412 * Returns a mutating iterator pointing to the first item of the list.
universe@630 413 *
universe@630 414 * The returned iterator is position-aware.
universe@630 415 *
universe@630 416 * If the list is empty, a past-the-end iterator will be returned.
universe@630 417 *
universe@630 418 * @param list the list
universe@630 419 * @return a new iterator
universe@630 420 */
universe@630 421 __attribute__((__nonnull__, __warn_unused_result__))
universe@630 422 static inline CxMutIterator cxListBeginMut(CxList *list) {
universe@630 423 return list->cl->mut_iterator(list, 0);
universe@630 424 }
universe@630 425
universe@630 426 /**
universe@464 427 * Returns the index of the first element that equals \p elem.
universe@464 428 *
universe@464 429 * Determining equality is performed by the list's comparator function.
universe@464 430 *
universe@464 431 * @param list the list
universe@464 432 * @param elem the element to find
universe@464 433 * @return the index of the element or \c (size+1) if the element is not found
universe@464 434 */
universe@508 435 __attribute__((__nonnull__))
universe@489 436 static inline size_t cxListFind(
universe@621 437 CxList const *list,
universe@489 438 void const *elem
universe@489 439 ) {
universe@469 440 return list->cl->find(list, elem);
universe@469 441 }
universe@398 442
universe@464 443 /**
universe@469 444 * Sorts the list in place.
universe@469 445 *
universe@469 446 * \remark The underlying sort algorithm is implementation defined.
universe@469 447 *
universe@469 448 * @param list the list
universe@469 449 */
universe@508 450 __attribute__((__nonnull__))
universe@500 451 static inline void cxListSort(CxList *list) {
universe@469 452 list->cl->sort(list);
universe@469 453 }
universe@404 454
universe@488 455 /**
universe@490 456 * Reverses the order of the items.
universe@490 457 *
universe@490 458 * @param list the list
universe@490 459 */
universe@508 460 __attribute__((__nonnull__))
universe@500 461 static inline void cxListReverse(CxList *list) {
universe@490 462 list->cl->reverse(list);
universe@490 463 }
universe@490 464
universe@490 465 /**
universe@488 466 * Compares a list to another list of the same type.
universe@488 467 *
universe@618 468 * First, the list sizes are compared.
universe@618 469 * If they match, the lists are compared element-wise.
universe@488 470 *
universe@488 471 * @param list the list
universe@488 472 * @param other the list to compare to
universe@618 473 * @return zero, if both lists are equal element wise,
universe@618 474 * negative if the first list is smaller, positive if the first list is larger
universe@488 475 */
universe@508 476 __attribute__((__nonnull__))
universe@618 477 int cxListCompare(
universe@618 478 CxList const *list,
universe@618 479 CxList const *other
universe@618 480 );
universe@488 481
universe@503 482 /**
universe@528 483 * Deallocates the memory of the specified list structure.
universe@528 484 *
universe@528 485 * Also calls content a destructor function, depending on the configuration
universe@528 486 * in CxList.content_destructor_type.
universe@503 487 *
universe@503 488 * This function itself is a destructor function for the CxList.
universe@503 489 *
universe@528 490 * @param list the list which shall be destroyed
universe@503 491 */
universe@503 492 __attribute__((__nonnull__))
universe@528 493 void cxListDestroy(CxList *list);
universe@503 494
universe@415 495 #ifdef __cplusplus
universe@628 496 } // extern "C"
universe@415 497 #endif
universe@415 498
universe@628 499 #endif // UCX_LIST_H

mercurial