Commit f6e96254 authored by Chris Müller's avatar Chris Müller

lib: use crystal name_conventation for red-black trees.

lib: red-black nodes can store a data pointer now.
parent b13a7ef3
This diff is collapsed.
......@@ -21,19 +21,20 @@
#include "structures.h"
enum RBNodeColor { RED, BLACK };
enum CryRbNodeColor { RED, BLACK };
struct RBNode {
struct CryRbNode {
const_pointer entry;
enum RBNodeColor color;
struct RBNode* parent;
struct RBNode* left;
struct RBNode* right;
const_pointer data;
enum CryRbNodeColor color;
struct CryRbNode* parent;
struct CryRbNode* left;
struct CryRbNode* right;
};
struct RBTree {
struct RBNode* root;
struct CryRbTree {
struct CryRbNode* root;
cry_ordering_funptr compare;
size_t nodes;
};
......@@ -43,21 +44,21 @@ struct RBTree {
#define RB_GRAND_PARENT(node) node->parent->parent
struct RBTree* rb_tree_new(cry_ordering_funptr compare);
void rb_tree_clear(struct RBTree* tree, cry_free_funptr fun);
struct CryRbTree* cry_rbtree_new(cry_ordering_funptr compare);
void cry_rbtree_clear(struct CryRbTree* tree, cry_free_funptr fun_entry, cry_free_funptr fun_data);
struct RBNode* rb_tree_lookup(struct RBTree* tree, const_pointer entry);
struct RBNode* rb_tree_insert(struct RBTree* tree, const_pointer entry);
struct RBNode* rb_tree_remove(struct RBTree* tree, const_pointer entry);
struct CryRbNode* cry_rbtree_lookup(struct CryRbTree* tree, const_pointer entry);
struct CryRbNode* cry_rbtree_insert(struct CryRbTree* tree, const_pointer entry, const_pointer data);
struct CryRbNode* cry_rbtree_remove(struct CryRbTree* tree, const_pointer entry);
struct RBNode* rb_node_first(struct RBTree* tree);
struct RBNode* rb_node_last(struct RBTree* tree);
struct CryRbNode* cry_rbnode_first(struct CryRbTree* tree);
struct CryRbNode* cry_rbnode_last(struct CryRbTree* tree);
struct RBNode* rb_node_next(struct RBNode* node);
struct RBNode* rb_node_prev(struct RBNode* node);
struct CryRbNode* cry_rbnode_next(struct CryRbNode* node);
struct CryRbNode* cry_rbnode_prev(struct CryRbNode* node);
struct RBNode* rb_node_new(enum RBNodeColor color, struct RBNode* parent, const_pointer entry);
void rb_node_free(struct RBNode* node, cry_free_funptr fun);
struct CryRbNode* cry_rbnode_new(enum CryRbNodeColor color, struct CryRbNode* parent, const_pointer entry, const_pointer data);
void cry_rbnode_free(struct CryRbNode* node, cry_free_funptr fun_entry, cry_free_funptr fun_data);
......
......@@ -7,12 +7,12 @@
static void
test_red_black_tree_initialization(const_pointer data)
{
struct RBTree* tree = rb_tree_new(cry_int_compare);
struct CryRbTree* tree = cry_rbtree_new(cry_int_compare);
assert(tree->nodes == 0);
assert(rb_tree_lookup(tree, "unknown_key") == 0);
assert(cry_rbtree_lookup(tree, "unknown_key") == 0);
rb_tree_clear(tree, 0);
cry_rbtree_clear(tree, 0, 0);
cry_free(tree);
}
......@@ -20,13 +20,13 @@ test_red_black_tree_initialization(const_pointer data)
static void
test_red_black_tree_insertion(const_pointer data)
{
struct RBTree* tree = rb_tree_new(cry_int_compare);
struct CryRbTree* tree = cry_rbtree_new(cry_int_compare);
const int* keys = cry_cast(const int*, data);
assert(tree->nodes == 0);
while(*keys > 0) {
assert(rb_tree_insert(tree, keys) != 0);
assert(cry_rbtree_insert(tree, keys, 0) != 0);
++keys;
}
......@@ -35,7 +35,7 @@ test_red_black_tree_insertion(const_pointer data)
keys = cry_cast(const int*, data);
while(*keys > 0) {
struct RBNode* node = rb_tree_lookup(tree, keys);
struct CryRbNode* node = cry_rbtree_lookup(tree, keys);
assert(node != 0);
assert(node->entry == keys);
......@@ -43,7 +43,7 @@ test_red_black_tree_insertion(const_pointer data)
++keys;
}
rb_tree_clear(tree, 0);
cry_rbtree_clear(tree, 0, 0);
cry_free(tree);
}
......@@ -51,14 +51,14 @@ test_red_black_tree_insertion(const_pointer data)
static void
test_red_black_tree_removal(const_pointer list)
{
struct RBTree* tree = rb_tree_new(cry_int_compare);
struct CryRbTree* tree = cry_rbtree_new(cry_int_compare);
const int* keys = cry_cast(const int*, list);
struct RBNode* data = 0;
struct CryRbNode* data = 0;
assert(tree->nodes == 0);
while(*keys > 0) {
assert(rb_tree_insert(tree, keys) != 0);
assert(cry_rbtree_insert(tree, keys, 0) != 0);
++keys;
}
......@@ -67,48 +67,48 @@ test_red_black_tree_removal(const_pointer list)
keys = cry_cast(const int*, list);
// remove 3
data = rb_tree_remove(tree, keys + 4);
data = cry_rbtree_remove(tree, keys + 4);
assert(data != 0 && data->entry == keys + 4);
rb_node_free(data, 0);
cry_rbnode_free(data, 0, 0);
// remove 12
data = rb_tree_remove(tree, keys + 2);
data = cry_rbtree_remove(tree, keys + 2);
assert(data != 0 && data->entry == keys + 2);
rb_node_free(data, 0);
cry_rbnode_free(data, 0, 0);
// remove 17
data = rb_tree_remove(tree, keys + 9);
data = cry_rbtree_remove(tree, keys + 9);
assert(data != 0 && data->entry == keys + 9);
rb_node_free(data, 0);
cry_rbnode_free(data, 0, 0);
// remove 18
data = rb_tree_remove(tree, keys + 7);
data = cry_rbtree_remove(tree, keys + 7);
assert(data != 0 && data->entry == keys + 7);
rb_node_free(data, 0);
cry_rbnode_free(data, 0, 0);
// remove 15
data = rb_tree_remove(tree, keys + 3);
data = cry_rbtree_remove(tree, keys + 3);
assert(data != 0 && data->entry == keys + 3);
rb_node_free(data, 0);
cry_rbnode_free(data, 0, 0);
// remove 16
data = rb_tree_remove(tree, keys + 8);
data = cry_rbtree_remove(tree, keys + 8);
assert(data != 0 && data->entry == keys + 8);
rb_node_free(data, 0);
cry_rbnode_free(data, 0, 0);
// contains 4
assert(rb_tree_lookup(tree, keys) != 0);
assert(cry_rbtree_lookup(tree, keys) != 0);
// contains 7
assert(rb_tree_lookup(tree, keys + 1) != 0);
assert(cry_rbtree_lookup(tree, keys + 1) != 0);
// contains 5
assert(rb_tree_lookup(tree, keys + 5) != 0);
assert(cry_rbtree_lookup(tree, keys + 5) != 0);
// contains 14
assert(rb_tree_lookup(tree, keys + 6) != 0);
assert(cry_rbtree_lookup(tree, keys + 6) != 0);
rb_tree_clear(tree, 0);
cry_rbtree_clear(tree, 0, 0);
cry_free(tree);
}
......@@ -116,14 +116,14 @@ test_red_black_tree_removal(const_pointer list)
static void
test_red_black_tree_traversal(const_pointer list)
{
struct RBTree* tree = rb_tree_new(cry_int_compare);
struct CryRbTree* tree = cry_rbtree_new(cry_int_compare);
const int* keys = cry_cast(const int*, list);
struct RBNode* data = 0;
struct CryRbNode* data = 0;
assert(tree->nodes == 0);
while(*keys > 0) {
assert(rb_tree_insert(tree, keys) != 0);
assert(cry_rbtree_insert(tree, keys, 0) != 0);
++keys;
}
......@@ -131,20 +131,20 @@ test_red_black_tree_traversal(const_pointer list)
keys = cry_cast(const int*, list);
data = rb_node_first(tree);
data = cry_rbnode_first(tree);
while(data != 0) {
data = rb_node_next(data);
data = cry_rbnode_next(data);
}
data = rb_node_last(tree);
data = cry_rbnode_last(tree);
while(data != 0) {
data = rb_node_prev(data);
data = cry_rbnode_prev(data);
}
rb_tree_clear(tree, 0);
cry_rbtree_clear(tree, 0, 0);
cry_free(tree);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment