src/ucx/list.h

Fri, 11 May 2018 18:35:08 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 11 May 2018 18:35:08 +0200
changeset 293
d994325445f1
parent 291
deb0035635eb
child 323
b8c49e7a1dba
permissions
-rw-r--r--

adds deprecation notice for *_append/prepend_once()

universe@103 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@103 3 *
universe@259 4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
universe@103 5 *
universe@103 6 * Redistribution and use in source and binary forms, with or without
universe@103 7 * modification, are permitted provided that the following conditions are met:
universe@103 8 *
universe@103 9 * 1. Redistributions of source code must retain the above copyright
universe@103 10 * notice, this list of conditions and the following disclaimer.
universe@103 11 *
universe@103 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@103 13 * notice, this list of conditions and the following disclaimer in the
universe@103 14 * documentation and/or other materials provided with the distribution.
universe@103 15 *
universe@103 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@103 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@103 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@103 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@103 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@103 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@103 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@103 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@103 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@103 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@103 26 * POSSIBILITY OF SUCH DAMAGE.
universe@4 27 */
universe@123 28 /**
universe@128 29 * Doubly linked list implementation.
universe@123 30 *
universe@123 31 * @file list.h
universe@123 32 * @author Mike Becker
universe@123 33 * @author Olaf Wintermann
universe@123 34 */
universe@4 35
universe@122 36 #ifndef UCX_LIST_H
universe@122 37 #define UCX_LIST_H
universe@4 38
universe@259 39 #include "ucx.h"
universe@259 40 #include "allocator.h"
universe@7 41
universe@4 42 #ifdef __cplusplus
universe@4 43 extern "C" {
universe@4 44 #endif
universe@4 45
universe@121 46 /**
universe@121 47 * Loop statement for UCX lists.
universe@121 48 *
universe@220 49 * The first argument is the name of the iteration variable. The scope of
universe@220 50 * this variable is limited to the <code>UCX_FOREACH</code> statement.
universe@220 51 *
universe@220 52 * The second argument is a pointer to the list. In most cases this will be the
universe@121 53 * pointer to the first element of the list, but it may also be an arbitrary
universe@121 54 * element of the list. The iteration will then start with that element.
universe@121 55 *
universe@121 56 * @param list The first element of the list
universe@121 57 * @param elem The variable name of the element
universe@121 58 */
universe@122 59 #define UCX_FOREACH(elem,list) \
universe@122 60 for (UcxList* elem = list ; elem != NULL ; elem = elem->next)
universe@121 61
universe@123 62 /**
universe@129 63 * UCX list type.
universe@123 64 * @see UcxList
universe@123 65 */
universe@122 66 typedef struct UcxList UcxList;
universe@146 67
universe@129 68 /**
universe@129 69 * UCX list structure.
universe@129 70 */
universe@122 71 struct UcxList {
universe@123 72 /**
universe@123 73 * List element payload.
universe@123 74 */
universe@122 75 void *data;
universe@123 76 /**
universe@123 77 * Pointer to the next list element or <code>NULL</code>, if this is the
universe@123 78 * last element.
universe@123 79 */
universe@122 80 UcxList *next;
universe@123 81 /**
universe@123 82 * Pointer to the previous list element or <code>NULL</code>, if this is
universe@123 83 * the first element.
universe@123 84 */
universe@122 85 UcxList *prev;
universe@4 86 };
universe@4 87
universe@128 88 /**
universe@128 89 * Creates an element-wise copy of a list.
universe@128 90 *
universe@128 91 * This function clones the specified list by creating new list elements and
universe@128 92 * copying the data with the specified copy_func(). If no copy_func() is
universe@128 93 * specified, a shallow copy is created and the new list will reference the
universe@128 94 * same data as the source list.
universe@128 95 *
universe@128 96 * @param list the list to copy
universe@128 97 * @param cpyfnc a pointer to the function that shall copy an element (may be
universe@128 98 * <code>NULL</code>)
universe@128 99 * @param data additional data for the copy_func()
universe@128 100 * @return a pointer to the copy
universe@128 101 */
universe@125 102 UcxList *ucx_list_clone(UcxList *list, copy_func cpyfnc, void* data);
universe@146 103
universe@128 104 /**
universe@225 105 * Creates an element-wise copy of a list using a UcxAllocator.
universe@128 106 *
universe@128 107 * See ucx_list_clone() for details.
universe@128 108 *
universe@225 109 * You might want to pass the allocator via the <code>data</code> parameter,
universe@225 110 * to access it within the copy function for making deep copies.
universe@128 111 *
universe@129 112 * @param allocator the allocator to use
universe@128 113 * @param list the list to copy
universe@128 114 * @param cpyfnc a pointer to the function that shall copy an element (may be
universe@128 115 * <code>NULL</code>)
universe@128 116 * @param data additional data for the copy_func()
universe@128 117 * @return a pointer to the copy
universe@128 118 * @see ucx_list_clone()
universe@128 119 */
universe@125 120 UcxList *ucx_list_clone_a(UcxAllocator *allocator, UcxList *list,
universe@125 121 copy_func cpyfnc, void* data);
universe@18 122
universe@123 123 /**
universe@123 124 * Compares two UCX lists element-wise by using a compare function.
universe@123 125 *
universe@123 126 * Each element of the two specified lists are compared by using the specified
universe@123 127 * compare function and the additional data. The type and content of this
universe@123 128 * additional data depends on the cmp_func() used.
universe@123 129 *
universe@123 130 * If the list pointers denote elements within a list, the lists are compared
universe@123 131 * starting with the denoted elements. Thus any previous elements are not taken
universe@123 132 * into account. This might be useful to check, if certain list tails match
universe@123 133 * each other.
universe@123 134 *
universe@123 135 * @param list1 the first list
universe@123 136 * @param list2 the second list
universe@123 137 * @param cmpfnc the compare function
universe@123 138 * @param data additional data for the compare function
universe@123 139 * @return 1, if and only if the two lists equal element-wise, 0 otherwise
universe@123 140 */
universe@123 141 int ucx_list_equals(const UcxList *list1, const UcxList *list2,
universe@123 142 cmp_func cmpfnc, void* data);
universe@4 143
universe@123 144 /**
universe@123 145 * Destroys the entire list.
universe@123 146 *
universe@123 147 * The members of the list are not automatically freed, so ensure they are
universe@211 148 * otherwise referenced or destroyed by ucx_list_free_contents().
universe@211 149 * Otherwise, a memory leak is likely to occur.
universe@123 150 *
universe@123 151 * <b>Caution:</b> the argument <b>MUST</b> denote an entire list (i.e. a call
universe@123 152 * to ucx_list_first() on the argument must return the argument itself)
universe@123 153 *
universe@128 154 * @param list the list to free
universe@211 155 * @see ucx_list_free_contents()
universe@123 156 */
universe@123 157 void ucx_list_free(UcxList *list);
universe@146 158
universe@128 159 /**
universe@225 160 * Destroys the entire list using a UcxAllocator.
universe@128 161 *
universe@128 162 * See ucx_list_free() for details.
universe@128 163 *
universe@128 164 * @param allocator the allocator to use
universe@128 165 * @param list the list to free
universe@128 166 * @see ucx_list_free()
universe@128 167 */
universe@125 168 void ucx_list_free_a(UcxAllocator *allocator, UcxList *list);
universe@146 169
universe@128 170 /**
universe@211 171 * Destroys the contents of the specified list by calling the specified
universe@211 172 * destructor on each of them.
universe@211 173 *
universe@211 174 * Note, that the contents are not usable afterwards and the list should be
universe@211 175 * destroyed with ucx_list_free().
universe@277 176 *
universe@277 177 * If no destructor is specified (<code>NULL</code>), stdlib's free() is used.
universe@211 178 *
universe@211 179 * @param list the list for which the contents shall be freed
universe@277 180 * @param destr optional destructor function
universe@211 181 * @see ucx_list_free()
universe@211 182 */
universe@212 183 void ucx_list_free_content(UcxList* list, ucx_destructor destr);
universe@211 184
universe@211 185
universe@211 186 /**
universe@128 187 * Inserts an element at the end of the list.
universe@128 188 *
universe@172 189 * This is generally an O(n) operation, as the end of the list is retrieved with
universe@128 190 * ucx_list_last().
universe@128 191 *
universe@128 192 * @param list the list where to append the data, or <code>NULL</code> to
universe@128 193 * create a new list
universe@128 194 * @param data the data to insert
universe@128 195 * @return <code>list</code>, if it is not <code>NULL</code> or a pointer to
universe@128 196 * the newly created list otherwise
universe@128 197 */
universe@123 198 UcxList *ucx_list_append(UcxList *list, void *data);
universe@146 199
universe@128 200 /**
universe@225 201 * Inserts an element at the end of the list using a UcxAllocator.
universe@128 202 *
universe@128 203 * See ucx_list_append() for details.
universe@128 204 *
universe@128 205 * @param allocator the allocator to use
universe@128 206 * @param list the list where to append the data, or <code>NULL</code> to
universe@128 207 * create a new list
universe@128 208 * @param data the data to insert
universe@128 209 * @return <code>list</code>, if it is not <code>NULL</code> or a pointer to
universe@128 210 * the newly created list otherwise
universe@128 211 * @see ucx_list_append()
universe@128 212 */
universe@125 213 UcxList *ucx_list_append_a(UcxAllocator *allocator, UcxList *list, void *data);
universe@146 214
universe@128 215 /**
universe@228 216 * Inserts an element at the end of the list, if it is not present in the list.
universe@228 217 *
universe@293 218 * <b>Note:</b> You should not try to store a freshly allocated object. Since
universe@293 219 * it might be a duplicate, the memory allocated for that copy would be leaking
universe@293 220 * afterwards.
universe@293 221 *
universe@293 222 * <b>Deprecation notice:</b> This function is considered to do more harm than
universe@293 223 * it adds usefulness and is going to be removed in a future UCX release.
universe@228 224 *
universe@228 225 * @param list the list where to append the data, or <code>NULL</code> to
universe@228 226 * create a new list
universe@228 227 * @param data the data to insert
universe@228 228 * @param cmpfnc the compare function
universe@228 229 * @param cmpdata additional data for the compare function
universe@228 230 * @return <code>list</code>, if it is not <code>NULL</code> or a pointer to
universe@228 231 * the newly created list otherwise
universe@228 232 * @see ucx_list_append()
universe@228 233 */
universe@228 234 UcxList *ucx_list_append_once(UcxList *list, void *data,
universe@228 235 cmp_func cmpfnc, void *cmpdata);
universe@228 236
universe@228 237 /**
universe@228 238 * Inserts an element at the end of the list, if it is not present in the list,
universe@228 239 * using a UcxAllocator.
universe@228 240 *
universe@293 241 * <b>Note:</b> You should not try to store a freshly allocated object. Since
universe@293 242 * it might be a duplicate, the memory allocated for that copy would be leaking
universe@293 243 * afterwards.
universe@293 244 *
universe@293 245 * <b>Deprecation notice:</b> This function is considered to do more harm than
universe@293 246 * it adds usefulness and is going to be removed in a future UCX release.
universe@293 247 *
universe@228 248 * @param allocator the allocator to use
universe@228 249 * @param list the list where to append the data, or <code>NULL</code> to
universe@228 250 * create a new list
universe@228 251 * @param data the data to insert
universe@228 252 * @param cmpfnc the compare function
universe@228 253 * @param cmpdata additional data for the compare function
universe@228 254 * @return <code>list</code>, if it is not <code>NULL</code> or a pointer to
universe@228 255 * the newly created list otherwise
universe@228 256 * @see ucx_list_append_a()
universe@228 257 */
universe@228 258 UcxList *ucx_list_append_once_a(UcxAllocator *allocator,
universe@228 259 UcxList *list, void *data, cmp_func cmpfnc, void *cmpdata);
universe@228 260
universe@228 261 /**
universe@291 262 * Inserts an element at the beginning of the list, if it is not present
universe@291 263 * in the list.
universe@291 264 *
universe@293 265 * <b>Note:</b> You should not try to store a freshly allocated object. Since
universe@293 266 * it might be a duplicate, the memory allocated for that copy would be leaking
universe@293 267 * afterwards.
universe@293 268 *
universe@293 269 * <b>Deprecation notice:</b> This function is considered to do more harm than
universe@293 270 * it adds usefulness and is going to be removed in a future UCX release.
universe@291 271 *
universe@291 272 * @param list the list where to prepend the data, or <code>NULL</code> to
universe@291 273 * create a new list
universe@291 274 * @param data the data to insert
universe@291 275 * @param cmpfnc the compare function
universe@291 276 * @param cmpdata additional data for the compare function
universe@291 277 * @return a pointer to the new list head
universe@291 278 * @see ucx_list_prepend()
universe@291 279 */
universe@291 280 UcxList *ucx_list_prepend_once(UcxList *list, void *data,
universe@291 281 cmp_func cmpfnc, void *cmpdata);
universe@291 282
universe@291 283 /**
universe@291 284 * Inserts an element at the beginning of the list, if it is not present in
universe@291 285 * the list, using a UcxAllocator.
universe@291 286 *
universe@293 287 * <b>Note:</b> You should not try to store a freshly allocated object. Since
universe@293 288 * it might be a duplicate, the memory allocated for that copy would be leaking
universe@293 289 * afterwards.
universe@293 290 *
universe@293 291 * <b>Deprecation notice:</b> This function is considered to do more harm than
universe@293 292 * it adds usefulness and is going to be removed in a future UCX release.
universe@293 293 *
universe@291 294 * @param allocator the allocator to use
universe@291 295 * @param list the list where to prepend the data, or <code>NULL</code> to
universe@291 296 * create a new list
universe@291 297 * @param data the data to insert
universe@291 298 * @param cmpfnc the compare function
universe@291 299 * @param cmpdata additional data for the compare function
universe@291 300 * @return a pointer to the new list head
universe@291 301 * @see ucx_list_prepend_a()
universe@291 302 */
universe@291 303 UcxList *ucx_list_prepend_once_a(UcxAllocator *allocator,
universe@291 304 UcxList *list, void *data, cmp_func cmpfnc, void *cmpdata);
universe@291 305
universe@291 306 /**
universe@128 307 * Inserts an element at the beginning of the list.
universe@128 308 *
universe@128 309 * You <i>should</i> overwrite the old list pointer by calling
universe@128 310 * <code>mylist = ucx_list_prepend(mylist, mydata);</code>. However, you may
universe@128 311 * also perform successive calls of ucx_list_prepend() on the same list pointer,
universe@128 312 * as this function always searchs for the head of the list with
universe@128 313 * ucx_list_first().
universe@128 314 *
universe@128 315 * @param list the list where to insert the data or <code>NULL</code> to create
universe@128 316 * a new list
universe@128 317 * @param data the data to insert
universe@128 318 * @return a pointer to the new list head
universe@128 319 */
universe@123 320 UcxList *ucx_list_prepend(UcxList *list, void *data);
universe@146 321
universe@128 322 /**
universe@225 323 * Inserts an element at the beginning of the list using a UcxAllocator.
universe@128 324 *
universe@128 325 * See ucx_list_prepend() for details.
universe@128 326 *
universe@128 327 * @param allocator the allocator to use
universe@128 328 * @param list the list where to insert the data or <code>NULL</code> to create
universe@128 329 * a new list
universe@128 330 * @param data the data to insert
universe@128 331 * @return a pointer to the new list head
universe@128 332 * @see ucx_list_prepend()
universe@128 333 */
universe@125 334 UcxList *ucx_list_prepend_a(UcxAllocator *allocator, UcxList *list, void *data);
universe@146 335
universe@128 336 /**
universe@128 337 * Concatenates two lists.
universe@128 338 *
universe@128 339 * Either of the two arguments may be <code>NULL</code>.
universe@128 340 *
universe@128 341 * This function modifies the references to the next/previous element of
universe@128 342 * the last/first element of <code>list1</code>/<code>
universe@128 343 * list2</code>.
universe@128 344 *
universe@128 345 * @param list1 first list
universe@128 346 * @param list2 second list
universe@128 347 * @return if <code>list1</code> is <code>NULL</code>, <code>list2</code> is
universe@128 348 * returned, otherwise <code>list1</code> is returned
universe@128 349 */
universe@123 350 UcxList *ucx_list_concat(UcxList *list1, UcxList *list2);
universe@146 351
universe@124 352 /**
universe@124 353 * Returns the first element of a list.
universe@124 354 *
universe@124 355 * If the argument is the list pointer, it is directly returned. Otherwise
universe@124 356 * this function traverses to the first element of the list and returns the
universe@124 357 * list pointer.
universe@124 358 *
universe@124 359 * @param elem one element of the list
universe@124 360 * @return the first element of the list, the specified element is a member of
universe@124 361 */
universe@124 362 UcxList *ucx_list_first(const UcxList *elem);
universe@146 363
universe@124 364 /**
universe@124 365 * Returns the last element of a list.
universe@124 366 *
universe@124 367 * If the argument has no successor, it is the last element and therefore
universe@124 368 * directly returned. Otherwise this function traverses to the last element of
universe@124 369 * the list and returns it.
universe@124 370 *
universe@124 371 * @param elem one element of the list
universe@124 372 * @return the last element of the list, the specified element is a member of
universe@124 373 */
universe@124 374 UcxList *ucx_list_last(const UcxList *elem);
universe@146 375
universe@128 376 /**
universe@128 377 * Returns the list element at the specified index.
universe@128 378 *
universe@128 379 * @param list the list to retrieve the element from
universe@128 380 * @param index index of the element to return
universe@128 381 * @return the element at the specified index or <code>NULL</code>, if the
universe@128 382 * index is greater than the list size
universe@128 383 */
universe@172 384 UcxList *ucx_list_get(const UcxList *list, size_t index);
universe@146 385
universe@128 386 /**
universe@128 387 * Returns the index of an element.
universe@128 388 *
universe@128 389 * @param list the list where to search for the element
universe@128 390 * @param elem the element to find
universe@128 391 * @return the index of the element or -1 if the list does not contain the
universe@128 392 * element
universe@128 393 */
universe@123 394 ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem);
universe@146 395
universe@128 396 /**
universe@128 397 * Returns the element count of the list.
universe@128 398 *
universe@128 399 * @param list the list whose elements are counted
universe@128 400 * @return the element count
universe@128 401 */
universe@123 402 size_t ucx_list_size(const UcxList *list);
universe@146 403
universe@128 404 /**
universe@128 405 * Returns the index of an element containing the specified data.
universe@128 406 *
universe@128 407 * This function uses a cmp_func() to compare the data of each list element
universe@128 408 * with the specified data. If no cmp_func is provided, the pointers are
universe@128 409 * compared.
universe@128 410 *
universe@128 411 * If the list contains the data more than once, the index of the first
universe@128 412 * occurrence is returned.
universe@128 413 *
universe@128 414 * @param list the list where to search for the data
universe@128 415 * @param elem the element data
universe@128 416 * @param cmpfnc the compare function
universe@128 417 * @param data additional data for the compare function
universe@128 418 * @return the index of the element containing the specified data or -1 if the
universe@128 419 * data is not found in this list
universe@128 420 */
universe@124 421 ssize_t ucx_list_find(UcxList *list, void *elem, cmp_func cmpfnc, void *data);
universe@146 422
universe@128 423 /**
universe@128 424 * Checks, if a list contains a specific element.
universe@128 425 *
universe@128 426 * An element is found, if ucx_list_find() returns a value greater than -1.
universe@128 427 *
universe@128 428 * @param list the list where to search for the data
universe@128 429 * @param elem the element data
universe@128 430 * @param cmpfnc the compare function
universe@128 431 * @param data additional data for the compare function
universe@128 432 * @return 1, if and only if the list contains the specified element data
universe@128 433 * @see ucx_list_find()
universe@128 434 */
universe@124 435 int ucx_list_contains(UcxList *list, void *elem, cmp_func cmpfnc, void *data);
universe@35 436
universe@128 437 /**
universe@225 438 * Sorts a UcxList with natural merge sort.
universe@128 439 *
universe@128 440 * This function uses O(n) additional temporary memory for merge operations
universe@128 441 * that is automatically freed after each merge.
universe@128 442 *
universe@128 443 * As the head of the list might change, you <b>MUST</b> call this function
universe@128 444 * as follows: <code>mylist = ucx_list_sort(mylist, mycmpfnc, mydata);</code>.
universe@128 445 *
universe@128 446 * @param list the list to sort
universe@128 447 * @param cmpfnc the function that shall be used to compare the element data
universe@128 448 * @param data additional data for the cmp_func()
universe@128 449 * @return the sorted list
universe@128 450 */
universe@123 451 UcxList *ucx_list_sort(UcxList *list, cmp_func cmpfnc, void *data);
universe@123 452
universe@124 453 /**
universe@124 454 * Removes an element from the list.
universe@124 455 *
universe@124 456 * If the first element is removed, the list pointer changes. So it is
universe@124 457 * <i>highly recommended</i> to <i>always</i> update the pointer by calling
universe@124 458 * <code>mylist = ucx_list_remove(mylist, myelem);</code>.
universe@124 459 *
universe@124 460 * @param list the list from which the element shall be removed
universe@172 461 * @param element the element to remove
universe@124 462 * @return returns the updated list pointer or <code>NULL</code>, if the list
universe@124 463 * is now empty
universe@124 464 */
universe@123 465 UcxList *ucx_list_remove(UcxList *list, UcxList *element);
universe@146 466
universe@128 467 /**
universe@225 468 * Removes an element from the list using a UcxAllocator.
universe@128 469 *
universe@128 470 * See ucx_list_remove() for details.
universe@128 471 *
universe@128 472 * @param allocator the allocator to use
universe@128 473 * @param list the list from which the element shall be removed
universe@172 474 * @param element the element to remove
universe@128 475 * @return returns the updated list pointer or <code>NULL</code>, if the list
universe@128 476 * @see ucx_list_remove()
universe@128 477 */
universe@125 478 UcxList *ucx_list_remove_a(UcxAllocator *allocator, UcxList *list,
universe@125 479 UcxList *element);
universe@4 480
universe@4 481 #ifdef __cplusplus
universe@4 482 }
universe@4 483 #endif
universe@4 484
universe@122 485 #endif /* UCX_LIST_H */
universe@4 486

mercurial