src/cx/iterator.h

Mon, 18 Dec 2023 14:14:47 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 18 Dec 2023 14:14:47 +0100
changeset 759
475335643af4
parent 740
378578666c83
child 829
7d4e31d295af
permissions
-rw-r--r--

increase version number to 3.1

remove per-file version information
from Doxygen output

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  */
    28 /**
    29  * \file iterator.h
    30  * \brief Interface for iterator implementations.
    31  * \author Mike Becker
    32  * \author Olaf Wintermann
    33  * \copyright 2-Clause BSD License
    34  */
    36 #ifndef UCX_ITERATOR_H
    37 #define UCX_ITERATOR_H
    39 #include "common.h"
    41 /**
    42  * The base of mutating and non-mutating iterators.
    43  */
    44 struct cx_iterator_base_s {
    45     /**
    46      * True iff the iterator points to valid data.
    47      */
    48     __attribute__ ((__nonnull__))
    49     bool (*valid)(void const *);
    51     /**
    52      * Returns a pointer to the current element.
    53      *
    54      * When valid returns false, the behavior of this function is undefined.
    55      */
    56     __attribute__ ((__nonnull__))
    57     void *(*current)(void const *);
    59     /**
    60      * Original implementation in case the function needs to be wrapped.
    61      */
    62     __attribute__ ((__nonnull__))
    63     void *(*current_impl)(void const *);
    65     /**
    66      * Advances the iterator.
    67      *
    68      * When valid returns false, the behavior of this function is undefined.
    69      */
    70     __attribute__ ((__nonnull__))
    71     void (*next)(void *);
    73     /**
    74      * Flag current element for removal, if possible.
    75      *
    76      * When valid returns false, the behavior of this function is undefined.
    77      */
    78     __attribute__ ((__nonnull__))
    79     bool (*flag_removal)(void *);
    81     /**
    82      * Indicates whether this iterator may remove elements.
    83      */
    84     bool mutating;
    86     /**
    87      * Internal flag for removing the current element when advancing.
    88      */
    89     bool remove;
    90 };
    92 /**
    93  * Internal iterator struct - use CxMutIterator.
    94  */
    95 struct cx_mut_iterator_s {
    97     /**
    98      * The base properties of this iterator.
    99      */
   100     struct cx_iterator_base_s base;
   102     /**
   103      * Handle for the current element, if required.
   104      */
   105     void *elem_handle;
   107     /**
   108      * Handle for the source collection, if any.
   109      */
   110     void *src_handle;
   112     /**
   113      * Field for storing a key-value pair.
   114      * May be used by iterators that iterate over k/v-collections.
   115      */
   116     struct {
   117         /**
   118          * A pointer to the key.
   119          */
   120         void const *key;
   121         /**
   122          * A pointer to the value.
   123          */
   124         void *value;
   125     } kv_data;
   127     /**
   128      * Field for storing a slot number.
   129      * May be used by iterators that iterate over multi-bucket collections.
   130      */
   131     size_t slot;
   133     /**
   134      * If the iterator is position-aware, contains the index of the element in the underlying collection.
   135      * Otherwise, this field is usually uninitialized.
   136      */
   137     size_t index;
   138 };
   140 /**
   141  * Mutating iterator value type.
   142  *
   143  * An iterator points to a certain element in an (possibly unbounded) chain of elements.
   144  * Iterators that are based on collections (which have a defined "first" element), are supposed
   145  * to be "position-aware", which means that they keep track of the current index within the collection.
   146  *
   147  * @note Objects that are pointed to by an iterator are mutable through that iterator. However, if the
   148  * iterator is based on a collection and the underlying collection is mutated by other means than this iterator
   149  * (e.g. elements added or removed), the iterator becomes invalid (regardless of what cxIteratorValid() returns)
   150  * and MUST be re-obtained from the collection.
   151  *
   152  * @see CxIterator
   153  */
   154 typedef struct cx_mut_iterator_s CxMutIterator;
   156 /**
   157  * Internal iterator struct - use CxIterator.
   158  */
   159 struct cx_iterator_s {
   161     /**
   162      * The base properties of this iterator.
   163      */
   164     struct cx_iterator_base_s base;
   166     /**
   167      * Handle for the current element, if required.
   168      */
   169     void *elem_handle;
   171     /**
   172      * Handle for the source collection, if any.
   173      */
   174     void const *src_handle;
   176     /**
   177      * Field for storing a key-value pair.
   178      * May be used by iterators that iterate over k/v-collections.
   179      */
   180     struct {
   181         /**
   182          * A pointer to the key.
   183          */
   184         void const *key;
   185         /**
   186          * A pointer to the value.
   187          */
   188         void *value;
   189     } kv_data;
   191     /**
   192      * Field for storing a slot number.
   193      * May be used by iterators that iterate over multi-bucket collections.
   194      */
   195     size_t slot;
   197     /**
   198      * If the iterator is position-aware, contains the index of the element in the underlying collection.
   199      * Otherwise, this field is usually uninitialized.
   200      */
   201     size_t index;
   202 };
   204 /**
   205  * Iterator value type.
   206  * An iterator points to a certain element in a (possibly unbounded) chain of elements.
   207  * Iterators that are based on collections (which have a defined "first" element), are supposed
   208  * to be "position-aware", which means that they keep track of the current index within the collection.
   209  *
   210  * @note Objects that are pointed to by an iterator are always mutable through that iterator. However,
   211  * this iterator cannot mutate the collection itself (add or remove elements) and any mutation of the
   212  * collection by other means makes this iterator invalid (regardless of what cxIteratorValid() returns).
   213  *
   214  * @see CxMutIterator
   215  */
   216 typedef struct cx_iterator_s CxIterator;
   218 /**
   219  * Checks if the iterator points to valid data.
   220  *
   221  * This is especially false for past-the-end iterators.
   222  *
   223  * @param iter the iterator
   224  * @return true iff the iterator points to valid data
   225  */
   226 #define cxIteratorValid(iter) (iter).base.valid(&(iter))
   228 /**
   229  * Returns a pointer to the current element.
   230  *
   231  * The behavior is undefined if this iterator is invalid.
   232  *
   233  * @param iter the iterator
   234  * @return a pointer to the current element
   235  */
   236 #define cxIteratorCurrent(iter) (iter).base.current(&iter)
   238 /**
   239  * Advances the iterator to the next element.
   240  *
   241  * @param iter the iterator
   242  */
   243 #define cxIteratorNext(iter) (iter).base.next(&iter)
   245 /**
   246  * Flags the current element for removal.
   247  *
   248  * @param iter the iterator
   249  * @return false if this iterator cannot remove the element
   250  */
   251 #define cxIteratorFlagRemoval(iter) (iter).base.flag_removal(&iter)
   253 /**
   254  * Loops over an iterator.
   255  * @param type the type of the elements
   256  * @param elem the name of the iteration variable
   257  * @param iter the iterator
   258  */
   259 #define cx_foreach(type, elem, iter) \
   260 for (type elem; cxIteratorValid(iter) && (elem = (type)cxIteratorCurrent(iter)) != NULL ; cxIteratorNext(iter))
   262 #endif // UCX_ITERATOR_H

mercurial