src/cx/list.h

Sun, 30 Jan 2022 14:19:00 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 30 Jan 2022 14:19:00 +0100
changeset 500
eb9e7bd40a8e
parent 499
3dc9075df822
child 503
a89857072ace
permissions
-rw-r--r--

do not hide pointers behind typedefs

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@494 42 #include "iterator.h"
universe@398 43
universe@415 44 #ifdef __cplusplus
universe@415 45 extern "C" {
universe@415 46 #endif
universe@415 47
universe@464 48 /**
universe@464 49 * A comparator function comparing two list elements.
universe@464 50 */
universe@489 51 typedef int(*CxListComparator)(
universe@489 52 void const *left,
universe@489 53 void const *right
universe@489 54 );
universe@398 55
universe@464 56 /**
universe@500 57 * List class type.
universe@464 58 */
universe@500 59 typedef struct cx_list_class_s cx_list_class;
universe@435 60
universe@464 61 /**
universe@464 62 * Structure for holding the base data of a list.
universe@464 63 */
universe@435 64 struct cx_list_s {
universe@464 65 /**
universe@464 66 * The list class definition.
universe@464 67 */
universe@435 68 cx_list_class *cl;
universe@464 69 /**
universe@464 70 * The allocator to use.
universe@464 71 */
universe@500 72 CxAllocator *allocator;
universe@464 73 /**
universe@464 74 * The comparator function for the elements.
universe@464 75 */
universe@398 76 CxListComparator cmpfunc;
universe@464 77 /**
universe@464 78 * The size of each element (payload only).
universe@464 79 */
universe@401 80 size_t itemsize;
universe@464 81 /**
universe@464 82 * The size of the list (number of currently stored elements).
universe@464 83 */
universe@401 84 size_t size;
universe@464 85 /**
universe@464 86 * The capacity of the list (maximum number of elements).
universe@464 87 */
universe@401 88 size_t capacity;
universe@435 89 };
universe@398 90
universe@464 91 /**
universe@500 92 * The class definition for arbitrary lists.
universe@500 93 */
universe@500 94 struct cx_list_class_s {
universe@500 95 /**
universe@500 96 * Member function for adding an element.
universe@500 97 */
universe@500 98 int (*add)(
universe@500 99 struct cx_list_s *list,
universe@500 100 void const *elem
universe@500 101 );
universe@500 102
universe@500 103 /**
universe@500 104 * Member function for inserting an element.
universe@500 105 */
universe@500 106 int (*insert)(
universe@500 107 struct cx_list_s *list,
universe@500 108 size_t index,
universe@500 109 void const *elem
universe@500 110 );
universe@500 111
universe@500 112 /**
universe@500 113 * Member function for inserting an element relative to an iterator position.
universe@500 114 */
universe@500 115 int (*insert_iter)(
universe@500 116 struct cx_iterator_s *iter,
universe@500 117 void const *elem,
universe@500 118 int prepend
universe@500 119 );
universe@500 120
universe@500 121 /**
universe@500 122 * Member function for removing an element.
universe@500 123 */
universe@500 124 int (*remove)(
universe@500 125 struct cx_list_s *list,
universe@500 126 size_t index
universe@500 127 );
universe@500 128
universe@500 129 /**
universe@500 130 * Member function for element lookup.
universe@500 131 */
universe@500 132 void *(*at)(
universe@500 133 struct cx_list_s const *list,
universe@500 134 size_t index
universe@500 135 );
universe@500 136
universe@500 137 /**
universe@500 138 * Member function for finding an element.
universe@500 139 */
universe@500 140 size_t (*find)(
universe@500 141 struct cx_list_s const *list,
universe@500 142 void const *elem
universe@500 143 );
universe@500 144
universe@500 145 /**
universe@500 146 * Member function for sorting the list in place.
universe@500 147 */
universe@500 148 void (*sort)(struct cx_list_s *list);
universe@500 149
universe@500 150 /**
universe@500 151 * Member function for comparing this list to another list of the same type.
universe@500 152 */
universe@500 153 int (*compare)(
universe@500 154 struct cx_list_s const *list,
universe@500 155 struct cx_list_s const *other
universe@500 156 );
universe@500 157
universe@500 158 /**
universe@500 159 * Member function for reversing the order of the items.
universe@500 160 */
universe@500 161 void (*reverse)(struct cx_list_s *list);
universe@500 162
universe@500 163 /**
universe@500 164 * Returns an iterator pointing to the specified index.
universe@500 165 */
universe@500 166 struct cx_iterator_s (*iterator)(
universe@500 167 struct cx_list_s *list,
universe@500 168 size_t index
universe@500 169 );
universe@500 170 };
universe@500 171
universe@500 172 /**
universe@464 173 * Common type for all list implementations.
universe@464 174 */
universe@500 175 typedef struct cx_list_s CxList;
universe@398 176
universe@464 177 /**
universe@464 178 * Adds an item to the end of the list.
universe@464 179 *
universe@464 180 * @param list the list
universe@464 181 * @param elem a pointer to the element to add
universe@464 182 * @return zero on success, non-zero on memory allocation failure
universe@464 183 */
universe@489 184 static inline int cxListAdd(
universe@500 185 CxList *list,
universe@489 186 void const *elem
universe@489 187 ) {
universe@469 188 return list->cl->add(list, elem);
universe@469 189 }
universe@398 190
universe@464 191 /**
universe@464 192 * Inserts an item at the specified index.
universe@464 193 *
universe@464 194 * If \p index equals the list \c size, this is effectively cxListAdd().
universe@464 195 *
universe@464 196 * @param list the list
universe@464 197 * @param index the index the element shall have
universe@464 198 * @param elem a pointer to the element to add
universe@464 199 * @return zero on success, non-zero on memory allocation failure
universe@464 200 * or when the index is out of bounds
universe@499 201 * @see cxListInsertAfter()
universe@499 202 * @see cxListInsertBefore()
universe@464 203 */
universe@489 204 static inline int cxListInsert(
universe@500 205 CxList *list,
universe@489 206 size_t index,
universe@489 207 void const *elem
universe@489 208 ) {
universe@469 209 return list->cl->insert(list, index, elem);
universe@469 210 }
universe@398 211
universe@464 212 /**
universe@499 213 * Inserts an element after the current location of the specified iterator.
universe@499 214 *
universe@499 215 * The used iterator remains operational, but all other active iterators should
universe@499 216 * be considered invalidated.
universe@499 217 *
universe@499 218 * If \p iter is not a list iterator, the behavior is undefined.
universe@499 219 * If \p iter is a past-the-end iterator, the new element gets appended to the list.
universe@499 220 *
universe@499 221 * @param iter an iterator
universe@499 222 * @param elem the element to insert
universe@499 223 * @return zero on success, non-zero on memory allocation failure
universe@499 224 * @see cxListInsert()
universe@499 225 * @see cxListInsertBefore()
universe@499 226 */
universe@499 227 static inline int cxListInsertAfter(
universe@499 228 CxIterator *iter,
universe@499 229 void const *elem
universe@499 230 ) {
universe@500 231 return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 0);
universe@499 232 }
universe@499 233
universe@499 234 /**
universe@499 235 * Inserts an element before the current location of the specified iterator.
universe@499 236 *
universe@499 237 * The used iterator remains operational, but all other active iterators should
universe@499 238 * be considered invalidated.
universe@499 239 *
universe@499 240 * If \p iter is not a list iterator, the behavior is undefined.
universe@499 241 * If \p iter is a past-the-end iterator, the new element gets appended to the list.
universe@499 242 *
universe@499 243 * @param iter an iterator
universe@499 244 * @param elem the element to insert
universe@499 245 * @return zero on success, non-zero on memory allocation failure
universe@499 246 * @see cxListInsert()
universe@499 247 * @see cxListInsertAfter()
universe@499 248 */
universe@499 249 static inline int cxListInsertBefore(
universe@499 250 CxIterator *iter,
universe@499 251 void const *elem
universe@499 252 ) {
universe@500 253 return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 1);
universe@499 254 }
universe@499 255
universe@499 256 /**
universe@464 257 * Removes the element at the specified index.
universe@464 258 * @param list the list
universe@464 259 * @param index the index of the element
universe@464 260 * @return zero on success, non-zero if the index is out of bounds
universe@464 261 */
universe@489 262 static inline int cxListRemove(
universe@500 263 CxList *list,
universe@489 264 size_t index
universe@489 265 ) {
universe@469 266 return list->cl->remove(list, index);
universe@469 267 }
universe@398 268
universe@464 269 /**
universe@464 270 * Returns a pointer to the element at the specified index.
universe@464 271 *
universe@464 272 * @param list the list
universe@464 273 * @param index the index of the element
universe@464 274 * @return a pointer to the element or \c NULL if the index is out of bounds
universe@464 275 */
universe@489 276 static inline void *cxListAt(
universe@500 277 CxList *list,
universe@489 278 size_t index
universe@489 279 ) {
universe@469 280 return list->cl->at(list, index);
universe@469 281 }
universe@439 282
universe@464 283 /**
universe@494 284 * Returns an iterator pointing to the item at the specified index.
universe@494 285 *
universe@494 286 * The returned iterator is position-aware.
universe@494 287 *
universe@494 288 * If the index is out of range, a past-the-end iterator will be returned.
universe@494 289 *
universe@494 290 * @param list the list
universe@494 291 * @param index the index where the iterator shall point at
universe@494 292 * @return a new iterator
universe@494 293 */
universe@494 294 static inline CxIterator cxListIterator(
universe@500 295 CxList *list,
universe@494 296 size_t index
universe@494 297 ) {
universe@494 298 return list->cl->iterator(list, index);
universe@494 299 }
universe@494 300
universe@494 301 /**
universe@494 302 * Returns an iterator pointing to the first item of the list.
universe@494 303 *
universe@494 304 * The returned iterator is position-aware.
universe@494 305 *
universe@494 306 * If the list is empty, a past-the-end iterator will be returned.
universe@494 307 *
universe@494 308 * @param list the list
universe@494 309 * @return a new iterator
universe@494 310 */
universe@500 311 static inline CxIterator cxListBegin(CxList *list) {
universe@494 312 return list->cl->iterator(list, 0);
universe@494 313 }
universe@494 314
universe@494 315 /**
universe@464 316 * Returns the index of the first element that equals \p elem.
universe@464 317 *
universe@464 318 * Determining equality is performed by the list's comparator function.
universe@464 319 *
universe@464 320 * @param list the list
universe@464 321 * @param elem the element to find
universe@464 322 * @return the index of the element or \c (size+1) if the element is not found
universe@464 323 */
universe@489 324 static inline size_t cxListFind(
universe@500 325 CxList *list,
universe@489 326 void const *elem
universe@489 327 ) {
universe@469 328 return list->cl->find(list, elem);
universe@469 329 }
universe@398 330
universe@464 331 /**
universe@469 332 * Sorts the list in place.
universe@469 333 *
universe@469 334 * \remark The underlying sort algorithm is implementation defined.
universe@469 335 *
universe@469 336 * @param list the list
universe@469 337 */
universe@500 338 static inline void cxListSort(CxList *list) {
universe@469 339 list->cl->sort(list);
universe@469 340 }
universe@404 341
universe@488 342 /**
universe@490 343 * Reverses the order of the items.
universe@490 344 *
universe@490 345 * @param list the list
universe@490 346 */
universe@500 347 static inline void cxListReverse(CxList *list) {
universe@490 348 list->cl->reverse(list);
universe@490 349 }
universe@490 350
universe@490 351 /**
universe@488 352 * Compares a list to another list of the same type.
universe@488 353 *
universe@488 354 * First, the list sizes are compared. If they match, the lists are compared element-wise.
universe@488 355 *
universe@488 356 * @param list the list
universe@488 357 * @param other the list to compare to
universe@488 358 * @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 359 */
universe@488 360 static inline int cxListCompare(
universe@500 361 CxList *list,
universe@500 362 CxList *other
universe@488 363 ) {
universe@488 364 return list->cl->compare(list, other);
universe@488 365 }
universe@488 366
universe@415 367 #ifdef __cplusplus
universe@415 368 } /* extern "C" */
universe@415 369 #endif
universe@415 370
universe@393 371 #endif /* UCX_LIST_H */

mercurial