diff options
author | EuAndreh <eu@euandre.org> | 2025-01-12 00:14:03 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2025-01-12 14:27:57 -0300 |
commit | 44d56f5311f98a8955c67638e7520963dbd4d845 (patch) | |
tree | fbb2c58c79f1730ff62c83cef116fb5c0e035dfe /src/tree.c | |
parent | Replace src/config.h with <s.h>; incorporate changes from other projects (diff) | |
download | pindaiba-44d56f5311f98a8955c67638e7520963dbd4d845.tar.gz pindaiba-44d56f5311f98a8955c67638e7520963dbd4d845.tar.xz |
Revamp lib, simplify it a bit and address some FIXMEs
Diffstat (limited to 'src/tree.c')
-rw-r--r-- | src/tree.c | 47 |
1 files changed, 44 insertions, 3 deletions
@@ -1,4 +1,4 @@ -#include "config.h" +#include <s.h> #include <assert.h> #include <errno.h> @@ -14,7 +14,22 @@ +// https://www.sanfoundry.com/c-program-implement-avl-tree/ + +/* + Implement: + + - AVL tree + - B-tree + - Red-black tree + - Hash array-mapped trie + + Also, persistent variations of all of them. +*/ + + struct Tree { + // struct Data data; const void *const value; const size_t value_size; const struct Tree *left; @@ -24,6 +39,30 @@ struct Tree { +/* +static enum Comparison +cmp(struct Data lhs, struct Data rhs) { + if (lhs.length < rhs.length) { + return Comparison_LESSER; + } + + if (lhs.length > rhs.length) { + return Comparison_GREATER; + } + + const int c = memcmp(lhs.data, rhs.data, lhs.length); + if (c < 0) { + return Comparison_LESSER; + } + + if (c > 0) { + return Comparison_GREATER; + } + + return Comparison_EQUAL; +} +*/ + int tree_new(const size_t value_size, const struct Tree **const out) { int rc = -1; @@ -48,7 +87,8 @@ tree_new(const size_t value_size, const struct Tree **const out) { out: if (rc) { if (ret != NULL) { - freeit((void *)&ret); + free((struct Tree *)ret); + ret = NULL; } } return rc; @@ -70,7 +110,8 @@ tree_free(const struct Tree **const t) { tree_free(&right); } - freeit((void *)t); + free((struct Tree *)(*t)); + *t = NULL; } static void |