ldns documentation

- Main Page
- Related Pages
- Data Structures
- Files

- File List
- Globals

radix.h
Go to the documentation of this file.
1 /*
2 * radix.h -- generic radix tree
3 *
4 * Copyright (c) 2012, NLnet Labs. All rights reserved.
5 *
6 * This software is open source.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 *
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
43 #ifndef LDNS_RADIX_H_
44 #define LDNS_RADIX_H_
45
46 #include <ldns/error.h>
47
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51
52 typedef uint16_t radix_strlen_t;
53 typedef struct ldns_radix_array_t ldns_radix_array_t;
54 typedef struct ldns_radix_node_t ldns_radix_node_t;
55 typedef struct ldns_radix_t ldns_radix_t;
56
58 struct ldns_radix_array_t {
60 uint8_t* str;
62 radix_strlen_t len;
64 ldns_radix_node_t* edge;
65 };
66
68 struct ldns_radix_node_t {
70 uint8_t* key;
72 radix_strlen_t klen;
74 void* data;
76 ldns_radix_node_t* parent;
78 uint8_t parent_index;
80 uint16_t len;
82 uint16_t offset;
84 uint16_t capacity;
86 ldns_radix_array_t* array;
87 };
88
90 struct ldns_radix_t {
92 ldns_radix_node_t* root;
94 size_t count;
95 };
96
102 ldns_radix_t* ldns_radix_create(void);
103
109 void ldns_radix_init(ldns_radix_t* tree);
110
116 void ldns_radix_free(ldns_radix_t* tree);
117
127 ldns_status ldns_radix_insert(ldns_radix_t* tree, uint8_t* key,
128 radix_strlen_t len, void* data);
129
138 void* ldns_radix_delete(ldns_radix_t* tree, uint8_t* key, radix_strlen_t len);
139
148 ldns_radix_node_t* ldns_radix_search(ldns_radix_t* tree, uint8_t* key,
149 radix_strlen_t len);
150
162 int ldns_radix_find_less_equal(ldns_radix_t* tree, uint8_t* key,
163 radix_strlen_t len, ldns_radix_node_t** result);
164
171 ldns_radix_node_t* ldns_radix_first(ldns_radix_t* tree);
172
179 ldns_radix_node_t* ldns_radix_last(ldns_radix_t* tree);
180
187 ldns_radix_node_t* ldns_radix_next(ldns_radix_node_t* node);
188
195 ldns_radix_node_t* ldns_radix_prev(ldns_radix_node_t* node);
196
205 ldns_status ldns_radix_split(ldns_radix_t* tree1, size_t num,
206 ldns_radix_t** tree2);
207
215 ldns_status ldns_radix_join(ldns_radix_t* tree1, ldns_radix_t* tree2);
216
225 void ldns_radix_traverse_postorder(ldns_radix_node_t* node,
226 void (*func)(ldns_radix_node_t*, void*), void* arg);
227
234 void ldns_radix_printf(FILE* fd, ldns_radix_t* tree);
235
236 #ifdef __cplusplus
237 }
238 #endif
239
240 #endif /* LDNS_RADIX_H_ */
ldns_radix_node_t::capacity
uint16_t capacity
Capacity of the array.
Definition: radix.h:84
ldns_radix_array_t::edge
ldns_radix_node_t * edge
Node that deals with byte+str.
Definition: radix.h:64
ldns_radix_node_t::klen
radix_strlen_t klen
Key length corresponding to this node.
Definition: radix.h:72
ldns_radix_find_less_equal
int ldns_radix_find_less_equal(ldns_radix_t *tree, 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...
Definition: radix.c:380
ldns_radix_create
ldns_radix_t * ldns_radix_create(void)
Create a new radix tree.
Definition: radix.c:114
ldns_radix_prev
ldns_radix_node_t * ldns_radix_prev(ldns_radix_node_t *node)
Previous element.
Definition: radix.c:554
ldns_radix_node_t::array
ldns_radix_array_t * array
Select edge array.
Definition: radix.h:86
ldns_radix_t::count
size_t count
Number of nodes in tree.
Definition: radix.h:94
ldns_radix_array_t
Radix node select edge array.
Definition: radix.h:58
ldns_radix_node_t::data
void * data
Data corresponding to this node.
Definition: radix.h:74
ldns_radix_delete
void * ldns_radix_delete(ldns_radix_t *tree, uint8_t *key, radix_strlen_t len)
Delete data from the tree.
Definition: radix.c:314
ldns_radix_t
An entire radix tree.
Definition: radix.h:90
ldns_radix_search
ldns_radix_node_t * ldns_radix_search(ldns_radix_t *tree, uint8_t *key, radix_strlen_t len)
Search data in the tree.
Definition: radix.c:334
ldns_radix_node_t::parent
ldns_radix_node_t * parent
Parent node.
Definition: radix.h:76
error.h
Defines error numbers and functions to translate those to a readable string.
ldns_radix_node_t::offset
uint16_t offset
Offset of the array.
Definition: radix.h:82
ldns_radix_array_t::str
uint8_t * str
Additional string after the selection byte for this edge.
Definition: radix.h:60
radix_strlen_t
uint16_t radix_strlen_t
Definition: radix.h:52
ldns_radix_join
ldns_status ldns_radix_join(ldns_radix_t *tree1, ldns_radix_t *tree2)
Join two radix trees.
Definition: radix.c:643
ldns_radix_split
ldns_status ldns_radix_split(ldns_radix_t *tree1, size_t num, ldns_radix_t **tree2)
Split radix tree intwo.
Definition: radix.c:682
ldns_radix_printf
void ldns_radix_printf(FILE *fd, ldns_radix_t *tree)
Print radix tree (for debugging purposes).
Definition: radix.c:624
ldns_radix_last
ldns_radix_node_t * ldns_radix_last(ldns_radix_t *tree)
Get the last element in the tree.
Definition: radix.c:499
ldns_radix_node_t::len
uint16_t len
Length of the array.
Definition: radix.h:80
ldns_radix_first
ldns_radix_node_t * ldns_radix_first(ldns_radix_t *tree)
Get the first element in the tree.
Definition: radix.c:480
ldns_radix_init
void ldns_radix_init(ldns_radix_t *tree)
Initialize radix tree.
Definition: radix.c:134
ldns_status
enum ldns_enum_status ldns_status
Definition: error.h:131
ldns_radix_free
void ldns_radix_free(ldns_radix_t *tree)
Free the radix tree.
Definition: radix.c:150
ldns_radix_node_t::key
uint8_t * key
Key corresponding to this node.
Definition: radix.h:70
ldns_radix_node_t
A node in a radix tree.
Definition: radix.h:68
ldns_radix_next
ldns_radix_node_t * ldns_radix_next(ldns_radix_node_t *node)
Next element.
Definition: radix.c:513
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.
Definition: radix.c:168
ldns_radix_array_t::len
radix_strlen_t len
Length of additional string for this edge.
Definition: radix.h:62
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...
Definition: radix.c:740
ldns_radix_t::root
ldns_radix_node_t * root
Root.
Definition: radix.h:92
ldns_radix_node_t::parent_index
uint8_t parent_index
Index in the the parent node select edge array.
Definition: radix.h:78