Implementation of a radix tree. More...
Go to the source code of this file.
Functions | |
ldns_radix_t * | ldns_radix_create (void) |
Create a new radix tree. More... | |
void | ldns_radix_init (ldns_radix_t *tree) |
Initialize radix tree. More... | |
void | ldns_radix_free (ldns_radix_t *tree) |
Free radix tree. More... | |
ldns_status | ldns_radix_insert (ldns_radix_t *tree, uint8_t *key, radix_strlen_t len, void *data) |
Insert data into the tree. More... | |
void * | ldns_radix_delete (ldns_radix_t *tree, const uint8_t *key, radix_strlen_t len) |
Delete data from the tree. More... | |
ldns_radix_node_t * | ldns_radix_search (ldns_radix_t *tree, const uint8_t *key, radix_strlen_t len) |
Search data in the tree. More... | |
int | ldns_radix_find_less_equal (ldns_radix_t *tree, const uint8_t *key, radix_strlen_t len, ldns_radix_node_t **result) |
Search data in the tree, and if not found, find the closest smaller element in the tree. More... | |
ldns_radix_node_t * | ldns_radix_first (const ldns_radix_t *tree) |
Get the first element in the tree. More... | |
ldns_radix_node_t * | ldns_radix_last (const ldns_radix_t *tree) |
Get the last element in the tree. More... | |
ldns_radix_node_t * | ldns_radix_next (ldns_radix_node_t *node) |
Next element. More... | |
ldns_radix_node_t * | ldns_radix_prev (ldns_radix_node_t *node) |
Previous element. More... | |
void | ldns_radix_printf (FILE *fd, const ldns_radix_t *tree) |
Print radix tree. More... | |
ldns_status | ldns_radix_join (ldns_radix_t *tree1, ldns_radix_t *tree2) |
Join two radix trees. More... | |
ldns_status | ldns_radix_split (ldns_radix_t *tree1, size_t num, ldns_radix_t **tree2) |
Split a radix tree intwo. More... | |
void | ldns_radix_traverse_postorder (ldns_radix_node_t *node, void(*func)(ldns_radix_node_t *, void *), void *arg) |
Call function for all nodes in the tree, such that leaf nodes are called before parent nodes. More... | |
Implementation of a radix tree.
Definition in file radix.c.
ldns_radix_t* ldns_radix_create | ( | void | ) |
Create a new radix tree.
Allocate memory for it
Initialize it
Definition at line 114 of file radix.c.
References LDNS_MALLOC, and ldns_radix_init().
void ldns_radix_init | ( | ldns_radix_t * | tree | ) |
Initialize radix tree.
tree | uninitialized radix tree. |
Initialize it
Definition at line 134 of file radix.c.
References ldns_radix_t::count, and ldns_radix_t::root.
void ldns_radix_free | ( | ldns_radix_t * | tree | ) |
Free radix tree.
Free the radix tree.
Definition at line 150 of file radix.c.
References ldns_radix_traverse_postorder(), and ldns_radix_t::root.
ldns_status ldns_radix_insert | ( | ldns_radix_t * | tree, |
uint8_t * | key, | ||
radix_strlen_t | len, | ||
void * | data | ||
) |
Insert data into the tree.
tree | tree to insert to. |
key | key. |
len | length of key. |
data | data. |
Search the trie until we can make no further process.
No prefix found
Example 1: The root: | [0]
Example 2: 'dns': | [0] –| [d+ns] dns
Find some space in the array for the first byte
Set relational pointers
Store the remainder of the prefix
Exact match found
Prefix found
Find some space in the array for the byte.
Example 3: 'ldns' | [0] –| [d+ns] dns –| [l+dns] ldns
Create remainder of the string.
Add new node.
Use existing element.
Example 4: 'edns' | [0] –| [d+ns] dns –| [e+dns] edns –| [l+dns] ldns
Create remainder of the string.
Add new node.
Use existing element, but it has a shared prefix, we need a split.
Definition at line 168 of file radix.c.
References LDNS_STATUS_NULL.
void* ldns_radix_delete | ( | ldns_radix_t * | tree, |
const uint8_t * | key, | ||
radix_strlen_t | len | ||
) |
Delete data from the tree.
tree | tree to insert to. |
key | key. |
len | length of key. |
Definition at line 314 of file radix.c.
References ldns_radix_t::count, ldns_radix_node_t::data, and ldns_radix_search().
ldns_radix_node_t* ldns_radix_search | ( | ldns_radix_t * | tree, |
const uint8_t * | key, | ||
radix_strlen_t | len | ||
) |
Search data in the tree.
tree | tree to insert to. |
key | key. |
len | length of key. |
Must match additional string.
Definition at line 334 of file radix.c.
References ldns_radix_node_t::array, ldns_radix_node_t::data, ldns_radix_array_t::edge, ldns_radix_array_t::len, ldns_radix_node_t::len, ldns_radix_node_t::offset, ldns_radix_t::root, and ldns_radix_array_t::str.
int ldns_radix_find_less_equal | ( | ldns_radix_t * | tree, |
const uint8_t * | key, | ||
radix_strlen_t | len, | ||
ldns_radix_node_t ** | result | ||
) |
Search data in the tree, and if not found, find the closest smaller element in the tree.
tree | tree to insert to. | |
key | key. | |
len | length of key. | |
[out] | result | the radix node with the exact or closest match. NULL if the key is smaller than the smallest key in the tree. |
No exact match. The lesser is in this or the previous node.
No exact match. The lesser is in this node or the last of this array, or something before this node.
No exact match. Find the previous in the array from this index.
Must match additional string.
Additional string is longer than key.
Key is before this node.
Key is after additional string.
Exact match.
There is a node which is an exact match, but has no element.
Definition at line 380 of file radix.c.
References ldns_radix_t::root.
ldns_radix_node_t* ldns_radix_first | ( | const ldns_radix_t * | tree | ) |
Get the first element in the tree.
tree | tree. |
Definition at line 480 of file radix.c.
References ldns_radix_node_t::data, ldns_radix_next(), and ldns_radix_t::root.
ldns_radix_node_t* ldns_radix_last | ( | const ldns_radix_t * | tree | ) |
Get the last element in the tree.
tree | tree. |
Definition at line 499 of file radix.c.
References ldns_radix_t::root.
ldns_radix_node_t* ldns_radix_next | ( | ldns_radix_node_t * | node | ) |
Next element.
node | node. |
Go down: most-left child is the next.
No elements in subtree, get to parent and go down next branch.
Node itself.
Dive into subtree.
Definition at line 513 of file radix.c.
References ldns_radix_node_t::len.
ldns_radix_node_t* ldns_radix_prev | ( | ldns_radix_node_t * | node | ) |
Previous element.
node | node. |
Get to parent and go down previous branch.
Definition at line 554 of file radix.c.
References ldns_radix_node_t::len, ldns_radix_node_t::parent, and ldns_radix_node_t::parent_index.
void ldns_radix_printf | ( | FILE * | fd, |
const ldns_radix_t * | tree | ||
) |
Print radix tree.
Print radix tree (for debugging purposes).
Definition at line 624 of file radix.c.
References ldns_radix_t::root.
ldns_status ldns_radix_join | ( | ldns_radix_t * | tree1, |
ldns_radix_t * | tree2 | ||
) |
Join two radix trees.
tree1 | one tree. |
tree2 | another tree. |
Add all elements from tree2 into tree1.
Insert current node into tree1
Exist errors may occur
Definition at line 643 of file radix.c.
References ldns_radix_node_t::data, ldns_radix_node_t::key, ldns_radix_node_t::klen, ldns_radix_delete(), ldns_radix_first(), ldns_radix_insert(), ldns_radix_next(), LDNS_STATUS_EXISTS_ERR, LDNS_STATUS_NO_DATA, LDNS_STATUS_OK, and ldns_radix_t::root.
ldns_status ldns_radix_split | ( | ldns_radix_t * | tree1, |
size_t | num, | ||
ldns_radix_t ** | tree2 | ||
) |
Split a radix tree intwo.
Split radix tree intwo.
Delete current node from tree1.
Insert current node into tree2/
Update count; get first element from tree1 again.
Definition at line 682 of file radix.c.
References ldns_radix_node_t::data, ldns_radix_node_t::key, ldns_radix_node_t::klen, ldns_radix_create(), ldns_radix_delete(), ldns_radix_first(), ldns_radix_insert(), ldns_radix_next(), LDNS_STATUS_EXISTS_ERR, LDNS_STATUS_MEM_ERR, LDNS_STATUS_NO_DATA, LDNS_STATUS_NULL, LDNS_STATUS_OK, and ldns_radix_t::root.
void ldns_radix_traverse_postorder | ( | ldns_radix_node_t * | node, |
void(*)(ldns_radix_node_t *, void *) | func, | ||
void * | arg | ||
) |
Call function for all nodes in the tree, such that leaf nodes are called before parent nodes.
node | start node. |
func | function. |
arg | user argument. |
Call user function
Definition at line 740 of file radix.c.
References ldns_radix_node_t::array, ldns_radix_array_t::edge, and ldns_radix_node_t::len.