src/cx/list.h

Wed, 25 Jan 2023 19:19:29 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 25 Jan 2023 19:19:29 +0100
changeset 640
55cc3b373c5e
parent 638
eafb45eefc51
child 641
d402fead3386
permissions
-rw-r--r--

simplify list class - fixes #236

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

mercurial