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