~brenns10/funlisp

ref: 7347d0eca6d0a50799d3048c21f376277e827224 funlisp/src/iter.h -rw-r--r-- 1.0 KiB
7347d0ecStephen Brennan Updates for readthedocs 3 years ago
                                                                                
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
/*
 * iter.h: generic iterator interface
 *
 * Stephen Brennan <stephen@brennan.io>
 */
#include <stdbool.h>

#ifndef _ITER_H
#define _ITER_H

struct iterator {
	void *ds;        /* the container data structure */
	int index;    /* zero-based index for the iterator */
	int state_int;   /* some state variables that may help */
	void *state_ptr;

	/* do we have a next item? */
	bool (*has_next)(struct iterator *iter);

	/* return the next item (or null) */
	void *(*next)(struct iterator *iter);

	/* free resources held by the iterator */
	void (*close)(struct iterator *iter);
};

void iterator_close_noop(struct iterator *iter);
struct iterator iterator_empty();
struct iterator iterator_single_value(void *value);

/* concatenate n iterators. takes ownership of the *its pointer */
struct iterator iterator_concat(struct iterator *its, size_t n);

/* these use the above function */
struct iterator iterator_concat2(struct iterator left, struct iterator right);
struct iterator iterator_concat3(
	struct iterator a, struct iterator b, struct iterator c);

#endif