14#ifndef UTILS_GP_AVL_TREE_H
15#define UTILS_GP_AVL_TREE_H
48static inline int gp_avl_tree_depth_diff_(
gp_avl_node *root)
53static inline int gp_avl_tree_left_deeper_(
gp_avl_node *root)
55 return gp_avl_tree_depth_diff_(root) >= 0;
58static inline int gp_avl_tree_right_deeper_(
gp_avl_node *root)
60 return gp_avl_tree_depth_diff_(root) <= 0;
63static inline void gp_avl_tree_update_subtree_depth_(
gp_avl_node *root)
71 root->
depth = right + 1;
81 gp_avl_tree_update_subtree_depth_(root);
82 gp_avl_tree_update_subtree_depth_(pivot);
94 gp_avl_tree_update_subtree_depth_(root);
95 gp_avl_tree_update_subtree_depth_(pivot);
102 root->
left = gp_avl_tree_rotate_right_(root->
left);
104 return gp_avl_tree_rotate_left_(root);
109 root->
right = gp_avl_tree_rotate_left_(root->
right);
111 return gp_avl_tree_rotate_right_(root);
116 switch (gp_avl_tree_depth_diff_(root)) {
118 if (gp_avl_tree_left_deeper_(root->
left))
119 root = gp_avl_tree_rotate_left_(root);
121 root = gp_avl_tree_rotate_left_right_(root);
124 if (gp_avl_tree_right_deeper_(root->
right))
125 root = gp_avl_tree_rotate_right_(root);
127 root = gp_avl_tree_rotate_right_left_(root);
130 gp_avl_tree_update_subtree_depth_(root);
141 if (cmp(root, node) < 0)
142 root->
left = gp_avl_tree_ins_(root->
left, node, cmp);
144 root->
right = gp_avl_tree_ins_(root->
right, node, cmp);
146 return gp_avl_tree_balance_(root);
163 return gp_avl_tree_ins_(root, node, cmp);
188 gp_avl_tree_update_subtree_depth_(root->
right);
190 return gp_avl_tree_balance_(root);
215 gp_avl_tree_update_subtree_depth_(root->
left);
217 return gp_avl_tree_balance_(root);
237 res = cmp(root, key);
264 return gp_avl_tree_balance_(root);
267 return gp_avl_tree_balance_(root);
286 ret = cmp(root, key);
static gp_avl_node * gp_avl_tree_del_min(gp_avl_node *root, gp_avl_node **min)
Removes a minimal node from an AVL tree.
static gp_avl_node * gp_avl_tree_ins(gp_avl_node *root, gp_avl_node *node, int(*cmp)(gp_avl_node *a, gp_avl_node *b))
Inserts a node into an AVL tree.
static gp_avl_node * gp_avl_tree_del_max(gp_avl_node *root, gp_avl_node **max)
Removes a maximal node from an AVL tree.
static gp_avl_node * gp_avl_tree_lookup(gp_avl_node *root, const void *key, int(*cmp)(gp_avl_node *a, const void *key))
Looks up a node by a key.
static unsigned int gp_avl_tree_depth(gp_avl_node *root)
Returns AVL tree depth.
static gp_avl_node * gp_avl_tree_del(gp_avl_node *root, const void *key, gp_avl_node **del, int(*cmp)(gp_avl_node *a, const void *key))
Removes a node by a key from an AVL tree.
A compiler dependent macros.
#define GP_WUR
Expands to warn_unused_result attribute when supported by the compiler.