Sun, 20 Nov 2022 21:08:36 +0100
use //-style single line comments everywhere
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@494 | 43 | * Internal iterator struct - use CxIterator. |
universe@494 | 44 | */ |
universe@494 | 45 | struct cx_iterator_s { |
universe@494 | 46 | /** |
universe@494 | 47 | * True iff the iterator points to valid data. |
universe@494 | 48 | */ |
universe@551 | 49 | __attribute__ ((__nonnull__)) |
universe@551 | 50 | bool (*valid)(struct cx_iterator_s const *); |
universe@494 | 51 | |
universe@494 | 52 | /** |
universe@494 | 53 | * Returns a pointer to the current element. |
universe@494 | 54 | */ |
universe@551 | 55 | __attribute__ ((__nonnull__)) |
universe@551 | 56 | void *(*current)(struct cx_iterator_s const *); |
universe@494 | 57 | |
universe@494 | 58 | /** |
universe@494 | 59 | * Advances the iterator. |
universe@494 | 60 | */ |
universe@551 | 61 | __attribute__ ((__nonnull__)) |
universe@551 | 62 | void (*next)(struct cx_iterator_s *); |
universe@495 | 63 | |
universe@495 | 64 | /** |
universe@495 | 65 | * Handle for the current element, if required. |
universe@495 | 66 | */ |
universe@495 | 67 | void *elem_handle; |
universe@495 | 68 | |
universe@495 | 69 | /** |
universe@495 | 70 | * Handle for the source collection, if any. |
universe@495 | 71 | */ |
universe@495 | 72 | void *src_handle; |
universe@495 | 73 | |
universe@495 | 74 | /** |
universe@551 | 75 | * Field for storing a key-value pair. |
universe@551 | 76 | * May be used by iterators that iterate over k/v-collections. |
universe@551 | 77 | */ |
universe@551 | 78 | struct { |
universe@551 | 79 | /** |
universe@551 | 80 | * A pointer to the key. |
universe@551 | 81 | */ |
universe@551 | 82 | void *key; |
universe@551 | 83 | /** |
universe@551 | 84 | * A pointer to the value. |
universe@551 | 85 | */ |
universe@551 | 86 | void *value; |
universe@551 | 87 | } kv_data; |
universe@551 | 88 | |
universe@551 | 89 | /** |
universe@551 | 90 | * Field for storing a slot number. |
universe@551 | 91 | * May be used by iterators that iterate over multi-bucket collections. |
universe@551 | 92 | */ |
universe@551 | 93 | size_t slot; |
universe@551 | 94 | |
universe@551 | 95 | /** |
universe@495 | 96 | * If the iterator is position-aware, contains the index of the element in the underlying collection. |
universe@495 | 97 | * Otherwise, this field is usually uninitialized. |
universe@495 | 98 | */ |
universe@495 | 99 | size_t index; |
universe@495 | 100 | |
universe@495 | 101 | /** |
universe@495 | 102 | * Users may set this to true, if the current element shall be removed from the underlying collection |
universe@495 | 103 | * when the iterator advances. |
universe@495 | 104 | * Has no effect on iterators that are not based on a collection. |
universe@495 | 105 | */ |
universe@495 | 106 | bool remove; |
universe@494 | 107 | }; |
universe@494 | 108 | |
universe@494 | 109 | /** |
universe@500 | 110 | * Iterator value type. |
universe@500 | 111 | * An iterator points to a certain element in an (possibly unbounded) chain of elements. |
universe@500 | 112 | * Iterators that are based on collections (which have a defined "first" element), are supposed |
universe@500 | 113 | * to be "position-aware", which means that they keep track of the current index within the collection. |
universe@500 | 114 | * |
universe@500 | 115 | * @note Objects that are pointed to by an iterator are mutable through that iterator. However, if the |
universe@500 | 116 | * iterator is based on a collection and the underlying collection is mutated (elements added or removed), |
universe@500 | 117 | * the iterator becomes invalid (regardless of what cxIteratorValid() returns) and MUST be re-obtained |
universe@500 | 118 | * from the collection. |
universe@500 | 119 | */ |
universe@500 | 120 | typedef struct cx_iterator_s CxIterator; |
universe@500 | 121 | |
universe@500 | 122 | /** |
universe@494 | 123 | * Checks if the iterator points to valid data. |
universe@494 | 124 | * |
universe@494 | 125 | * This is especially false for past-the-end iterators. |
universe@494 | 126 | * |
universe@496 | 127 | * @param iter a pointer to the iterator |
universe@494 | 128 | * @return true iff the iterator points to valid data |
universe@494 | 129 | */ |
universe@494 | 130 | __attribute__ ((__nonnull__)) |
universe@494 | 131 | static inline bool cxIteratorValid(CxIterator const *iter) { |
universe@494 | 132 | return iter->valid(iter); |
universe@494 | 133 | } |
universe@494 | 134 | |
universe@494 | 135 | /** |
universe@494 | 136 | * Returns a pointer to the current element. |
universe@494 | 137 | * |
universe@494 | 138 | * The behavior is undefined if this iterator is invalid. |
universe@494 | 139 | * |
universe@496 | 140 | * @param iter a pointer to the iterator |
universe@494 | 141 | * @return a pointer to the current element |
universe@494 | 142 | */ |
universe@494 | 143 | __attribute__ ((__nonnull__)) |
universe@494 | 144 | static inline void *cxIteratorCurrent(CxIterator const *iter) { |
universe@494 | 145 | return iter->current(iter); |
universe@494 | 146 | } |
universe@494 | 147 | |
universe@494 | 148 | /** |
universe@494 | 149 | * Advances the iterator to the next element. |
universe@494 | 150 | * |
universe@496 | 151 | * @param iter a pointer to the iterator |
universe@494 | 152 | */ |
universe@494 | 153 | __attribute__ ((__nonnull__)) |
universe@494 | 154 | static inline void cxIteratorNext(CxIterator *iter) { |
universe@494 | 155 | iter->next(iter); |
universe@494 | 156 | } |
universe@494 | 157 | |
universe@496 | 158 | /** |
universe@496 | 159 | * Loops over an iterator. |
universe@496 | 160 | * @param type the type of the elements |
universe@496 | 161 | * @param elem the name of the iteration variable |
universe@496 | 162 | * @param iter the iterator |
universe@496 | 163 | */ |
universe@496 | 164 | #define cx_foreach(type, elem, iter) \ |
universe@516 | 165 | for (type elem; cxIteratorValid(&iter) && (elem = (type)cxIteratorCurrent(&iter)) != NULL ; cxIteratorNext(&iter)) // NOLINT(bugprone-macro-parentheses) |
universe@496 | 166 | |
universe@628 | 167 | #endif // UCX_ITERATOR_H |