From 0c0c4040cb80bd2d85cf0f137dfe0480a42c2791 Mon Sep 17 00:00:00 2001 From: Kristiyan Stoimenov Date: Tue, 16 Aug 2022 12:28:13 +0300 Subject: [PATCH] rbtree: Define API --- include/equilibrium/rbtree.h | 26 +++++++++++++++++++- src/rbtree.c | 46 ++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/include/equilibrium/rbtree.h b/include/equilibrium/rbtree.h index 05bb74d..83935b2 100644 --- a/include/equilibrium/rbtree.h +++ b/include/equilibrium/rbtree.h @@ -15,19 +15,43 @@ struct rbtree_node { enum rbtree_color _color; struct rbtree_node *_left; struct rbtree_node *_right; + struct rbtree_node *_parent; void *_data_ptr; }; +typedef int (*rbtree_node_compare)(struct rbtree_node *, struct rbtree_node *); + struct rbtree { struct rbtree_node *_root; struct rbtree_node *_smallest; + struct rbtree_node *_biggest; size_t _size; size_t _depth; }; +struct rbtree_iterator { + struct rbtree_node *_ptr; + struct rbtree *_tree; +}; + +// Tree operations int rbtree_init(struct rbtree *); int rbtree_destroy(struct rbtree *); +int rbtree_copy(struct rbtree *); +// Mutable data operations int rbtree_insert(struct rbtree *); int rbtree_remove(struct rbtree *); -int rbtree_find(struct rbtree *); + +// Immutable data operations +int rbtree_find(struct rbtree *, rbtree_node_compare cb); +struct rbtree_iterator rbtree_find_it(struct rbtree *, rbtree_node_compare cb); +struct rbtree_iterator rbtree_begin(struct rbtree *); +struct rbtree_iterator rbtree_end(struct rbtree *); +int rbtree_next(struct rbtree_iterator *); +int rbtree_prev(struct rbtree_iterator *); + +#define rbtree_for_each_node(tree, it) \ + for (struct rbtree_iterator it = rbtree_begin(&(tree)), \ + end = rbtree_end(&(tree)); \ + it._ptr != end._ptr; rbtree_next(it._ptr)) diff --git a/src/rbtree.c b/src/rbtree.c index b712c83..72b1885 100644 --- a/src/rbtree.c +++ b/src/rbtree.c @@ -1 +1,47 @@ +#include +#include + #include + +static void rotate_subtree_left(struct rbtree_node *root, + struct rbtree_node *pivot) +{ +} +static void rotate_subtree_right(struct rbtree_node *root, + struct rbtree_node *pivot) +{ +} + +int rbtree_init(struct rbtree *rbtree) +{ +} +int rbtree_destroy(struct rbtree *) +{ +} +int rbtree_copy(struct rbtree *) +{ +} +int rbtree_insert(struct rbtree *) +{ +} +int rbtree_remove(struct rbtree *) +{ +} +int rbtree_find(struct rbtree *, rbtree_node_compare) +{ +} +struct rbtree_iterator rbtree_find_it(struct rbtree *, rbtree_node_compare) +{ +} +struct rbtree_iterator rbtree_begin(struct rbtree *) +{ +} +struct rbtree_iterator rbtree_end(struct rbtree *) +{ +} +int rbtree_next(struct rbtree_iterator *) +{ +} +int rbtree_prev(struct rbtree_iterator *) +{ +}