src/cx/iterator.h

Sun, 22 Dec 2024 23:10:07 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 22 Dec 2024 23:10:07 +0100
changeset 1050
3df63e95921a
parent 1035
9b6ded88d7a0
permissions
-rw-r--r--

make cx_strcast() also support cxstring

also makes the solution for issue #536 a lot nicer

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
933
40bf2f212d2a add extern "C" to iterator.h
Mike Becker <universe@uap-core.de>
parents: 890
diff changeset
41 #ifdef __cplusplus
40bf2f212d2a add extern "C" to iterator.h
Mike Becker <universe@uap-core.de>
parents: 890
diff changeset
42 extern "C" {
40bf2f212d2a add extern "C" to iterator.h
Mike Becker <universe@uap-core.de>
parents: 890
diff changeset
43 #endif
40bf2f212d2a add extern "C" to iterator.h
Mike Becker <universe@uap-core.de>
parents: 890
diff changeset
44
1035
9b6ded88d7a0 add missing documentation in iterator.h
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
45 /**
9b6ded88d7a0 add missing documentation in iterator.h
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
46 * Common data for all iterators.
9b6ded88d7a0 add missing documentation in iterator.h
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
47 */
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
48 struct cx_iterator_base_s {
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
49 /**
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
50 * True iff the iterator points to valid data.
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
51 */
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 933
diff changeset
52 cx_attr_nonnull
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 858
diff changeset
53 bool (*valid)(const void *);
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
54
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
55 /**
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
56 * Returns a pointer to the current element.
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
57 *
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
58 * When valid returns false, the behavior of this function is undefined.
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
59 */
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 933
diff changeset
60 cx_attr_nonnull
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 933
diff changeset
61 cx_attr_nodiscard
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 858
diff changeset
62 void *(*current)(const void *);
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
63
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
64 /**
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
65 * Original implementation in case the function needs to be wrapped.
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
66 */
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 933
diff changeset
67 cx_attr_nonnull
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 933
diff changeset
68 cx_attr_nodiscard
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 858
diff changeset
69 void *(*current_impl)(const void *);
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
70
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
71 /**
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
72 * Advances the iterator.
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
73 *
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
74 * When valid returns false, the behavior of this function is undefined.
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
75 */
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 933
diff changeset
76 cx_attr_nonnull
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
77 void (*next)(void *);
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
78 /**
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
79 * Indicates whether this iterator may remove elements.
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
80 */
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
81 bool mutating;
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
82 /**
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
83 * Internal flag for removing the current element when advancing.
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
84 */
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 852
diff changeset
85 bool remove;
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
86 };
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
87
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
88 /**
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
89 * Declares base attributes for an iterator.
858
d9ad7904c4c2 add cxIteratorRef() macro
Mike Becker <universe@uap-core.de>
parents: 854
diff changeset
90 * Must be the first member of an iterator structure.
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
91 */
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
92 #define CX_ITERATOR_BASE struct cx_iterator_base_s base
494
6ce8cfa10a96 add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 852
diff changeset
94 /**
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 852
diff changeset
95 * Internal iterator struct - use CxIterator.
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 852
diff changeset
96 */
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 852
diff changeset
97 struct cx_iterator_s {
1035
9b6ded88d7a0 add missing documentation in iterator.h
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
98 /**
9b6ded88d7a0 add missing documentation in iterator.h
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
99 * Inherited common data for all iterators.
9b6ded88d7a0 add missing documentation in iterator.h
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
100 */
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
101 CX_ITERATOR_BASE;
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
102
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
103 /**
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 852
diff changeset
104 * Handle for the current element.
495
2856c74e18ba add the feature to remove items during iteration
Mike Becker <universe@uap-core.de>
parents: 494
diff changeset
105 */
2856c74e18ba add the feature to remove items during iteration
Mike Becker <universe@uap-core.de>
parents: 494
diff changeset
106 void *elem_handle;
2856c74e18ba add the feature to remove items during iteration
Mike Becker <universe@uap-core.de>
parents: 494
diff changeset
107
2856c74e18ba add the feature to remove items during iteration
Mike Becker <universe@uap-core.de>
parents: 494
diff changeset
108 /**
2856c74e18ba add the feature to remove items during iteration
Mike Becker <universe@uap-core.de>
parents: 494
diff changeset
109 * 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
110 */
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 852
diff changeset
111 union {
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 852
diff changeset
112 /**
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 852
diff changeset
113 * Access for mutating iterators.
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 852
diff changeset
114 */
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 852
diff changeset
115 void *m;
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 852
diff changeset
116 /**
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 852
diff changeset
117 * Access for normal iterators.
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 852
diff changeset
118 */
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 858
diff changeset
119 const void *c;
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 852
diff changeset
120 } src_handle;
495
2856c74e18ba add the feature to remove items during iteration
Mike Becker <universe@uap-core.de>
parents: 494
diff changeset
121
2856c74e18ba add the feature to remove items during iteration
Mike Becker <universe@uap-core.de>
parents: 494
diff changeset
122 /**
551
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 516
diff changeset
123 * Field for storing a key-value pair.
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 516
diff changeset
124 * 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
125 */
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 516
diff changeset
126 struct {
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 516
diff changeset
127 /**
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 516
diff changeset
128 * A pointer to the key.
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 516
diff changeset
129 */
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 858
diff changeset
130 const void *key;
551
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 516
diff changeset
131 /**
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 516
diff changeset
132 * A pointer to the value.
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 516
diff changeset
133 */
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 516
diff changeset
134 void *value;
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 516
diff changeset
135 } kv_data;
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 516
diff changeset
136
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 516
diff changeset
137 /**
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 516
diff changeset
138 * Field for storing a slot number.
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 516
diff changeset
139 * 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
140 */
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 516
diff changeset
141 size_t slot;
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 516
diff changeset
142
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 516
diff changeset
143 /**
495
2856c74e18ba add the feature to remove items during iteration
Mike Becker <universe@uap-core.de>
parents: 494
diff changeset
144 * 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
145 * 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
146 */
2856c74e18ba add the feature to remove items during iteration
Mike Becker <universe@uap-core.de>
parents: 494
diff changeset
147 size_t index;
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
148
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
149 /**
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
150 * The size of an individual element.
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
151 */
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
152 size_t elem_size;
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
153
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
154 /**
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
155 * May contain the total number of elements, if known.
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
156 * Shall be set to \c SIZE_MAX when the total number is unknown during iteration.
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
157 */
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
158 size_t elem_count;
630
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
159 };
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 /**
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 852
diff changeset
162 * Iterator type.
630
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
163 *
740
378578666c83 fix iterator doc
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
164 * 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
165 * 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
166 * 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
167 *
630
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
168 * @note Objects that are pointed to by an iterator are always mutable through that iterator. However,
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 852
diff changeset
169 * any concurrent mutation of the collection other than by this iterator makes this iterator invalid
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 852
diff changeset
170 * and it must not be used anymore.
500
eb9e7bd40a8e do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents: 497
diff changeset
171 */
eb9e7bd40a8e do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents: 497
diff changeset
172 typedef struct cx_iterator_s CxIterator;
eb9e7bd40a8e do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents: 497
diff changeset
173
eb9e7bd40a8e do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents: 497
diff changeset
174 /**
494
6ce8cfa10a96 add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
diff changeset
175 * Checks if the iterator points to valid data.
6ce8cfa10a96 add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176 *
6ce8cfa10a96 add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 * 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
178 *
630
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
179 * @param iter the iterator
494
6ce8cfa10a96 add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
diff changeset
180 * @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
181 */
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
182 #define cxIteratorValid(iter) (iter).base.valid(&(iter))
494
6ce8cfa10a96 add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
diff changeset
183
6ce8cfa10a96 add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
diff changeset
184 /**
6ce8cfa10a96 add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
diff changeset
185 * Returns a pointer to the current element.
6ce8cfa10a96 add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
diff changeset
186 *
6ce8cfa10a96 add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
diff changeset
187 * 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
188 *
630
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
189 * @param iter the iterator
494
6ce8cfa10a96 add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
diff changeset
190 * @return a pointer to the current element
6ce8cfa10a96 add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191 */
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
192 #define cxIteratorCurrent(iter) (iter).base.current(&iter)
494
6ce8cfa10a96 add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193
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 * Advances the iterator to the next element.
6ce8cfa10a96 add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196 *
630
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
197 * @param iter the iterator
494
6ce8cfa10a96 add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198 */
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
199 #define cxIteratorNext(iter) (iter).base.next(&iter)
630
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
200
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
201 /**
829
7d4e31d295af remove unnecessary flag_removal function
Mike Becker <universe@uap-core.de>
parents: 759
diff changeset
202 * 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
203 *
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
204 * @param iter the iterator
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
205 */
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
206 #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
207
496
1a07e24801a9 add cx_foreach macro
Mike Becker <universe@uap-core.de>
parents: 495
diff changeset
208 /**
858
d9ad7904c4c2 add cxIteratorRef() macro
Mike Becker <universe@uap-core.de>
parents: 854
diff changeset
209 * Obtains a reference to an arbitrary iterator.
d9ad7904c4c2 add cxIteratorRef() macro
Mike Becker <universe@uap-core.de>
parents: 854
diff changeset
210 *
d9ad7904c4c2 add cxIteratorRef() macro
Mike Becker <universe@uap-core.de>
parents: 854
diff changeset
211 * This is useful for APIs that expect some iterator as an argument.
d9ad7904c4c2 add cxIteratorRef() macro
Mike Becker <universe@uap-core.de>
parents: 854
diff changeset
212 *
d9ad7904c4c2 add cxIteratorRef() macro
Mike Becker <universe@uap-core.de>
parents: 854
diff changeset
213 * @param iter the iterator
d9ad7904c4c2 add cxIteratorRef() macro
Mike Becker <universe@uap-core.de>
parents: 854
diff changeset
214 */
d9ad7904c4c2 add cxIteratorRef() macro
Mike Becker <universe@uap-core.de>
parents: 854
diff changeset
215 #define cxIteratorRef(iter) &((iter).base)
d9ad7904c4c2 add cxIteratorRef() macro
Mike Becker <universe@uap-core.de>
parents: 854
diff changeset
216
d9ad7904c4c2 add cxIteratorRef() macro
Mike Becker <universe@uap-core.de>
parents: 854
diff changeset
217 /**
496
1a07e24801a9 add cx_foreach macro
Mike Becker <universe@uap-core.de>
parents: 495
diff changeset
218 * Loops over an iterator.
1a07e24801a9 add cx_foreach macro
Mike Becker <universe@uap-core.de>
parents: 495
diff changeset
219 * @param type the type of the elements
1a07e24801a9 add cx_foreach macro
Mike Becker <universe@uap-core.de>
parents: 495
diff changeset
220 * @param elem the name of the iteration variable
1a07e24801a9 add cx_foreach macro
Mike Becker <universe@uap-core.de>
parents: 495
diff changeset
221 * @param iter the iterator
1a07e24801a9 add cx_foreach macro
Mike Becker <universe@uap-core.de>
parents: 495
diff changeset
222 */
1a07e24801a9 add cx_foreach macro
Mike Becker <universe@uap-core.de>
parents: 495
diff changeset
223 #define cx_foreach(type, elem, iter) \
630
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
224 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
225
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
226
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
227 /**
852
16e2a3391e88 fix documentation
Mike Becker <universe@uap-core.de>
parents: 851
diff changeset
228 * Creates an iterator for the specified plain array.
851
adb4e0737c33 issue #389 : add separate function for immutable arrays
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
229 *
adb4e0737c33 issue #389 : add separate function for immutable arrays
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
230 * The \p array can be \c NULL in which case the iterator will be immediately
adb4e0737c33 issue #389 : add separate function for immutable arrays
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
231 * initialized such that #cxIteratorValid() returns \c false.
adb4e0737c33 issue #389 : add separate function for immutable arrays
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
232 *
adb4e0737c33 issue #389 : add separate function for immutable arrays
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
233 *
adb4e0737c33 issue #389 : add separate function for immutable arrays
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
234 * @param array a pointer to the array (can be \c NULL)
adb4e0737c33 issue #389 : add separate function for immutable arrays
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
235 * @param elem_size the size of one array element
adb4e0737c33 issue #389 : add separate function for immutable arrays
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
236 * @param elem_count the number of elements in the array
adb4e0737c33 issue #389 : add separate function for immutable arrays
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
237 * @return an iterator for the specified array
adb4e0737c33 issue #389 : add separate function for immutable arrays
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
238 */
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 933
diff changeset
239 cx_attr_nodiscard
851
adb4e0737c33 issue #389 : add separate function for immutable arrays
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
240 CxIterator cxIterator(
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 858
diff changeset
241 const void *array,
851
adb4e0737c33 issue #389 : add separate function for immutable arrays
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
242 size_t elem_size,
adb4e0737c33 issue #389 : add separate function for immutable arrays
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
243 size_t elem_count
adb4e0737c33 issue #389 : add separate function for immutable arrays
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
244 );
adb4e0737c33 issue #389 : add separate function for immutable arrays
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
245
adb4e0737c33 issue #389 : add separate function for immutable arrays
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
246 /**
852
16e2a3391e88 fix documentation
Mike Becker <universe@uap-core.de>
parents: 851
diff changeset
247 * Creates a mutating iterator for the specified plain array.
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
248 *
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
249 * While the iterator is in use, the array may only be altered by removing
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
250 * elements through #cxIteratorFlagRemoval(). Every other change to the array
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
251 * will bring this iterator to an undefined state.
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
252 *
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
253 * When \p remove_keeps_order is set to \c false, removing an element will only
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
254 * move the last element to the position of the removed element, instead of
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
255 * moving all subsequent elements by one. Usually, when the order of elements is
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
256 * not important, this parameter should be set to \c false.
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
257 *
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
258 * The \p array can be \c NULL in which case the iterator will be immediately
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
259 * initialized such that #cxIteratorValid() returns \c false.
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
260 *
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
261 *
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
262 * @param array a pointer to the array (can be \c NULL)
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
263 * @param elem_size the size of one array element
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
264 * @param elem_count the number of elements in the array
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
265 * @param remove_keeps_order \c true if the order of elements must be preserved
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
266 * when removing an element
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
267 * @return an iterator for the specified array
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
268 */
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 933
diff changeset
269 cx_attr_nodiscard
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 852
diff changeset
270 CxIterator cxMutIterator(
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
271 void *array,
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
272 size_t elem_size,
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
273 size_t elem_count,
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
274 bool remove_keeps_order
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
275 );
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
276
933
40bf2f212d2a add extern "C" to iterator.h
Mike Becker <universe@uap-core.de>
parents: 890
diff changeset
277 #ifdef __cplusplus
40bf2f212d2a add extern "C" to iterator.h
Mike Becker <universe@uap-core.de>
parents: 890
diff changeset
278 } // extern "C"
40bf2f212d2a add extern "C" to iterator.h
Mike Becker <universe@uap-core.de>
parents: 890
diff changeset
279 #endif
40bf2f212d2a add extern "C" to iterator.h
Mike Becker <universe@uap-core.de>
parents: 890
diff changeset
280
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 551
diff changeset
281 #endif // UCX_ITERATOR_H

mercurial