C Standard Library Extensions 1.2.4
|
Typedefs | |
typedef cxbool(* | cx_tree_compare_func) (cxcptr, cxcptr) |
The tree's key comparison operator function. More... | |
Functions | |
cx_tree_iterator | cx_tree_begin (const cx_tree *tree) |
Get an iterator to the first pair in the tree. More... | |
cx_tree_iterator | cx_tree_end (const cx_tree *tree) |
Get an iterator for the position after the last pair in the tree. More... | |
cx_tree_iterator | cx_tree_next (const cx_tree *tree, cx_tree_const_iterator position) |
Get an iterator for the next pair in the tree. More... | |
cx_tree_iterator | cx_tree_previous (const cx_tree *tree, cx_tree_const_iterator position) |
Get an iterator for the previous pair in the tree. More... | |
void | cx_tree_clear (cx_tree *tree) |
Remove all pairs from a tree. More... | |
cxbool | cx_tree_empty (const cx_tree *tree) |
Check whether a tree is empty. More... | |
cx_tree * | cx_tree_new (cx_tree_compare_func compare, cx_free_func key_destroy, cx_free_func value_destroy) |
Create a new tree without any elements. More... | |
void | cx_tree_delete (cx_tree *tree) |
Destroy a tree and all its elements. More... | |
cxsize | cx_tree_size (const cx_tree *tree) |
Get the actual number of pairs in the tree. More... | |
cxsize | cx_tree_max_size (const cx_tree *tree) |
Get the maximum number of pairs possible. More... | |
cx_tree_compare_func | cx_tree_key_comp (const cx_tree *tree) |
Get the key comparison function. More... | |
void | cx_tree_swap (cx_tree *tree1, cx_tree *tree2) |
Swap the contents of two trees. More... | |
cxptr | cx_tree_assign (cx_tree *tree, cx_tree_iterator position, cxcptr data) |
Assign data to an iterator position. More... | |
cxptr | cx_tree_get_key (const cx_tree *tree, cx_tree_const_iterator position) |
Get the key from a given iterator position. More... | |
cxptr | cx_tree_get_value (const cx_tree *tree, cx_tree_const_iterator position) |
Get the data from a given iterator position. More... | |
cx_tree_iterator | cx_tree_find (const cx_tree *tree, cxcptr key) |
Locate an element in the tree. More... | |
cx_tree_iterator | cx_tree_lower_bound (const cx_tree *tree, cxcptr key) |
Find the beginning of a subsequence. More... | |
cx_tree_iterator | cx_tree_upper_bound (const cx_tree *tree, cxcptr key) |
Find the end of a subsequence. More... | |
void | cx_tree_equal_range (const cx_tree *tree, cxcptr key, cx_tree_iterator *begin, cx_tree_iterator *end) |
Find a subsequence matching a given key. More... | |
cxsize | cx_tree_count (const cx_tree *tree, cxcptr key) |
Get the number of elements matching a key. More... | |
cx_tree_iterator | cx_tree_insert_unique (cx_tree *tree, cxcptr key, cxcptr data) |
Attempt to insert data into a tree. More... | |
cx_tree_iterator | cx_tree_insert_equal (cx_tree *tree, cxcptr key, cxcptr data) |
Insert data into a tree. More... | |
void | cx_tree_erase_position (cx_tree *tree, cx_tree_iterator position) |
Erase an element from a tree. More... | |
void | cx_tree_erase_range (cx_tree *tree, cx_tree_iterator begin, cx_tree_iterator end) |
Erase a range of elements from a tree. More... | |
cxsize | cx_tree_erase (cx_tree *tree, cxcptr key) |
Erase all elements from a tree matching the provided key. More... | |
cxbool | cx_tree_verify (const cx_tree *tree) |
Validate a tree. More... | |
The module implements a balanced binary tree type, i.e. a container managing key/value pairs as elements. The container is optimized for lookup operations.
typedef cxbool(* cx_tree_compare_func) (cxcptr, cxcptr) |
The tree's key comparison operator function.
This type of function is used by a tree internally to compare the keys of its elements. A key comparison operator returns TRUE
if the comparison of its first argument with the second argument succeeds, and FALSE
otherwise, as, for instance, the logical operators <, >, ==, and != do.
Examples:
cxptr cx_tree_assign | ( | cx_tree * | tree, |
cx_tree_iterator | position, | ||
cxcptr | data | ||
) |
Assign data to an iterator position.
tree | A tree. |
position | Iterator positions where the data will be stored. |
data | Data to store. |
The function assigns a data object reference data to the iterator position position of the tree tree.
Referenced by cx_map_assign(), cx_map_put(), and cx_multimap_assign().
cx_tree_iterator cx_tree_begin | ( | const cx_tree * | tree | ) |
Get an iterator to the first pair in the tree.
tree | The tree to query. |
The function returns a handle for the first pair in the tree tree. The returned iterator cannot be used directly to access the value field of the key/value pair, but only through the appropriate methods.
Referenced by cx_map_begin(), and cx_multimap_begin().
void cx_tree_clear | ( | cx_tree * | tree | ) |
Remove all pairs from a tree.
tree | Tree to be cleared. |
The tree tree is cleared, i.e. all pairs are removed from the tree. Keys and values are destroyed using the key and value destructors set up during tree creation. After calling this function the tree is empty.
Referenced by cx_map_clear(), and cx_multimap_clear().
cxsize cx_tree_count | ( | const cx_tree * | tree, |
cxcptr | key | ||
) |
Get the number of elements matching a key.
tree | A tree. |
key | Key of the (key, value) pair(s) to locate. |
Counts all elements of the tree tree matching the key key.
Referenced by cx_multimap_count().
void cx_tree_delete | ( | cx_tree * | tree | ) |
Destroy a tree and all its elements.
tree | The tree to destroy. |
The tree tree is deallocated. All data values and keys are deallocated using the tree's key and value destructor. If no key and/or value destructor was set when the tree was created the keys and the stored data values are left untouched. In this case the key and value deallocation is the responsibility of the user.
Referenced by cx_map_delete(), and cx_multimap_delete().
cxbool cx_tree_empty | ( | const cx_tree * | tree | ) |
Check whether a tree is empty.
tree | A tree. |
TRUE
if the tree is empty, and FALSE
otherwise.The function checks if the tree contains any pairs. Calling this function is equivalent to the statement:
Referenced by cx_map_empty(), and cx_multimap_empty().
cx_tree_iterator cx_tree_end | ( | const cx_tree * | tree | ) |
Get an iterator for the position after the last pair in the tree.
tree | The tree to query. |
The function returns an iterator for the position one past the last pair in the tree tree. The iteration is done in ascending order according to the keys. The returned iterator cannot be used directly to access the value field of the key/value pair, but only through the appropriate methods.
Referenced by cx_map_count(), cx_map_end(), cx_map_get(), cx_map_put(), and cx_multimap_end().
void cx_tree_equal_range | ( | const cx_tree * | tree, |
cxcptr | key, | ||
cx_tree_iterator * | begin, | ||
cx_tree_iterator * | end | ||
) |
Find a subsequence matching a given key.
tree | A tree. |
key | The key of the (key, value) pair(s) to be located. |
begin | First element with key key. |
end | Last element with key key. |
The function returns the beginning and the end of a subsequence of tree elements with the key key through through the begin and end arguments. After calling this function begin possibly points to the first element of tree matching the key key and end possibly points to the last element of the sequence. If key is not present in the tree begin points to the next greater element or, if no such element exists, to cx_tree_end().
Referenced by cx_map_equal_range(), and cx_multimap_equal_range().
cxsize cx_tree_erase | ( | cx_tree * | tree, |
cxcptr | key | ||
) |
Erase all elements from a tree matching the provided key.
tree | A tree. |
key | Key of the element to be erased. |
This function erases all elements with the specified key key, from tree. Key and value associated with the erased pairs are deallocated using the tree's key and value destructors, provided they have been set.
Referenced by cx_map_erase(), and cx_multimap_erase().
void cx_tree_erase_position | ( | cx_tree * | tree, |
cx_tree_iterator | position | ||
) |
Erase an element from a tree.
tree | A tree. |
position | Iterator position of the element to be erased. |
This function erases an element, specified by the iterator position, from tree. Key and value associated with the erased pair are deallocated using the tree's key and value destructors, provided they have been set.
Referenced by cx_map_erase_position(), and cx_multimap_erase_position().
void cx_tree_erase_range | ( | cx_tree * | tree, |
cx_tree_iterator | begin, | ||
cx_tree_iterator | end | ||
) |
Erase a range of elements from a tree.
tree | A tree. |
begin | Iterator pointing to the start of the range to erase. |
end | Iterator pointing to the end of the range to erase. |
This function erases all elements in the range [begin, end) from the tree tree. Key and value associated with the erased pair(s) are deallocated using the tree's key and value destructors, provided they have been set.
Referenced by cx_map_erase_range(), and cx_multimap_erase_range().
cx_tree_iterator cx_tree_find | ( | const cx_tree * | tree, |
cxcptr | key | ||
) |
Locate an element in the tree.
tree | A tree. |
key | Key of the (key, value) pair to locate. |
The function searches the tree tree for an element with a key matching key. If the search was successful an iterator to the sought-after pair is returned. If the search did not succeed, i.e. key is not present in the tree, a one past the end iterator is returned.
Referenced by cx_map_count(), cx_map_find(), and cx_multimap_find().
cxptr cx_tree_get_key | ( | const cx_tree * | tree, |
cx_tree_const_iterator | position | ||
) |
Get the key from a given iterator position.
tree | A tree. |
position | Iterator position the data is retrieved from. |
The function returns a reference to the key associated with the iterator position position in the tree tree.
Referenced by cx_map_get(), cx_map_get_key(), and cx_multimap_get_key().
cxptr cx_tree_get_value | ( | const cx_tree * | tree, |
cx_tree_const_iterator | position | ||
) |
Get the data from a given iterator position.
tree | A tree. |
position | Iterator position the data is retrieved from. |
The function returns a reference to the data stored at iterator position position in the tree tree.
Referenced by cx_map_get(), cx_map_get_value(), and cx_multimap_get_value().
cx_tree_iterator cx_tree_insert_equal | ( | cx_tree * | tree, |
cxcptr | key, | ||
cxcptr | data | ||
) |
Insert data into a tree.
tree | A tree. |
key | Key used to store the data. |
data | Data to insert. |
This function inserts a (key, value) pair into the tree tree. Contrary to cx_tree_insert_unique() the key key used for inserting data may already be present in the tree.
Referenced by cx_multimap_insert().
cx_tree_iterator cx_tree_insert_unique | ( | cx_tree * | tree, |
cxcptr | key, | ||
cxcptr | data | ||
) |
Attempt to insert data into a tree.
tree | A tree. |
key | Key used to store the data. |
data | Data to insert. |
NULL
if the pair could not be inserted.This function attempts to insert a (key, value) pair into the tree tree. The insertion fails if the key already present in the tree, i.e. if the key is not unique.
Referenced by cx_map_get(), cx_map_insert(), and cx_map_put().
cx_tree_compare_func cx_tree_key_comp | ( | const cx_tree * | tree | ) |
Get the key comparison function.
tree | The tree to query. |
The function retrieves the function used by the tree methods for comparing keys. The key comparison function is set during tree creation.
Referenced by cx_map_get(), cx_map_key_comp(), and cx_multimap_key_comp().
cx_tree_iterator cx_tree_lower_bound | ( | const cx_tree * | tree, |
cxcptr | key | ||
) |
Find the beginning of a subsequence.
tree | A tree. |
key | Key of the (key, value) pair(s) to locate. |
The function returns the first element of a subsequence of elements in the tree that match the given key key. If key is not present in the tree tree an iterator pointing to the first element that has a greater key than key or cx_tree_end() if no such element exists.
Referenced by cx_map_get(), cx_map_lower_bound(), cx_map_put(), and cx_multimap_lower_bound().
cxsize cx_tree_max_size | ( | const cx_tree * | tree | ) |
Get the maximum number of pairs possible.
tree | A tree. |
Retrieves the tree's capacity, i.e. the maximum possible number of pairs a tree can manage.
Referenced by cx_map_max_size(), and cx_multimap_max_size().
cx_tree * cx_tree_new | ( | cx_tree_compare_func | compare, |
cx_free_func | key_destroy, | ||
cx_free_func | value_destroy | ||
) |
Create a new tree without any elements.
compare | Function used to compare keys. |
key_destroy | Destructor for the keys. |
value_destroy | Destructor for the value field. |
Memory for a new tree is allocated and the tree is initialized to be a valid empty tree.
The tree's key comparison function is set to compare. It must return TRUE
or FALSE
if the comparison of the first argument passed to it with the second argument is found to be true or false respectively.
The destructors for a tree node's key and value field are set to key_destroy and value_destroy. Whenever a tree node is destroyed these functions are used to deallocate the memory used by the key and the value. Each of the destructors might be NULL
, i.e. keys and values are not deallocated during destroy operations.
References cx_malloc().
Referenced by cx_map_new(), and cx_multimap_new().
cx_tree_iterator cx_tree_next | ( | const cx_tree * | tree, |
cx_tree_const_iterator | position | ||
) |
Get an iterator for the next pair in the tree.
tree | A tree. |
position | Current iterator position. |
The function returns an iterator for the next pair in the tree tree with respect to the current iterator position position. Iteration is done in ascending order according to the keys. If the tree is empty or position points to the end of the tree the function returns cx_tree_end().
Referenced by cx_map_next(), and cx_multimap_next().
cx_tree_iterator cx_tree_previous | ( | const cx_tree * | tree, |
cx_tree_const_iterator | position | ||
) |
Get an iterator for the previous pair in the tree.
tree | A tree. |
position | Current iterator position. |
The function returns an iterator for the previous pair in the tree tree with respect to the current iterator position position. Iteration is done in ascending order according to the keys. If the tree is empty or position points to the beginning of the tree the function returns cx_tree_end().
Referenced by cx_map_previous(), and cx_multimap_previous().
cxsize cx_tree_size | ( | const cx_tree * | tree | ) |
Get the actual number of pairs in the tree.
tree | A tree. |
Retrieves the current number of pairs stored in the tree.
Referenced by cx_map_size(), and cx_multimap_size().
void cx_tree_swap | ( | cx_tree * | tree1, |
cx_tree * | tree2 | ||
) |
Swap the contents of two trees.
tree1 | First tree. |
tree2 | Second tree. |
All pairs stored in the first tree tree1 are moved to the second tree tree2, while the pairs from tree2 are moved to tree1. Also the key comparison function, the key and the value destructor are exchanged.
Referenced by cx_map_swap(), and cx_multimap_swap().
cx_tree_iterator cx_tree_upper_bound | ( | const cx_tree * | tree, |
cxcptr | key | ||
) |
Find the end of a subsequence.
tree | A tree. |
key | Key of the (key, value) pair(s) to locate. |
The function returns the last element of a subsequence of elements in the tree that match the given key key. If key is not present in the tree tree an iterator pointing to the first element that has a greater key than key or cx_tree_end() if no such element exists.
Referenced by cx_map_upper_bound(), and cx_multimap_upper_bound().
cxbool cx_tree_verify | ( | const cx_tree * | tree | ) |
Validate a tree.
tree | The tree to verify. |
TRUE
if the tree is valid, or FALSE
otherwise.The function is provided for debugging purposes. It verifies that the internal tree structure of tree is valid.