ucx
UAP Common Extensions
Loading...
Searching...
No Matches
iterator.h
Go to the documentation of this file.
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 */
35
36#ifndef UCX_ITERATOR_H
37#define UCX_ITERATOR_H
38
39#include "common.h"
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
52 bool (*valid)(const void *);
56 bool (*valid_impl)(const void *);
62 void *(*current)(const void *);
63
67 void *(*current_impl)(const void *);
73 void (*next)(void *);
77 void (*next_impl)(void *);
85 bool remove;
86};
87
93
98#define CX_ITERATOR_BASE struct cx_iterator_base_s base
99
108
113
118
123 size_t index;
124
128 size_t elem_size;
129
135};
136
149
157#define cxIteratorValid(iter) (iter).base.valid(&(iter))
158
168#define cxIteratorCurrent(iter) (iter).base.current(&iter)
169
175#define cxIteratorNext(iter) (iter).base.next(&iter)
176
183#define cxIteratorFlagRemoval(iter) ((iter).base.remove = (iter).base.allow_remove)
184
193#define cxIteratorRef(iter) &((iter).base)
194
202#define cx_foreach(type, elem, iter) \
203for (type elem; cxIteratorValid(iter) && (elem = (type)cxIteratorCurrent(iter)) != NULL ; cxIteratorNext(iter))
204
205
236 size_t elem_size, size_t elem_count, bool remove_keeps_order);
237
264 bool remove_keeps_order);
265
266#ifdef __cplusplus
267} // extern "C"
268#endif
269
270#endif // UCX_ITERATOR_H
Common definitions and feature checks.
#define CX_EXPORT
Only used for building Windows DLLs.
Definition common.h:278
#define cx_attr_nodiscard
Warn about discarded return value.
Definition common.h:256
struct cx_iterator_s CxIterator
Iterator type.
Definition iterator.h:148
CxIterator cxIteratorPtr(const void *array, size_t elem_count, bool remove_keeps_order)
Creates an iterator for the specified plain pointer array.
#define CX_ITERATOR_BASE
Declares base attributes for an iterator.
Definition iterator.h:98
CxIterator cxIterator(const void *array, size_t elem_size, size_t elem_count, bool remove_keeps_order)
Creates an iterator for the specified plain array.
struct cx_iterator_base_s CxIteratorBase
Convenience type definition for the base structure of an iterator.
Definition iterator.h:92
Common data for all iterators.
Definition iterator.h:48
bool(* valid_impl)(const void *)
Original implementation in case the function needs to be wrapped.
Definition iterator.h:56
bool allow_remove
Indicates whether this iterator may remove elements.
Definition iterator.h:81
bool remove
Internal flag for removing the current element when advancing.
Definition iterator.h:85
void(* next_impl)(void *)
Original implementation in case the function needs to be wrapped.
Definition iterator.h:77
bool(* valid)(const void *)
True if the iterator points to valid data.
Definition iterator.h:52
void(* next)(void *)
Advances the iterator.
Definition iterator.h:73
Internal iterator struct - use CxIterator.
Definition iterator.h:103
size_t elem_count
May contain the total number of elements, if known.
Definition iterator.h:134
void * elem_handle
Handle for the current element.
Definition iterator.h:112
size_t index
If the iterator is position-aware, contains the index of the element in the underlying collection.
Definition iterator.h:123
void * src_handle
Handle for the source collection, if any.
Definition iterator.h:117
size_t elem_size
The size of an individual element.
Definition iterator.h:128