73static bool vt_tr_tree_verify(
const void* obj) {
return tr_tree_verify(obj); }
74static bool vt_tr_itor_remove(
void* itor) {
return tr_itor_remove(itor); }
115static tr_node* node_new(
void* key);
128 tree->prio_func = prio_func;
142 dct->
_vtable = &tr_tree_vtable;
159 parent = node; node = node->
llink;
160 }
else if (cmp > 0) {
161 parent = node; node = node->
rlink;
166 if (!(node = node_new(key)))
168 node->
prio =
tree->prio_func ?
tree->prio_func(key) : dict_rand();
170 if (!(node->
parent = parent)) {
176 parent->
llink = node;
178 parent->
rlink = node;
180 unsigned rotations = 0;
183 if (parent->
llink == node)
187 if (!(parent = node->
parent))
199 unsigned rotations = 0;
226 remove_node(
tree, node);
271 while (node && node->
llink)
282 while (parent && parent->
rlink == node) {
312 itor->
_vtable = &tr_tree_itor_vtable;
int(* dict_compare_func)(const void *, const void *)
A pointer to a function that compares two keys.
void(* dict_delete_func)(void *, void *)
A pointer to a function that is called when a key-value pair gets removed from a dictionary.
unsigned(* dict_prio_func)(const void *)
A pointer to a function that returns the priority of a key, used by tr_tree (a treap).
bool(* dict_visit_func)(const void *, void *, void *)
A pointer to a function used for iterating over dictionary contents.
const itor_vtable * _vtable
The operations of one container type.
const dict_vtable * _vtable
dict_compare_func cmp_func
void ** tr_itor_datum(tr_itor *itor)
Return the address of the datum |itor| is positioned on, through which it can be read or replaced,...
bool tr_itor_search_le(tr_itor *itor, const void *key)
Position |itor| on the greatest key less than or equal to |key| and return true, or unposition it and...
dict_itor * tr_dict_itor_new(tr_tree *tree)
Like tr_itor_new(), but wrapped in a generic dict_itor for use with the dict_itor_* macros.
dict_remove_result tr_tree_remove(tr_tree *tree, const void *key)
Remove the element with key |key|.
bool tr_itor_remove(tr_itor *it)
Remove the element |itor| is positioned on and unposition |itor|; return false if it was not position...
size_t tr_tree_total_path_length(const tr_tree *tree)
Return the sum of the depths of all nodes, the root being at depth 1, or 0 for an empty tree.
void tr_itor_invalidate(tr_itor *itor)
Unposition |itor|, so that it is not positioned on any element.
tr_tree * tr_tree_new(dict_compare_func cmp_func, dict_prio_func prio_func)
Create an empty treap that orders keys with |compare_func| (must not be NULL) and takes node prioriti...
bool tr_tree_select(tr_tree *tree, size_t n, const void **key, void **datum)
Store the key and datum of the |n|th element in key order (0-based) through |key| and |datum| and ret...
bool tr_itor_last(tr_itor *itor)
Position |itor| on the last element in key order and return true, or unposition it and return false i...
bool tr_itor_search_lt(tr_itor *itor, const void *key)
Position |itor| on the greatest key less than |key| and return true, or unposition it and return fals...
void ** tr_tree_search_le(tr_tree *tree, const void *key)
Return the address of the datum of the greatest key less than or equal to |key|, or NULL if there is ...
bool tr_itor_nextn(tr_itor *itor, size_t count)
Move |itor| |count| elements forward and return whether it is still positioned.
size_t tr_tree_clear(tr_tree *tree, dict_delete_func delete_func)
Remove every element, calling |delete_func|(key, datum) for each unless |delete_func| is NULL.
bool tr_itor_next(tr_itor *itor)
Move |itor| to the next element in key order and return true, or unposition it and return false if th...
size_t tr_tree_traverse(tr_tree *tree, dict_visit_func visit, void *user_data)
Call |visit|(key, datum, |user_data|) for each element in key order, stopping early if |visit| return...
size_t tr_tree_free(tr_tree *tree, dict_delete_func delete_func)
Remove every element, calling |delete_func|(key, datum) for each unless |delete_func| is NULL,...
bool tr_itor_first(tr_itor *itor)
Position |itor| on the first element in key order and return true, or unposition it and return false ...
tr_itor * tr_itor_new(tr_tree *tree)
Create an iterator over |tree| that is not positioned on any element yet.
bool tr_itor_prev(tr_itor *itor)
Move |itor| to the previous element in key order and return true, or unposition it and return false i...
bool tr_itor_search_ge(tr_itor *itor, const void *key)
Position |itor| on the least key greater than or equal to |key| and return true, or unposition it and...
size_t tr_tree_min_path_length(const tr_tree *tree)
Return the shallowest depth at which a child link is absent, the root being at depth 1,...
bool tr_itor_search_gt(tr_itor *itor, const void *key)
Position |itor| on the least key greater than |key| and return true, or unposition it and return fals...
const void * tr_itor_key(const tr_itor *itor)
Return the key |itor| is positioned on, or NULL if it is not positioned.
bool tr_itor_valid(const tr_itor *itor)
Return whether |itor| is positioned on an element.
bool tr_tree_verify(const tr_tree *tree)
Check the internal invariants of the tree and return true if they hold, or print the first failed che...
bool tr_itor_prevn(tr_itor *itor, size_t count)
Move |itor| |count| elements back and return whether it is still positioned.
void tr_itor_free(tr_itor *itor)
Free an iterator.
dict * tr_dict_new(dict_compare_func cmp_func, dict_prio_func prio_func)
Like tr_tree_new(), but wrapped in a generic dict for use with the dict_* macros.
size_t tr_tree_count(const tr_tree *tree)
Return the number of elements.
int tr_itor_compare(const tr_itor *i1, const tr_itor *i2)
Compare the positions of two iterators over the same tree: return a negative value,...
void ** tr_tree_search_lt(tr_tree *tree, const void *key)
Return the address of the datum of the greatest key less than |key|, or NULL if there is none.
void ** tr_tree_search_ge(tr_tree *tree, const void *key)
Return the address of the datum of the least key greater than or equal to |key|, or NULL if there is ...
void ** tr_tree_search_gt(tr_tree *tree, const void *key)
Return the address of the datum of the least key greater than |key|, or NULL if there is none.
dict_insert_result tr_tree_insert(tr_tree *tree, void *key)
Insert |key| unless an equal key is present.
bool tr_itor_search(tr_itor *itor, const void *key)
Position |itor| on |key| and return true, or unposition it and return false if |key| is not present.
size_t tr_tree_max_path_length(const tr_tree *tree)
Return the greatest depth of any node, the root being at depth 1, or 0 for an empty tree: the height ...
void ** tr_tree_search(tr_tree *tree, const void *key)
Return the address of the datum stored for |key|, or NULL if |key| is not present.
Treap: a binary search tree on the keys that is at the same time a max-heap on per-node priorities.
bool tree_iterator_last(void *Iterator)
size_t tree_free(void *Tree, dict_delete_func delete_func)
void ** tree_iterator_datum(void *Iterator)
bool tree_iterator_next(void *Iterator)
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_node_rot_left(void *Tree, void *Node)
void * tree_search_node(void *Tree, const void *key)
bool tree_verify_common(const void *Tree)
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)
size_t tree_clear(void *Tree, dict_delete_func delete_func)
const void * tree_iterator_key(const void *Iterator)
size_t tree_min_path_length(const void *Tree)
void ** tree_search(void *Tree, const void *key)
bool tree_iterator_nextn(void *Iterator, size_t count)
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)
bool tree_select(void *Tree, size_t n, const void **key, void **datum)
void tree_iterator_free(void *Iterator)
bool tree_iterator_prevn(void *Iterator, size_t count)
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)
void * tree_iterator_key_vt(void *Iterator)
int tree_iterator_compare_vt(void *Iterator1, void *Iterator2)
bool tree_iterator_search_gt(void *Iterator, const void *key)
void ** tree_search_ge(void *Tree, const void *key)
void tree_node_rot_right(void *Tree, void *Node)
void ** tree_search_le(void *Tree, const void *key)
bool tree_iterator_search(void *Iterator, const void *key)
bool tree_iterator_prev(void *Iterator)
#define TREE_NODE_FIELDS(node_type)
#define TREE_ITERATOR_FIELDS(tree_type, node_type)
#define TREE_FIELDS(node_type)