|
|
Message-Id: <20230209204342.643785-1-brgl@bgdev.pl>
Date: Thu, 9 Feb 2023 21:43:42 +0100
From: Bartosz Golaszewski <brgl@...ev.pl>
To: Rich Felker <dalias@...c.org>
Cc: musl@...ts.openwall.com,
Bartosz Golaszewski <bartosz.golaszewski@...aro.org>
Subject: [PATCH] search: provide twalk_r()
From: Bartosz Golaszewski <bartosz.golaszewski@...aro.org>
Provide a variant of twalk() that allows callers to pass custom user
data to it without resorting to global variables.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@...aro.org>
---
include/search.h | 1 +
src/search/twalk_r.c | 24 ++++++++++++++++++++++++
2 files changed, 25 insertions(+)
create mode 100644 src/search/twalk_r.c
diff --git a/include/search.h b/include/search.h
index 02e407e3..95b4cdc2 100644
--- a/include/search.h
+++ b/include/search.h
@@ -53,6 +53,7 @@ struct qelem {
char q_data[1];
};
+void twalk_r(const void *, void (*)(const void *, VISIT, void *), void *);
void tdestroy(void *, void (*)(void *));
#endif
diff --git a/src/search/twalk_r.c b/src/search/twalk_r.c
new file mode 100644
index 00000000..9497273f
--- /dev/null
+++ b/src/search/twalk_r.c
@@ -0,0 +1,24 @@
+#include <search.h>
+#include "tsearch.h"
+
+static void
+walk(const struct node *r, void (*action)(const void *, VISIT, void *), void *data)
+{
+ if (!r)
+ return;
+
+ if (r->h == 1) {
+ action(r, leaf, data);
+ } else {
+ action(r, preorder, data);
+ walk(r->a[0], action, data);
+ action(r, postorder, data);
+ walk(r->a[1], action, data);
+ action(r, endorder, data);
+ }
+}
+
+void twalk_r(const void *root, void (*action)(const void *, VISIT, void *), void *data)
+{
+ walk(root, action, data);
+}
--
2.37.2
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.