tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

rbtree(3) and FOREACH_SAFE



Hi List

tree(3) states that it's deprecated and rbtree(3) should be used instead.
There seems to be no equivalent of the _FOREACH_SAFE macros.
I'm really new to rbtree stuff, but to convert from my existing TAILQ implementation, I need this.

Attached is a patch (not even compile tested yet) to add the _FOREACH_SAFE macros and also _NEXT and _PREV as well.
Does this look good or have I made a schoolboy error somewhere?

Thanks

Roy
Index: rbtree.h
===================================================================
RCS file: /cvsroot/src/sys/sys/rbtree.h,v
retrieving revision 1.2
diff -u -p -r1.2 rbtree.h
--- rbtree.h	17 Feb 2012 08:20:55 -0000	1.2
+++ rbtree.h	3 Mar 2019 12:49:35 -0000
@@ -102,12 +102,16 @@ typedef struct rb_node {
 
 #define RB_TREE_MIN(T) rb_tree_iterate((T), NULL, RB_DIR_LEFT)
 #define RB_TREE_MAX(T) rb_tree_iterate((T), NULL, RB_DIR_RIGHT)
+#define RB_TREE_NEXT(T, N) rb_tree_iterate((T), (N), RB_DIR_RIGHT)
+#define RB_TREE_PREV(T, N) rb_tree_iterate((T), (N), RB_DIR_LEFT)
 #define RB_TREE_FOREACH(N, T) \
-    for ((N) = RB_TREE_MIN(T); (N); \
-	(N) = rb_tree_iterate((T), (N), RB_DIR_RIGHT))
+    for ((N) = RB_TREE_MIN(T); (N); (N) = RB_TREE_NEXT((T), (N))
 #define RB_TREE_FOREACH_REVERSE(N, T) \
-    for ((N) = RB_TREE_MAX(T); (N); \
-	(N) = rb_tree_iterate((T), (N), RB_DIR_LEFT))
+    for ((N) = RB_TREE_MAX(T); (N); (N) = RB_TREE_PREV((T), (N))
+#define RB_TREE_FOREACH_SAFE(N, T, S) \
+    for ((N) = RB_TREE_MIN(T;) (N) && (S) = RB_TREE_NEXT((T), (N)), (N) = (S))
+#define RB_TREE_FOREACH_REVERSE_SAFE(N, T, S) \
+    for ((N) = RB_TREE_MAX(T;) (N) && (S) = RB_TREE_PREV((T), (N)), (N) = (S))
 
 #ifdef RBDEBUG
 TAILQ_HEAD(rb_node_qh, rb_node);


Home | Main Index | Thread Index | Old Index