src/cx/list.h

Sat, 29 Jan 2022 14:32:04 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 29 Jan 2022 14:32:04 +0100
changeset 499
3dc9075df822
parent 495
2856c74e18ba
child 500
eb9e7bd40a8e
permissions
-rw-r--r--

add cxListInsertAfter() and cxListInsertBefore()

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@464 57 * Internal type for the list structure - use CxList instead.
universe@464 58 */
universe@435 59 typedef struct cx_list_s cx_list_s;
universe@435 60
universe@464 61 /**
universe@464 62 * The class definition for arbitrary lists.
universe@464 63 */
universe@398 64 typedef struct {
universe@464 65 /**
universe@464 66 * Member function for adding an element.
universe@464 67 */
universe@489 68 int (*add)(
universe@489 69 cx_list_s *list,
universe@489 70 void const *elem
universe@489 71 );
universe@435 72
universe@464 73 /**
universe@464 74 * Member function for inserting an element.
universe@464 75 */
universe@489 76 int (*insert)(
universe@489 77 cx_list_s *list,
universe@489 78 size_t index,
universe@489 79 void const *elem
universe@489 80 );
universe@435 81
universe@464 82 /**
universe@499 83 * Member function for inserting an element relative to an iterator position.
universe@499 84 */
universe@499 85 int (*insert_iter)(
universe@499 86 CxIterator *iter,
universe@499 87 void const *elem,
universe@499 88 int prepend
universe@499 89 );
universe@499 90
universe@499 91 /**
universe@464 92 * Member function for removing an element.
universe@464 93 */
universe@489 94 int (*remove)(
universe@489 95 cx_list_s *list,
universe@489 96 size_t index
universe@489 97 );
universe@435 98
universe@464 99 /**
universe@464 100 * Member function for element lookup.
universe@464 101 */
universe@489 102 void *(*at)(
universe@489 103 cx_list_s const *list,
universe@489 104 size_t index
universe@489 105 );
universe@439 106
universe@464 107 /**
universe@464 108 * Member function for finding an element.
universe@464 109 */
universe@488 110 size_t (*find)(
universe@489 111 cx_list_s const *list,
universe@489 112 void const *elem
universe@488 113 );
universe@435 114
universe@464 115 /**
universe@469 116 * Member function for sorting the list in place.
universe@469 117 */
universe@469 118 void (*sort)(cx_list_s *list);
universe@488 119
universe@488 120 /**
universe@488 121 * Member function for comparing this list to another list of the same type.
universe@488 122 */
universe@488 123 int (*compare)(
universe@489 124 cx_list_s const *list,
universe@489 125 cx_list_s const *other
universe@488 126 );
universe@490 127
universe@490 128 /**
universe@490 129 * Member function for reversing the order of the items.
universe@490 130 */
universe@490 131 void (*reverse)(cx_list_s *list);
universe@494 132
universe@494 133 /**
universe@494 134 * Returns an iterator pointing to the specified index.
universe@494 135 */
universe@494 136 CxIterator (*iterator)(
universe@495 137 cx_list_s *list,
universe@494 138 size_t index
universe@494 139 );
universe@435 140 } cx_list_class;
universe@435 141
universe@464 142 /**
universe@464 143 * Structure for holding the base data of a list.
universe@464 144 */
universe@435 145 struct cx_list_s {
universe@464 146 /**
universe@464 147 * The list class definition.
universe@464 148 */
universe@435 149 cx_list_class *cl;
universe@464 150 /**
universe@464 151 * The allocator to use.
universe@464 152 */
universe@398 153 CxAllocator allocator;
universe@464 154 /**
universe@464 155 * The comparator function for the elements.
universe@464 156 */
universe@398 157 CxListComparator cmpfunc;
universe@464 158 /**
universe@464 159 * The size of each element (payload only).
universe@464 160 */
universe@401 161 size_t itemsize;
universe@464 162 /**
universe@464 163 * The size of the list (number of currently stored elements).
universe@464 164 */
universe@401 165 size_t size;
universe@464 166 /**
universe@464 167 * The capacity of the list (maximum number of elements).
universe@464 168 */
universe@401 169 size_t capacity;
universe@435 170 };
universe@398 171
universe@464 172 /**
universe@464 173 * Common type for all list implementations.
universe@464 174 */
universe@412 175 typedef 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@489 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@489 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@499 231 return ((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@499 253 return ((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@489 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@489 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@494 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@494 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@489 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@469 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@490 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@488 361 CxList list,
universe@488 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