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