ucx/list.h

Wed, 24 Jul 2013 14:26:17 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 24 Jul 2013 14:26:17 +0200
changeset 129
61edec666928
parent 128
b79b1ce438dd
child 146
aa376dba1ba8
permissions
-rw-r--r--

documentation for UcxLogger

universe@103 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@103 3 *
universe@103 4 * Copyright 2013 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@7 39 #include "ucx.h"
universe@125 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@121 49 * The first argument is a pointer to the list. In most cases this will be the
universe@121 50 * pointer to the first element of the list, but it may also be an arbitrary
universe@121 51 * element of the list. The iteration will then start with that element.
universe@121 52 *
universe@121 53 * The second argument is the name of the iteration variable. The scope of
universe@121 54 * this variable is limited to the <code>UCX_FOREACH</code> statement.
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@129 67 /**
universe@129 68 * UCX list structure.
universe@129 69 */
universe@122 70 struct UcxList {
universe@123 71 /**
universe@123 72 * List element payload.
universe@123 73 */
universe@122 74 void *data;
universe@123 75 /**
universe@123 76 * Pointer to the next list element or <code>NULL</code>, if this is the
universe@123 77 * last element.
universe@123 78 */
universe@122 79 UcxList *next;
universe@123 80 /**
universe@123 81 * Pointer to the previous list element or <code>NULL</code>, if this is
universe@123 82 * the first element.
universe@123 83 */
universe@122 84 UcxList *prev;
universe@4 85 };
universe@4 86
universe@128 87 /**
universe@128 88 * Creates an element-wise copy of a list.
universe@128 89 *
universe@128 90 * This function clones the specified list by creating new list elements and
universe@128 91 * copying the data with the specified copy_func(). If no copy_func() is
universe@128 92 * specified, a shallow copy is created and the new list will reference the
universe@128 93 * same data as the source list.
universe@128 94 *
universe@128 95 * @param list the list to copy
universe@128 96 * @param cpyfnc a pointer to the function that shall copy an element (may be
universe@128 97 * <code>NULL</code>)
universe@128 98 * @param data additional data for the copy_func()
universe@128 99 * @return a pointer to the copy
universe@128 100 */
universe@125 101 UcxList *ucx_list_clone(UcxList *list, copy_func cpyfnc, void* data);
universe@128 102 /**
universe@128 103 * Creates an element-wise copy of a list using an UcxAllocator.
universe@128 104 *
universe@128 105 * See ucx_list_clone() for details.
universe@128 106 *
universe@128 107 * Keep in mind, that you might want to pass the allocator as an (part of the)
universe@128 108 * argument for the <code>data</code> parameter, if you want the copy_func() to
universe@128 109 * make use of the allocator.
universe@128 110 *
universe@129 111 * @param allocator the allocator to use
universe@128 112 * @param list the list to copy
universe@128 113 * @param cpyfnc a pointer to the function that shall copy an element (may be
universe@128 114 * <code>NULL</code>)
universe@128 115 * @param data additional data for the copy_func()
universe@128 116 * @return a pointer to the copy
universe@128 117 * @see ucx_list_clone()
universe@128 118 */
universe@125 119 UcxList *ucx_list_clone_a(UcxAllocator *allocator, UcxList *list,
universe@125 120 copy_func cpyfnc, void* data);
universe@18 121
universe@123 122 /**
universe@123 123 * Compares two UCX lists element-wise by using a compare function.
universe@123 124 *
universe@123 125 * Each element of the two specified lists are compared by using the specified
universe@123 126 * compare function and the additional data. The type and content of this
universe@123 127 * additional data depends on the cmp_func() used.
universe@123 128 *
universe@123 129 * If the list pointers denote elements within a list, the lists are compared
universe@123 130 * starting with the denoted elements. Thus any previous elements are not taken
universe@123 131 * into account. This might be useful to check, if certain list tails match
universe@123 132 * each other.
universe@123 133 *
universe@123 134 * @param list1 the first list
universe@123 135 * @param list2 the second list
universe@123 136 * @param cmpfnc the compare function
universe@123 137 * @param data additional data for the compare function
universe@123 138 * @return 1, if and only if the two lists equal element-wise, 0 otherwise
universe@123 139 */
universe@123 140 int ucx_list_equals(const UcxList *list1, const UcxList *list2,
universe@123 141 cmp_func cmpfnc, void* data);
universe@4 142
universe@123 143 /**
universe@123 144 * Destroys the entire list.
universe@123 145 *
universe@123 146 * The members of the list are not automatically freed, so ensure they are
universe@123 147 * otherwise referenced or a memory leak will occur.
universe@123 148 *
universe@123 149 * <b>Caution:</b> the argument <b>MUST</b> denote an entire list (i.e. a call
universe@123 150 * to ucx_list_first() on the argument must return the argument itself)
universe@123 151 *
universe@128 152 * @param list the list to free
universe@123 153 */
universe@123 154 void ucx_list_free(UcxList *list);
universe@128 155 /**
universe@128 156 * Destroys the entire list using an UcxAllocator.
universe@128 157 *
universe@128 158 * See ucx_list_free() for details.
universe@128 159 *
universe@128 160 * @param allocator the allocator to use
universe@128 161 * @param list the list to free
universe@128 162 * @see ucx_list_free()
universe@128 163 */
universe@125 164 void ucx_list_free_a(UcxAllocator *allocator, UcxList *list);
universe@128 165 /**
universe@128 166 * Inserts an element at the end of the list.
universe@128 167 *
universe@128 168 * This is generally an O(n) operation, as the end of the list is seeked with
universe@128 169 * ucx_list_last().
universe@128 170 *
universe@128 171 * @param list the list where to append the data, or <code>NULL</code> to
universe@128 172 * create a new list
universe@128 173 * @param data the data to insert
universe@128 174 * @return <code>list</code>, if it is not <code>NULL</code> or a pointer to
universe@128 175 * the newly created list otherwise
universe@128 176 */
universe@123 177 UcxList *ucx_list_append(UcxList *list, void *data);
universe@128 178 /**
universe@128 179 * Inserts an element at the end of the list using an UcxAllocator.
universe@128 180 *
universe@128 181 * See ucx_list_append() for details.
universe@128 182 *
universe@128 183 * @param allocator the allocator to use
universe@128 184 * @param list the list where to append the data, or <code>NULL</code> to
universe@128 185 * create a new list
universe@128 186 * @param data the data to insert
universe@128 187 * @return <code>list</code>, if it is not <code>NULL</code> or a pointer to
universe@128 188 * the newly created list otherwise
universe@128 189 * @see ucx_list_append()
universe@128 190 */
universe@125 191 UcxList *ucx_list_append_a(UcxAllocator *allocator, UcxList *list, void *data);
universe@128 192 /**
universe@128 193 * Inserts an element at the beginning of the list.
universe@128 194 *
universe@128 195 * You <i>should</i> overwrite the old list pointer by calling
universe@128 196 * <code>mylist = ucx_list_prepend(mylist, mydata);</code>. However, you may
universe@128 197 * also perform successive calls of ucx_list_prepend() on the same list pointer,
universe@128 198 * as this function always searchs for the head of the list with
universe@128 199 * ucx_list_first().
universe@128 200 *
universe@128 201 * @param list the list where to insert the data or <code>NULL</code> to create
universe@128 202 * a new list
universe@128 203 * @param data the data to insert
universe@128 204 * @return a pointer to the new list head
universe@128 205 */
universe@123 206 UcxList *ucx_list_prepend(UcxList *list, void *data);
universe@128 207 /**
universe@128 208 * Inserts an element at the beginning of the list using an UcxAllocator.
universe@128 209 *
universe@128 210 * See ucx_list_prepend() for details.
universe@128 211 *
universe@128 212 * @param allocator the allocator to use
universe@128 213 * @param list the list where to insert the data or <code>NULL</code> to create
universe@128 214 * a new list
universe@128 215 * @param data the data to insert
universe@128 216 * @return a pointer to the new list head
universe@128 217 * @see ucx_list_prepend()
universe@128 218 */
universe@125 219 UcxList *ucx_list_prepend_a(UcxAllocator *allocator, UcxList *list, void *data);
universe@128 220 /**
universe@128 221 * Concatenates two lists.
universe@128 222 *
universe@128 223 * Either of the two arguments may be <code>NULL</code>.
universe@128 224 *
universe@128 225 * This function modifies the references to the next/previous element of
universe@128 226 * the last/first element of <code>list1</code>/<code>
universe@128 227 * list2</code>.
universe@128 228 *
universe@128 229 * @param list1 first list
universe@128 230 * @param list2 second list
universe@128 231 * @return if <code>list1</code> is <code>NULL</code>, <code>list2</code> is
universe@128 232 * returned, otherwise <code>list1</code> is returned
universe@128 233 */
universe@123 234 UcxList *ucx_list_concat(UcxList *list1, UcxList *list2);
universe@124 235 /**
universe@124 236 * Returns the first element of a list.
universe@124 237 *
universe@124 238 * If the argument is the list pointer, it is directly returned. Otherwise
universe@124 239 * this function traverses to the first element of the list and returns the
universe@124 240 * list pointer.
universe@124 241 *
universe@124 242 * @param elem one element of the list
universe@124 243 * @return the first element of the list, the specified element is a member of
universe@124 244 */
universe@124 245 UcxList *ucx_list_first(const UcxList *elem);
universe@124 246 /**
universe@124 247 * Returns the last element of a list.
universe@124 248 *
universe@124 249 * If the argument has no successor, it is the last element and therefore
universe@124 250 * directly returned. Otherwise this function traverses to the last element of
universe@124 251 * the list and returns it.
universe@124 252 *
universe@124 253 * @param elem one element of the list
universe@124 254 * @return the last element of the list, the specified element is a member of
universe@124 255 */
universe@124 256 UcxList *ucx_list_last(const UcxList *elem);
universe@128 257 /**
universe@128 258 * Returns the list element at the specified index.
universe@128 259 *
universe@128 260 * @param list the list to retrieve the element from
universe@128 261 * @param index index of the element to return
universe@128 262 * @return the element at the specified index or <code>NULL</code>, if the
universe@128 263 * index is greater than the list size
universe@128 264 */
universe@123 265 UcxList *ucx_list_get(const UcxList *list, int index);
universe@128 266 /**
universe@128 267 * Returns the index of an element.
universe@128 268 *
universe@128 269 * @param list the list where to search for the element
universe@128 270 * @param elem the element to find
universe@128 271 * @return the index of the element or -1 if the list does not contain the
universe@128 272 * element
universe@128 273 */
universe@123 274 ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem);
universe@128 275 /**
universe@128 276 * Returns the element count of the list.
universe@128 277 *
universe@128 278 * @param list the list whose elements are counted
universe@128 279 * @return the element count
universe@128 280 */
universe@123 281 size_t ucx_list_size(const UcxList *list);
universe@128 282 /**
universe@128 283 * Returns the index of an element containing the specified data.
universe@128 284 *
universe@128 285 * This function uses a cmp_func() to compare the data of each list element
universe@128 286 * with the specified data. If no cmp_func is provided, the pointers are
universe@128 287 * compared.
universe@128 288 *
universe@128 289 * If the list contains the data more than once, the index of the first
universe@128 290 * occurrence is returned.
universe@128 291 *
universe@128 292 * @param list the list where to search for the data
universe@128 293 * @param elem the element data
universe@128 294 * @param cmpfnc the compare function
universe@128 295 * @param data additional data for the compare function
universe@128 296 * @return the index of the element containing the specified data or -1 if the
universe@128 297 * data is not found in this list
universe@128 298 */
universe@124 299 ssize_t ucx_list_find(UcxList *list, void *elem, cmp_func cmpfnc, void *data);
universe@128 300 /**
universe@128 301 * Checks, if a list contains a specific element.
universe@128 302 *
universe@128 303 * An element is found, if ucx_list_find() returns a value greater than -1.
universe@128 304 *
universe@128 305 * @param list the list where to search for the data
universe@128 306 * @param elem the element data
universe@128 307 * @param cmpfnc the compare function
universe@128 308 * @param data additional data for the compare function
universe@128 309 * @return 1, if and only if the list contains the specified element data
universe@128 310 * @see ucx_list_find()
universe@128 311 */
universe@124 312 int ucx_list_contains(UcxList *list, void *elem, cmp_func cmpfnc, void *data);
universe@35 313
universe@128 314 /**
universe@128 315 * Sorts an UcxList with natural merge sort.
universe@128 316 *
universe@128 317 * This function uses O(n) additional temporary memory for merge operations
universe@128 318 * that is automatically freed after each merge.
universe@128 319 *
universe@128 320 * As the head of the list might change, you <b>MUST</b> call this function
universe@128 321 * as follows: <code>mylist = ucx_list_sort(mylist, mycmpfnc, mydata);</code>.
universe@128 322 *
universe@128 323 * @param list the list to sort
universe@128 324 * @param cmpfnc the function that shall be used to compare the element data
universe@128 325 * @param data additional data for the cmp_func()
universe@128 326 * @return the sorted list
universe@128 327 */
universe@123 328 UcxList *ucx_list_sort(UcxList *list, cmp_func cmpfnc, void *data);
universe@123 329
universe@124 330 /**
universe@124 331 * Removes an element from the list.
universe@124 332 *
universe@124 333 * If the first element is removed, the list pointer changes. So it is
universe@124 334 * <i>highly recommended</i> to <i>always</i> update the pointer by calling
universe@124 335 * <code>mylist = ucx_list_remove(mylist, myelem);</code>.
universe@124 336 *
universe@124 337 * @param list the list from which the element shall be removed
universe@124 338 * @param element the element to removed
universe@124 339 * @return returns the updated list pointer or <code>NULL</code>, if the list
universe@124 340 * is now empty
universe@124 341 */
universe@123 342 UcxList *ucx_list_remove(UcxList *list, UcxList *element);
universe@128 343 /**
universe@128 344 * Removes an element from the list using an UcxAllocator.
universe@128 345 *
universe@128 346 * See ucx_list_remove() for details.
universe@128 347 *
universe@128 348 * @param allocator the allocator to use
universe@128 349 * @param list the list from which the element shall be removed
universe@128 350 * @param element the element to removed
universe@128 351 * @return returns the updated list pointer or <code>NULL</code>, if the list
universe@128 352 * @see ucx_list_remove()
universe@128 353 */
universe@125 354 UcxList *ucx_list_remove_a(UcxAllocator *allocator, UcxList *list,
universe@125 355 UcxList *element);
universe@4 356
universe@4 357 #ifdef __cplusplus
universe@4 358 }
universe@4 359 #endif
universe@4 360
universe@122 361 #endif /* UCX_LIST_H */
universe@4 362

mercurial