summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-02-22 01:09:33 +0100
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-02-22 01:20:58 +0100
commit0b1fce762dca88659b3103783e8852cb7265823e (patch)
treea2475dabf455e90a2209edff0b6c351e3098678f /include
parentf4c137c2c4e6b85849e34cac55846ebd422659bd (diff)
Add a very simple red-black tree implementation
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'include')
-rw-r--r--include/rbtree.h63
1 files changed, 63 insertions, 0 deletions
diff --git a/include/rbtree.h b/include/rbtree.h
new file mode 100644
index 0000000..7fe1752
--- /dev/null
+++ b/include/rbtree.h
@@ -0,0 +1,63 @@
+/* SPDX-License-Identifier: LGPL-3.0-or-later */
+/*
+ * rbtree.h
+ *
+ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
+ */
+#ifndef SQFS_RBTREE_H
+#define SQFS_RBTREE_H
+
+#include "config.h"
+#include "sqfs/predef.h"
+#include "compat.h"
+
+#include <stddef.h>
+
+typedef struct rbtree_node_t {
+ struct rbtree_node_t *left;
+ struct rbtree_node_t *right;
+ sqfs_u32 value_offset;
+ sqfs_u32 is_red : 1;
+ sqfs_u32 pad0 : 31;
+
+ sqfs_u8 data[];
+} rbtree_node_t;
+
+typedef struct rbtree_t {
+ rbtree_node_t *root;
+
+ int (*key_compare)(const void *lhs, const void *hrs);
+
+ size_t key_size;
+ size_t key_size_padded;
+
+ size_t value_size;
+} rbtree_t;
+
+static SQFS_INLINE void *rbtree_node_key(rbtree_node_t *n)
+{
+ return n->data;
+}
+
+static SQFS_INLINE void *rbtree_node_value(rbtree_node_t *n)
+{
+ return n->data + n->value_offset;
+}
+
+SQFS_INTERNAL int rbtree_init(rbtree_t *tree, size_t keysize, size_t valuesize,
+ int(*key_compare)(const void *, const void *));
+
+SQFS_INTERNAL void rbtree_cleanup(rbtree_t *tree);
+
+SQFS_INTERNAL int rbtree_insert(rbtree_t *tree, const void *key,
+ const void *value);
+
+SQFS_INTERNAL rbtree_node_t *rbtree_lookup(const rbtree_t *tree,
+ const void *key);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* TOOLS_RBTREE_H */
+