src/cx/iterator.h

Sat, 08 Jun 2024 20:08:09 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 08 Jun 2024 20:08:09 +0200
changeset 858
d9ad7904c4c2
parent 854
fe0d69d72bcd
permissions
-rw-r--r--

add cxIteratorRef() macro

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@854 41 struct cx_iterator_base_s {
universe@854 42 /**
universe@854 43 * True iff the iterator points to valid data.
universe@854 44 */
universe@854 45 __attribute__ ((__nonnull__))
universe@854 46 bool (*valid)(void const *);
universe@854 47
universe@854 48 /**
universe@854 49 * Returns a pointer to the current element.
universe@854 50 *
universe@854 51 * When valid returns false, the behavior of this function is undefined.
universe@854 52 */
universe@854 53 __attribute__ ((__nonnull__))
universe@854 54 void *(*current)(void const *);
universe@854 55
universe@854 56 /**
universe@854 57 * Original implementation in case the function needs to be wrapped.
universe@854 58 */
universe@854 59 __attribute__ ((__nonnull__))
universe@854 60 void *(*current_impl)(void const *);
universe@854 61
universe@854 62 /**
universe@854 63 * Advances the iterator.
universe@854 64 *
universe@854 65 * When valid returns false, the behavior of this function is undefined.
universe@854 66 */
universe@854 67 __attribute__ ((__nonnull__))
universe@854 68 void (*next)(void *);
universe@854 69 /**
universe@854 70 * Indicates whether this iterator may remove elements.
universe@854 71 */
universe@854 72 bool mutating;
universe@854 73 /**
universe@854 74 * Internal flag for removing the current element when advancing.
universe@854 75 */
universe@853 76 bool remove;
universe@854 77 };
universe@854 78
universe@854 79 /**
universe@854 80 * Declares base attributes for an iterator.
universe@858 81 * Must be the first member of an iterator structure.
universe@854 82 */
universe@854 83 #define CX_ITERATOR_BASE struct cx_iterator_base_s base
universe@853 84
universe@494 85 /**
universe@853 86 * Internal iterator struct - use CxIterator.
universe@494 87 */
universe@853 88 struct cx_iterator_s {
universe@854 89 CX_ITERATOR_BASE;
universe@494 90
universe@494 91 /**
universe@853 92 * Handle for the current element.
universe@495 93 */
universe@495 94 void *elem_handle;
universe@495 95
universe@495 96 /**
universe@495 97 * Handle for the source collection, if any.
universe@495 98 */
universe@853 99 union {
universe@853 100 /**
universe@853 101 * Access for mutating iterators.
universe@853 102 */
universe@853 103 void *m;
universe@853 104 /**
universe@853 105 * Access for normal iterators.
universe@853 106 */
universe@853 107 void const *c;
universe@853 108 } src_handle;
universe@495 109
universe@495 110 /**
universe@551 111 * Field for storing a key-value pair.
universe@551 112 * May be used by iterators that iterate over k/v-collections.
universe@551 113 */
universe@551 114 struct {
universe@551 115 /**
universe@551 116 * A pointer to the key.
universe@551 117 */
universe@630 118 void const *key;
universe@551 119 /**
universe@551 120 * A pointer to the value.
universe@551 121 */
universe@551 122 void *value;
universe@551 123 } kv_data;
universe@551 124
universe@551 125 /**
universe@551 126 * Field for storing a slot number.
universe@551 127 * May be used by iterators that iterate over multi-bucket collections.
universe@551 128 */
universe@551 129 size_t slot;
universe@551 130
universe@551 131 /**
universe@495 132 * If the iterator is position-aware, contains the index of the element in the underlying collection.
universe@495 133 * Otherwise, this field is usually uninitialized.
universe@495 134 */
universe@495 135 size_t index;
universe@850 136
universe@850 137 /**
universe@850 138 * The size of an individual element.
universe@850 139 */
universe@850 140 size_t elem_size;
universe@850 141
universe@850 142 /**
universe@850 143 * May contain the total number of elements, if known.
universe@850 144 * Shall be set to \c SIZE_MAX when the total number is unknown during iteration.
universe@850 145 */
universe@850 146 size_t elem_count;
universe@630 147 };
universe@630 148
universe@630 149 /**
universe@853 150 * Iterator type.
universe@630 151 *
olaf@740 152 * An iterator points to a certain element in a (possibly unbounded) chain of elements.
universe@500 153 * Iterators that are based on collections (which have a defined "first" element), are supposed
universe@500 154 * to be "position-aware", which means that they keep track of the current index within the collection.
universe@500 155 *
universe@630 156 * @note Objects that are pointed to by an iterator are always mutable through that iterator. However,
universe@853 157 * any concurrent mutation of the collection other than by this iterator makes this iterator invalid
universe@853 158 * and it must not be used anymore.
universe@500 159 */
universe@500 160 typedef struct cx_iterator_s CxIterator;
universe@500 161
universe@500 162 /**
universe@494 163 * Checks if the iterator points to valid data.
universe@494 164 *
universe@494 165 * This is especially false for past-the-end iterators.
universe@494 166 *
universe@630 167 * @param iter the iterator
universe@494 168 * @return true iff the iterator points to valid data
universe@494 169 */
universe@854 170 #define cxIteratorValid(iter) (iter).base.valid(&(iter))
universe@494 171
universe@494 172 /**
universe@494 173 * Returns a pointer to the current element.
universe@494 174 *
universe@494 175 * The behavior is undefined if this iterator is invalid.
universe@494 176 *
universe@630 177 * @param iter the iterator
universe@494 178 * @return a pointer to the current element
universe@494 179 */
universe@854 180 #define cxIteratorCurrent(iter) (iter).base.current(&iter)
universe@494 181
universe@494 182 /**
universe@494 183 * Advances the iterator to the next element.
universe@494 184 *
universe@630 185 * @param iter the iterator
universe@494 186 */
universe@854 187 #define cxIteratorNext(iter) (iter).base.next(&iter)
universe@630 188
universe@630 189 /**
universe@829 190 * Flags the current element for removal, if this iterator is mutating.
universe@630 191 *
universe@630 192 * @param iter the iterator
universe@630 193 */
universe@854 194 #define cxIteratorFlagRemoval(iter) (iter).base.remove |= (iter).base.mutating
universe@494 195
universe@496 196 /**
universe@858 197 * Obtains a reference to an arbitrary iterator.
universe@858 198 *
universe@858 199 * This is useful for APIs that expect some iterator as an argument.
universe@858 200 *
universe@858 201 * @param iter the iterator
universe@858 202 */
universe@858 203 #define cxIteratorRef(iter) &((iter).base)
universe@858 204
universe@858 205 /**
universe@496 206 * Loops over an iterator.
universe@496 207 * @param type the type of the elements
universe@496 208 * @param elem the name of the iteration variable
universe@496 209 * @param iter the iterator
universe@496 210 */
universe@496 211 #define cx_foreach(type, elem, iter) \
universe@630 212 for (type elem; cxIteratorValid(iter) && (elem = (type)cxIteratorCurrent(iter)) != NULL ; cxIteratorNext(iter))
universe@496 213
universe@850 214
universe@850 215 /**
universe@852 216 * Creates an iterator for the specified plain array.
universe@851 217 *
universe@851 218 * The \p array can be \c NULL in which case the iterator will be immediately
universe@851 219 * initialized such that #cxIteratorValid() returns \c false.
universe@851 220 *
universe@851 221 *
universe@851 222 * @param array a pointer to the array (can be \c NULL)
universe@851 223 * @param elem_size the size of one array element
universe@851 224 * @param elem_count the number of elements in the array
universe@851 225 * @return an iterator for the specified array
universe@851 226 */
universe@851 227 __attribute__((__warn_unused_result__))
universe@851 228 CxIterator cxIterator(
universe@851 229 void const *array,
universe@851 230 size_t elem_size,
universe@851 231 size_t elem_count
universe@851 232 );
universe@851 233
universe@851 234 /**
universe@852 235 * Creates a mutating iterator for the specified plain array.
universe@850 236 *
universe@850 237 * While the iterator is in use, the array may only be altered by removing
universe@850 238 * elements through #cxIteratorFlagRemoval(). Every other change to the array
universe@850 239 * will bring this iterator to an undefined state.
universe@850 240 *
universe@850 241 * When \p remove_keeps_order is set to \c false, removing an element will only
universe@850 242 * move the last element to the position of the removed element, instead of
universe@850 243 * moving all subsequent elements by one. Usually, when the order of elements is
universe@850 244 * not important, this parameter should be set to \c false.
universe@850 245 *
universe@850 246 * The \p array can be \c NULL in which case the iterator will be immediately
universe@850 247 * initialized such that #cxIteratorValid() returns \c false.
universe@850 248 *
universe@850 249 *
universe@850 250 * @param array a pointer to the array (can be \c NULL)
universe@850 251 * @param elem_size the size of one array element
universe@850 252 * @param elem_count the number of elements in the array
universe@850 253 * @param remove_keeps_order \c true if the order of elements must be preserved
universe@850 254 * when removing an element
universe@850 255 * @return an iterator for the specified array
universe@850 256 */
universe@850 257 __attribute__((__warn_unused_result__))
universe@853 258 CxIterator cxMutIterator(
universe@850 259 void *array,
universe@850 260 size_t elem_size,
universe@850 261 size_t elem_count,
universe@850 262 bool remove_keeps_order
universe@850 263 );
universe@850 264
universe@628 265 #endif // UCX_ITERATOR_H

mercurial