mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Bartosz Golaszewski <brgl@bgdev.pl>
To: Rich Felker <dalias@libc.org>
Cc: musl@lists.openwall.com,
	Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Subject: [musl] [PATCH] search: provide twalk_r()
Date: Thu,  9 Feb 2023 21:43:42 +0100	[thread overview]
Message-ID: <20230209204342.643785-1-brgl@bgdev.pl> (raw)

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


             reply	other threads:[~2023-02-09 20:44 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-09 20:43 Bartosz Golaszewski [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230209204342.643785-1-brgl@bgdev.pl \
    --to=brgl@bgdev.pl \
    --cc=bartosz.golaszewski@linaro.org \
    --cc=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).