src/cx/iterator.h

Thu, 14 Mar 2024 22:07:19 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 14 Mar 2024 22:07:19 +0100
changeset 844
3270ea9e41ef
parent 829
7d4e31d295af
child 850
b2bc48c2b251
permissions
-rw-r--r--

fix another superfluous semicolon...

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

mercurial