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