aboutsummaryrefslogtreecommitdiffstats
path: root/libparc/parc/algol/parc_Iterator.h
blob: 1f402cba8676364597c43757c114d4067d6737ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file parc_Iterator.h
 * @ingroup memory
 * @brief An iterator over any kind of iteratable collection
 *
 * Implementations of a PARCIterator must provide the following functions,
 * each of which supports the operation of the iterator.
 * @code
 * _TYPEIterator * PREFIX_Init(TYPE *object)
 *
 * bool PREFIX_Fini(TYPE *map, _TYPEIterator *state)
 *
 * _PARCHashMapIterator *PREFIX_Next(PARCHashMap *map, _TYPEIterator *state)
 *
 * void PREFIX_Remove(PARCHashMap *map, _TYPEIterator **statePtr )
 *
 * bool PREFIX_HasNext(PARCHashMap *map, _TYPEIterator *state)
 *
 * PARCObject *PREFIX_Element(PARCHashMap *map, const _TYPEIterator *state)
 *
 * PARCObject *PREFIX_Element(TYPE *map, const _TYPEIterator *state)
 * @endcode
 *
 */
#ifndef libparc_parc_Iterator_h
#define libparc_parc_Iterator_h

#include <stdbool.h>
#include <parc/algol/parc_Object.h>

struct parc_iterator;
typedef struct parc_iterator PARCIterator;

/**
 * Create a new instance of `PARCIterator`
 *
 * Each instance must be provided pointers to functions implementing the iteration primitives for the given PARCObject.
 *
 * * `init`
 * This function is called only when a PARCIterator is created.
 * The function is expected to initialise and return a pointer to whatever internal state necessary
 * to provide the subsequent operations.
 * In subsequent operations of hasNext, next, getElement, and fini, this pointer is provided as a function parameter.
 *
 * * `hasNext`
 * This function returns true if the iteration has more elements.
 *
 * * `next`
 * Returns the next element in the iteration.
 * If there are no remaining elements in the iteration, then this function must induce a parcTrapOutOfBounds
 *
 * * `remove`
 * Removes the element returned by the `next` function.
 *
 * * `getElement`
 * This function is invoked only from within the `parcIterator_Next` function and
 * returns the actual iteration value from the current iterator's state.
 *
 * * `fini`
 * This function is called only when the PARCIterator is being destroyed.
 *
 * @param [in] object A pointer to the PARCObject that implements the underlying iteration.
 * @param [in] hasNext A pointer to a function that returns true if there are more elements.
 * @param [in] next A pointer to a function that returns the next element.
 * @param [in] remove A pointer to a function that removes the element last returned by the @p next function.
 * @param [in] getElement A pointer to a function that, given the current position, return the element at the current position.
 * @param [in] fini A pointer to a function that will be invoked when the PARCIterator is finally deallocated.
 * @param [in] isValid A pointer to a function that performs validation of the iterator state.
 *
 * @return A `PARCIterator`
 */
PARCIterator *parcIterator_Create(PARCObject *object,
                                  void *(*init)(PARCObject *object),
                                  bool (*hasNext)(PARCObject *object, void *state),
                                  void *(*next)(PARCObject *object, void *state),
                                  void (*remove)(PARCObject *, void **state),
                                  void *(*getElement)(PARCObject *object, void *state),
                                  void (*fini)(PARCObject *object, void *state),
                                  void (*isValid)(const void *));

/**
 * Determine if an instance of `PARCIterator` is valid.
 *
 * Valid means the internal state of the type is consistent with its required current or future behaviour.
 * This may include the validation of internal instances of types.
 *
 * @param [in] instance A pointer to a `PARCIterator` instance.
 *
 * @return true The instance is valid.
 * @return false The instance is not valid.
 *
 * Example:
 * @code
 * {
 *     PARCIterator *instance = parcIterator_Create(...);
 *
 *     if (parcIterator_IsValid(instance)) {
 *         printf("Instance is valid.\n");
 *     }
 * }
 * @endcode
 */
bool parcIterator_IsValid(const PARCIterator *instance);

/**
 * Assert that the given `PARCIterator` instance is valid.
 *
 * @param [in] iterator A pointer to a valid PARCIterator instance.
 *
 * Example:
 * @code
 * {
 *     PARCIterator *a = parcIterator_Create(...);
 *
 *     parcIterator_AssertValid(a);
 *
 *     printf("Instance is valid.\n");
 *
 *     parcIterator_Release(&b);
 * }
 * @endcode
 */
#ifdef PARCLibrary_DISABLE_VALIDATION
#  define parcIterator_OptionalAssertValid(_instance_)
#else
#  define parcIterator_OptionalAssertValid(_instance_) parcIterator_AssertValid(_instance_)
#endif
void parcIterator_AssertValid(const PARCIterator *iterator);

/**
 * Increase the number of references to a `PARCIterator`.
 *
 * Note that new `PARCIterator` is not created,
 * only that the given `PARCIterator` reference count is incremented.
 * Discard the reference by invoking `parcIterator_Release`.
 *
 * @param [in] instance A pointer to a `PARCIterator` instance.
 *
 * @return The input `PARCIterator` pointer.
 *
 * Example:
 * @code
 * {
 *     PARCIterator *a = parcIterator_Create(...);
 *
 *     PARCIterator *b = parcIterator_Acquire(a);
 *
 *     parcIterator_Release(&a);
 *     parcIterator_Release(&b);
 * }
 * @endcode
 */
PARCIterator *parcIterator_Acquire(const PARCIterator *instance);

/**
 * Release a previously acquired reference to the specified instance,
 * decrementing the reference count for the instance.
 *
 * The pointer to the instance is set to NULL as a side-effect of this function.
 *
 * If the invocation causes the last reference to the instance to be released,
 * the instance is deallocated and the instance's implementation will perform
 * additional cleanup and release other privately held references.
 *
 * @param [in,out] iteratorPtr A pointer to a pointer to the instance to release, which will be set to NULL.
 *
 * Example:
 * @code
 * {
 *     PARCIterator *a = parcIterator_Create(...);
 *
 *     parcIterator_Release(&a);
 * }
 * @endcode
 */
void parcIterator_Release(PARCIterator **iteratorPtr);

/**
 * Return the next item in the iterated list
 *
 * @param [in] iterator A pointer to the instance of `PARCIterator`
 *
 * @return A pointer to the next item in the iterated list
 *
 * Example:
 * @code
 * {
 *      while (parcIterator_HasNext(iterator)) {
 *          void *element = parcIterator_Next(iterator);
 *      }
 * }
 * @endcode
 */
void *parcIterator_Next(PARCIterator *iterator);

/**
 * Return true if there are more items left over which to iterate.
 *
 * @param [in] iterator A pointer to the instance of `PARCIterator`
 *
 * @return True if there is are more items over which to iterate; false otherwise.
 *
 * Example:
 * @code
 * {
 *      while (parcIterator_HasNext(iterator)) {
 *          void *element = parcIterator_Next(iterator);
 *      }
 * }
 * @endcode
 */
bool parcIterator_HasNext(const PARCIterator *iterator);

/**
 * Removes from the underlying collection the last element returned by the iterator (optional operation).
 * This function can be called only once per call to `parcIterator_Next`.
 * The behavior of an iterator is unspecified if the underlying collection is
 * modified while the iteration is in progress in any way other than by calling this method.
 *
 * Pointers to the element after removing it via this function may point to invalid remnants of the object.
 * To avoid this, acquire a reference to the element before invoking this function.
 *
 * @param [in] iterator A pointer to the instance of `PARCIterator`
 *
 * Example:
 * @code
 * {
 *      while (parcIterator_HasNext(iterator)) {
 *          void *element = parcIterator_Next(iterator);
 *      }
 * }
 * @endcode
 */
void parcIterator_Remove(PARCIterator *iterator);
#endif // libparc_parc_Iterator_h