Tue, 23 Jul 2013 12:06:28 +0200
changed suffix for allocator aware functions + added allocator aware functions for UcxList
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@123 | 29 | * Double 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 | #include <stddef.h> |
universe@7 | 42 | |
universe@4 | 43 | #ifdef __cplusplus |
universe@4 | 44 | extern "C" { |
universe@4 | 45 | #endif |
universe@4 | 46 | |
universe@121 | 47 | /** |
universe@121 | 48 | * Loop statement for UCX lists. |
universe@121 | 49 | * |
universe@121 | 50 | * The first argument is a pointer to the list. In most cases this will be the |
universe@121 | 51 | * pointer to the first element of the list, but it may also be an arbitrary |
universe@121 | 52 | * element of the list. The iteration will then start with that element. |
universe@121 | 53 | * |
universe@121 | 54 | * The second argument is the name of the iteration variable. The scope of |
universe@121 | 55 | * this variable is limited to the <code>UCX_FOREACH</code> statement. |
universe@121 | 56 | * |
universe@121 | 57 | * @param list The first element of the list |
universe@121 | 58 | * @param elem The variable name of the element |
universe@121 | 59 | */ |
universe@122 | 60 | #define UCX_FOREACH(elem,list) \ |
universe@122 | 61 | for (UcxList* elem = list ; elem != NULL ; elem = elem->next) |
universe@121 | 62 | |
universe@123 | 63 | /** |
universe@123 | 64 | * UCX list type |
universe@123 | 65 | * @see UcxList |
universe@123 | 66 | */ |
universe@122 | 67 | typedef struct UcxList UcxList; |
universe@122 | 68 | struct UcxList { |
universe@123 | 69 | /** |
universe@123 | 70 | * List element payload. |
universe@123 | 71 | */ |
universe@122 | 72 | void *data; |
universe@123 | 73 | /** |
universe@123 | 74 | * Pointer to the next list element or <code>NULL</code>, if this is the |
universe@123 | 75 | * last element. |
universe@123 | 76 | */ |
universe@122 | 77 | UcxList *next; |
universe@123 | 78 | /** |
universe@123 | 79 | * Pointer to the previous list element or <code>NULL</code>, if this is |
universe@123 | 80 | * the first element. |
universe@123 | 81 | */ |
universe@122 | 82 | UcxList *prev; |
universe@4 | 83 | }; |
universe@4 | 84 | |
universe@125 | 85 | UcxList *ucx_list_clone(UcxList *list, copy_func cpyfnc, void* data); |
universe@125 | 86 | UcxList *ucx_list_clone_a(UcxAllocator *allocator, UcxList *list, |
universe@125 | 87 | copy_func cpyfnc, void* data); |
universe@18 | 88 | |
universe@123 | 89 | /** |
universe@123 | 90 | * Compares two UCX lists element-wise by using a compare function. |
universe@123 | 91 | * |
universe@123 | 92 | * Each element of the two specified lists are compared by using the specified |
universe@123 | 93 | * compare function and the additional data. The type and content of this |
universe@123 | 94 | * additional data depends on the cmp_func() used. |
universe@123 | 95 | * |
universe@123 | 96 | * If the list pointers denote elements within a list, the lists are compared |
universe@123 | 97 | * starting with the denoted elements. Thus any previous elements are not taken |
universe@123 | 98 | * into account. This might be useful to check, if certain list tails match |
universe@123 | 99 | * each other. |
universe@123 | 100 | * |
universe@123 | 101 | * @param list1 the first list |
universe@123 | 102 | * @param list2 the second list |
universe@123 | 103 | * @param cmpfnc the compare function |
universe@123 | 104 | * @param data additional data for the compare function |
universe@123 | 105 | * @return 1, if and only if the two lists equal element-wise, 0 otherwise |
universe@123 | 106 | */ |
universe@123 | 107 | int ucx_list_equals(const UcxList *list1, const UcxList *list2, |
universe@123 | 108 | cmp_func cmpfnc, void* data); |
universe@4 | 109 | |
universe@123 | 110 | /** |
universe@123 | 111 | * Destroys the entire list. |
universe@123 | 112 | * |
universe@123 | 113 | * The members of the list are not automatically freed, so ensure they are |
universe@123 | 114 | * otherwise referenced or a memory leak will occur. |
universe@123 | 115 | * |
universe@123 | 116 | * <b>Caution:</b> the argument <b>MUST</b> denote an entire list (i.e. a call |
universe@123 | 117 | * to ucx_list_first() on the argument must return the argument itself) |
universe@123 | 118 | * |
universe@123 | 119 | * @param list The list to free. |
universe@123 | 120 | */ |
universe@123 | 121 | void ucx_list_free(UcxList *list); |
universe@125 | 122 | void ucx_list_free_a(UcxAllocator *allocator, UcxList *list); |
universe@123 | 123 | UcxList *ucx_list_append(UcxList *list, void *data); |
universe@125 | 124 | UcxList *ucx_list_append_a(UcxAllocator *allocator, UcxList *list, void *data); |
universe@123 | 125 | UcxList *ucx_list_prepend(UcxList *list, void *data); |
universe@125 | 126 | UcxList *ucx_list_prepend_a(UcxAllocator *allocator, UcxList *list, void *data); |
universe@123 | 127 | UcxList *ucx_list_concat(UcxList *list1, UcxList *list2); |
universe@124 | 128 | /** |
universe@124 | 129 | * Returns the first element of a list. |
universe@124 | 130 | * |
universe@124 | 131 | * If the argument is the list pointer, it is directly returned. Otherwise |
universe@124 | 132 | * this function traverses to the first element of the list and returns the |
universe@124 | 133 | * list pointer. |
universe@124 | 134 | * |
universe@124 | 135 | * @param elem one element of the list |
universe@124 | 136 | * @return the first element of the list, the specified element is a member of |
universe@124 | 137 | */ |
universe@124 | 138 | UcxList *ucx_list_first(const UcxList *elem); |
universe@124 | 139 | /** |
universe@124 | 140 | * Returns the last element of a list. |
universe@124 | 141 | * |
universe@124 | 142 | * If the argument has no successor, it is the last element and therefore |
universe@124 | 143 | * directly returned. Otherwise this function traverses to the last element of |
universe@124 | 144 | * the list and returns it. |
universe@124 | 145 | * |
universe@124 | 146 | * @param elem one element of the list |
universe@124 | 147 | * @return the last element of the list, the specified element is a member of |
universe@124 | 148 | */ |
universe@124 | 149 | UcxList *ucx_list_last(const UcxList *elem); |
universe@123 | 150 | UcxList *ucx_list_get(const UcxList *list, int index); |
universe@123 | 151 | ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem); |
universe@123 | 152 | size_t ucx_list_size(const UcxList *list); |
universe@124 | 153 | ssize_t ucx_list_find(UcxList *list, void *elem, cmp_func cmpfnc, void *data); |
universe@124 | 154 | int ucx_list_contains(UcxList *list, void *elem, cmp_func cmpfnc, void *data); |
universe@35 | 155 | |
universe@123 | 156 | UcxList *ucx_list_sort(UcxList *list, cmp_func cmpfnc, void *data); |
universe@123 | 157 | |
universe@124 | 158 | /** |
universe@124 | 159 | * Removes an element from the list. |
universe@124 | 160 | * |
universe@124 | 161 | * If the first element is removed, the list pointer changes. So it is |
universe@124 | 162 | * <i>highly recommended</i> to <i>always</i> update the pointer by calling |
universe@124 | 163 | * <code>mylist = ucx_list_remove(mylist, myelem);</code>. |
universe@124 | 164 | * |
universe@124 | 165 | * @param list the list from which the element shall be removed |
universe@124 | 166 | * @param element the element to removed |
universe@124 | 167 | * @return returns the updated list pointer or <code>NULL</code>, if the list |
universe@124 | 168 | * is now empty |
universe@124 | 169 | */ |
universe@123 | 170 | UcxList *ucx_list_remove(UcxList *list, UcxList *element); |
universe@125 | 171 | UcxList *ucx_list_remove_a(UcxAllocator *allocator, UcxList *list, |
universe@125 | 172 | UcxList *element); |
universe@4 | 173 | |
universe@4 | 174 | #ifdef __cplusplus |
universe@4 | 175 | } |
universe@4 | 176 | #endif |
universe@4 | 177 | |
universe@122 | 178 | #endif /* UCX_LIST_H */ |
universe@4 | 179 |