src/cx/iterator.h

Sun, 21 May 2023 14:03:21 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 21 May 2023 14:03:21 +0200
changeset 704
35f06c5eeb0e
parent 695
eb1884a8b096
child 740
378578666c83
permissions
-rw-r--r--

add empty list implementation - fixes #258

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 * \version 3.0
universe@494 34 * \copyright 2-Clause BSD License
universe@494 35 */
universe@494 36
universe@494 37 #ifndef UCX_ITERATOR_H
universe@494 38 #define UCX_ITERATOR_H
universe@494 39
universe@494 40 #include "common.h"
universe@494 41
universe@494 42 /**
universe@630 43 * The base of mutating and non-mutating iterators.
universe@494 44 */
universe@630 45 struct cx_iterator_base_s {
universe@494 46 /**
universe@494 47 * True iff the iterator points to valid data.
universe@494 48 */
universe@551 49 __attribute__ ((__nonnull__))
universe@630 50 bool (*valid)(void const *);
universe@494 51
universe@494 52 /**
universe@494 53 * Returns a pointer to the current element.
universe@704 54 *
universe@704 55 * When valid returns false, the behavior of this function is undefined.
universe@494 56 */
universe@551 57 __attribute__ ((__nonnull__))
universe@630 58 void *(*current)(void const *);
universe@494 59
universe@494 60 /**
universe@641 61 * Original implementation in case the function needs to be wrapped.
universe@641 62 */
universe@641 63 __attribute__ ((__nonnull__))
universe@641 64 void *(*current_impl)(void const *);
universe@641 65
universe@641 66 /**
universe@494 67 * Advances the iterator.
universe@704 68 *
universe@704 69 * When valid returns false, the behavior of this function is undefined.
universe@494 70 */
universe@551 71 __attribute__ ((__nonnull__))
universe@630 72 void (*next)(void *);
universe@630 73
universe@630 74 /**
universe@630 75 * Flag current element for removal, if possible.
universe@704 76 *
universe@704 77 * When valid returns false, the behavior of this function is undefined.
universe@630 78 */
universe@630 79 __attribute__ ((__nonnull__))
universe@630 80 bool (*flag_removal)(void *);
universe@630 81
universe@630 82 /**
universe@695 83 * Indicates whether this iterator may remove elements.
universe@630 84 */
universe@630 85 bool mutating;
universe@630 86
universe@630 87 /**
universe@630 88 * Internal flag for removing the current element when advancing.
universe@630 89 */
universe@630 90 bool remove;
universe@630 91 };
universe@630 92
universe@630 93 /**
universe@630 94 * Internal iterator struct - use CxMutIterator.
universe@630 95 */
universe@630 96 struct cx_mut_iterator_s {
universe@630 97
universe@630 98 /**
universe@630 99 * The base properties of this iterator.
universe@630 100 */
universe@630 101 struct cx_iterator_base_s base;
universe@495 102
universe@495 103 /**
universe@495 104 * Handle for the current element, if required.
universe@495 105 */
universe@495 106 void *elem_handle;
universe@495 107
universe@495 108 /**
universe@495 109 * Handle for the source collection, if any.
universe@495 110 */
universe@495 111 void *src_handle;
universe@495 112
universe@495 113 /**
universe@551 114 * Field for storing a key-value pair.
universe@551 115 * May be used by iterators that iterate over k/v-collections.
universe@551 116 */
universe@551 117 struct {
universe@551 118 /**
universe@551 119 * A pointer to the key.
universe@551 120 */
universe@630 121 void const *key;
universe@551 122 /**
universe@551 123 * A pointer to the value.
universe@551 124 */
universe@551 125 void *value;
universe@551 126 } kv_data;
universe@551 127
universe@551 128 /**
universe@551 129 * Field for storing a slot number.
universe@551 130 * May be used by iterators that iterate over multi-bucket collections.
universe@551 131 */
universe@551 132 size_t slot;
universe@551 133
universe@551 134 /**
universe@495 135 * If the iterator is position-aware, contains the index of the element in the underlying collection.
universe@495 136 * Otherwise, this field is usually uninitialized.
universe@495 137 */
universe@495 138 size_t index;
universe@630 139 };
universe@630 140
universe@630 141 /**
universe@630 142 * Mutating iterator value type.
universe@630 143 *
universe@630 144 * An iterator points to a certain element in an (possibly unbounded) chain of elements.
universe@630 145 * Iterators that are based on collections (which have a defined "first" element), are supposed
universe@630 146 * to be "position-aware", which means that they keep track of the current index within the collection.
universe@630 147 *
universe@630 148 * @note Objects that are pointed to by an iterator are mutable through that iterator. However, if the
universe@630 149 * iterator is based on a collection and the underlying collection is mutated by other means than this iterator
universe@630 150 * (e.g. elements added or removed), the iterator becomes invalid (regardless of what cxIteratorValid() returns)
universe@630 151 * and MUST be re-obtained from the collection.
universe@630 152 *
universe@630 153 * @see CxIterator
universe@630 154 */
universe@630 155 typedef struct cx_mut_iterator_s CxMutIterator;
universe@630 156
universe@630 157 /**
universe@630 158 * Internal iterator struct - use CxIterator.
universe@630 159 */
universe@630 160 struct cx_iterator_s {
universe@495 161
universe@495 162 /**
universe@630 163 * The base properties of this iterator.
universe@495 164 */
universe@630 165 struct cx_iterator_base_s base;
universe@630 166
universe@630 167 /**
universe@630 168 * Handle for the current element, if required.
universe@630 169 */
universe@630 170 void *elem_handle;
universe@630 171
universe@630 172 /**
universe@630 173 * Handle for the source collection, if any.
universe@630 174 */
universe@630 175 void const *src_handle;
universe@630 176
universe@630 177 /**
universe@630 178 * Field for storing a key-value pair.
universe@630 179 * May be used by iterators that iterate over k/v-collections.
universe@630 180 */
universe@630 181 struct {
universe@630 182 /**
universe@630 183 * A pointer to the key.
universe@630 184 */
universe@630 185 void const *key;
universe@630 186 /**
universe@630 187 * A pointer to the value.
universe@630 188 */
universe@630 189 void *value;
universe@630 190 } kv_data;
universe@630 191
universe@630 192 /**
universe@630 193 * Field for storing a slot number.
universe@630 194 * May be used by iterators that iterate over multi-bucket collections.
universe@630 195 */
universe@630 196 size_t slot;
universe@630 197
universe@630 198 /**
universe@630 199 * If the iterator is position-aware, contains the index of the element in the underlying collection.
universe@630 200 * Otherwise, this field is usually uninitialized.
universe@630 201 */
universe@630 202 size_t index;
universe@494 203 };
universe@494 204
universe@494 205 /**
universe@500 206 * Iterator value type.
universe@500 207 * An iterator points to a certain element in an (possibly unbounded) chain of elements.
universe@500 208 * Iterators that are based on collections (which have a defined "first" element), are supposed
universe@500 209 * to be "position-aware", which means that they keep track of the current index within the collection.
universe@500 210 *
universe@630 211 * @note Objects that are pointed to by an iterator are always mutable through that iterator. However,
universe@630 212 * this iterator cannot mutate the collection itself (add or remove elements) and any mutation of the
universe@630 213 * collection by other means make this iterator invalid (regardless of what cxIteratorValid() returns).
universe@630 214 *
universe@630 215 * @see CxMutIterator
universe@500 216 */
universe@500 217 typedef struct cx_iterator_s CxIterator;
universe@500 218
universe@500 219 /**
universe@494 220 * Checks if the iterator points to valid data.
universe@494 221 *
universe@494 222 * This is especially false for past-the-end iterators.
universe@494 223 *
universe@630 224 * @param iter the iterator
universe@494 225 * @return true iff the iterator points to valid data
universe@494 226 */
universe@630 227 #define cxIteratorValid(iter) (iter).base.valid(&(iter))
universe@494 228
universe@494 229 /**
universe@494 230 * Returns a pointer to the current element.
universe@494 231 *
universe@494 232 * The behavior is undefined if this iterator is invalid.
universe@494 233 *
universe@630 234 * @param iter the iterator
universe@494 235 * @return a pointer to the current element
universe@494 236 */
universe@630 237 #define cxIteratorCurrent(iter) (iter).base.current(&iter)
universe@494 238
universe@494 239 /**
universe@494 240 * Advances the iterator to the next element.
universe@494 241 *
universe@630 242 * @param iter the iterator
universe@494 243 */
universe@630 244 #define cxIteratorNext(iter) (iter).base.next(&iter)
universe@630 245
universe@630 246 /**
universe@630 247 * Flags the current element for removal.
universe@630 248 *
universe@630 249 * @param iter the iterator
universe@630 250 * @return false if this iterator cannot remove the element
universe@630 251 */
universe@630 252 #define cxIteratorFlagRemoval(iter) (iter).base.flag_removal(&iter)
universe@494 253
universe@496 254 /**
universe@496 255 * Loops over an iterator.
universe@496 256 * @param type the type of the elements
universe@496 257 * @param elem the name of the iteration variable
universe@496 258 * @param iter the iterator
universe@496 259 */
universe@496 260 #define cx_foreach(type, elem, iter) \
universe@630 261 for (type elem; cxIteratorValid(iter) && (elem = (type)cxIteratorCurrent(iter)) != NULL ; cxIteratorNext(iter))
universe@496 262
universe@628 263 #endif // UCX_ITERATOR_H

mercurial