src/cx/list.h

Sat, 09 Apr 2022 16:37:43 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 09 Apr 2022 16:37:43 +0200
changeset 508
8aea65ae1eaf
parent 505
03e9151bea32
child 519
79d14e821b3a
permissions
-rw-r--r--

#168 - add attributes and const

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@503 74 * A mandatory destructor for the list structure.
universe@503 75 */
universe@503 76 cx_destructor_func list_destructor;
universe@503 77 /**
universe@503 78 * An optional destructor for the list contents.
universe@503 79 */
universe@503 80 cx_destructor_func content_destructor;
universe@503 81 /**
universe@464 82 * The comparator function for the elements.
universe@464 83 */
universe@398 84 CxListComparator cmpfunc;
universe@464 85 /**
universe@464 86 * The size of each element (payload only).
universe@464 87 */
universe@401 88 size_t itemsize;
universe@464 89 /**
universe@464 90 * The size of the list (number of currently stored elements).
universe@464 91 */
universe@401 92 size_t size;
universe@464 93 /**
universe@464 94 * The capacity of the list (maximum number of elements).
universe@464 95 */
universe@401 96 size_t capacity;
universe@503 97 /**
universe@503 98 * Flag indicating whether cxListDestroy() shall free the list structure,
universe@503 99 * even if cx_list_s.list_destructor did not do that.
universe@503 100 */
universe@503 101 bool autofree;
universe@503 102 /**
universe@503 103 * Flag indicating whether cxListDestroy() shall free each list element,
universe@503 104 * even if cx_list_s.content_destructor did not do that.
universe@503 105 */
universe@503 106 bool autofree_contents;
universe@435 107 };
universe@398 108
universe@464 109 /**
universe@500 110 * The class definition for arbitrary lists.
universe@500 111 */
universe@500 112 struct cx_list_class_s {
universe@500 113 /**
universe@500 114 * Member function for adding an element.
universe@500 115 */
universe@500 116 int (*add)(
universe@500 117 struct cx_list_s *list,
universe@500 118 void const *elem
universe@500 119 );
universe@500 120
universe@500 121 /**
universe@500 122 * Member function for inserting an element.
universe@500 123 */
universe@500 124 int (*insert)(
universe@500 125 struct cx_list_s *list,
universe@500 126 size_t index,
universe@500 127 void const *elem
universe@500 128 );
universe@500 129
universe@500 130 /**
universe@500 131 * Member function for inserting an element relative to an iterator position.
universe@500 132 */
universe@500 133 int (*insert_iter)(
universe@500 134 struct cx_iterator_s *iter,
universe@500 135 void const *elem,
universe@500 136 int prepend
universe@500 137 );
universe@500 138
universe@500 139 /**
universe@500 140 * Member function for removing an element.
universe@500 141 */
universe@500 142 int (*remove)(
universe@500 143 struct cx_list_s *list,
universe@500 144 size_t index
universe@500 145 );
universe@500 146
universe@500 147 /**
universe@500 148 * Member function for element lookup.
universe@500 149 */
universe@500 150 void *(*at)(
universe@500 151 struct cx_list_s const *list,
universe@500 152 size_t index
universe@500 153 );
universe@500 154
universe@500 155 /**
universe@500 156 * Member function for finding an element.
universe@500 157 */
universe@500 158 size_t (*find)(
universe@500 159 struct cx_list_s const *list,
universe@500 160 void const *elem
universe@500 161 );
universe@500 162
universe@500 163 /**
universe@500 164 * Member function for sorting the list in place.
universe@500 165 */
universe@500 166 void (*sort)(struct cx_list_s *list);
universe@500 167
universe@500 168 /**
universe@500 169 * Member function for comparing this list to another list of the same type.
universe@500 170 */
universe@500 171 int (*compare)(
universe@500 172 struct cx_list_s const *list,
universe@500 173 struct cx_list_s const *other
universe@500 174 );
universe@500 175
universe@500 176 /**
universe@500 177 * Member function for reversing the order of the items.
universe@500 178 */
universe@500 179 void (*reverse)(struct cx_list_s *list);
universe@500 180
universe@500 181 /**
universe@500 182 * Returns an iterator pointing to the specified index.
universe@500 183 */
universe@500 184 struct cx_iterator_s (*iterator)(
universe@500 185 struct cx_list_s *list,
universe@500 186 size_t index
universe@500 187 );
universe@500 188 };
universe@500 189
universe@500 190 /**
universe@464 191 * Common type for all list implementations.
universe@464 192 */
universe@500 193 typedef struct cx_list_s CxList;
universe@398 194
universe@464 195 /**
universe@505 196 * Convenience function to configure the memory management for this list.
universe@505 197 *
universe@504 198 * @param list the list to configure
universe@504 199 * @param list_destructor an alternative list destructor to use (if \c NULL, the current destructor remains unchanged)
universe@504 200 * @param content_destructor the content destructor to use (if \c NULL, no content destructor is used)
universe@504 201 * @param list_autofree a flag indicating, if the list allocator shall free the list, if the destructor did not do that
universe@504 202 * @param content_autofree a flag indicating, if the list allocator shall free an element,
universe@504 203 * if the content destructor did not do that or no content destructor exists
universe@504 204 */
universe@508 205 __attribute__((__nonnull__(1)))
universe@504 206 static inline void cxListMemoryMgmt(
universe@504 207 CxList *list,
universe@504 208 cx_destructor_func list_destructor,
universe@504 209 cx_destructor_func content_destructor,
universe@504 210 bool list_autofree,
universe@504 211 bool content_autofree
universe@504 212 ) {
universe@504 213 if (list_destructor != NULL) list->list_destructor = list_destructor;
universe@504 214 list->content_destructor = content_destructor;
universe@504 215 list->autofree = list_autofree;
universe@504 216 list->autofree_contents = content_autofree;
universe@504 217 }
universe@504 218
universe@504 219 /**
universe@464 220 * Adds an item to the end of the list.
universe@464 221 *
universe@464 222 * @param list the list
universe@464 223 * @param elem a pointer to the element to add
universe@464 224 * @return zero on success, non-zero on memory allocation failure
universe@464 225 */
universe@508 226 __attribute__((__nonnull__))
universe@489 227 static inline int cxListAdd(
universe@500 228 CxList *list,
universe@489 229 void const *elem
universe@489 230 ) {
universe@469 231 return list->cl->add(list, elem);
universe@469 232 }
universe@398 233
universe@464 234 /**
universe@464 235 * Inserts an item at the specified index.
universe@464 236 *
universe@464 237 * If \p index equals the list \c size, this is effectively cxListAdd().
universe@464 238 *
universe@464 239 * @param list the list
universe@464 240 * @param index the index the element shall have
universe@464 241 * @param elem a pointer to the element to add
universe@464 242 * @return zero on success, non-zero on memory allocation failure
universe@464 243 * or when the index is out of bounds
universe@499 244 * @see cxListInsertAfter()
universe@499 245 * @see cxListInsertBefore()
universe@464 246 */
universe@508 247 __attribute__((__nonnull__))
universe@489 248 static inline int cxListInsert(
universe@500 249 CxList *list,
universe@489 250 size_t index,
universe@489 251 void const *elem
universe@489 252 ) {
universe@469 253 return list->cl->insert(list, index, elem);
universe@469 254 }
universe@398 255
universe@464 256 /**
universe@499 257 * Inserts an element after the current location of the specified iterator.
universe@499 258 *
universe@499 259 * The used iterator remains operational, but all other active iterators should
universe@499 260 * be considered invalidated.
universe@499 261 *
universe@499 262 * If \p iter is not a list iterator, the behavior is undefined.
universe@499 263 * If \p iter is a past-the-end iterator, the new element gets appended to the list.
universe@499 264 *
universe@499 265 * @param iter an iterator
universe@499 266 * @param elem the element to insert
universe@499 267 * @return zero on success, non-zero on memory allocation failure
universe@499 268 * @see cxListInsert()
universe@499 269 * @see cxListInsertBefore()
universe@499 270 */
universe@508 271 __attribute__((__nonnull__))
universe@499 272 static inline int cxListInsertAfter(
universe@499 273 CxIterator *iter,
universe@499 274 void const *elem
universe@499 275 ) {
universe@500 276 return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 0);
universe@499 277 }
universe@499 278
universe@499 279 /**
universe@499 280 * Inserts an element before the current location of the specified iterator.
universe@499 281 *
universe@499 282 * The used iterator remains operational, but all other active iterators should
universe@499 283 * be considered invalidated.
universe@499 284 *
universe@499 285 * If \p iter is not a list iterator, the behavior is undefined.
universe@499 286 * If \p iter is a past-the-end iterator, the new element gets appended to the list.
universe@499 287 *
universe@499 288 * @param iter an iterator
universe@499 289 * @param elem the element to insert
universe@499 290 * @return zero on success, non-zero on memory allocation failure
universe@499 291 * @see cxListInsert()
universe@499 292 * @see cxListInsertAfter()
universe@499 293 */
universe@508 294 __attribute__((__nonnull__))
universe@499 295 static inline int cxListInsertBefore(
universe@499 296 CxIterator *iter,
universe@499 297 void const *elem
universe@499 298 ) {
universe@500 299 return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 1);
universe@499 300 }
universe@499 301
universe@499 302 /**
universe@464 303 * Removes the element at the specified index.
universe@464 304 * @param list the list
universe@464 305 * @param index the index of the element
universe@464 306 * @return zero on success, non-zero if the index is out of bounds
universe@464 307 */
universe@508 308 __attribute__((__nonnull__))
universe@489 309 static inline int cxListRemove(
universe@500 310 CxList *list,
universe@489 311 size_t index
universe@489 312 ) {
universe@469 313 return list->cl->remove(list, index);
universe@469 314 }
universe@398 315
universe@464 316 /**
universe@464 317 * Returns a pointer to the element at the specified index.
universe@464 318 *
universe@464 319 * @param list the list
universe@464 320 * @param index the index of the element
universe@464 321 * @return a pointer to the element or \c NULL if the index is out of bounds
universe@464 322 */
universe@508 323 __attribute__((__nonnull__))
universe@489 324 static inline void *cxListAt(
universe@500 325 CxList *list,
universe@489 326 size_t index
universe@489 327 ) {
universe@469 328 return list->cl->at(list, index);
universe@469 329 }
universe@439 330
universe@464 331 /**
universe@494 332 * Returns an iterator pointing to the item at the specified index.
universe@494 333 *
universe@494 334 * The returned iterator is position-aware.
universe@494 335 *
universe@494 336 * If the index is out of range, a past-the-end iterator will be returned.
universe@494 337 *
universe@494 338 * @param list the list
universe@494 339 * @param index the index where the iterator shall point at
universe@494 340 * @return a new iterator
universe@494 341 */
universe@508 342 __attribute__((__nonnull__, __warn_unused_result__))
universe@494 343 static inline CxIterator cxListIterator(
universe@500 344 CxList *list,
universe@494 345 size_t index
universe@494 346 ) {
universe@494 347 return list->cl->iterator(list, index);
universe@494 348 }
universe@494 349
universe@494 350 /**
universe@494 351 * Returns an iterator pointing to the first item of the list.
universe@494 352 *
universe@494 353 * The returned iterator is position-aware.
universe@494 354 *
universe@494 355 * If the list is empty, a past-the-end iterator will be returned.
universe@494 356 *
universe@494 357 * @param list the list
universe@494 358 * @return a new iterator
universe@494 359 */
universe@508 360 __attribute__((__nonnull__, __warn_unused_result__))
universe@500 361 static inline CxIterator cxListBegin(CxList *list) {
universe@494 362 return list->cl->iterator(list, 0);
universe@494 363 }
universe@494 364
universe@494 365 /**
universe@464 366 * Returns the index of the first element that equals \p elem.
universe@464 367 *
universe@464 368 * Determining equality is performed by the list's comparator function.
universe@464 369 *
universe@464 370 * @param list the list
universe@464 371 * @param elem the element to find
universe@464 372 * @return the index of the element or \c (size+1) if the element is not found
universe@464 373 */
universe@508 374 __attribute__((__nonnull__))
universe@489 375 static inline size_t cxListFind(
universe@500 376 CxList *list,
universe@489 377 void const *elem
universe@489 378 ) {
universe@469 379 return list->cl->find(list, elem);
universe@469 380 }
universe@398 381
universe@464 382 /**
universe@469 383 * Sorts the list in place.
universe@469 384 *
universe@469 385 * \remark The underlying sort algorithm is implementation defined.
universe@469 386 *
universe@469 387 * @param list the list
universe@469 388 */
universe@508 389 __attribute__((__nonnull__))
universe@500 390 static inline void cxListSort(CxList *list) {
universe@469 391 list->cl->sort(list);
universe@469 392 }
universe@404 393
universe@488 394 /**
universe@490 395 * Reverses the order of the items.
universe@490 396 *
universe@490 397 * @param list the list
universe@490 398 */
universe@508 399 __attribute__((__nonnull__))
universe@500 400 static inline void cxListReverse(CxList *list) {
universe@490 401 list->cl->reverse(list);
universe@490 402 }
universe@490 403
universe@490 404 /**
universe@488 405 * Compares a list to another list of the same type.
universe@488 406 *
universe@488 407 * First, the list sizes are compared. If they match, the lists are compared element-wise.
universe@488 408 *
universe@488 409 * @param list the list
universe@488 410 * @param other the list to compare to
universe@488 411 * @return zero, if both lists are equal element wise, negative if the first list is smaller, zero if the first list is larger
universe@488 412 */
universe@508 413 __attribute__((__nonnull__))
universe@488 414 static inline int cxListCompare(
universe@500 415 CxList *list,
universe@500 416 CxList *other
universe@488 417 ) {
universe@488 418 return list->cl->compare(list, other);
universe@488 419 }
universe@488 420
universe@503 421 /**
universe@503 422 * Calls the list's destructor function for every element.
universe@503 423 * If CxList.autofree_content is \c true, the elements are automatically free'd
universe@503 424 * unless the content destructor function did not already do that.
universe@503 425 * Similarly, if CxList.autofree is \c true, the list structure is free'd, unless
universe@503 426 * the list destructor function did not already do that.
universe@503 427 *
universe@503 428 * This function itself is a destructor function for the CxList.
universe@503 429 *
universe@503 430 * @param list the list which contents shall be destroyed
universe@508 431 * @return \p list if the list structure has not been free'd during the process
universe@503 432 */
universe@503 433 __attribute__((__nonnull__))
universe@503 434 CxList *cxListDestroy(CxList *list);
universe@503 435
universe@415 436 #ifdef __cplusplus
universe@415 437 } /* extern "C" */
universe@415 438 #endif
universe@415 439
universe@393 440 #endif /* UCX_LIST_H */

mercurial