summaryrefslogtreecommitdiff
path: root/src/search/twalk.c
diff options
context:
space:
mode:
authorSzabolcs Nagy <nsz@port70.net>2016-08-21 20:06:56 +0000
committerRich Felker <dalias@aerifal.cx>2018-09-20 17:57:47 -0400
commitc50985d5c8e316c5c464f352e79eeebfed1121a9 (patch)
tree46f4250072be1d98f3f98ccd3ae244b4eec5f3f1 /src/search/twalk.c
parent5ef60206ff9e697d3011bcf48afca50c9fe85e1b (diff)
downloadmusl-c50985d5c8e316c5c464f352e79eeebfed1121a9.tar.gz
new tsearch implementation
Rewrote the AVL tree implementation: - It is now non-recursive with fixed stack usage (large enough for worst case tree height). twalk and tdestroy are still recursive as that's smaller/simpler. - Moved unrelated interfaces into separate translation units. - The node structure is changed to use indexed children instead of left/right pointers, this simplifies the balancing logic. - Using void * pointers instead of struct node * in various places, because this better fits the api (node address is passed in a void** argument, so it is tempting to incorrectly cast it to struct node **). - As a further performance improvement the rebalancing now stops when it is not needed (subtree height is unchanged). Otherwise the behaviour should be the same as before (checked over generated random inputs that the resulting tree shape is equivalent). - Removed the old copyright notice (including prng related one: it should be licensed under the same terms as the rest of the project). .text size of pic tsearch + tfind + tdelete + twalk: x86_64 i386 aarch64 arm mips powerpc ppc64le sh4 m68k s390x old 941 899 1220 1068 1852 1400 1600 1008 1008 1488 new 857 881 1040 976 1564 1192 1360 736 820 1408
Diffstat (limited to 'src/search/twalk.c')
-rw-r--r--src/search/twalk.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/search/twalk.c b/src/search/twalk.c
new file mode 100644
index 00000000..53821cda
--- /dev/null
+++ b/src/search/twalk.c
@@ -0,0 +1,22 @@
+#include <search.h>
+#include "tsearch.h"
+
+static void walk(const struct node *r, void (*action)(const void *, VISIT, int), int d)
+{
+ if (!r)
+ return;
+ if (r->h == 1)
+ action(r, leaf, d);
+ else {
+ action(r, preorder, d);
+ walk(r->a[0], action, d+1);
+ action(r, postorder, d);
+ walk(r->a[1], action, d+1);
+ action(r, endorder, d);
+ }
+}
+
+void twalk(const void *root, void (*action)(const void *, VISIT, int))
+{
+ walk(root, action, 0);
+}