Sat, 22 Jan 2022 10:29:48 +0100
hide __attribute__ from compilers that may not support it
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@398 | 42 | |
universe@415 | 43 | #ifdef __cplusplus |
universe@415 | 44 | extern "C" { |
universe@415 | 45 | #endif |
universe@415 | 46 | |
universe@464 | 47 | /** |
universe@464 | 48 | * A comparator function comparing two list elements. |
universe@464 | 49 | */ |
universe@489 | 50 | typedef int(*CxListComparator)( |
universe@489 | 51 | void const *left, |
universe@489 | 52 | void const *right |
universe@489 | 53 | ); |
universe@398 | 54 | |
universe@464 | 55 | /** |
universe@464 | 56 | * Internal type for the list structure - use CxList instead. |
universe@464 | 57 | */ |
universe@435 | 58 | typedef struct cx_list_s cx_list_s; |
universe@435 | 59 | |
universe@464 | 60 | /** |
universe@464 | 61 | * The class definition for arbitrary lists. |
universe@464 | 62 | */ |
universe@398 | 63 | typedef struct { |
universe@464 | 64 | /** |
universe@464 | 65 | * Member function for adding an element. |
universe@464 | 66 | */ |
universe@489 | 67 | int (*add)( |
universe@489 | 68 | cx_list_s *list, |
universe@489 | 69 | void const *elem |
universe@489 | 70 | ); |
universe@435 | 71 | |
universe@464 | 72 | /** |
universe@464 | 73 | * Member function for inserting an element. |
universe@464 | 74 | */ |
universe@489 | 75 | int (*insert)( |
universe@489 | 76 | cx_list_s *list, |
universe@489 | 77 | size_t index, |
universe@489 | 78 | void const *elem |
universe@489 | 79 | ); |
universe@435 | 80 | |
universe@464 | 81 | /** |
universe@464 | 82 | * Member function for removing an element. |
universe@464 | 83 | */ |
universe@489 | 84 | int (*remove)( |
universe@489 | 85 | cx_list_s *list, |
universe@489 | 86 | size_t index |
universe@489 | 87 | ); |
universe@435 | 88 | |
universe@464 | 89 | /** |
universe@464 | 90 | * Member function for element lookup. |
universe@464 | 91 | */ |
universe@489 | 92 | void *(*at)( |
universe@489 | 93 | cx_list_s const *list, |
universe@489 | 94 | size_t index |
universe@489 | 95 | ); |
universe@439 | 96 | |
universe@464 | 97 | /** |
universe@464 | 98 | * Member function for finding an element. |
universe@464 | 99 | */ |
universe@488 | 100 | size_t (*find)( |
universe@489 | 101 | cx_list_s const *list, |
universe@489 | 102 | void const *elem |
universe@488 | 103 | ); |
universe@435 | 104 | |
universe@464 | 105 | /** |
universe@469 | 106 | * Member function for sorting the list in place. |
universe@469 | 107 | */ |
universe@469 | 108 | void (*sort)(cx_list_s *list); |
universe@488 | 109 | |
universe@488 | 110 | /** |
universe@488 | 111 | * Member function for comparing this list to another list of the same type. |
universe@488 | 112 | */ |
universe@488 | 113 | int (*compare)( |
universe@489 | 114 | cx_list_s const *list, |
universe@489 | 115 | cx_list_s const *other |
universe@488 | 116 | ); |
universe@490 | 117 | |
universe@490 | 118 | /** |
universe@490 | 119 | * Member function for reversing the order of the items. |
universe@490 | 120 | */ |
universe@490 | 121 | void (*reverse)(cx_list_s *list); |
universe@435 | 122 | } cx_list_class; |
universe@435 | 123 | |
universe@464 | 124 | /** |
universe@464 | 125 | * Structure for holding the base data of a list. |
universe@464 | 126 | */ |
universe@435 | 127 | struct cx_list_s { |
universe@464 | 128 | /** |
universe@464 | 129 | * The list class definition. |
universe@464 | 130 | */ |
universe@435 | 131 | cx_list_class *cl; |
universe@464 | 132 | /** |
universe@464 | 133 | * The allocator to use. |
universe@464 | 134 | */ |
universe@398 | 135 | CxAllocator allocator; |
universe@464 | 136 | /** |
universe@464 | 137 | * The comparator function for the elements. |
universe@464 | 138 | */ |
universe@398 | 139 | CxListComparator cmpfunc; |
universe@464 | 140 | /** |
universe@464 | 141 | * The size of each element (payload only). |
universe@464 | 142 | */ |
universe@401 | 143 | size_t itemsize; |
universe@464 | 144 | /** |
universe@464 | 145 | * The size of the list (number of currently stored elements). |
universe@464 | 146 | */ |
universe@401 | 147 | size_t size; |
universe@464 | 148 | /** |
universe@464 | 149 | * The capacity of the list (maximum number of elements). |
universe@464 | 150 | */ |
universe@401 | 151 | size_t capacity; |
universe@435 | 152 | }; |
universe@398 | 153 | |
universe@464 | 154 | /** |
universe@464 | 155 | * Common type for all list implementations. |
universe@464 | 156 | */ |
universe@412 | 157 | typedef cx_list_s *CxList; |
universe@398 | 158 | |
universe@464 | 159 | /** |
universe@464 | 160 | * Adds an item to the end of the list. |
universe@464 | 161 | * |
universe@464 | 162 | * \remark It is implementation defined whether |
universe@464 | 163 | * the contents of the element are copied or the |
universe@464 | 164 | * pointer is added to the list. In the latter case |
universe@464 | 165 | * the \c itemsize of the list SHALL be \c sizeof(void*). |
universe@464 | 166 | * |
universe@464 | 167 | * @param list the list |
universe@464 | 168 | * @param elem a pointer to the element to add |
universe@464 | 169 | * @return zero on success, non-zero on memory allocation failure |
universe@464 | 170 | */ |
universe@489 | 171 | static inline int cxListAdd( |
universe@489 | 172 | CxList list, |
universe@489 | 173 | void const *elem |
universe@489 | 174 | ) { |
universe@469 | 175 | return list->cl->add(list, elem); |
universe@469 | 176 | } |
universe@398 | 177 | |
universe@464 | 178 | /** |
universe@464 | 179 | * Inserts an item at the specified index. |
universe@464 | 180 | * |
universe@464 | 181 | * If \p index equals the list \c size, this is effectively cxListAdd(). |
universe@464 | 182 | * |
universe@464 | 183 | * \remark It is implementation defined whether |
universe@464 | 184 | * the contents of the element are copied or the |
universe@464 | 185 | * pointer is added to the list. In the latter case |
universe@464 | 186 | * the \c itemsize of the list SHALL be \c sizeof(void*). |
universe@464 | 187 | * |
universe@464 | 188 | * @param list the list |
universe@464 | 189 | * @param index the index the element shall have |
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 | * or when the index is out of bounds |
universe@464 | 193 | */ |
universe@489 | 194 | static inline int cxListInsert( |
universe@489 | 195 | CxList list, |
universe@489 | 196 | size_t index, |
universe@489 | 197 | void const *elem |
universe@489 | 198 | ) { |
universe@469 | 199 | return list->cl->insert(list, index, elem); |
universe@469 | 200 | } |
universe@398 | 201 | |
universe@464 | 202 | /** |
universe@464 | 203 | * Removes the element at the specified index. |
universe@464 | 204 | * @param list the list |
universe@464 | 205 | * @param index the index of the element |
universe@464 | 206 | * @return zero on success, non-zero if the index is out of bounds |
universe@464 | 207 | */ |
universe@489 | 208 | static inline int cxListRemove( |
universe@489 | 209 | CxList list, |
universe@489 | 210 | size_t index |
universe@489 | 211 | ) { |
universe@469 | 212 | return list->cl->remove(list, index); |
universe@469 | 213 | } |
universe@398 | 214 | |
universe@464 | 215 | /** |
universe@464 | 216 | * Returns a pointer to the element at the specified index. |
universe@464 | 217 | * |
universe@464 | 218 | * @param list the list |
universe@464 | 219 | * @param index the index of the element |
universe@464 | 220 | * @return a pointer to the element or \c NULL if the index is out of bounds |
universe@464 | 221 | */ |
universe@489 | 222 | static inline void *cxListAt( |
universe@489 | 223 | CxList list, |
universe@489 | 224 | size_t index |
universe@489 | 225 | ) { |
universe@469 | 226 | return list->cl->at(list, index); |
universe@469 | 227 | } |
universe@439 | 228 | |
universe@464 | 229 | /** |
universe@464 | 230 | * Returns the index of the first element that equals \p elem. |
universe@464 | 231 | * |
universe@464 | 232 | * Determining equality is performed by the list's comparator function. |
universe@464 | 233 | * |
universe@464 | 234 | * @param list the list |
universe@464 | 235 | * @param elem the element to find |
universe@464 | 236 | * @return the index of the element or \c (size+1) if the element is not found |
universe@464 | 237 | */ |
universe@489 | 238 | static inline size_t cxListFind( |
universe@489 | 239 | CxList list, |
universe@489 | 240 | void const *elem |
universe@489 | 241 | ) { |
universe@469 | 242 | return list->cl->find(list, elem); |
universe@469 | 243 | } |
universe@398 | 244 | |
universe@464 | 245 | /** |
universe@469 | 246 | * Sorts the list in place. |
universe@469 | 247 | * |
universe@469 | 248 | * \remark The underlying sort algorithm is implementation defined. |
universe@469 | 249 | * |
universe@469 | 250 | * @param list the list |
universe@469 | 251 | */ |
universe@469 | 252 | static inline void cxListSort(CxList list) { |
universe@469 | 253 | list->cl->sort(list); |
universe@469 | 254 | } |
universe@404 | 255 | |
universe@488 | 256 | /** |
universe@490 | 257 | * Reverses the order of the items. |
universe@490 | 258 | * |
universe@490 | 259 | * @param list the list |
universe@490 | 260 | */ |
universe@490 | 261 | static inline void cxListReverse(CxList list) { |
universe@490 | 262 | list->cl->reverse(list); |
universe@490 | 263 | } |
universe@490 | 264 | |
universe@490 | 265 | /** |
universe@488 | 266 | * Compares a list to another list of the same type. |
universe@488 | 267 | * |
universe@488 | 268 | * First, the list sizes are compared. If they match, the lists are compared element-wise. |
universe@488 | 269 | * |
universe@488 | 270 | * @param list the list |
universe@488 | 271 | * @param other the list to compare to |
universe@488 | 272 | * @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 | 273 | */ |
universe@488 | 274 | static inline int cxListCompare( |
universe@488 | 275 | CxList list, |
universe@488 | 276 | CxList other |
universe@488 | 277 | ) { |
universe@488 | 278 | return list->cl->compare(list, other); |
universe@488 | 279 | } |
universe@488 | 280 | |
universe@415 | 281 | #ifdef __cplusplus |
universe@415 | 282 | } /* extern "C" */ |
universe@415 | 283 | #endif |
universe@415 | 284 | |
universe@393 | 285 | #endif /* UCX_LIST_H */ |