src/cx/list.h

Mon, 23 Jan 2023 20:22:11 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 23 Jan 2023 20:22:11 +0100
changeset 638
eafb45eefc51
parent 630
ac5e7f789048
child 640
55cc3b373c5e
permissions
-rw-r--r--

add cxListInsertArray() - fixes #224

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

mercurial