46#define BAL_MASK ((intptr_t)3)
47#define PARENT(node) ((hb_node*) ((node)->bal & ~BAL_MASK))
48#define BAL_POS(node) ((node)->bal & 1)
49#define BAL_NEG(node) ((node)->bal & 2)
100static hb_node* node_new(
void* key);
103 unsigned* height,
size_t *count);
129 dct->
_vtable = &hb_tree_vtable;
152 if (delete_func) delete_func(node->
key, node->
datum);
176 q->bal = (intptr_t)qr | (qr_bal == 0);
177 if ((q->rlink = qrl) != NULL)
178 qrl->bal = (intptr_t)q | (qrl->bal &
BAL_MASK);
181 qr->bal = (intptr_t)qp | ((qr_bal == 0) << 1);
185 return (qr_bal == 0);
201 q->bal = (intptr_t)ql | ((ql_bal == 0) << 1);
202 if ((q->llink = qlr) != NULL)
203 qlr->bal = (intptr_t)q | (qlr->bal &
BAL_MASK);
206 ql->bal = (intptr_t)qp | (ql_bal == 0);
210 return (ql_bal == 0);
224 const int qrl_bal = qrl->bal &
BAL_MASK;
229 qrl->
bal = (intptr_t)qp;
235 q->bal = (intptr_t)qrl | ((qrl_bal == 1) << 1);
236 if ((q->rlink = qrll) != NULL)
237 qrll->bal = (intptr_t)q | (qrll->bal &
BAL_MASK);
240 qr->bal = (intptr_t)qrl | (qrl_bal == 2);
241 if ((qr->llink = qrlr) != NULL)
242 qrlr->bal = (intptr_t)qr | (qrlr->bal &
BAL_MASK);
256 const int qlr_bal = qlr->bal &
BAL_MASK;
261 qlr->
bal = (intptr_t)qp;
267 q->bal = (intptr_t)qlr | (qlr_bal == 2);
268 if ((q->llink = qlrr) != NULL)
269 qlrr->bal = (intptr_t)q | (qlrr->bal &
BAL_MASK);
272 ql->bal = (intptr_t)qlr | ((qlr_bal == 1) << 1);
273 if ((ql->rlink = qlrl) != NULL)
274 qlrl->bal = (intptr_t)ql | (qlrl->bal &
BAL_MASK);
288 parent = node; node = node->
llink;
289 }
else if (cmp > 0) {
290 parent = node; node = node->
rlink;
297 hb_node*
const add = node = node_new(key);
301 if (!(node->
pptr = parent)) {
307 parent->
llink = node;
309 parent->
rlink = node;
311 while (parent != q) {
313 if (parent->
llink == node)
322 if (q->
llink == node) {
369 SWAP(node->
key, out->key, tmp);
385 bool left = (p->
llink == node);
394 unsigned rotations = 0;
404 if (rotate_l(
tree, p))
break;
423 if (rotate_r(
tree, p))
439 if (p->
llink == node) {
456 remove_node(
tree, node);
481 node = node_prev(node);
485 node = node_next(node);
490 *datum = node->
datum;
504 ASSERT((((intptr_t)node) & 3) == 0);
520 while (parent && parent->
llink == node) {
533 while (parent && parent->
rlink == node) {
542 unsigned* height,
size_t *count)
547 if (parent->
llink == node) {
563 }
else if (bal == 1) {
567 unsigned lheight, rheight;
568 if (!node_verify(
tree, node, node->
llink, &lheight, count) ||
569 !node_verify(
tree, node, node->
rlink, &rheight, count))
571 VERIFY(bal == (
int)rheight - (
int)lheight);
573 *height =
MAX(lheight, rheight) + 1;
586 bool verified = node_verify(
tree, NULL,
tree->
root, NULL, &count);
611 itor->
_vtable = &hb_tree_itor_vtable;
623 return itor->
node != NULL;
629 return itor->
node != NULL;
633 while (itor->
node && count--)
635 return itor->
node != NULL;
639 while (itor->
node && count--)
641 return itor->
node != NULL;
660 remove_node(itor->
tree, itor->
node);
dict_remove_result(* dict_remove_func)(void *obj, const void *key)
int(* dict_compare_func)(const void *, const void *)
bool(* dict_select_func)(void *obj, size_t n, const void **key, void **datum)
void(* dict_invalidate_func)(void *itor)
size_t(* dict_traverse_func)(void *obj, dict_visit_func visit, void *user_data)
dict_insert_result(* dict_insert_func)(void *obj, void *key)
void(* dict_delete_func)(void *, void *)
int(* dict_icompare_func)(void *itor1, void *itor2)
bool(* dict_nextn_func)(void *itor, size_t count)
bool(* dict_valid_func)(const void *itor)
size_t(* dict_count_func)(const void *obj)
bool(* dict_first_func)(void *itor)
bool(* dict_next_func)(void *itor)
void *(* dict_key_func)(void *itor)
size_t(* dict_dfree_func)(void *obj, dict_delete_func delete_func)
void **(* dict_datum_func)(void *itor)
bool(* dict_prevn_func)(void *itor, size_t count)
void **(* dict_search_func)(void *obj, const void *key)
bool(* dict_isearch_func)(void *itor, const void *key)
bool(* dict_iremove_func)(void *itor)
bool(* dict_prev_func)(void *itor)
dict_itor *(* dict_inew_func)(void *obj)
bool(* dict_last_func)(void *itor)
bool(* dict_verify_func)(const void *obj)
bool(* dict_visit_func)(const void *, void *, void *)
size_t(* dict_clear_func)(void *obj, dict_delete_func delete_func)
void(* dict_ifree_func)(void *itor)
bool hb_itor_search(hb_itor *itor, const void *key)
dict_itor * hb_dict_itor_new(hb_tree *tree)
size_t hb_tree_clear(hb_tree *tree, dict_delete_func delete_func)
bool hb_itor_search_gt(hb_itor *itor, const void *key)
void ** hb_tree_search_lt(hb_tree *tree, const void *key)
void ** hb_tree_search(hb_tree *tree, const void *key)
bool hb_tree_select(hb_tree *tree, size_t n, const void **key, void **datum)
dict * hb_dict_new(dict_compare_func cmp_func)
bool hb_itor_search_ge(hb_itor *itor, const void *key)
void hb_itor_free(hb_itor *itor)
hb_itor * hb_itor_new(hb_tree *tree)
bool hb_itor_search_lt(hb_itor *itor, const void *key)
bool hb_itor_prev(hb_itor *itor)
void ** hb_tree_search_gt(hb_tree *tree, const void *key)
hb_tree * hb_tree_new(dict_compare_func cmp_func)
void hb_itor_invalidate(hb_itor *itor)
size_t hb_tree_count(const hb_tree *tree)
bool hb_itor_valid(const hb_itor *itor)
size_t hb_tree_min_path_length(const hb_tree *tree)
dict_insert_result hb_tree_insert(hb_tree *tree, void *key)
void ** hb_tree_search_ge(hb_tree *tree, const void *key)
bool hb_itor_nextn(hb_itor *itor, size_t count)
bool hb_itor_remove(hb_itor *itor)
size_t hb_tree_free(hb_tree *tree, dict_delete_func delete_func)
dict_remove_result hb_tree_remove(hb_tree *tree, const void *key)
size_t hb_tree_max_path_length(const hb_tree *tree)
int hb_itor_compare(const hb_itor *i1, const hb_itor *i2)
bool hb_itor_last(hb_itor *itor)
bool hb_itor_prevn(hb_itor *itor, size_t count)
void ** hb_itor_datum(hb_itor *itor)
bool hb_itor_next(hb_itor *itor)
const void * hb_itor_key(const hb_itor *itor)
bool hb_itor_search_le(hb_itor *itor, const void *key)
bool hb_tree_verify(const hb_tree *tree)
bool hb_itor_first(hb_itor *itor)
size_t hb_tree_traverse(hb_tree *tree, dict_visit_func visit, void *user_data)
void ** hb_tree_search_le(hb_tree *tree, const void *key)
size_t hb_tree_total_path_length(const hb_tree *tree)
const itor_vtable * _vtable
const dict_vtable * _vtable
dict_compare_func cmp_func
bool tree_iterator_last(void *Iterator)
void ** tree_iterator_datum(void *Iterator)
void * tree_node_max(void *Node)
void tree_iterator_invalidate(void *Iterator)
void ** tree_search_gt(void *Tree, const void *key)
int tree_iterator_compare(const void *Iterator1, const void *Iterator2)
void * tree_search_node(void *Tree, const void *key)
bool tree_iterator_search_le(void *Iterator, const void *key)
bool tree_iterator_valid(const void *Iterator)
size_t tree_count(const void *Tree)
void ** tree_search_lt(void *Tree, const void *key)
const void * tree_iterator_key(const void *Iterator)
size_t tree_min_path_length(const void *Tree)
void * tree_node_min(void *Node)
void ** tree_search(void *Tree, const void *key)
size_t tree_traverse(void *Tree, dict_visit_func visit, void *user_data)
size_t tree_total_path_length(const void *Tree)
bool tree_iterator_first(void *Iterator)
void tree_iterator_free(void *Iterator)
bool tree_iterator_search_lt(void *Iterator, const void *key)
bool tree_iterator_search_ge(void *Iterator, const void *key)
size_t tree_max_path_length(const void *Tree)
bool tree_iterator_search_gt(void *Iterator, const void *key)
void ** tree_search_ge(void *Tree, const void *key)
void ** tree_search_le(void *Tree, const void *key)
bool tree_iterator_search(void *Iterator, const void *key)
#define TREE_ITERATOR_FIELDS(tree_type, node_type)
#define TREE_FIELDS(node_type)