src/cx/iterator.h

Sat, 26 Nov 2022 16:58:41 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 26 Nov 2022 16:58:41 +0100
changeset 630
ac5e7f789048
parent 628
1e2be40f0cb5
child 641
d402fead3386
permissions
-rw-r--r--

separate iterators and mutating iterators

Trade tons of code duplication for const-correctness.

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

mercurial