universe@494: /* universe@494: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@494: * universe@494: * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. universe@494: * universe@494: * Redistribution and use in source and binary forms, with or without universe@494: * modification, are permitted provided that the following conditions are met: universe@494: * universe@494: * 1. Redistributions of source code must retain the above copyright universe@494: * notice, this list of conditions and the following disclaimer. universe@494: * universe@494: * 2. Redistributions in binary form must reproduce the above copyright universe@494: * notice, this list of conditions and the following disclaimer in the universe@494: * documentation and/or other materials provided with the distribution. universe@494: * universe@494: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@494: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@494: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@494: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@494: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@494: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@494: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@494: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@494: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@494: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@494: * POSSIBILITY OF SUCH DAMAGE. universe@494: */ universe@494: /** universe@494: * \file iterator.h universe@494: * \brief Interface for iterator implementations. universe@494: * \author Mike Becker universe@494: * \author Olaf Wintermann universe@494: * \copyright 2-Clause BSD License universe@494: */ universe@494: universe@494: #ifndef UCX_ITERATOR_H universe@494: #define UCX_ITERATOR_H universe@494: universe@494: #include "common.h" universe@494: universe@854: struct cx_iterator_base_s { universe@854: /** universe@854: * True iff the iterator points to valid data. universe@854: */ universe@854: __attribute__ ((__nonnull__)) universe@854: bool (*valid)(void const *); universe@854: universe@854: /** universe@854: * Returns a pointer to the current element. universe@854: * universe@854: * When valid returns false, the behavior of this function is undefined. universe@854: */ universe@854: __attribute__ ((__nonnull__)) universe@854: void *(*current)(void const *); universe@854: universe@854: /** universe@854: * Original implementation in case the function needs to be wrapped. universe@854: */ universe@854: __attribute__ ((__nonnull__)) universe@854: void *(*current_impl)(void const *); universe@854: universe@854: /** universe@854: * Advances the iterator. universe@854: * universe@854: * When valid returns false, the behavior of this function is undefined. universe@854: */ universe@854: __attribute__ ((__nonnull__)) universe@854: void (*next)(void *); universe@854: /** universe@854: * Indicates whether this iterator may remove elements. universe@854: */ universe@854: bool mutating; universe@854: /** universe@854: * Internal flag for removing the current element when advancing. universe@854: */ universe@853: bool remove; universe@854: }; universe@854: universe@854: /** universe@854: * Declares base attributes for an iterator. universe@858: * Must be the first member of an iterator structure. universe@854: */ universe@854: #define CX_ITERATOR_BASE struct cx_iterator_base_s base universe@853: universe@494: /** universe@853: * Internal iterator struct - use CxIterator. universe@494: */ universe@853: struct cx_iterator_s { universe@854: CX_ITERATOR_BASE; universe@494: universe@494: /** universe@853: * Handle for the current element. universe@495: */ universe@495: void *elem_handle; universe@495: universe@495: /** universe@495: * Handle for the source collection, if any. universe@495: */ universe@853: union { universe@853: /** universe@853: * Access for mutating iterators. universe@853: */ universe@853: void *m; universe@853: /** universe@853: * Access for normal iterators. universe@853: */ universe@853: void const *c; universe@853: } src_handle; universe@495: universe@495: /** universe@551: * Field for storing a key-value pair. universe@551: * May be used by iterators that iterate over k/v-collections. universe@551: */ universe@551: struct { universe@551: /** universe@551: * A pointer to the key. universe@551: */ universe@630: void const *key; universe@551: /** universe@551: * A pointer to the value. universe@551: */ universe@551: void *value; universe@551: } kv_data; universe@551: universe@551: /** universe@551: * Field for storing a slot number. universe@551: * May be used by iterators that iterate over multi-bucket collections. universe@551: */ universe@551: size_t slot; universe@551: universe@551: /** universe@495: * If the iterator is position-aware, contains the index of the element in the underlying collection. universe@495: * Otherwise, this field is usually uninitialized. universe@495: */ universe@495: size_t index; universe@850: universe@850: /** universe@850: * The size of an individual element. universe@850: */ universe@850: size_t elem_size; universe@850: universe@850: /** universe@850: * May contain the total number of elements, if known. universe@850: * Shall be set to \c SIZE_MAX when the total number is unknown during iteration. universe@850: */ universe@850: size_t elem_count; universe@630: }; universe@630: universe@630: /** universe@853: * Iterator type. universe@630: * olaf@740: * An iterator points to a certain element in a (possibly unbounded) chain of elements. universe@500: * Iterators that are based on collections (which have a defined "first" element), are supposed universe@500: * to be "position-aware", which means that they keep track of the current index within the collection. universe@500: * universe@630: * @note Objects that are pointed to by an iterator are always mutable through that iterator. However, universe@853: * any concurrent mutation of the collection other than by this iterator makes this iterator invalid universe@853: * and it must not be used anymore. universe@500: */ universe@500: typedef struct cx_iterator_s CxIterator; universe@500: universe@500: /** universe@494: * Checks if the iterator points to valid data. universe@494: * universe@494: * This is especially false for past-the-end iterators. universe@494: * universe@630: * @param iter the iterator universe@494: * @return true iff the iterator points to valid data universe@494: */ universe@854: #define cxIteratorValid(iter) (iter).base.valid(&(iter)) universe@494: universe@494: /** universe@494: * Returns a pointer to the current element. universe@494: * universe@494: * The behavior is undefined if this iterator is invalid. universe@494: * universe@630: * @param iter the iterator universe@494: * @return a pointer to the current element universe@494: */ universe@854: #define cxIteratorCurrent(iter) (iter).base.current(&iter) universe@494: universe@494: /** universe@494: * Advances the iterator to the next element. universe@494: * universe@630: * @param iter the iterator universe@494: */ universe@854: #define cxIteratorNext(iter) (iter).base.next(&iter) universe@630: universe@630: /** universe@829: * Flags the current element for removal, if this iterator is mutating. universe@630: * universe@630: * @param iter the iterator universe@630: */ universe@854: #define cxIteratorFlagRemoval(iter) (iter).base.remove |= (iter).base.mutating universe@494: universe@496: /** universe@858: * Obtains a reference to an arbitrary iterator. universe@858: * universe@858: * This is useful for APIs that expect some iterator as an argument. universe@858: * universe@858: * @param iter the iterator universe@858: */ universe@858: #define cxIteratorRef(iter) &((iter).base) universe@858: universe@858: /** universe@496: * Loops over an iterator. universe@496: * @param type the type of the elements universe@496: * @param elem the name of the iteration variable universe@496: * @param iter the iterator universe@496: */ universe@496: #define cx_foreach(type, elem, iter) \ universe@630: for (type elem; cxIteratorValid(iter) && (elem = (type)cxIteratorCurrent(iter)) != NULL ; cxIteratorNext(iter)) universe@496: universe@850: universe@850: /** universe@852: * Creates an iterator for the specified plain array. universe@851: * universe@851: * The \p array can be \c NULL in which case the iterator will be immediately universe@851: * initialized such that #cxIteratorValid() returns \c false. universe@851: * universe@851: * universe@851: * @param array a pointer to the array (can be \c NULL) universe@851: * @param elem_size the size of one array element universe@851: * @param elem_count the number of elements in the array universe@851: * @return an iterator for the specified array universe@851: */ universe@851: __attribute__((__warn_unused_result__)) universe@851: CxIterator cxIterator( universe@851: void const *array, universe@851: size_t elem_size, universe@851: size_t elem_count universe@851: ); universe@851: universe@851: /** universe@852: * Creates a mutating iterator for the specified plain array. universe@850: * universe@850: * While the iterator is in use, the array may only be altered by removing universe@850: * elements through #cxIteratorFlagRemoval(). Every other change to the array universe@850: * will bring this iterator to an undefined state. universe@850: * universe@850: * When \p remove_keeps_order is set to \c false, removing an element will only universe@850: * move the last element to the position of the removed element, instead of universe@850: * moving all subsequent elements by one. Usually, when the order of elements is universe@850: * not important, this parameter should be set to \c false. universe@850: * universe@850: * The \p array can be \c NULL in which case the iterator will be immediately universe@850: * initialized such that #cxIteratorValid() returns \c false. universe@850: * universe@850: * universe@850: * @param array a pointer to the array (can be \c NULL) universe@850: * @param elem_size the size of one array element universe@850: * @param elem_count the number of elements in the array universe@850: * @param remove_keeps_order \c true if the order of elements must be preserved universe@850: * when removing an element universe@850: * @return an iterator for the specified array universe@850: */ universe@850: __attribute__((__warn_unused_result__)) universe@853: CxIterator cxMutIterator( universe@850: void *array, universe@850: size_t elem_size, universe@850: size_t elem_count, universe@850: bool remove_keeps_order universe@850: ); universe@850: universe@628: #endif // UCX_ITERATOR_H