61# define ALPHA_0 .292893f
62# define ALPHA_1 .707106f
63# define ALPHA_2 .414213f
64# define ALPHA_3 .585786f
73#define WEIGHT(n) ((n) ? (n)->weight : 1U)
91static bool vt_wb_tree_select(
void* obj,
size_t n,
const void** key,
void** datum) {
return wb_tree_select(obj, n, key, datum); }
92static bool vt_wb_tree_verify(
const void* obj) {
return wb_tree_verify(obj); }
93static bool vt_wb_itor_remove(
void* itor) {
return wb_itor_remove(itor); }
134static wb_node* node_new(
void* key);
160 dct->
_vtable = &wb_tree_vtable;
171static inline unsigned
174 unsigned rotations = 0;
176 if ((uint64_t) weight * 1000U < (uint64_t) n->
weight * 293U) {
180 if ((uint64_t)
WEIGHT(nrl) * 1000U < (uint64_t) nr->
weight * 586U) {
196 if ((n->
rlink = a) != NULL)
202 if ((nr->
llink = b) != NULL)
209 }
else if ((uint64_t) weight * 1000U > (uint64_t) n->
weight * 707U) {
213 if ((uint64_t) weight * 1000U > (uint64_t) nl->
weight * 414U) {
230 if ((nl->
rlink = a) != NULL)
236 if ((n->
llink = b) != NULL)
257 parent = node; node = node->
llink;
259 parent = node; node = node->
rlink;
264 wb_node*
const add = node = node_new(key);
268 if (!(node->
parent = parent)) {
274 parent->
llink = node;
276 parent->
rlink = node;
278 unsigned rotations = 0;
279 while ((node = parent) != NULL) {
282 rotations += fixup(
tree, node);
315 unsigned rotations = 0;
319 rotations += fixup(
tree, parent);
332 remove_node(
tree, node);
358 *datum = node->
datum;
391 unsigned *weight,
const void* lower,
const void* upper)
408 unsigned lweight, rweight;
409 if (!node_verify(
tree, node, node->
llink, &lweight, lower, node->
key) ||
410 !node_verify(
tree, node, node->
rlink, &rweight, node->
key, upper))
415 VERIFY((uint64_t) lweight * 1000U >= (uint64_t) node->
weight * 292U);
416 VERIFY((uint64_t) lweight * 1000U <= (uint64_t) node->
weight * 708U);
417 *weight = lweight + rweight;
435 unsigned root_weight;
436 return node_verify(
tree, NULL,
tree->
root, &root_weight, NULL, NULL);
459 itor->
_vtable = &wb_tree_itor_vtable;
487 remove_node(itor->
tree, itor->
node);
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.
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
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_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_node_rot_left(void *Tree, void *Node)
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)
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_node_min(void *Node)
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)
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)
bool wb_itor_first(wb_itor *itor)
Position |itor| on the first element in key order and return true, or unposition it and return false ...
size_t wb_tree_clear(wb_tree *tree, dict_delete_func delete_func)
Remove every element, calling |delete_func|(key, datum) for each unless |delete_func| is NULL.
void wb_itor_free(wb_itor *itor)
Free an iterator.
void ** wb_tree_search_ge(wb_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 ** wb_tree_search_le(wb_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 ...
size_t wb_tree_count(const wb_tree *tree)
Return the number of elements.
int wb_itor_compare(const wb_itor *i1, const wb_itor *i2)
Compare the positions of two iterators over the same tree: return a negative value,...
bool wb_itor_last(wb_itor *itor)
Position |itor| on the last element in key order and return true, or unposition it and return false i...
bool wb_itor_search_lt(wb_itor *itor, const void *key)
Position |itor| on the greatest key less than |key| and return true, or unposition it and return fals...
const void * wb_itor_key(const wb_itor *itor)
Return the key |itor| is positioned on, or NULL if it is not positioned.
bool wb_itor_remove(wb_itor *itor)
Remove the element |itor| is positioned on and unposition |itor|; return false if it was not position...
bool wb_itor_search_gt(wb_itor *itor, const void *key)
Position |itor| on the least key greater than |key| and return true, or unposition it and return fals...
void ** wb_tree_search_lt(wb_tree *tree, const void *key)
Return the address of the datum of the greatest key less than |key|, or NULL if there is none.
size_t wb_tree_traverse(wb_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...
wb_tree * wb_tree_new(dict_compare_func cmp_func)
Create an empty tree that orders keys with |cmp_func| (must not be NULL).
dict_insert_result wb_tree_insert(wb_tree *tree, void *key)
Insert |key| unless an equal key is present.
dict * wb_dict_new(dict_compare_func cmp_func)
Like wb_tree_new(), but wrapped in a generic dict for use with the dict_* macros.
bool wb_itor_prevn(wb_itor *itor, size_t count)
Move |itor| |count| elements back and return whether it is still positioned.
dict_itor * wb_dict_itor_new(wb_tree *tree)
Like wb_itor_new(), but wrapped in a generic dict_itor for use with the dict_itor_* macros.
bool wb_itor_search(wb_itor *itor, const void *key)
Position |itor| on |key| and return true, or unposition it and return false if |key| is not present.
bool wb_tree_verify(const wb_tree *tree)
Check the internal invariants of the tree and return true if they hold, or print the first failed che...
void ** wb_itor_datum(wb_itor *itor)
Return the address of the datum |itor| is positioned on, through which it can be read or replaced,...
bool wb_itor_search_ge(wb_itor *itor, const void *key)
Position |itor| on the least key greater than or equal to |key| and return true, or unposition it and...
bool wb_itor_nextn(wb_itor *itor, size_t count)
Move |itor| |count| elements forward and return whether it is still positioned.
bool wb_itor_search_le(wb_itor *itor, const void *key)
Position |itor| on the greatest key less than or equal to |key| and return true, or unposition it and...
void wb_itor_invalidate(wb_itor *itor)
Unposition |itor|, so that it is not positioned on any element.
size_t wb_tree_free(wb_tree *tree, dict_delete_func delete_func)
Remove every element, calling |delete_func|(key, datum) for each unless |delete_func| is NULL,...
bool wb_itor_valid(const wb_itor *itor)
Return whether |itor| is positioned on an element.
bool wb_itor_prev(wb_itor *itor)
Move |itor| to the previous element in key order and return true, or unposition it and return false i...
bool wb_tree_select(wb_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...
size_t wb_tree_min_path_length(const wb_tree *tree)
Return the shallowest depth at which a child link is absent, the root being at depth 1,...
dict_remove_result wb_tree_remove(wb_tree *tree, const void *key)
Remove the element with key |key|.
bool wb_itor_next(wb_itor *itor)
Move |itor| to the next element in key order and return true, or unposition it and return false if th...
void ** wb_tree_search(wb_tree *tree, const void *key)
Return the address of the datum stored for |key|, or NULL if |key| is not present.
void ** wb_tree_search_gt(wb_tree *tree, const void *key)
Return the address of the datum of the least key greater than |key|, or NULL if there is none.
wb_itor * wb_itor_new(wb_tree *tree)
Create an iterator over |tree| that is not positioned on any element yet.
size_t wb_tree_max_path_length(const wb_tree *tree)
Return the greatest depth of any node, the root being at depth 1, or 0 for an empty tree: the height ...
size_t wb_tree_total_path_length(const wb_tree *tree)
Return the sum of the depths of all nodes, the root being at depth 1, or 0 for an empty tree.
Weight-balanced tree, BB[alpha] with alpha = 1 - sqrt(2)/2.