src/ucx/cx/iterator.h

Mon, 10 Nov 2025 21:06:55 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 10 Nov 2025 21:06:55 +0100
changeset 622
6e44c7ce0834
parent 621
956c03c25edd
permissions
-rw-r--r--

add ucx kv_list

415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28 /**
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
29 * @file iterator.h
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
30 * @brief Interface for iterator implementations.
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
31 * @author Mike Becker
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
32 * @author Olaf Wintermann
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
33 * @copyright 2-Clause BSD License
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 */
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 #ifndef UCX_ITERATOR_H
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 #define UCX_ITERATOR_H
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 #include "common.h"
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
41 #ifdef __cplusplus
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
42 extern "C" {
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
43 #endif
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
44
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 /**
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
46 * Common data for all iterators.
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 */
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
48 struct cx_iterator_base_s {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 /**
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
50 * True if the iterator points to valid data.
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 */
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
52 bool (*valid)(const void *);
621
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
53 /**
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
54 * Original implementation in case the function needs to be wrapped.
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
55 */
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
56 bool (*valid_impl)(const void *);
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 /**
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 * Returns a pointer to the current element.
504
c094afcdfb27 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 490
diff changeset
59 *
c094afcdfb27 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 490
diff changeset
60 * When valid returns false, the behavior of this function is undefined.
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 */
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
62 void *(*current)(const void *);
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 /**
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
65 * Original implementation in case the function needs to be wrapped.
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
66 */
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
67 void *(*current_impl)(const void *);
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
68 /**
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 * Advances the iterator.
504
c094afcdfb27 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 490
diff changeset
70 *
c094afcdfb27 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 490
diff changeset
71 * When valid returns false, the behavior of this function is undefined.
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 */
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
73 void (*next)(void *);
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
74 /**
621
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
75 * Original implementation in case the function needs to be wrapped.
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
76 */
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
77 void (*next_impl)(void *);
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
78 /**
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
79 * Indicates whether this iterator may remove elements.
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
80 */
621
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
81 bool allow_remove;
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
82 /**
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
83 * Internal flag for removing the current element when advancing.
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
84 */
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
85 bool remove;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
86 };
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
87
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
88 /**
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
89 * Convenience type definition for the base structure of an iterator.
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
90 * @see #CX_ITERATOR_BASE
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
91 */
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
92 typedef struct cx_iterator_base_s CxIteratorBase;
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
93
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
94 /**
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
95 * Declares base attributes for an iterator.
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
96 * Must be the first member of an iterator structure.
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
97 */
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
98 #define CX_ITERATOR_BASE struct cx_iterator_base_s base
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
99
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
100 /**
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
101 * Internal iterator struct - use CxIterator.
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
102 */
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
103 struct cx_iterator_s {
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
104 /**
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
105 * Inherited common data for all iterators.
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
106 */
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
107 CX_ITERATOR_BASE;
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
108
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
109 /**
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
110 * Handle for the current element.
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 */
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 void *elem_handle;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 /**
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 * Handle for the source collection, if any.
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 */
621
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
117 void *src_handle;
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 /**
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 * If the iterator is position-aware, contains the index of the element in the underlying collection.
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 * Otherwise, this field is usually uninitialized.
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 */
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 size_t index;
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
124
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
125 /**
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
126 * The size of an individual element.
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
127 */
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
128 size_t elem_size;
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
129
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
130 /**
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
131 * May contain the total number of elements, if known.
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
132 * Shall be set to @c SIZE_MAX when the total number is unknown during iteration.
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
133 */
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
134 size_t elem_count;
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
135 };
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
136
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
137 /**
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
138 * Iterator type.
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
139 *
504
c094afcdfb27 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 490
diff changeset
140 * An iterator points to a certain element in a (possibly unbounded) chain of elements.
621
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
141 * Iterators that are based on collections (which have a defined "first" element) are supposed
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 * to be "position-aware", which means that they keep track of the current index within the collection.
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 *
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
144 * @note Objects that are pointed to by an iterator are always mutable through that iterator. However,
621
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
145 * any concurrent mutation of the collection other than by this iterator makes this iterator obsolete,
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
146 * and it must not be used anymore.
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 */
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 typedef struct cx_iterator_s CxIterator;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 /**
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 * Checks if the iterator points to valid data.
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 *
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
153 * @param iter the iterator
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
154 * @retval true if the iterator points to valid data
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
155 * @retval false if the iterator already moved past the end
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 */
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
157 #define cxIteratorValid(iter) (iter).base.valid(&(iter))
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 /**
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 * Returns a pointer to the current element.
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 *
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 * The behavior is undefined if this iterator is invalid.
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 *
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
164 * @param iter the iterator
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 * @return a pointer to the current element
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
166 * @see cxIteratorValid()
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 */
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
168 #define cxIteratorCurrent(iter) (iter).base.current(&iter)
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 /**
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 * Advances the iterator to the next element.
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 *
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
173 * @param iter the iterator
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 */
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
175 #define cxIteratorNext(iter) (iter).base.next(&iter)
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
176
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
177 /**
621
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
178 * Flags the current element for removal if the iterator allows it.
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
179 *
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
180 * @param iter the iterator
621
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
181 * @return @c true if removal is allowed, @c false otherwise
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
182 */
621
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
183 #define cxIteratorFlagRemoval(iter) ((iter).base.remove = (iter).base.allow_remove)
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
184
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
185 /**
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
186 * Obtains a reference to an arbitrary iterator.
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
187 *
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
188 * This is useful for APIs that expect some iterator as an argument.
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
189 *
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
190 * @param iter the iterator
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
191 * @return (@c struct @c cx_iterator_base_s*) a pointer to the iterator
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
192 */
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
193 #define cxIteratorRef(iter) &((iter).base)
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 /**
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 * Loops over an iterator.
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
197 *
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 * @param type the type of the elements
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 * @param elem the name of the iteration variable
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 * @param iter the iterator
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 */
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 #define cx_foreach(type, elem, iter) \
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
203 for (type elem; cxIteratorValid(iter) && (elem = (type)cxIteratorCurrent(iter)) != NULL ; cxIteratorNext(iter))
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
205
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
206 /**
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
207 * Creates an iterator for the specified plain array.
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
208 *
621
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
209 * The @p array can be @c NULL, in which case the iterator will be immediately
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
210 * initialized such that #cxIteratorValid() returns @c false.
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
211 *
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
212 * This iterator yields the addresses of the array elements.
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
213 * If you want to iterator over an array of pointers, you can
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
214 * use cxIteratorPtr() to create an iterator which directly
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
215 * yields the stored pointers.
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
216 *
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
217 * While the iterator is in use, the array may only be altered by removing
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
218 * elements through #cxIteratorFlagRemoval(). Every other change to the array
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
219 * will bring this iterator to an undefined state.
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
220 *
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
221 * When @p remove_keeps_order is set to @c false, removing an element will only
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
222 * move the last element to the position of the removed element, instead of
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
223 * moving all subsequent elements by one. Usually, when the order of elements is
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
224 * not important, this parameter should be set to @c false.
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
225 *
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
226 * @param array a pointer to the array (can be @c NULL)
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
227 * @param elem_size the size of one array element
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
228 * @param elem_count the number of elements in the array
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
229 * @param remove_keeps_order @c true if the order of elements must be preserved
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
230 * when removing an element
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
231 * @return an iterator for the specified array
621
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
232 * @see cxIteratorPtr()
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
233 */
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
234 cx_attr_nodiscard
621
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
235 CX_EXPORT CxIterator cxIterator(const void *array,
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
236 size_t elem_size, size_t elem_count, bool remove_keeps_order);
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
237
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
238 /**
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
239 * Creates an iterator for the specified plain pointer array.
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
240 *
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
241 * This iterator assumes that every element in the array is a pointer
621
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
242 * and yields exactly those pointers during iteration (on the other
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
243 * hand, an iterator created with cxIterator() would return the
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
244 * addresses of those pointers within the array).
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
245 *
621
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
246 * While the iterator is in use, the array may only be altered by removing
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
247 * elements through #cxIteratorFlagRemoval(). Every other change to the array
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
248 * will bring this iterator to an undefined state.
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
249 *
621
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
250 * When @p remove_keeps_order is set to @c false, removing an element will only
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
251 * move the last element to the position of the removed element, instead of
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
252 * moving all subsequent elements by one. Usually, when the order of elements is
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
253 * not important, this parameter should be set to @c false.
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
254 *
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
255 * @param array a pointer to the array (can be @c NULL)
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
256 * @param elem_count the number of elements in the array
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
257 * @param remove_keeps_order @c true if the order of elements must be preserved
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
258 * when removing an element
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
259 * @return an iterator for the specified array
621
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
260 * @see cxIterator()
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
261 */
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
262 cx_attr_nodiscard
621
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
263 CX_EXPORT CxIterator cxIteratorPtr(const void *array, size_t elem_count,
956c03c25edd update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 579
diff changeset
264 bool remove_keeps_order);
579
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
265
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
266 #ifdef __cplusplus
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
267 } // extern "C"
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
268 #endif
e10457d74fe1 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 504
diff changeset
269
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
270 #endif // UCX_ITERATOR_H

mercurial