Project Euler Solutions
iterator.h
Go to the documentation of this file.
1 
7 #ifndef ITERATOR_H
8 #define ITERATOR_H
9 
10 #include <stdbool.h>
11 #include <stdint.h>
12 #include "macros.h"
13 
14 #ifdef DOXYGEN
16 #endif
17 
18 void no_destructor(void *it);
19 inline void no_destructor(void *it) {}
20 
46 #define IteratorTail(return_type, struct_type) \
47  return_type (*const iterator_function)(struct_type *it); \
48  void (*const destructor)(void *it); \
49  bool exhausted : 1;\
50  bool started : 1;\
51  bool phase : 1;
74 #define IterationHead(it) \
75  it->started = 1; \
76  it->phase = !(it->phase)
77 
111 #define IteratorInitHead(advance, ...) {.iterator_function = &advance, AddDestructor(no_destructor), __VA_ARGS__}
112 
145 #define AddDestructor(func) ExtendInit(destructor, (void (*const)(void *)) &func)
146 
168 #define ExtendInit(name, value) .name = value
169 
180 #define next(state) (*(state.iterator_function))(&state)
181 
192 #define next_p(state) (*(state->iterator_function))(state)
193 
207 #define free_iterator(it) (*(it.destructor))(&it)
208 
222 #define free_iterator_p(it) (*(it->destructor))(it)
223 
224 typedef struct Iterator Iterator;
230 struct Iterator {
231  bool exhausted : 1;
232  bool started : 1;
233  bool phase : 1;
234 };
235 // note that this exists purely for documentation purposes. The tail macro has these features
236 // marked to be hidden so that these attributes properly appear as inherited.
237 
238 typedef struct counter counter;
243 struct counter {
244  uintmax_t idx;
245  uintmax_t stop;
246  intmax_t step;
247  IteratorTail(uintmax_t, counter)
248 };
249 
258 static uintmax_t iterate_counter(counter *i);
259 static inline uintmax_t iterate_counter(counter *i) {
260  IterationHead(i);
261  uintmax_t ret = i->idx;
262  intmax_t step = i->step;
263  i->idx += step;
264  if ((step > 0 && i->idx >= i->stop) || (step < 0 && i->idx <= i->stop)) {
265  i->exhausted = 1;
266  }
267  return ret;
268 }
269 
279 counter count_by(uintmax_t start, uintmax_t stop, intmax_t step);
280 inline counter count_by(uintmax_t start, uintmax_t stop, intmax_t step) {
281  return (counter) IteratorInitHead(
283  ExtendInit(idx, start),
284  ExtendInit(stop, stop),
285  ExtendInit(step, step)
286  );
287 }
288 
297 counter count_in_range(uintmax_t start, uintmax_t stop);
298 inline counter count_in_range(uintmax_t start, uintmax_t stop) {
299  return count_by(start, stop, 1);
300 }
301 
309 counter count_to(uintmax_t stop);
310 inline counter count_to(uintmax_t stop) {
311  return count_in_range(0, stop);
312 }
313 
314 #ifdef DOXYGEN
315 };
316 #endif
317 
318 #endif
void no_destructor(void *it)
Definition: iterator.h:19
uintmax_t idx
The current position of the counter.
Definition: iterator.h:244
uintmax_t stop
The value before which the counter will stop.
Definition: iterator.h:245
bool started
An indicator that the iterator has started.
Definition: iterator.h:232
#define IteratorInitHead(advance,...)
The base macro for all iterator initialization functions in this project.
Definition: iterator.h:111
#define IteratorTail(return_type, struct_type)
The base definition macro for all iterators in this project.
Definition: iterator.h:46
bool exhausted
An indicator that the iterator has stopped.
Definition: iterator.h:231
intmax_t step
The amount by which the counter will move.
Definition: iterator.h:246
counter count_by(uintmax_t start, uintmax_t stop, intmax_t step)
Definition: iterator.h:280
#define ExtendInit(name, value)
The extension macro for initializing more complicated Iterators.
Definition: iterator.h:168
counter count_to(uintmax_t stop)
Definition: iterator.h:310
bool phase
An indicator that changes each time the iterator moves.
Definition: iterator.h:233
An implementation of Python-like iterators and generators in C.
Definition: iterator.h:230
#define IterationHead(it)
The base macro for all iteration functions in this project.
Definition: iterator.h:74
counter count_in_range(uintmax_t start, uintmax_t stop)
Definition: iterator.h:298
static uintmax_t iterate_counter(counter *i)
Definition: iterator.h:259
Definition: iterator.h:15
The reference struct for all iterators in this project.
Definition: iterator.h:243