mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] [PATCH] search: provide twalk_r()
@ 2023-02-09 20:43 Bartosz Golaszewski
  2023-02-09 21:16 ` Rich Felker
  0 siblings, 1 reply; 7+ messages in thread
From: Bartosz Golaszewski @ 2023-02-09 20:43 UTC (permalink / raw)
  To: Rich Felker; +Cc: musl, Bartosz Golaszewski

From: Bartosz Golaszewski <bartosz.golaszewski@linaro.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@linaro.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


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-02-10 21:05 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-09 20:43 [musl] [PATCH] search: provide twalk_r() Bartosz Golaszewski
2023-02-09 21:16 ` Rich Felker
2023-02-09 21:26   ` Bartosz Golaszewski
2023-02-10  2:18     ` Khem Raj
2023-02-10  8:35       ` Bartosz Golaszewski
2023-02-10 20:07         ` Markus Wichmann
2023-02-10 21:05           ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).