src/cx/iterator.h

Thu, 23 May 2024 19:29:14 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 23 May 2024 19:29:14 +0200
changeset 853
d4baf4dd55c3
parent 852
16e2a3391e88
child 854
fe0d69d72bcd
permissions
-rw-r--r--

simplify iterator structures

universe@494 1 /*
universe@494 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@494 3 *
universe@494 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@494 5 *
universe@494 6 * Redistribution and use in source and binary forms, with or without
universe@494 7 * modification, are permitted provided that the following conditions are met:
universe@494 8 *
universe@494 9 * 1. Redistributions of source code must retain the above copyright
universe@494 10 * notice, this list of conditions and the following disclaimer.
universe@494 11 *
universe@494 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@494 13 * notice, this list of conditions and the following disclaimer in the
universe@494 14 * documentation and/or other materials provided with the distribution.
universe@494 15 *
universe@494 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@494 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@494 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@494 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@494 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@494 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@494 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@494 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@494 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@494 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@494 26 * POSSIBILITY OF SUCH DAMAGE.
universe@494 27 */
universe@494 28 /**
universe@494 29 * \file iterator.h
universe@494 30 * \brief Interface for iterator implementations.
universe@494 31 * \author Mike Becker
universe@494 32 * \author Olaf Wintermann
universe@494 33 * \copyright 2-Clause BSD License
universe@494 34 */
universe@494 35
universe@494 36 #ifndef UCX_ITERATOR_H
universe@494 37 #define UCX_ITERATOR_H
universe@494 38
universe@494 39 #include "common.h"
universe@494 40
universe@853 41 #define CX_ITERATOR_BASE \
universe@853 42 /** \
universe@853 43 * True iff the iterator points to valid data. \
universe@853 44 */ \
universe@853 45 __attribute__ ((__nonnull__)) \
universe@853 46 bool (*valid)(void const *); \
universe@853 47 /** \
universe@853 48 * Returns a pointer to the current element. \
universe@853 49 * \
universe@853 50 * When valid returns false, the behavior of this function is undefined. \
universe@853 51 */ \
universe@853 52 __attribute__ ((__nonnull__)) \
universe@853 53 void *(*current)(void const *); \
universe@853 54 /** \
universe@853 55 * Original implementation in case the function needs to be wrapped. \
universe@853 56 */ \
universe@853 57 __attribute__ ((__nonnull__)) \
universe@853 58 void *(*current_impl)(void const *); \
universe@853 59 /** \
universe@853 60 * Advances the iterator. \
universe@853 61 * \
universe@853 62 * When valid returns false, the behavior of this function is undefined. \
universe@853 63 */ \
universe@853 64 __attribute__ ((__nonnull__)) \
universe@853 65 void (*next)(void *); \
universe@853 66 /** \
universe@853 67 * Indicates whether this iterator may remove elements. \
universe@853 68 */ \
universe@853 69 bool mutating; \
universe@853 70 /** \
universe@853 71 * Internal flag for removing the current element when advancing. \
universe@853 72 */ \
universe@853 73 bool remove;
universe@853 74
universe@494 75 /**
universe@853 76 * Internal iterator struct - use CxIterator.
universe@494 77 */
universe@853 78 struct cx_iterator_s {
universe@853 79 CX_ITERATOR_BASE
universe@494 80
universe@494 81 /**
universe@853 82 * Handle for the current element.
universe@495 83 */
universe@495 84 void *elem_handle;
universe@495 85
universe@495 86 /**
universe@495 87 * Handle for the source collection, if any.
universe@495 88 */
universe@853 89 union {
universe@853 90 /**
universe@853 91 * Access for mutating iterators.
universe@853 92 */
universe@853 93 void *m;
universe@853 94 /**
universe@853 95 * Access for normal iterators.
universe@853 96 */
universe@853 97 void const *c;
universe@853 98 } src_handle;
universe@495 99
universe@495 100 /**
universe@551 101 * Field for storing a key-value pair.
universe@551 102 * May be used by iterators that iterate over k/v-collections.
universe@551 103 */
universe@551 104 struct {
universe@551 105 /**
universe@551 106 * A pointer to the key.
universe@551 107 */
universe@630 108 void const *key;
universe@551 109 /**
universe@551 110 * A pointer to the value.
universe@551 111 */
universe@551 112 void *value;
universe@551 113 } kv_data;
universe@551 114
universe@551 115 /**
universe@551 116 * Field for storing a slot number.
universe@551 117 * May be used by iterators that iterate over multi-bucket collections.
universe@551 118 */
universe@551 119 size_t slot;
universe@551 120
universe@551 121 /**
universe@495 122 * If the iterator is position-aware, contains the index of the element in the underlying collection.
universe@495 123 * Otherwise, this field is usually uninitialized.
universe@495 124 */
universe@495 125 size_t index;
universe@850 126
universe@850 127 /**
universe@850 128 * The size of an individual element.
universe@850 129 */
universe@850 130 size_t elem_size;
universe@850 131
universe@850 132 /**
universe@850 133 * May contain the total number of elements, if known.
universe@850 134 * Shall be set to \c SIZE_MAX when the total number is unknown during iteration.
universe@850 135 */
universe@850 136 size_t elem_count;
universe@630 137 };
universe@630 138
universe@630 139 /**
universe@853 140 * Iterator type.
universe@630 141 *
olaf@740 142 * An iterator points to a certain element in a (possibly unbounded) chain of elements.
universe@500 143 * Iterators that are based on collections (which have a defined "first" element), are supposed
universe@500 144 * to be "position-aware", which means that they keep track of the current index within the collection.
universe@500 145 *
universe@630 146 * @note Objects that are pointed to by an iterator are always mutable through that iterator. However,
universe@853 147 * any concurrent mutation of the collection other than by this iterator makes this iterator invalid
universe@853 148 * and it must not be used anymore.
universe@500 149 */
universe@500 150 typedef struct cx_iterator_s CxIterator;
universe@500 151
universe@500 152 /**
universe@494 153 * Checks if the iterator points to valid data.
universe@494 154 *
universe@494 155 * This is especially false for past-the-end iterators.
universe@494 156 *
universe@630 157 * @param iter the iterator
universe@494 158 * @return true iff the iterator points to valid data
universe@494 159 */
universe@853 160 #define cxIteratorValid(iter) (iter).valid(&(iter))
universe@494 161
universe@494 162 /**
universe@494 163 * Returns a pointer to the current element.
universe@494 164 *
universe@494 165 * The behavior is undefined if this iterator is invalid.
universe@494 166 *
universe@630 167 * @param iter the iterator
universe@494 168 * @return a pointer to the current element
universe@494 169 */
universe@853 170 #define cxIteratorCurrent(iter) (iter).current(&iter)
universe@494 171
universe@494 172 /**
universe@494 173 * Advances the iterator to the next element.
universe@494 174 *
universe@630 175 * @param iter the iterator
universe@494 176 */
universe@853 177 #define cxIteratorNext(iter) (iter).next(&iter)
universe@630 178
universe@630 179 /**
universe@829 180 * Flags the current element for removal, if this iterator is mutating.
universe@630 181 *
universe@630 182 * @param iter the iterator
universe@630 183 */
universe@853 184 #define cxIteratorFlagRemoval(iter) (iter).remove |= (iter).mutating
universe@494 185
universe@496 186 /**
universe@496 187 * Loops over an iterator.
universe@496 188 * @param type the type of the elements
universe@496 189 * @param elem the name of the iteration variable
universe@496 190 * @param iter the iterator
universe@496 191 */
universe@496 192 #define cx_foreach(type, elem, iter) \
universe@630 193 for (type elem; cxIteratorValid(iter) && (elem = (type)cxIteratorCurrent(iter)) != NULL ; cxIteratorNext(iter))
universe@496 194
universe@850 195
universe@850 196 /**
universe@852 197 * Creates an iterator for the specified plain array.
universe@851 198 *
universe@851 199 * The \p array can be \c NULL in which case the iterator will be immediately
universe@851 200 * initialized such that #cxIteratorValid() returns \c false.
universe@851 201 *
universe@851 202 *
universe@851 203 * @param array a pointer to the array (can be \c NULL)
universe@851 204 * @param elem_size the size of one array element
universe@851 205 * @param elem_count the number of elements in the array
universe@851 206 * @return an iterator for the specified array
universe@851 207 */
universe@851 208 __attribute__((__warn_unused_result__))
universe@851 209 CxIterator cxIterator(
universe@851 210 void const *array,
universe@851 211 size_t elem_size,
universe@851 212 size_t elem_count
universe@851 213 );
universe@851 214
universe@851 215 /**
universe@852 216 * Creates a mutating iterator for the specified plain array.
universe@850 217 *
universe@850 218 * While the iterator is in use, the array may only be altered by removing
universe@850 219 * elements through #cxIteratorFlagRemoval(). Every other change to the array
universe@850 220 * will bring this iterator to an undefined state.
universe@850 221 *
universe@850 222 * When \p remove_keeps_order is set to \c false, removing an element will only
universe@850 223 * move the last element to the position of the removed element, instead of
universe@850 224 * moving all subsequent elements by one. Usually, when the order of elements is
universe@850 225 * not important, this parameter should be set to \c false.
universe@850 226 *
universe@850 227 * The \p array can be \c NULL in which case the iterator will be immediately
universe@850 228 * initialized such that #cxIteratorValid() returns \c false.
universe@850 229 *
universe@850 230 *
universe@850 231 * @param array a pointer to the array (can be \c NULL)
universe@850 232 * @param elem_size the size of one array element
universe@850 233 * @param elem_count the number of elements in the array
universe@850 234 * @param remove_keeps_order \c true if the order of elements must be preserved
universe@850 235 * when removing an element
universe@850 236 * @return an iterator for the specified array
universe@850 237 */
universe@850 238 __attribute__((__warn_unused_result__))
universe@853 239 CxIterator cxMutIterator(
universe@850 240 void *array,
universe@850 241 size_t elem_size,
universe@850 242 size_t elem_count,
universe@850 243 bool remove_keeps_order
universe@850 244 );
universe@850 245
universe@628 246 #endif // UCX_ITERATOR_H

mercurial