tree.h
Go to the documentation of this file.
1 /* vim: set ts=2 expandtab: */
2 /**
3  * @file tree.h
4  * @brief String-indexed search tree for Commotion object system.
5  *
6  * @author Josh King (jheretic), jking@chambana.net
7  *
8  * @internal
9  * Created 11/14/2013 10:43:57 PM
10  * Compiler gcc/g++
11  * Organization The Open Technology Institute
12  * Copyright Copyright (c) 2013, Josh King
13  *
14  * This file is part of Commotion, Copyright (c) 2013, Josh King
15  *
16  * Commotion is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Affero General Public License as
18  * published by the Free Software Foundation, either version 3 of the
19  * License, or (at your option) any later version.
20  *
21  * Commotion is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU Affero General Public License for more details.
25  *
26  * You should have received a copy of the GNU Affero General Public License
27  * along with Commotion. If not, see <http://www.gnu.org/licenses/>.
28  *
29  * =====================================================================================
30  */
31 #ifndef _TREE_H
32 #define _TREE_H
33 #include <stdlib.h>
34 #include <stddef.h>
35 #include <stdint.h>
36 #include "debug.h"
37 #include "extern/halloc.h"
38 
39 typedef struct _treenode_t _treenode_t;
40 
41 /**
42  * @struct _treenode_t a data structure that holds one node in search tree
43  */
45 {
46  char splitchar;
47  _treenode_t *low;
48  _treenode_t *equal;
49  _treenode_t *high;
50  co_obj_t *key;
51  co_obj_t *value;
52 } __attribute__((packed));
53 
54 /* Type "tree" declaration macros */
55 #define _DECLARE_TREE(L) typedef struct __attribute__((packed)) { co_obj_t _header; uint##L##_t _len; \
56  _treenode_t *root;} co_tree##L##_t; int co_tree##L##_alloc(\
57  co_obj_t *output); co_obj_t *co_tree##L##_create(void);
58 
59 _DECLARE_TREE(16);
60 _DECLARE_TREE(32);
61 
62 /**
63  * @brief return key object for given node
64  * @param node node to return object for
65  */
67 
68 /**
69  * @brief return value object for given node
70  * @param node node to return object for
71  */
73 
74 /**
75  * @brief find node in given tree
76  * @param root tree in which to search
77  * @param key key to search for
78  * @param klen length of key
79  */
80 _treenode_t *co_tree_find_node(_treenode_t *root, const char *key, const size_t klen);
81 
82 /**
83  * @brief return root node of tree object
84  * @param tree tree object that contains root
85  */
86 _treenode_t *co_tree_root(const co_obj_t *tree);
87 
88 /**
89  * @brief return length (number of key-value pairs) of given tree
90  * @param tree tree object
91  */
92 ssize_t co_tree_length(co_obj_t *tree);
93 
94 /**
95  * @brief return value from given tree that corresponds to key
96  * @param root tree object
97  * @param key key to search for
98  * @param klen length of key
99  */
100 co_obj_t *co_tree_find(const co_obj_t *root, const char *key, const size_t klen);
101 
102 /**
103  * @brief delete value from given tree that corresponds to key
104  * @param root tree object
105  * @param key key to search for
106  * @param klen length of key
107  */
108 co_obj_t *co_tree_delete(co_obj_t *root, const char *key, const size_t klen);
109 
110 /**
111  * @brief insert object into given tree and associate with key
112  * @param root tree object
113  * @param key key to search for
114  * @param klen length of key
115  * @param value value object to insert
116  */
117 int co_tree_insert(co_obj_t *root, const char *key, const size_t klen, co_obj_t *value);
118 
119 /**
120  * @brief insert object into given tree and associate with key, where value is not tied to tree
121  * @param root tree object
122  * @param key key to search for
123  * @param klen length of key
124  * @param value value object to insert
125  */
126 int co_tree_insert_unsafe(co_obj_t *root, const char *key, const size_t klen, co_obj_t *value);
127 
128 /**
129  * @brief insert object into given tree and associate with key (overwrite if it exists)
130  * @param root tree object
131  * @param key key to search for
132  * @param klen length of key
133  * @param value value object to insert
134  */
135 int co_tree_insert_force(co_obj_t *root, const char *key, const size_t klen, co_obj_t *value);
136 
137 /**
138  * @brief insert object into given tree and associate with key, where value is not tied to tree (overwrite if it exists)
139  * @param root tree object
140  * @param key key to search for
141  * @param klen length of key
142  * @param value value object to insert
143  */
144 int co_tree_insert_unsafe_force(co_obj_t *root, const char *key, const size_t klen, co_obj_t *value);
145 
146 /**
147  * @brief set value contained in an object in the tree with a specified key (if a string)
148  * @param root tree object
149  * @param key key to search for
150  * @param klen length of key
151  * @param value value to insert
152  * @param vlen length of value
153  */
154 int co_tree_set_str(co_obj_t *root, const char *key, const size_t klen, const char *value, const size_t vlen);
155 
156 /**
157  * @brief set value contained in an object in the tree with a specified key (if an int)
158  * @param root tree object
159  * @param key key to search for
160  * @param klen length of key
161  * @param value value to insert
162  */
163 int co_tree_set_int(co_obj_t *root, const char *key, const size_t klen, const signed long value);
164 
165 /**
166  * @brief set value contained in an object in the tree with a specified key (if an unsigned int)
167  * @param root tree object
168  * @param key key to search for
169  * @param klen length of key
170  * @param value value to insert
171  */
172 int co_tree_set_uint(co_obj_t *root, const char *key, const size_t klen, const unsigned long value);
173 
174 /**
175  * @brief set value contained in an object in the tree with a specified key (if a float)
176  * @param root tree object
177  * @param key key to search for
178  * @param klen length of key
179  * @param value value to insert
180  */
181 int co_tree_set_float(co_obj_t *root, const char *key, const size_t klen, const double value);
182 
183 /**
184  * @brief process tree with given iterator function
185  * @param tree tree object to process
186  * @param iter iterator function
187  * @param context additional arguments to iterator
188  */
189 int co_tree_process(co_obj_t *tree, const co_iter_t iter, void *context);
190 
191 /**
192  * @brief free tree structure
193  * @param root tree object to free
194  */
195 void co_tree_destroy(co_obj_t *root);
196 
197 /**
198  * @brief dump raw representation of tree
199  * @param output output buffer
200  * @param olen length of output buffer
201  * @param tree tree to dump
202  */
203 size_t co_tree_raw(char *output, const size_t olen, const co_obj_t *tree);
204 
205 /**
206  * @brief import raw representation of tree
207  * @param tree target pointer of new, imported tree object
208  * @param input input buffer
209  * @param ilen length of input buffer
210  */
211 size_t co_tree_import(co_obj_t **tree, const char *input, const size_t ilen);
212 
213 /**
214  * @brief print tree with indent
215  * @param tree tree object to print
216  * @param indent level of indent
217  */
218 void co_tree_print_indent(co_obj_t *tree, int indent);
219 
220 /**
221  * @brief print tree
222  * @param tree tree object to print
223  */
224 int co_tree_print(co_obj_t *tree);
225 
226 /**
227  * @brief print raw dump of tree
228  * @param tree tree object to print
229  */
230 int co_tree_print_raw(co_obj_t *tree);
231 
232 #endif
void co_tree_destroy(co_obj_t *root)
free tree structure
Definition: tree.c:575
co_obj_t * co_node_value(_treenode_t *node)
return value object for given node
Definition: tree.c:98
int co_tree_set_float(co_obj_t *root, const char *key, const size_t klen, const double value)
set value contained in an object in the tree with a specified key (if a float)
Definition: tree.c:524
co_obj_t * co_tree_find(const co_obj_t *root, const char *key, const size_t klen)
return value from given tree that corresponds to key
Definition: tree.c:173
_treenode_t * co_tree_find_node(_treenode_t *root, const char *key, const size_t klen)
find node in given tree
Definition: tree.c:140
ssize_t co_tree_length(co_obj_t *tree)
return length (number of key-value pairs) of given tree
Definition: tree.c:122
int co_tree_process(co_obj_t *tree, const co_iter_t iter, void *context)
process tree with given iterator function
Definition: tree.c:555
int co_tree_print_raw(co_obj_t *tree)
print raw dump of tree
Definition: tree.c:858
debug macros for the Commotion daemon
int co_tree_set_str(co_obj_t *root, const char *key, const size_t klen, const char *value, const size_t vlen)
set value contained in an object in the tree with a specified key (if a string)
Definition: tree.c:408
int co_tree_insert(co_obj_t *root, const char *key, const size_t klen, co_obj_t *value)
insert object into given tree and associate with key
Definition: tree.c:332
void co_tree_print_indent(co_obj_t *tree, int indent)
print tree with indent
Definition: tree.c:809
Definition: tree.h:44
int co_tree_set_int(co_obj_t *root, const char *key, const size_t klen, const signed long value)
set value contained in an object in the tree with a specified key (if an int)
Definition: tree.c:449
int co_tree_insert_unsafe_force(co_obj_t *root, const char *key, const size_t klen, co_obj_t *value)
insert object into given tree and associate with key, where value is not tied to tree (overwrite if i...
Definition: tree.c:358
co_obj_t * co_tree_delete(co_obj_t *root, const char *key, const size_t klen)
delete value from given tree that corresponds to key
Definition: tree.c:227
co_obj_t * co_node_key(_treenode_t *node)
return key object for given node
Definition: tree.c:89
_treenode_t * co_tree_root(const co_obj_t *tree)
return root node of tree object
Definition: tree.c:69
Definition: obj.h:131
size_t co_tree_import(co_obj_t **tree, const char *input, const size_t ilen)
import raw representation of tree
Definition: tree.c:659
size_t co_tree_raw(char *output, const size_t olen, const co_obj_t *tree)
dump raw representation of tree
Definition: tree.c:623
int co_tree_insert_unsafe(co_obj_t *root, const char *key, const size_t klen, co_obj_t *value)
insert object into given tree and associate with key, where value is not tied to tree ...
Definition: tree.c:342
int co_tree_insert_force(co_obj_t *root, const char *key, const size_t klen, co_obj_t *value)
insert object into given tree and associate with key (overwrite if it exists)
Definition: tree.c:352
int co_tree_print(co_obj_t *tree)
print tree
Definition: tree.c:822
int co_tree_set_uint(co_obj_t *root, const char *key, const size_t klen, const unsigned long value)
set value contained in an object in the tree with a specified key (if an unsigned int) ...
Definition: tree.c:490