src/cx/list.h

Wed, 23 Nov 2022 22:40:55 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 23 Nov 2022 22:40:55 +0100
changeset 629
6c81ee4f11ad
parent 628
1e2be40f0cb5
child 630
ac5e7f789048
permissions
-rw-r--r--

#224 add cxListAddArray()

This also replaces cxLinkedListFromArray().

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@500 154 struct cx_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@500 205 struct cx_list_s *list,
universe@500 206 size_t index
universe@500 207 );
universe@500 208 };
universe@500 209
universe@500 210 /**
universe@464 211 * Common type for all list implementations.
universe@464 212 */
universe@500 213 typedef struct cx_list_s CxList;
universe@398 214
universe@464 215 /**
universe@464 216 * Adds an item to the end of the list.
universe@464 217 *
universe@464 218 * @param list the list
universe@464 219 * @param elem a pointer to the element to add
universe@464 220 * @return zero on success, non-zero on memory allocation failure
universe@629 221 * @see cxListAddArray()
universe@464 222 */
universe@508 223 __attribute__((__nonnull__))
universe@489 224 static inline int cxListAdd(
universe@500 225 CxList *list,
universe@489 226 void const *elem
universe@489 227 ) {
universe@469 228 return list->cl->add(list, elem);
universe@469 229 }
universe@398 230
universe@464 231 /**
universe@629 232 * Adds multiple items to the end of the list.
universe@629 233 *
universe@629 234 * This method is more efficient than invoking cxListAdd() multiple times.
universe@629 235 *
universe@629 236 * If there is not enough memory to add all elements, the returned value is
universe@629 237 * less than \p n.
universe@629 238 *
universe@629 239 * @param list the list
universe@629 240 * @param array a pointer to the elements to add
universe@629 241 * @param n the number of elements to add
universe@629 242 * @return the number of added elements
universe@629 243 */
universe@629 244 __attribute__((__nonnull__))
universe@629 245 static inline size_t cxListAddArray(
universe@629 246 CxList *list,
universe@629 247 void const *array,
universe@629 248 size_t n
universe@629 249 ) {
universe@629 250 return list->cl->add_array(list, array, n);
universe@629 251 }
universe@629 252
universe@629 253 /**
universe@464 254 * Inserts an item at the specified index.
universe@464 255 *
universe@464 256 * If \p index equals the list \c size, this is effectively cxListAdd().
universe@464 257 *
universe@464 258 * @param list the list
universe@464 259 * @param index the index the element shall have
universe@464 260 * @param elem a pointer to the element to add
universe@464 261 * @return zero on success, non-zero on memory allocation failure
universe@464 262 * or when the index is out of bounds
universe@499 263 * @see cxListInsertAfter()
universe@499 264 * @see cxListInsertBefore()
universe@464 265 */
universe@508 266 __attribute__((__nonnull__))
universe@489 267 static inline int cxListInsert(
universe@500 268 CxList *list,
universe@489 269 size_t index,
universe@489 270 void const *elem
universe@489 271 ) {
universe@469 272 return list->cl->insert(list, index, elem);
universe@469 273 }
universe@398 274
universe@464 275 /**
universe@499 276 * Inserts an element after the current location of the specified iterator.
universe@499 277 *
universe@499 278 * The used iterator remains operational, but all other active iterators should
universe@499 279 * be considered invalidated.
universe@499 280 *
universe@499 281 * If \p iter is not a list iterator, the behavior is undefined.
universe@499 282 * If \p iter is a past-the-end iterator, the new element gets appended to the list.
universe@499 283 *
universe@499 284 * @param iter an iterator
universe@499 285 * @param elem the element to insert
universe@499 286 * @return zero on success, non-zero on memory allocation failure
universe@499 287 * @see cxListInsert()
universe@499 288 * @see cxListInsertBefore()
universe@499 289 */
universe@508 290 __attribute__((__nonnull__))
universe@499 291 static inline int cxListInsertAfter(
universe@499 292 CxIterator *iter,
universe@499 293 void const *elem
universe@499 294 ) {
universe@500 295 return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 0);
universe@499 296 }
universe@499 297
universe@499 298 /**
universe@499 299 * Inserts an element before the current location of the specified iterator.
universe@499 300 *
universe@499 301 * The used iterator remains operational, but all other active iterators should
universe@499 302 * be considered invalidated.
universe@499 303 *
universe@499 304 * If \p iter is not a list iterator, the behavior is undefined.
universe@499 305 * If \p iter is a past-the-end iterator, the new element gets appended to the list.
universe@499 306 *
universe@499 307 * @param iter an iterator
universe@499 308 * @param elem the element to insert
universe@499 309 * @return zero on success, non-zero on memory allocation failure
universe@499 310 * @see cxListInsert()
universe@499 311 * @see cxListInsertAfter()
universe@499 312 */
universe@508 313 __attribute__((__nonnull__))
universe@499 314 static inline int cxListInsertBefore(
universe@499 315 CxIterator *iter,
universe@499 316 void const *elem
universe@499 317 ) {
universe@500 318 return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 1);
universe@499 319 }
universe@499 320
universe@499 321 /**
universe@464 322 * Removes the element at the specified index.
universe@464 323 * @param list the list
universe@464 324 * @param index the index of the element
universe@464 325 * @return zero on success, non-zero if the index is out of bounds
universe@464 326 */
universe@508 327 __attribute__((__nonnull__))
universe@489 328 static inline int cxListRemove(
universe@500 329 CxList *list,
universe@489 330 size_t index
universe@489 331 ) {
universe@469 332 return list->cl->remove(list, index);
universe@469 333 }
universe@398 334
universe@464 335 /**
universe@464 336 * Returns a pointer to the element at the specified index.
universe@464 337 *
universe@464 338 * @param list the list
universe@464 339 * @param index the index of the element
universe@464 340 * @return a pointer to the element or \c NULL if the index is out of bounds
universe@464 341 */
universe@508 342 __attribute__((__nonnull__))
universe@489 343 static inline void *cxListAt(
universe@500 344 CxList *list,
universe@489 345 size_t index
universe@489 346 ) {
universe@469 347 return list->cl->at(list, index);
universe@469 348 }
universe@439 349
universe@464 350 /**
universe@494 351 * Returns an iterator pointing to the item at the specified index.
universe@494 352 *
universe@494 353 * The returned iterator is position-aware.
universe@494 354 *
universe@494 355 * If the index is out of range, a past-the-end iterator will be returned.
universe@494 356 *
universe@494 357 * @param list the list
universe@494 358 * @param index the index where the iterator shall point at
universe@494 359 * @return a new iterator
universe@494 360 */
universe@508 361 __attribute__((__nonnull__, __warn_unused_result__))
universe@494 362 static inline CxIterator cxListIterator(
universe@500 363 CxList *list,
universe@494 364 size_t index
universe@494 365 ) {
universe@494 366 return list->cl->iterator(list, index);
universe@494 367 }
universe@494 368
universe@494 369 /**
universe@494 370 * Returns an iterator pointing to the first item of the list.
universe@494 371 *
universe@494 372 * The returned iterator is position-aware.
universe@494 373 *
universe@494 374 * If the list is empty, a past-the-end iterator will be returned.
universe@494 375 *
universe@494 376 * @param list the list
universe@494 377 * @return a new iterator
universe@494 378 */
universe@508 379 __attribute__((__nonnull__, __warn_unused_result__))
universe@500 380 static inline CxIterator cxListBegin(CxList *list) {
universe@494 381 return list->cl->iterator(list, 0);
universe@494 382 }
universe@494 383
universe@494 384 /**
universe@464 385 * Returns the index of the first element that equals \p elem.
universe@464 386 *
universe@464 387 * Determining equality is performed by the list's comparator function.
universe@464 388 *
universe@464 389 * @param list the list
universe@464 390 * @param elem the element to find
universe@464 391 * @return the index of the element or \c (size+1) if the element is not found
universe@464 392 */
universe@508 393 __attribute__((__nonnull__))
universe@489 394 static inline size_t cxListFind(
universe@621 395 CxList const *list,
universe@489 396 void const *elem
universe@489 397 ) {
universe@469 398 return list->cl->find(list, elem);
universe@469 399 }
universe@398 400
universe@464 401 /**
universe@469 402 * Sorts the list in place.
universe@469 403 *
universe@469 404 * \remark The underlying sort algorithm is implementation defined.
universe@469 405 *
universe@469 406 * @param list the list
universe@469 407 */
universe@508 408 __attribute__((__nonnull__))
universe@500 409 static inline void cxListSort(CxList *list) {
universe@469 410 list->cl->sort(list);
universe@469 411 }
universe@404 412
universe@488 413 /**
universe@490 414 * Reverses the order of the items.
universe@490 415 *
universe@490 416 * @param list the list
universe@490 417 */
universe@508 418 __attribute__((__nonnull__))
universe@500 419 static inline void cxListReverse(CxList *list) {
universe@490 420 list->cl->reverse(list);
universe@490 421 }
universe@490 422
universe@490 423 /**
universe@488 424 * Compares a list to another list of the same type.
universe@488 425 *
universe@618 426 * First, the list sizes are compared.
universe@618 427 * If they match, the lists are compared element-wise.
universe@488 428 *
universe@488 429 * @param list the list
universe@488 430 * @param other the list to compare to
universe@618 431 * @return zero, if both lists are equal element wise,
universe@618 432 * negative if the first list is smaller, positive if the first list is larger
universe@488 433 */
universe@508 434 __attribute__((__nonnull__))
universe@618 435 int cxListCompare(
universe@618 436 CxList const *list,
universe@618 437 CxList const *other
universe@618 438 );
universe@488 439
universe@503 440 /**
universe@528 441 * Deallocates the memory of the specified list structure.
universe@528 442 *
universe@528 443 * Also calls content a destructor function, depending on the configuration
universe@528 444 * in CxList.content_destructor_type.
universe@503 445 *
universe@503 446 * This function itself is a destructor function for the CxList.
universe@503 447 *
universe@528 448 * @param list the list which shall be destroyed
universe@503 449 */
universe@503 450 __attribute__((__nonnull__))
universe@528 451 void cxListDestroy(CxList *list);
universe@503 452
universe@415 453 #ifdef __cplusplus
universe@628 454 } // extern "C"
universe@415 455 #endif
universe@415 456
universe@628 457 #endif // UCX_LIST_H

mercurial