ucx/cx/iterator.h

Wed, 27 Nov 2024 13:27:30 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 27 Nov 2024 13:27:30 +0100
changeset 90
f501f0efc9a8
parent 49
2f71f4ee247a
permissions
-rw-r--r--

implement gtk dnd

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

mercurial