/build/ldns-Ls6SRU/ldns-1.8.4/radix.c File Reference

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.
void | ldns_radix_init (ldns_radix_t *tree)
| Initialize radix tree.
void | ldns_radix_free (ldns_radix_t *tree)
| Free radix tree.
ldns_status | ldns_radix_insert (ldns_radix_t *tree, uint8_t *key, radix_strlen_t len, void *data)
| Insert data into the tree.
void * | ldns_radix_delete (ldns_radix_t *tree, const uint8_t *key, radix_strlen_t len)
| Delete data from the tree.
ldns_radix_node_t * | ldns_radix_search (ldns_radix_t *tree, const uint8_t *key, radix_strlen_t len)
| Search data in the tree.
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.
ldns_radix_node_t * | ldns_radix_first (const ldns_radix_t *tree)
| Get the first element in the tree.
ldns_radix_node_t * | ldns_radix_last (const ldns_radix_t *tree)
| Get the last element in the tree.
ldns_radix_node_t * | ldns_radix_next (ldns_radix_node_t *node)
| Next element.
ldns_radix_node_t * | ldns_radix_prev (ldns_radix_node_t *node)
| Previous element.
void | ldns_radix_printf (FILE *fd, const ldns_radix_t *tree)
| Print radix tree.
ldns_status | ldns_radix_join (ldns_radix_t *tree1, ldns_radix_t *tree2)
| Join two radix trees.
ldns_status | ldns_radix_split (ldns_radix_t *tree1, size_t num, ldns_radix_t **tree2)
| Split a radix tree intwo.
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.

## Detailed Description

Implementation of a radix tree.

Definition in file radix.c.

## Function Documentation

## ldns_radix_create()

ldns_radix_t * ldns_radix_create | ( | void |  | ) |

Create a new radix tree.

Returns
  : new radix tree.

Allocate memory for it

Initialize it

Definition at line 114 of file radix.c.

References LDNS_MALLOC, and ldns_radix_init().

## ldns_radix_init()

void ldns_radix_init | ( | ldns_radix_t * | tree | ) |

Initialize radix tree.

Parameters

  tree | uninitialized radix tree.

Initialize it

Definition at line 134 of file radix.c.

References ldns_radix_t::count, and ldns_radix_t::root.

## ldns_radix_free()

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_FREE, ldns_radix_traverse_postorder(), and ldns_radix_t::root.

## ldns_radix_insert()

ldns_status ldns_radix_insert | ( | ldns_radix_t * | tree,
|  | uint8_t * | key,
|  | radix_strlen_t | len,
|  | void * | data )

Insert data into the tree.

Parameters

  tree | tree to insert to.
  key | key.
  len | length of key.
  data | data.

Returns
  : status.

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_radix_node_t::array, ldns_radix_t::count, ldns_radix_node_t::data, ldns_radix_array_t::edge, ldns_radix_node_t::key, ldns_radix_node_t::klen, LDNS_FREE, LDNS_STATUS_EXISTS_ERR, LDNS_STATUS_MEM_ERR, LDNS_STATUS_NULL, LDNS_STATUS_OK, ldns_radix_array_t::len, ldns_radix_node_t::len, ldns_radix_node_t::offset, ldns_radix_node_t::parent, ldns_radix_node_t::parent_index, ldns_radix_t::root, and ldns_radix_array_t::str.

## ldns_radix_delete()

void * ldns_radix_delete | ( | ldns_radix_t * | tree,
|  | const uint8_t * | key,
|  | radix_strlen_t | len )

Delete data from the tree.

Parameters

  tree | tree to insert to.
  key | key.
  len | length of key.

Returns
  : unlinked data or NULL if not present.

Definition at line 314 of file radix.c.

References ldns_radix_t::count, ldns_radix_node_t::data, and ldns_radix_search().

## 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.

Parameters

  tree | tree to insert to.
  key | key.
  len | length of key.

Returns
  : the radix node or NULL if not found.

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.

## ldns_radix_find_less_equal()

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.

Parameters

  | 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.

Returns
  1 if exact match, 0 otherwise.

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_node_t::array, ldns_radix_node_t::data, ldns_radix_array_t::edge, ldns_radix_prev(), 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.

## ldns_radix_first()

ldns_radix_node_t * ldns_radix_first | ( | const ldns_radix_t * | tree | ) |

Get the first element in the tree.

Parameters

  tree | tree.

Returns
  : the radix node with the first element.

Definition at line 480 of file radix.c.

References ldns_radix_node_t::data, ldns_radix_next(), and ldns_radix_t::root.

## ldns_radix_last()

ldns_radix_node_t * ldns_radix_last | ( | const ldns_radix_t * | tree | ) |

Get the last element in the tree.

Parameters

  tree | tree.

Returns
  : the radix node with the last element.

Definition at line 499 of file radix.c.

References ldns_radix_t::root.

## ldns_radix_next()

ldns_radix_node_t * ldns_radix_next | ( | ldns_radix_node_t * | node | ) |

Next element.

Parameters

  node | node.

Returns
  : node with next element.

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::array, ldns_radix_node_t::data, ldns_radix_array_t::edge, ldns_radix_node_t::len, ldns_radix_node_t::parent, and ldns_radix_node_t::parent_index.

## ldns_radix_prev()

ldns_radix_node_t * ldns_radix_prev | ( | ldns_radix_node_t * | node | ) |

Previous element.

Parameters

  node | node.

Returns
  : node with previous element.

Get to parent and go down previous branch.

Definition at line 554 of file radix.c.

References ldns_radix_node_t::data, ldns_radix_node_t::len, ldns_radix_node_t::parent, and ldns_radix_node_t::parent_index.

## ldns_radix_printf()

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_radix_join()

ldns_status ldns_radix_join | ( | ldns_radix_t * | tree1,
|  | ldns_radix_t * | tree2 )

Join two radix trees.

Parameters

  tree1 | one tree.
  tree2 | another tree.

Returns
  : status.

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_radix_split()

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.

## ldns_radix_traverse_postorder()

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.

Parameters

  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, ldns_radix_traverse_postorder(), and ldns_radix_node_t::len.