libdict 1.0.7
Data Structure C Library
Loading...
Searching...
No Matches
hb_tree.c File Reference
#include "hb_tree.h"
#include "dict_private.h"
#include "tree_common.h"
Include dependency graph for hb_tree.c:

Go to the source code of this file.

Data Structures

struct  hb_node
 
struct  hb_tree
 
struct  hb_itor
 

Macros

#define BAL_MASK   ((intptr_t)3)
 
#define PARENT(node)
 
#define BAL_POS(node)
 
#define BAL_NEG(node)
 

Typedefs

typedef struct hb_node hb_node
 

Functions

hb_tree * hb_tree_new (dict_compare_func cmp_func)
 Create an empty tree that orders keys with |cmp_func| (must not be NULL).
 
dict * hb_dict_new (dict_compare_func cmp_func)
 Like hb_tree_new(), but wrapped in a generic dict for use with the dict_* macros.
 
size_t hb_tree_free (hb_tree *tree, dict_delete_func delete_func)
 Remove every element, calling |delete_func|(key, datum) for each unless |delete_func| is NULL, and free the tree.
 
size_t hb_tree_clear (hb_tree *tree, dict_delete_func delete_func)
 Remove every element, calling |delete_func|(key, datum) for each unless |delete_func| is NULL.
 
dict_insert_result hb_tree_insert (hb_tree *tree, void *key)
 Insert |key| unless an equal key is present.
 
void ** hb_tree_search (hb_tree *tree, const void *key)
 Return the address of the datum stored for |key|, or NULL if |key| is not present.
 
void ** hb_tree_search_le (hb_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 none.
 
void ** hb_tree_search_lt (hb_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 ** hb_tree_search_ge (hb_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 none.
 
void ** hb_tree_search_gt (hb_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_remove_result hb_tree_remove (hb_tree *tree, const void *key)
 Remove the element with key |key|.
 
size_t hb_tree_traverse (hb_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| returns false.
 
bool hb_tree_select (hb_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 return true, or store NULL through them and return false if |n| is out of range.
 
size_t hb_tree_count (const hb_tree *tree)
 Return the number of elements.
 
size_t hb_tree_min_path_length (const hb_tree *tree)
 Return the shallowest depth at which a child link is absent, the root being at depth 1, or 0 for an empty tree.
 
size_t hb_tree_max_path_length (const hb_tree *tree)
 Return the greatest depth of any node, the root being at depth 1, or 0 for an empty tree: the height of the tree.
 
size_t hb_tree_total_path_length (const hb_tree *tree)
 Return the sum of the depths of all nodes, the root being at depth 1, or 0 for an empty tree.
 
bool hb_tree_verify (const hb_tree *tree)
 Check the internal invariants of the tree and return true if they hold, or print the first failed check to stderr and return false.
 
hb_itor * hb_itor_new (hb_tree *tree)
 Create an iterator over |tree| that is not positioned on any element yet.
 
dict_itor * hb_dict_itor_new (hb_tree *tree)
 Like hb_itor_new(), but wrapped in a generic dict_itor for use with the dict_itor_* macros.
 
void hb_itor_free (hb_itor *itor)
 Free an iterator.
 
bool hb_itor_valid (const hb_itor *itor)
 Return whether |itor| is positioned on an element.
 
void hb_itor_invalidate (hb_itor *itor)
 Unposition |itor|, so that it is not positioned on any element.
 
bool hb_itor_next (hb_itor *itor)
 Move |itor| to the next element in key order and return true, or unposition it and return false if there is none.
 
bool hb_itor_prev (hb_itor *itor)
 Move |itor| to the previous element in key order and return true, or unposition it and return false if there is none.
 
bool hb_itor_nextn (hb_itor *itor, size_t count)
 Move |itor| |count| elements forward and return whether it is still positioned.
 
bool hb_itor_prevn (hb_itor *itor, size_t count)
 Move |itor| |count| elements back and return whether it is still positioned.
 
bool hb_itor_first (hb_itor *itor)
 Position |itor| on the first element in key order and return true, or unposition it and return false if the tree is empty.
 
bool hb_itor_last (hb_itor *itor)
 Position |itor| on the last element in key order and return true, or unposition it and return false if the tree is empty.
 
bool hb_itor_search (hb_itor *itor, const void *key)
 Position |itor| on |key| and return true, or unposition it and return false if |key| is not present.
 
bool hb_itor_search_le (hb_itor *itor, const void *key)
 Position |itor| on the greatest key less than or equal to |key| and return true, or unposition it and return false if there is none.
 
bool hb_itor_search_lt (hb_itor *itor, const void *key)
 Position |itor| on the greatest key less than |key| and return true, or unposition it and return false if there is none.
 
bool hb_itor_search_ge (hb_itor *itor, const void *key)
 Position |itor| on the least key greater than or equal to |key| and return true, or unposition it and return false if there is none.
 
bool hb_itor_search_gt (hb_itor *itor, const void *key)
 Position |itor| on the least key greater than |key| and return true, or unposition it and return false if there is none.
 
int hb_itor_compare (const hb_itor *i1, const hb_itor *i2)
 Compare the positions of two iterators over the same tree: return a negative value, zero, or a positive value as |i1| is before, at, or after |i2| in key order.
 
const void * hb_itor_key (const hb_itor *itor)
 Return the key |itor| is positioned on, or NULL if it is not positioned.
 
void ** hb_itor_datum (hb_itor *itor)
 Return the address of the datum |itor| is positioned on, through which it can be read or replaced, or NULL if it is not positioned.
 
bool hb_itor_remove (hb_itor *itor)
 Remove the element |itor| is positioned on and unposition |itor|; return false if it was not positioned.
 

Macro Definition Documentation

◆ BAL_MASK

#define BAL_MASK   ((intptr_t)3)

Definition at line 46 of file hb_tree.c.

◆ BAL_NEG

#define BAL_NEG ( node)
Value:
((node)->bal & 2)

Definition at line 49 of file hb_tree.c.

◆ BAL_POS

#define BAL_POS ( node)
Value:
((node)->bal & 1)

Definition at line 48 of file hb_tree.c.

◆ PARENT

#define PARENT ( node)
Value:
((hb_node*) ((node)->bal & ~BAL_MASK))
#define BAL_MASK
Definition hb_tree.c:46

Definition at line 47 of file hb_tree.c.

Typedef Documentation

◆ hb_node

typedef struct hb_node hb_node

Definition at line 33 of file hb_tree.c.

Function Documentation

◆ hb_dict_itor_new()

dict_itor * hb_dict_itor_new ( hb_tree * tree)

Like hb_itor_new(), but wrapped in a generic dict_itor for use with the dict_itor_* macros.

Free it with dict_itor_free().

Definition at line 674 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_dict_new()

dict * hb_dict_new ( dict_compare_func cmp_func)

Like hb_tree_new(), but wrapped in a generic dict for use with the dict_* macros.

Free it with dict_free(). Returns NULL if memory cannot be allocated.

Definition at line 161 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_itor_compare()

int hb_itor_compare ( const hb_itor * i1,
const hb_itor * i2 )

Compare the positions of two iterators over the same tree: return a negative value, zero, or a positive value as |i1| is before, at, or after |i2| in key order.

An unpositioned iterator compares before every positioned one.

Definition at line 722 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_itor_datum()

void ** hb_itor_datum ( hb_itor * itor)

Return the address of the datum |itor| is positioned on, through which it can be read or replaced, or NULL if it is not positioned.

Definition at line 724 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_itor_first()

bool hb_itor_first ( hb_itor * itor)

Position |itor| on the first element in key order and return true, or unposition it and return false if the tree is empty.

Definition at line 715 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_itor_free()

void hb_itor_free ( hb_itor * itor)

Free an iterator.

Definition at line 687 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_itor_invalidate()

void hb_itor_invalidate ( hb_itor * itor)

Unposition |itor|, so that it is not positioned on any element.

Definition at line 689 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_itor_key()

const void * hb_itor_key ( const hb_itor * itor)

Return the key |itor| is positioned on, or NULL if it is not positioned.

The key belongs to the container and must not be modified in a way that changes how it compares.

Definition at line 723 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_itor_last()

bool hb_itor_last ( hb_itor * itor)

Position |itor| on the last element in key order and return true, or unposition it and return false if the tree is empty.

Definition at line 716 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_itor_new()

hb_itor * hb_itor_new ( hb_tree * tree)

Create an iterator over |tree| that is not positioned on any element yet.

Returns NULL if memory cannot be allocated.

Definition at line 663 of file hb_tree.c.

◆ hb_itor_next()

bool hb_itor_next ( hb_itor * itor)

Move |itor| to the next element in key order and return true, or unposition it and return false if there is none.

An unpositioned iterator stays unpositioned and returns false.

Definition at line 691 of file hb_tree.c.

◆ hb_itor_nextn()

bool hb_itor_nextn ( hb_itor * itor,
size_t count )

Move |itor| |count| elements forward and return whether it is still positioned.

It becomes unpositioned if it runs past the last element.

Definition at line 703 of file hb_tree.c.

◆ hb_itor_prev()

bool hb_itor_prev ( hb_itor * itor)

Move |itor| to the previous element in key order and return true, or unposition it and return false if there is none.

An unpositioned iterator stays unpositioned and returns false.

Definition at line 697 of file hb_tree.c.

◆ hb_itor_prevn()

bool hb_itor_prevn ( hb_itor * itor,
size_t count )

Move |itor| |count| elements back and return whether it is still positioned.

It becomes unpositioned if it runs past the first element.

Definition at line 709 of file hb_tree.c.

◆ hb_itor_remove()

bool hb_itor_remove ( hb_itor * itor)

Remove the element |itor| is positioned on and unposition |itor|; return false if it was not positioned.

No delete function is called, so fetch the key and datum first if they need freeing. See Iterator invalidation for which iterators this invalidates.

Definition at line 727 of file hb_tree.c.

◆ hb_itor_search()

bool hb_itor_search ( hb_itor * itor,
const void * key )

Position |itor| on |key| and return true, or unposition it and return false if |key| is not present.

Definition at line 717 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_itor_search_ge()

bool hb_itor_search_ge ( hb_itor * itor,
const void * key )

Position |itor| on the least key greater than or equal to |key| and return true, or unposition it and return false if there is none.

Definition at line 720 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_itor_search_gt()

bool hb_itor_search_gt ( hb_itor * itor,
const void * key )

Position |itor| on the least key greater than |key| and return true, or unposition it and return false if there is none.

Definition at line 721 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_itor_search_le()

bool hb_itor_search_le ( hb_itor * itor,
const void * key )

Position |itor| on the greatest key less than or equal to |key| and return true, or unposition it and return false if there is none.

Definition at line 718 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_itor_search_lt()

bool hb_itor_search_lt ( hb_itor * itor,
const void * key )

Position |itor| on the greatest key less than |key| and return true, or unposition it and return false if there is none.

Definition at line 719 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_itor_valid()

bool hb_itor_valid ( const hb_itor * itor)

Return whether |itor| is positioned on an element.

Definition at line 688 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_tree_clear()

size_t hb_tree_clear ( hb_tree * tree,
dict_delete_func delete_func )

Remove every element, calling |delete_func|(key, datum) for each unless |delete_func| is NULL.

Returns the number of elements removed. The tree stays usable. See Iterator invalidation for which iterators this invalidates.

Definition at line 182 of file hb_tree.c.

◆ hb_tree_count()

size_t hb_tree_count ( const hb_tree * tree)

Return the number of elements.

Takes O(1) time.

Definition at line 548 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_tree_free()

size_t hb_tree_free ( hb_tree * tree,
dict_delete_func delete_func )

Remove every element, calling |delete_func|(key, datum) for each unless |delete_func| is NULL, and free the tree.

Returns the number of elements removed. Iterators over the tree may afterwards only be freed.

Definition at line 175 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_tree_insert()

dict_insert_result hb_tree_insert ( hb_tree * tree,
void * key )

Insert |key| unless an equal key is present.

Returns {&datum, true} for a new key, whose datum starts as NULL; the tree stores the |key| pointer itself, it does not copy the key. If an equal key is present, returns {&datum, false} with that key's datum, and |key| is not stored and still belongs to the caller. Returns {NULL, false} if memory cannot be allocated.

Definition at line 319 of file hb_tree.c.

◆ hb_tree_max_path_length()

size_t hb_tree_max_path_length ( const hb_tree * tree)

Return the greatest depth of any node, the root being at depth 1, or 0 for an empty tree: the height of the tree.

A statistic for testing and benchmarking.

Definition at line 550 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_tree_min_path_length()

size_t hb_tree_min_path_length ( const hb_tree * tree)

Return the shallowest depth at which a child link is absent, the root being at depth 1, or 0 for an empty tree.

A statistic for testing and benchmarking.

Definition at line 549 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_tree_new()

hb_tree * hb_tree_new ( dict_compare_func cmp_func)

Create an empty tree that orders keys with |cmp_func| (must not be NULL).

Returns NULL if memory cannot be allocated.

Definition at line 146 of file hb_tree.c.

◆ hb_tree_remove()

dict_remove_result hb_tree_remove ( hb_tree * tree,
const void * key )

Remove the element with key |key|.

Returns {key, datum, true}, handing the stored key and datum back to the caller (no delete function is called), or {NULL, NULL, false} if |key| is not present. See Iterator invalidation for which iterators this invalidates.

Definition at line 493 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_tree_search()

void ** hb_tree_search ( hb_tree * tree,
const void * key )

Return the address of the datum stored for |key|, or NULL if |key| is not present.

Definition at line 397 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_tree_search_ge()

void ** hb_tree_search_ge ( hb_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 none.

Definition at line 400 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_tree_search_gt()

void ** hb_tree_search_gt ( hb_tree * tree,
const void * key )

Return the address of the datum of the least key greater than |key|, or NULL if there is none.

Definition at line 401 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_tree_search_le()

void ** hb_tree_search_le ( hb_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 none.

Definition at line 398 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_tree_search_lt()

void ** hb_tree_search_lt ( hb_tree * tree,
const void * key )

Return the address of the datum of the greatest key less than |key|, or NULL if there is none.

Definition at line 399 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_tree_select()

bool hb_tree_select ( hb_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 return true, or store NULL through them and return false if |n| is out of range.

Either may be NULL. Takes O(n) time.

Definition at line 521 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_tree_total_path_length()

size_t hb_tree_total_path_length ( const hb_tree * tree)

Return the sum of the depths of all nodes, the root being at depth 1, or 0 for an empty tree.

A statistic for testing and benchmarking.

Definition at line 551 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_tree_traverse()

size_t hb_tree_traverse ( hb_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| returns false.

Returns the number of calls made.

Definition at line 504 of file hb_tree.c.

Here is the call graph for this function:

◆ hb_tree_verify()

bool hb_tree_verify ( const hb_tree * tree)

Check the internal invariants of the tree and return true if they hold, or print the first failed check to stderr and return false.

Meant for testing and debugging; takes O(n) time.

Definition at line 650 of file hb_tree.c.