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
48 bool (*valid)(const void *);
52 bool (*valid_impl)(const void *);
58 void *(*current)(const void *);
59
63 void *(*current_impl)(const void *);
69 void (*next)(void *);
73 void (*next_impl)(void *);
81 bool remove;
82};
83
89
94#define CX_ITERATOR_BASE struct cx_iterator_base_s base
95
104
109
114
119 size_t index;
120
124 size_t elem_size;
125
131};
132
145
153#define cxIteratorValid(iter) (iter).base.valid(&(iter))
154
164#define cxIteratorCurrent(iter) (iter).base.current(&iter)
165
171#define cxIteratorNext(iter) (iter).base.next(&iter)
172
179#define cxIteratorFlagRemoval(iter) ((iter).base.remove = (iter).base.allow_remove)
180
188#define cx_foreach(type, elem, iter) \
189for (type elem; cxIteratorValid(iter) && (elem = (type)cxIteratorCurrent(iter)) != NULL ; cxIteratorNext(iter))
190
191
210CxIterator cxIterator(const void *array,
211 size_t elem_size, size_t elem_count);
212
227CxIterator cxIteratorPtr(const void *array, size_t elem_count);
228
229#endif // UCX_ITERATOR_H
Common definitions and feature checks.
#define CX_NODISCARD
Warn about discarded return value.
Definition common.h:256
#define CX_EXTERN
Declares a function with external linkage.
Definition common.h:297
struct cx_iterator_s CxIterator
Iterator type.
Definition iterator.h:144
#define CX_ITERATOR_BASE
Declares base attributes for an iterator.
Definition iterator.h:94
CxIterator cxIteratorPtr(const void *array, size_t elem_count)
Creates an iterator for the specified plain pointer array.
CxIterator cxIterator(const void *array, size_t elem_size, size_t elem_count)
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:88
Common data for all iterators.
Definition iterator.h:44
bool(* valid_impl)(const void *)
Original implementation in case the function needs to be wrapped.
Definition iterator.h:52
bool allow_remove
Indicates whether this iterator may remove elements.
Definition iterator.h:77
bool remove
Internal flag for removing the current element when advancing.
Definition iterator.h:81
void(* next_impl)(void *)
Original implementation in case the function needs to be wrapped.
Definition iterator.h:73
bool(* valid)(const void *)
True if the iterator points to valid data.
Definition iterator.h:48
void(* next)(void *)
Advances the iterator.
Definition iterator.h:69
Internal iterator struct - use CxIterator.
Definition iterator.h:99
size_t elem_count
May contain the total number of elements, if known.
Definition iterator.h:130
void * elem_handle
Handle for the current element.
Definition iterator.h:108
size_t index
If the iterator is position-aware, contains the index of the element in the underlying collection.
Definition iterator.h:119
void * src_handle
Handle for the source collection, if any.
Definition iterator.h:113
size_t elem_size
The size of an individual element.
Definition iterator.h:124