libdict 1.0.7
Data Structure C Library
Loading...
Searching...
No Matches
sp_tree.h File Reference

Splay tree. More...

#include "dict.h"
Include dependency graph for sp_tree.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct sp_tree sp_tree
 A splay tree.
 
typedef struct sp_itor sp_itor
 An iterator over a splay tree.
 

Functions

sp_tree * sp_tree_new (dict_compare_func cmp_func)
 Create an empty tree that orders keys with |cmp_func| (must not be NULL).
 
dict * sp_dict_new (dict_compare_func cmp_func)
 Like sp_tree_new(), but wrapped in a generic dict for use with the dict_* macros.
 
size_t sp_tree_free (sp_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.
 
dict_insert_result sp_tree_insert (sp_tree *tree, void *key)
 Insert |key| unless an equal key is present.
 
void ** sp_tree_search (sp_tree *tree, const void *key)
 Return the address of the datum stored for |key|, or NULL if |key| is not present.
 
void ** sp_tree_search_le (sp_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 ** sp_tree_search_lt (sp_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 ** sp_tree_search_ge (sp_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 ** sp_tree_search_gt (sp_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 sp_tree_remove (sp_tree *tree, const void *key)
 Remove the element with key |key|.
 
size_t sp_tree_clear (sp_tree *tree, dict_delete_func delete_func)
 Remove every element, calling |delete_func|(key, datum) for each unless |delete_func| is NULL.
 
size_t sp_tree_traverse (sp_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 sp_tree_select (sp_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 sp_tree_count (const sp_tree *tree)
 Return the number of elements.
 
size_t sp_tree_min_path_length (const sp_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 sp_tree_max_path_length (const sp_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 sp_tree_total_path_length (const sp_tree *tree)
 Return the sum of the depths of all nodes, the root being at depth 1, or 0 for an empty tree.
 
bool sp_tree_verify (const sp_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.
 
sp_itor * sp_itor_new (sp_tree *tree)
 Create an iterator over |tree| that is not positioned on any element yet.
 
dict_itor * sp_dict_itor_new (sp_tree *tree)
 Like sp_itor_new(), but wrapped in a generic dict_itor for use with the dict_itor_* macros.
 
void sp_itor_free (sp_itor *itor)
 Free an iterator.
 
bool sp_itor_valid (const sp_itor *itor)
 Return whether |itor| is positioned on an element.
 
void sp_itor_invalidate (sp_itor *itor)
 Unposition |itor|, so that it is not positioned on any element.
 
bool sp_itor_next (sp_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 sp_itor_prev (sp_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 sp_itor_nextn (sp_itor *itor, size_t count)
 Move |itor| |count| elements forward and return whether it is still positioned.
 
bool sp_itor_prevn (sp_itor *itor, size_t count)
 Move |itor| |count| elements back and return whether it is still positioned.
 
bool sp_itor_first (sp_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 sp_itor_last (sp_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 sp_itor_search (sp_itor *itor, const void *key)
 Position |itor| on |key| and return true, or unposition it and return false if |key| is not present.
 
bool sp_itor_search_le (sp_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 sp_itor_search_lt (sp_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 sp_itor_search_ge (sp_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 sp_itor_search_gt (sp_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.
 
const void * sp_itor_key (const sp_itor *itor)
 Return the key |itor| is positioned on, or NULL if it is not positioned.
 
void ** sp_itor_datum (sp_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.
 
int sp_itor_compare (const sp_itor *i1, const sp_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.
 
bool sp_itor_remove (sp_itor *itor)
 Remove the element |itor| is positioned on and unposition |itor|; return false if it was not positioned.
 

Detailed Description

Splay tree.

Every access moves the key it finds to the root ("splaying"), so a sequence of m operations on n elements takes O((n + m) log(n + m)) time: amortized O(log n) per operation, though a single operation can take O(n). Recently used keys are cheap to find again. Because searches restructure the tree, a splay tree must not be read concurrently even without writers. Selecting the nth element takes O(n).

Definition in file sp_tree.h.

Typedef Documentation

◆ sp_itor

typedef struct sp_itor sp_itor

An iterator over a splay tree.

Opaque: create one with sp_itor_new().

Definition at line 126 of file sp_tree.h.

◆ sp_tree

typedef struct sp_tree sp_tree

A splay tree.

Opaque: create one with sp_tree_new().

Definition at line 47 of file sp_tree.h.

Function Documentation

◆ sp_dict_itor_new()

dict_itor * sp_dict_itor_new ( sp_tree * tree)

Like sp_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 449 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_dict_new()

dict * sp_dict_new ( dict_compare_func cmp_func)

Like sp_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 141 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_itor_compare()

int sp_itor_compare ( const sp_itor * i1,
const sp_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 481 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_itor_datum()

void ** sp_itor_datum ( sp_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 478 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_itor_first()

bool sp_itor_first ( sp_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 469 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_itor_free()

void sp_itor_free ( sp_itor * itor)

Free an iterator.

Definition at line 462 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_itor_invalidate()

void sp_itor_invalidate ( sp_itor * itor)

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

Definition at line 464 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_itor_key()

const void * sp_itor_key ( const sp_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 477 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_itor_last()

bool sp_itor_last ( sp_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 470 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_itor_new()

sp_itor * sp_itor_new ( sp_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 438 of file sp_tree.c.

◆ sp_itor_next()

bool sp_itor_next ( sp_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 465 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_itor_nextn()

bool sp_itor_nextn ( sp_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 467 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_itor_prev()

bool sp_itor_prev ( sp_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 466 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_itor_prevn()

bool sp_itor_prevn ( sp_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 468 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_itor_remove()

bool sp_itor_remove ( sp_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 487 of file sp_tree.c.

◆ sp_itor_search()

bool sp_itor_search ( sp_itor * itor,
const void * key )

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

Unlike sp_tree_search(), this does not splay.

Definition at line 472 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_itor_search_ge()

bool sp_itor_search_ge ( sp_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 475 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_itor_search_gt()

bool sp_itor_search_gt ( sp_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 476 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_itor_search_le()

bool sp_itor_search_le ( sp_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 473 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_itor_search_lt()

bool sp_itor_search_lt ( sp_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 474 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_itor_valid()

bool sp_itor_valid ( const sp_itor * itor)

Return whether |itor| is positioned on an element.

Definition at line 463 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_tree_clear()

size_t sp_tree_clear ( sp_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 155 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_tree_count()

size_t sp_tree_count ( const sp_tree * tree)

Return the number of elements.

Takes O(1) time.

Definition at line 408 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_tree_free()

size_t sp_tree_free ( sp_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 154 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_tree_insert()

dict_insert_result sp_tree_insert ( sp_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. Splays the key it finds to the root, which restructures the tree.

Definition at line 260 of file sp_tree.c.

◆ sp_tree_max_path_length()

size_t sp_tree_max_path_length ( const sp_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 410 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_tree_min_path_length()

size_t sp_tree_min_path_length ( const sp_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 409 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_tree_new()

sp_tree * sp_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 126 of file sp_tree.c.

◆ sp_tree_remove()

dict_remove_result sp_tree_remove ( sp_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 396 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_tree_search()

void ** sp_tree_search ( sp_tree * tree,
const void * key )

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

Splays the key it finds to the root, which restructures the tree.

Definition at line 301 of file sp_tree.c.

◆ sp_tree_search_ge()

void ** sp_tree_search_ge ( sp_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.

Splays the key it finds to the root, which restructures the tree.

Definition at line 347 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_tree_search_gt()

void ** sp_tree_search_gt ( sp_tree * tree,
const void * key )

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

Splays the key it finds to the root, which restructures the tree.

Definition at line 359 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_tree_search_le()

void ** sp_tree_search_le ( sp_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.

Splays the key it finds to the root, which restructures the tree.

Definition at line 323 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_tree_search_lt()

void ** sp_tree_search_lt ( sp_tree * tree,
const void * key )

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

Splays the key it finds to the root, which restructures the tree.

Definition at line 335 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_tree_select()

bool sp_tree_select ( sp_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 407 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_tree_total_path_length()

size_t sp_tree_total_path_length ( const sp_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 411 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_tree_traverse()

size_t sp_tree_traverse ( sp_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 406 of file sp_tree.c.

Here is the call graph for this function:

◆ sp_tree_verify()

bool sp_tree_verify ( const sp_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 428 of file sp_tree.c.

Here is the call graph for this function: