mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-17 19:41:36 +08:00
将对外用不到的接口放在tree.c作为局部函数调用
This commit is contained in:
parent
3ed9fb8437
commit
ecc568bb90
@ -71,21 +71,15 @@ struct _tree
|
|||||||
|
|
||||||
struct _iterator _iter;
|
struct _iterator _iter;
|
||||||
|
|
||||||
|
void (*destory)(struct _tree* self);
|
||||||
|
|
||||||
// -------------------- public --------------------
|
// -------------------- public --------------------
|
||||||
// kernel
|
// kernel
|
||||||
bool (*insert)(struct _tree* self, void* obj);
|
bool (*insert)(struct _tree* self, void* obj);
|
||||||
bool (*delete)(struct _tree* self, void* obj);
|
bool (*delete)(struct _tree* self, void* obj);
|
||||||
|
|
||||||
struct _tree_node* (*find)(struct _tree* self, void* obj);
|
|
||||||
struct _tree_node* (*find_min)(struct _tree* self, struct _tree_node* root);
|
|
||||||
struct _tree_node* (*find_max)(struct _tree* self, struct _tree_node* root);
|
|
||||||
|
|
||||||
bool (*rebalance)(struct _tree* self, struct _tree_node* root);
|
bool (*rebalance)(struct _tree* self, struct _tree_node* root);
|
||||||
int32_t (*height)(struct _tree* self, struct _tree_node* root);
|
int32_t (*height)(struct _tree* self, struct _tree_node* root);
|
||||||
|
|
||||||
bool (*min)(struct _tree* self, void* obj);
|
|
||||||
bool (*max)(struct _tree* self, void* obj);
|
|
||||||
|
|
||||||
// base
|
// base
|
||||||
bool (*clear)(struct _tree* self);
|
bool (*clear)(struct _tree* self);
|
||||||
bool (*empty)(struct _tree* self);
|
bool (*empty)(struct _tree* self);
|
||||||
@ -94,18 +88,12 @@ struct _tree
|
|||||||
// iter
|
// iter
|
||||||
iterator_t (*iter)(struct _tree* self, enum _order);
|
iterator_t (*iter)(struct _tree* self, enum _order);
|
||||||
|
|
||||||
/**
|
// others
|
||||||
* @brief obj compare with obj2
|
bool (*min)(struct _tree* self, void* obj);
|
||||||
*
|
bool (*max)(struct _tree* self, void* obj);
|
||||||
* @return
|
|
||||||
* obj < obj2 return -1
|
|
||||||
* obj == obj2 return 0
|
|
||||||
* obj > obj2 return 1
|
|
||||||
*/
|
|
||||||
int (*compare)(void* obj, void* obj2);
|
|
||||||
|
|
||||||
// free
|
// config
|
||||||
void (*destory)(struct _tree* self);
|
compare_fun_t compare; // !!! you have to implement this function
|
||||||
|
|
||||||
// -------------------- debug --------------------
|
// -------------------- debug --------------------
|
||||||
void (*print_obj)(void* obj);
|
void (*print_obj)(void* obj);
|
||||||
|
182
src/tree.c
182
src/tree.c
@ -297,6 +297,86 @@ static bool tree_avl_rebalance(struct _tree* self, struct _tree_node* root)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static struct _tree_node* tree_find(struct _tree* self, void* obj)
|
||||||
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
struct _tree_node* root = self->_root;
|
||||||
|
while (root != NULL)
|
||||||
|
{
|
||||||
|
if (self->compare(obj, root->obj) == 0)
|
||||||
|
{
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
else if (self->compare(obj, root->obj) < 0)
|
||||||
|
{
|
||||||
|
root = root->left;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
root = root->right;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct _tree_node* tree_find_min(struct _tree* self, struct _tree_node* root)
|
||||||
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
#if 0
|
||||||
|
if (root == NULL)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (root->left == NULL)
|
||||||
|
{
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
return tree_find_min(self, root->left);
|
||||||
|
#else
|
||||||
|
while (root != NULL)
|
||||||
|
{
|
||||||
|
if (root->left != NULL)
|
||||||
|
{
|
||||||
|
root = root->left;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct _tree_node* tree_find_max(struct _tree* self, struct _tree_node* root)
|
||||||
|
{
|
||||||
|
#if 0
|
||||||
|
assert(self != NULL);
|
||||||
|
if (root == NULL)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (root->right == NULL)
|
||||||
|
{
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
return tree_find_max(self, root->right);
|
||||||
|
#else
|
||||||
|
while (root != NULL)
|
||||||
|
{
|
||||||
|
if (root->right != NULL)
|
||||||
|
{
|
||||||
|
root = root->right;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
static struct _tree_node* tree_node_new(struct _tree* self, void* obj)
|
static struct _tree_node* tree_node_new(struct _tree* self, void* obj)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
@ -497,7 +577,7 @@ static bool tree_avl_delete_double_child(struct _tree* self, struct _tree_node*
|
|||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
assert(node != NULL);
|
assert(node != NULL);
|
||||||
struct _tree_node* tmp = self->find_min(self, node->right);
|
struct _tree_node* tmp = tree_find_min(self, node->right);
|
||||||
if (tmp != NULL)
|
if (tmp != NULL)
|
||||||
{
|
{
|
||||||
memmove(node->obj, tmp->obj, self->_obj_size);
|
memmove(node->obj, tmp->obj, self->_obj_size);
|
||||||
@ -517,7 +597,7 @@ static bool tree_avl_delete(struct _tree* self, void* obj)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct _tree_node* node = self->find(self, obj);
|
struct _tree_node* node = tree_find(self, obj);
|
||||||
if (node == NULL)
|
if (node == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@ -538,28 +618,6 @@ static bool tree_avl_delete(struct _tree* self, void* obj)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct _tree_node* tree_find(struct _tree* self, void* obj)
|
|
||||||
{
|
|
||||||
assert(self != NULL);
|
|
||||||
struct _tree_node* root = self->_root;
|
|
||||||
while (root != NULL)
|
|
||||||
{
|
|
||||||
if (self->compare(obj, root->obj) == 0)
|
|
||||||
{
|
|
||||||
return root;
|
|
||||||
}
|
|
||||||
else if (self->compare(obj, root->obj) < 0)
|
|
||||||
{
|
|
||||||
root = root->left;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
root = root->right;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool tree_clear(struct _tree* self)
|
static bool tree_clear(struct _tree* self)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
@ -621,64 +679,6 @@ static void tree_destory(struct _tree* self)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct _tree_node* tree_find_min(struct _tree* self, struct _tree_node* root)
|
|
||||||
{
|
|
||||||
assert(self != NULL);
|
|
||||||
#if 0
|
|
||||||
if (root == NULL)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if (root->left == NULL)
|
|
||||||
{
|
|
||||||
return root;
|
|
||||||
}
|
|
||||||
return tree_find_min(self, root->left);
|
|
||||||
#else
|
|
||||||
while (root != NULL)
|
|
||||||
{
|
|
||||||
if (root->left != NULL)
|
|
||||||
{
|
|
||||||
root = root->left;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return root;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return root;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static struct _tree_node* tree_find_max(struct _tree* self, struct _tree_node* root)
|
|
||||||
{
|
|
||||||
#if 0
|
|
||||||
assert(self != NULL);
|
|
||||||
if (root == NULL)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if (root->right == NULL)
|
|
||||||
{
|
|
||||||
return root;
|
|
||||||
}
|
|
||||||
return tree_find_max(self, root->right);
|
|
||||||
#else
|
|
||||||
while (root != NULL)
|
|
||||||
{
|
|
||||||
if (root->right != NULL)
|
|
||||||
{
|
|
||||||
root = root->right;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return root;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return root;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool tree_min(struct _tree* self, void* obj)
|
static bool tree_min(struct _tree* self, void* obj)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
@ -1086,7 +1086,7 @@ static bool tree_rb_delete(struct _tree* self, void* obj)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct _tree_node* node = self->find(self, obj);
|
struct _tree_node* node = tree_find(self, obj);
|
||||||
if (node == NULL)
|
if (node == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@ -1114,7 +1114,7 @@ static bool tree_rb_delete(struct _tree* self, void* obj)
|
|||||||
// 1. find the min node in right subtree
|
// 1. find the min node in right subtree
|
||||||
// 2. replace the node with min node
|
// 2. replace the node with min node
|
||||||
// 3. delete the min node
|
// 3. delete the min node
|
||||||
tmp = self->find_min(self, node->right);
|
tmp = tree_find_min(self, node->right);
|
||||||
memmove(node->obj, tmp->obj, self->_obj_size);
|
memmove(node->obj, tmp->obj, self->_obj_size);
|
||||||
if (tmp->right != NULL)
|
if (tmp->right != NULL)
|
||||||
{
|
{
|
||||||
@ -1150,7 +1150,7 @@ static bool tree_rb_delete(struct _tree* self, void* obj)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
iterator_t tree_iter(struct _tree* self, enum _order order)
|
static iterator_t tree_iter(struct _tree* self, enum _order order)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
iterator_t iter = &self->_iter;
|
iterator_t iter = &self->_iter;
|
||||||
@ -1234,7 +1234,7 @@ iterator_t tree_iter(struct _tree* self, enum _order order)
|
|||||||
return iter;
|
return iter;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool tree_iter_hasnext(struct _iterator* iter)
|
static bool tree_iter_hasnext(struct _iterator* iter)
|
||||||
{
|
{
|
||||||
assert(iter != NULL);
|
assert(iter != NULL);
|
||||||
assert(iter->parent != NULL);
|
assert(iter->parent != NULL);
|
||||||
@ -1247,7 +1247,7 @@ bool tree_iter_hasnext(struct _iterator* iter)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const void* tree_iter_next(struct _iterator* iter)
|
static const void* tree_iter_next(struct _iterator* iter)
|
||||||
{
|
{
|
||||||
assert(iter != NULL);
|
assert(iter != NULL);
|
||||||
assert(iter->parent != NULL);
|
assert(iter->parent != NULL);
|
||||||
@ -1428,7 +1428,7 @@ const void* tree_iter_next(struct _iterator* iter)
|
|||||||
{
|
{
|
||||||
}break;
|
}break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1481,9 +1481,6 @@ static bool tree_avl_init(struct _tree* self, uint32_t obj_size)
|
|||||||
self->iter = tree_iter;
|
self->iter = tree_iter;
|
||||||
|
|
||||||
// others
|
// others
|
||||||
self->find = tree_find;
|
|
||||||
self->find_max = tree_find_max;
|
|
||||||
self->find_min = tree_find_min;
|
|
||||||
self->max = tree_max;
|
self->max = tree_max;
|
||||||
self->min = tree_min;
|
self->min = tree_min;
|
||||||
|
|
||||||
@ -1538,9 +1535,6 @@ static bool tree_rb_init(struct _tree* self, uint32_t obj_size)
|
|||||||
self->iter = tree_iter;
|
self->iter = tree_iter;
|
||||||
|
|
||||||
// others
|
// others
|
||||||
self->find = tree_find;
|
|
||||||
self->find_max = tree_find_max;
|
|
||||||
self->find_min = tree_find_min;
|
|
||||||
self->max = tree_max;
|
self->max = tree_max;
|
||||||
self->min = tree_min;
|
self->min = tree_min;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user