aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@hornquist.se>2019-02-09 12:08:53 +0100
committerHugo Hörnquist <hugo@hornquist.se>2019-02-09 12:08:53 +0100
commit91aeefea53fefb38de73d0cf5a812c45d74ed778 (patch)
tree34560d56d7dc8d9c2f7eb4ef2207e3cb31589031
parentAdd some more scheme bindings. (diff)
downloadcalp-91aeefea53fefb38de73d0cf5a812c45d74ed778.tar.gz
calp-91aeefea53fefb38de73d0cf5a812c45d74ed778.tar.xz
Add iterator macros.
-rw-r--r--linked_list.h12
-rw-r--r--macro.h15
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 */
diff --git a/macro.h b/macro.h
index bf0c1864..11287b44 100644
--- a/macro.h
+++ b/macro.h
@@ -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.