qLibc
|
Tree Table container that implements the Left-Leaning Red-Black BST algorithm. More...
Go to the source code of this file.
Functions | |
qtreetbl_t * | qtreetbl (int options) |
Initialize a tree table. | |
void | qtreetbl_set_compare (qtreetbl_t *tbl, int(*cmp)(const void *name1, size_t namesize1, const void *name2, size_t namesize2)) |
qtreetbl->set_compare(): Set user comparator. | |
bool | qtreetbl_put (qtreetbl_t *tbl, const char *name, const void *data, size_t datasize) |
qtreetbl->put(): Put an object into this table with string type key. | |
bool | qtreetbl_putstr (qtreetbl_t *tbl, const char *name, const char *str) |
qtreetbl->putstr(): Put a string into this table. | |
bool | qtreetbl_putstrf (qtreetbl_t *tbl, const char *name, const char *format,...) |
qtreetbl->putstrf(): Put a formatted string into this table. | |
bool | qtreetbl_putobj (qtreetbl_t *tbl, const void *name, size_t namesize, const void *data, size_t datasize) |
qtreetbl->putobj(): Put an object data into this table with an object key. | |
void * | qtreetbl_get (qtreetbl_t *tbl, const char *name, size_t *datasize, bool newmem) |
qtreetbl->get(): Get an object from this table. | |
char * | qtreetbl_getstr (qtreetbl_t *tbl, const char *name, const bool newmem) |
qtreetbl->getstr(): Finds an object and returns it as string type. | |
void * | qtreetbl_getobj (qtreetbl_t *tbl, const void *name, size_t namesize, size_t *datasize, bool newmem) |
qtreetbl->getobj(): Get an object from this table with an object name. | |
bool | qtreetbl_remove (qtreetbl_t *tbl, const char *name) |
qtreetbl->remove(): Remove an object from this table. | |
bool | qtreetbl_removeobj (qtreetbl_t *tbl, const void *name, size_t namesize) |
qtreetbl->remove(): Remove an object from this table with an object name. | |
bool | qtreetbl_getnext (qtreetbl_t *tbl, qtreetbl_obj_t *obj, const bool newmem) |
qtreetbl->getnext(): Get next element. | |
void * | qtreetbl_find_min (qtreetbl_t *tbl, size_t *namesize) |
qtreetbl->find_min(): Find the name of the leftmost object. | |
void * | qtreetbl_find_max (qtreetbl_t *tbl, size_t *namesize) |
qtreetbl->find_max(): Find the name of the rightmost object. | |
qtreetbl_obj_t | qtreetbl_find_nearest (qtreetbl_t *tbl, const void *name, size_t namesize, bool newmem) |
qtreetbl->find_nearest(): Find an object with matching key or nearest. | |
size_t | qtreetbl_size (qtreetbl_t *tbl) |
qtreetbl->size(): Returns the number of keys in the table. | |
void | qtreetbl_clear (qtreetbl_t *tbl) |
qtreetbl->clear(): Clears the table so that it contains no keys. | |
void | qtreetbl_lock (qtreetbl_t *tbl) |
qtreetbl->lock(): Enter critical section. | |
void | qtreetbl_unlock (qtreetbl_t *tbl) |
qtreetbl->unlock(): Leave critical section. | |
void | qtreetbl_free (qtreetbl_t *tbl) |
qtreetbl->free(): De-allocate the table. | |
int | qtreetbl_byte_cmp (const void *name1, size_t namesize1, const void *name2, size_t namesize2) |
bool | qtreetbl_debug (qtreetbl_t *tbl, FILE *out) |
qtreetbl->debug(): Print the internal tree structure in text. | |
int | node_check_root (qtreetbl_t *tbl) |
Verifies that root property of the red-black tree is satisfied. | |
int | node_check_red (qtreetbl_t *tbl, qtreetbl_obj_t *obj) |
Verifies that red property of the red-black tree is satisfied. | |
int | node_check_black (qtreetbl_t *tbl, qtreetbl_obj_t *obj, int *path_len) |
Verifies that black property of the red-black tree is satisfied. | |
int | node_check_llrb (qtreetbl_t *tbl, qtreetbl_obj_t *obj) |
Verifies that LLRB property of the left-leaning red-black tree is satisfied. | |
int | qtreetbl_check (qtreetbl_t *tbl) |
Verifies that the invariants of the red-black tree are satisfied. | |
Tree Table container that implements the Left-Leaning Red-Black BST algorithm.
qtreetbl implements a binary search tree that allows efficient in-order traversal with O(log n) search time.
The algorithm qtreetbl specifically implements is the left-leaning red-black tree algorithm invented in 2008 by Robert Sedgewick. A left-leaning red–black tree is a type of self-balancing binary search tree and it is a variant of the red–black tree which was invented in 1972 by Rudolf Bayer.
References:
(E) ______________|______________ / \ (C) (R) / \ / \ (A,B) (D) (I,N) (S,X) <2-3-4 Tree Data Structure> E ______________|______________ / \ C R _______|_______ _______|_______ / \ / \ B D N X// // // A* I* S*
<Left-Leaning Red-Black Tree Data Structure>Nodes A, I and S are nodes with RED upper link. Others are BLACK
The Red-Black BST algorithm has been one of the popular BST algorithms especially for in-memory operation. The Left-Leaning version of Red-Black especially improves performance and reduces overall complexity.
Since it's relatively new algorithm, there's not many practically functional working codes yet other than proof of concept kinds. Here's one of fully functional codes and I, Seungyoung Kim, would like to dedicate this code to the genius inventor Robert Sedgewick and to all the great qLibc users.
Additional features:
Definition in file qtreetbl.c.
qtreetbl_t * qtreetbl | ( | int | options | ) |
Initialize a tree table.
options | combination of initialization options. |
errno | will be set in error condition.
|
Definition at line 182 of file qtreetbl.c.
void qtreetbl_set_compare | ( | qtreetbl_t * | tbl, |
int(*)(const void *name1, size_t namesize1, const void *name2, size_t namesize2) | cmp | ||
) |
qtreetbl->set_compare(): Set user comparator.
tbl | qtreetbl_t container pointer. |
cmp | a pointer to the user comparator function. |
Definition at line 250 of file qtreetbl.c.
bool qtreetbl_put | ( | qtreetbl_t * | tbl, |
const char * | name, | ||
const void * | data, | ||
size_t | datasize | ||
) |
qtreetbl->put(): Put an object into this table with string type key.
tbl | qtreetbl_t container pointer. |
name | key name. |
data | data object. |
datasize | size of data object. |
errno | will be set in error condition.
|
Definition at line 269 of file qtreetbl.c.
bool qtreetbl_putstr | ( | qtreetbl_t * | tbl, |
const char * | name, | ||
const char * | str | ||
) |
qtreetbl->putstr(): Put a string into this table.
tbl | qtreetbl container pointer. |
name | key name. |
str | string data. |
errno | will be set in error condition.
|
Definition at line 287 of file qtreetbl.c.
bool qtreetbl_putstrf | ( | qtreetbl_t * | tbl, |
const char * | name, | ||
const char * | format, | ||
... | |||
) |
qtreetbl->putstrf(): Put a formatted string into this table.
tbl | qtreetbl_t container pointer. |
name | key name. |
format | formatted string data. |
errno | will be set in error condition.
|
Definition at line 305 of file qtreetbl.c.
bool qtreetbl_putobj | ( | qtreetbl_t * | tbl, |
const void * | name, | ||
size_t | namesize, | ||
const void * | data, | ||
size_t | datasize | ||
) |
qtreetbl->putobj(): Put an object data into this table with an object key.
tbl | qtreetbl_t container pointer. |
name | key name. |
namesize | key size. |
data | data object. |
datasize | size of data object. |
errno | will be set in error condition.
|
Definition at line 336 of file qtreetbl.c.
void * qtreetbl_get | ( | qtreetbl_t * | tbl, |
const char * | name, | ||
size_t * | datasize, | ||
bool | newmem | ||
) |
qtreetbl->get(): Get an object from this table.
tbl | qtreetbl_t container pointer. |
name | key name. |
datasize | if not NULL, object size will be stored. |
newmem | whether or not to allocate memory for the data. |
errno | will be set in error condition.
|
Definition at line 392 of file qtreetbl.c.
char * qtreetbl_getstr | ( | qtreetbl_t * | tbl, |
const char * | name, | ||
const bool | newmem | ||
) |
qtreetbl->getstr(): Finds an object and returns it as string type.
tbl | qtreetbl_t container pointer. |
name | key name. |
newmem | whether or not to allocate memory for the data. |
errno | will be set in error condition.
|
Definition at line 417 of file qtreetbl.c.
void * qtreetbl_getobj | ( | qtreetbl_t * | tbl, |
const void * | name, | ||
size_t | namesize, | ||
size_t * | datasize, | ||
bool | newmem | ||
) |
qtreetbl->getobj(): Get an object from this table with an object name.
tbl | qtreetbl_t container pointer. |
name | key name. |
namesize | key size. |
datasize | if not NULL, oject size will be stored. |
newmem | whether or not to allocate memory for the data. |
errno | will be set in error condition.
|
Definition at line 443 of file qtreetbl.c.
bool qtreetbl_remove | ( | qtreetbl_t * | tbl, |
const char * | name | ||
) |
qtreetbl->remove(): Remove an object from this table.
tbl | qtreetbl_t container pointer. |
name | key name. |
errno | will be set in error condition.
|
Definition at line 474 of file qtreetbl.c.
bool qtreetbl_removeobj | ( | qtreetbl_t * | tbl, |
const void * | name, | ||
size_t | namesize | ||
) |
qtreetbl->remove(): Remove an object from this table with an object name.
tbl | qtreetbl_t container pointer. |
name | key name. |
name | key size. |
errno | will be set in error condition.
|
Definition at line 491 of file qtreetbl.c.
bool qtreetbl_getnext | ( | qtreetbl_t * | tbl, |
qtreetbl_obj_t * | obj, | ||
const bool | newmem | ||
) |
qtreetbl->getnext(): Get next element.
tbl | qtreetbl_t container pointer. |
obj | found data will be stored in this object. |
newmem | whether or not to allocate memory for the data. |
errno | will be set in error condition.
|
Definition at line 580 of file qtreetbl.c.
void * qtreetbl_find_min | ( | qtreetbl_t * | tbl, |
size_t * | namesize | ||
) |
qtreetbl->find_min(): Find the name of the leftmost object.
tbl | qtreetbl_t container pointer. |
namesize | if not NULL, the size of key name will be stored. |
Definition at line 634 of file qtreetbl.c.
void * qtreetbl_find_max | ( | qtreetbl_t * | tbl, |
size_t * | namesize | ||
) |
qtreetbl->find_max(): Find the name of the rightmost object.
tbl | qtreetbl_t container pointer. |
namesize | if not NULL, the size of key name will be stored. |
Definition at line 662 of file qtreetbl.c.
qtreetbl_obj_t qtreetbl_find_nearest | ( | qtreetbl_t * | tbl, |
const void * | name, | ||
size_t | namesize, | ||
bool | newmem | ||
) |
qtreetbl->find_nearest(): Find an object with matching key or nearest.
find_nearest() returns matching key or nearest key object. If there's no keys in the table. It'll return empty qtreetbl_obj_t object with errno ENOENT.
tbl | qtreetbl_t container pointer. |
name | key name. |
namesize | key size. |
newmem | whether or not to allocate memory for the data. |
errno | will be set in error condition.
|
Definition at line 711 of file qtreetbl.c.
size_t qtreetbl_size | ( | qtreetbl_t * | tbl | ) |
qtreetbl->size(): Returns the number of keys in the table.
tbl | qtreetbl_t container pointer. |
Definition at line 775 of file qtreetbl.c.
void qtreetbl_clear | ( | qtreetbl_t * | tbl | ) |
qtreetbl->clear(): Clears the table so that it contains no keys.
tbl | qtreetbl_t container pointer. |
Definition at line 784 of file qtreetbl.c.
void qtreetbl_lock | ( | qtreetbl_t * | tbl | ) |
qtreetbl->lock(): Enter critical section.
tbl | qtreetbl_t container pointer. |
Definition at line 805 of file qtreetbl.c.
void qtreetbl_unlock | ( | qtreetbl_t * | tbl | ) |
qtreetbl->unlock(): Leave critical section.
tbl | qtreetbl_t container pointer. |
Definition at line 818 of file qtreetbl.c.
void qtreetbl_free | ( | qtreetbl_t * | tbl | ) |
qtreetbl->free(): De-allocate the table.
tbl | qtreetbl_t container pointer. |
Definition at line 827 of file qtreetbl.c.
int qtreetbl_byte_cmp | ( | const void * | name1, |
size_t | namesize1, | ||
const void * | name2, | ||
size_t | namesize2 | ||
) |
Definition at line 833 of file qtreetbl.c.
bool qtreetbl_debug | ( | qtreetbl_t * | tbl, |
FILE * | out | ||
) |
qtreetbl->debug(): Print the internal tree structure in text.
tbl | qtreetbl_t container pointer. |
out | output stream. |
errno | will be set in error condition.
|
[]
. In this example, 5 and 8 are Red nodes. Definition at line 871 of file qtreetbl.c.
int node_check_root | ( | qtreetbl_t * | tbl | ) |
Verifies that root property of the red-black tree is satisfied.
Root property: The root is black.
tbl | qtreetbl_t container pointer. |
Definition at line 890 of file qtreetbl.c.
int node_check_red | ( | qtreetbl_t * | tbl, |
qtreetbl_obj_t * | obj | ||
) |
Verifies that red property of the red-black tree is satisfied.
Red property: If a node is red, then both its children are black.
tbl | qtreetbl_t container pointer. |
obj | qtreetbl_obj_t object pointer. |
Definition at line 909 of file qtreetbl.c.
int node_check_black | ( | qtreetbl_t * | tbl, |
qtreetbl_obj_t * | obj, | ||
int * | path_len | ||
) |
Verifies that black property of the red-black tree is satisfied.
Black property: For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.
tbl | qtreetbl_t container pointer. |
obj | qtreetbl_obj_t object pointer. |
path_len | black path length. |
Definition at line 940 of file qtreetbl.c.
int node_check_llrb | ( | qtreetbl_t * | tbl, |
qtreetbl_obj_t * | obj | ||
) |
Verifies that LLRB property of the left-leaning red-black tree is satisfied.
LLRB property: 3-nodes always lean to the left and 4-nodes are balanced.
tbl | qtreetbl_t container pointer. |
obj | qtreetbl_obj_t object pointer. |
Definition at line 971 of file qtreetbl.c.
int qtreetbl_check | ( | qtreetbl_t * | tbl | ) |
Verifies that the invariants of the red-black tree are satisfied.
Root property: The root is black. Red property: If a node is red, then both its children are black. Black property: For each node, all simple paths from the node to descendant leaves contain the same number of black nodes. LLRB property: 3-nodes always lean to the left and 4-nodes are balanced.
tbl | qtreetbl_t container pointer. |
Definition at line 1001 of file qtreetbl.c.