summaryrefslogtreecommitdiff
path: root/rbtree.c
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2008-10-30 10:58:28 -0700
committerH. Peter Anvin <hpa@zytor.com>2008-10-30 10:58:28 -0700
commite8873121aadbbb465c0c48da3c56b47a6a5f1d68 (patch)
tree9333fb01193d78f0f02050359f8acfeb7cfc672a /rbtree.c
parent9656a581cdb6f505c9adc6b5158b395c21b5c43a (diff)
downloadnasm-e8873121aadbbb465c0c48da3c56b47a6a5f1d68.tar.gz
nasm-e8873121aadbbb465c0c48da3c56b47a6a5f1d68.tar.bz2
nasm-e8873121aadbbb465c0c48da3c56b47a6a5f1d68.zip
rbtree: drop the data pointer; instead rely on being embedded
Drop the data pointer, and instead assume the struct rbtree will be embedded in a bigger data structure (to be extracted via container_of()). Signed-off-by: H. Peter Anvin <hpa@zytor.com>
Diffstat (limited to 'rbtree.c')
-rw-r--r--rbtree.c28
1 files changed, 7 insertions, 21 deletions
diff --git a/rbtree.c b/rbtree.c
index adf2b8b..7e13bff 100644
--- a/rbtree.c
+++ b/rbtree.c
@@ -3,15 +3,14 @@
*
* Simple implementation of a left-leaning red-black tree with 64-bit
* integer keys. The search operation will return the highest node <=
- * the key; only search, insert, and full-tree deletion is supported,
- * but additional standard llrbtree operations can be coded up at will.
+ * the key; only search and insert are supported, but additional
+ * standard llrbtree operations can be coded up at will.
*
* See http://www.cs.princeton.edu/~rs/talks/LLRB/RedBlack.pdf for
* information about left-leaning red-black trees.
*/
#include "rbtree.h"
-#include "nasmlib.h"
const struct rbtree *rb_search(const struct rbtree *tree, uint64_t key)
{
@@ -62,24 +61,20 @@ static void color_flip(struct rbtree *h)
h->right->red = !h->right->red;
}
-struct rbtree *rb_insert(struct rbtree *tree, uint64_t key, void *data)
+struct rbtree *rb_insert(struct rbtree *tree, struct rbtree *node)
{
if (!tree) {
- struct rbtree *node = nasm_malloc(sizeof *node);
-
- node->key = key;
- node->data = data;
- node->red = true;
+ node->red = true;
return node;
}
if (is_red(tree->left) && is_red(tree->right))
color_flip(tree);
- if (key < tree->key)
- tree->left = rb_insert(tree->left, key, data);
+ if (node->key < tree->key)
+ tree->left = rb_insert(tree->left, node);
else
- tree->right = rb_insert(tree->right, key, data);
+ tree->right = rb_insert(tree->right, node);
if (is_red(tree->right))
tree = rotate_left(tree);
@@ -89,12 +84,3 @@ struct rbtree *rb_insert(struct rbtree *tree, uint64_t key, void *data)
return tree;
}
-
-void rb_free(struct rbtree *tree)
-{
- if (tree) {
- rb_free(tree->left);
- rb_free(tree->right);
- nasm_free(tree);
- }
-}