From 91aeefea53fefb38de73d0cf5a812c45d74ed778 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Sat, 9 Feb 2019 12:08:53 +0100 Subject: Add iterator macros. --- linked_list.h | 12 ++++++++++++ macro.h | 15 +++++++++++++++ 2 files changed, 27 insertions(+) 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 */ diff --git a/macro.h b/macro.h index bf0c1864..11287b44 100644 --- a/macro.h +++ b/macro.h @@ -78,6 +78,21 @@ #define FREE_F(T) int FREE(T) (T* this) +/* + * 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 -- cgit v1.2.3