diff options
author | Hugo Hörnquist <hugo@hornquist.se> | 2019-02-09 12:08:53 +0100 |
---|---|---|
committer | Hugo Hörnquist <hugo@hornquist.se> | 2019-02-09 12:08:53 +0100 |
commit | 91aeefea53fefb38de73d0cf5a812c45d74ed778 (patch) | |
tree | 34560d56d7dc8d9c2f7eb4ef2207e3cb31589031 | |
parent | Add some more scheme bindings. (diff) | |
download | calp-91aeefea53fefb38de73d0cf5a812c45d74ed778.tar.gz calp-91aeefea53fefb38de73d0cf5a812c45d74ed778.tar.xz |
Add iterator macros.
-rw-r--r-- | linked_list.h | 12 | ||||
-rw-r--r-- | macro.h | 15 |
2 files changed, 27 insertions, 0 deletions
diff --git a/linked_list.h b/linked_list.h index 4199ba57..6136d35f 100644 --- a/linked_list.h +++ b/linked_list.h @@ -53,4 +53,16 @@ int APPEND(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* new ); int SIZE(LLIST(TYPE)) ( LLIST(TYPE)* llist ); int EMPTY(LLIST(TYPE)) ( LLIST(TYPE)* llist ); +/* Iterator */ + +// #define __BEG_LLIST(v, set) v = (set)->head +#define __BEG_LLIST(v, set) v = FIRST(set) +#define BEG_LLIST(T) LINK(T)* __BEG_LLIST + +#define __END_LLIST(var, set) var == (set)->tail +#define END_LLIST(T) __END_LLIST + +#define __NXT_LLIST(var, set) var = var->after +#define NXT_LLIST(T) __NXT_LLIST + #endif /* TYPE */ @@ -79,6 +79,21 @@ /* + * Iterator macros. + */ +#define FOR(T, var, set) for( \ + BEG_ ## T (var, set); \ + END_ ## T (var, set); \ + NXT_ ## T (var, set)) + +/* Example int implementation + * FOR(int, i, 10) { } */ + +#define BEG_int(i, set) int i = 0 +#define NXT_int(i, set) i++ +#define END_int(i, set) i < set + +/* * General functions that different container types may implement. * Actuall implementation and type signature is mostly left to * individual implementations. |