source: mainline/uspace/app/bithenge/tree.c@ e8e31d9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e8e31d9 was e8e31d9, checked in by Sean Bartell <wingedtachikoma@…>, 13 years ago

Bithenge: add bithenge_node_get

  • Property mode set to 100644
File size: 9.2 KB
Line 
1/*
2 * Copyright (c) 2012 Sean Bartell
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup bithenge
30 * @{
31 */
32/**
33 * @file
34 * Trees and nodes.
35 */
36
37#include <errno.h>
38#include <stdlib.h>
39#include "blob.h"
40#include "os.h"
41#include "tree.h"
42
43static void blob_destroy(bithenge_node_t *base)
44{
45 bithenge_blob_t *self = bithenge_node_as_blob(base);
46 assert(self->base.blob_ops);
47 self->base.blob_ops->destroy(self);
48}
49
50static void node_destroy(bithenge_node_t *self)
51{
52 switch (bithenge_node_type(self)) {
53 case BITHENGE_NODE_BLOB:
54 blob_destroy(self);
55 return;
56 case BITHENGE_NODE_STRING:
57 if (self->string_value.needs_free)
58 free((void *)self->string_value.ptr);
59 break;
60 case BITHENGE_NODE_INTERNAL:
61 self->internal_ops->destroy(self);
62 return;
63 case BITHENGE_NODE_BOOLEAN:
64 return; /* The boolean nodes are allocated statically below. */
65 case BITHENGE_NODE_INTEGER: /* pass-through */
66 break;
67 }
68 free(self);
69}
70
71/** Decrement a node's reference count and free it if appropriate.
72 * @memberof bithenge_node_t
73 * @param node The node to dereference, or NULL. */
74void bithenge_node_dec_ref(bithenge_node_t *node)
75{
76 if (!node)
77 return;
78 if (--node->refs == 0)
79 node_destroy(node);
80}
81
82typedef struct {
83 bithenge_node_t *key;
84 bithenge_node_t **out;
85} get_for_each_data_t;
86
87static int get_for_each_func(bithenge_node_t *key, bithenge_node_t *value,
88 void *raw_data)
89{
90 get_for_each_data_t *data = (get_for_each_data_t *)raw_data;
91 bool equal = bithenge_node_equal(key, data->key);
92 bithenge_node_dec_ref(key);
93 if (equal) {
94 *data->out = value;
95 return EEXIST;
96 }
97 bithenge_node_dec_ref(value);
98 return EOK;
99}
100
101/** Get a child of a node. Takes ownership of the key. If the node does not
102 * provide this function, for_each will be used as an alternative, which may be
103 * very slow.
104 * @memberof bithenge_node_t
105 * @param self The internal node to find a child of.
106 * @param key The key to search for.
107 * @param[out] out Holds the found node.
108 * @return EOK on success, ENOENT if not found, or another error code from
109 * errno.h. */
110int bithenge_node_get(bithenge_node_t *self, bithenge_node_t *key,
111 bithenge_node_t **out)
112{
113 assert(self->type == BITHENGE_NODE_INTERNAL);
114 if (self->internal_ops->get)
115 return self->internal_ops->get(self, key, out);
116 *out = NULL;
117 get_for_each_data_t data = {key, out};
118 int rc = bithenge_node_for_each(self, get_for_each_func, &data);
119 bithenge_node_dec_ref(key);
120 if (rc == EEXIST && *out)
121 return EOK;
122 if (rc == EOK)
123 rc = ENOENT;
124 bithenge_node_dec_ref(*out);
125 return rc;
126}
127
128typedef struct
129{
130 bithenge_node_t base;
131 bithenge_node_t **nodes;
132 bithenge_int_t len;
133 bool needs_free;
134} simple_internal_node_t;
135
136static simple_internal_node_t *node_as_simple(bithenge_node_t *node)
137{
138 return (simple_internal_node_t *)node;
139}
140
141static bithenge_node_t *simple_as_node(simple_internal_node_t *node)
142{
143 return &node->base;
144}
145
146static int simple_internal_node_for_each(bithenge_node_t *base,
147 bithenge_for_each_func_t func, void *data)
148{
149 int rc;
150 simple_internal_node_t *self = node_as_simple(base);
151 for (bithenge_int_t i = 0; i < self->len; i++) {
152 bithenge_node_inc_ref(self->nodes[2*i+0]);
153 bithenge_node_inc_ref(self->nodes[2*i+1]);
154 rc = func(self->nodes[2*i+0], self->nodes[2*i+1], data);
155 if (rc != EOK)
156 return rc;
157 }
158 return EOK;
159}
160
161static void simple_internal_node_destroy(bithenge_node_t *base)
162{
163 simple_internal_node_t *self = node_as_simple(base);
164 for (bithenge_int_t i = 0; i < 2 * self->len; i++)
165 bithenge_node_dec_ref(self->nodes[i]);
166 if (self->needs_free)
167 free(self->nodes);
168 free(self);
169}
170
171static bithenge_internal_node_ops_t simple_internal_node_ops = {
172 .for_each = simple_internal_node_for_each,
173 .destroy = simple_internal_node_destroy,
174};
175
176/** Initialize an internal node.
177 * @memberof bithenge_node_t
178 * @param[out] self The node.
179 * @param[in] ops The operations provided.
180 * @return EOK on success or an error code from errno.h. */
181int bithenge_init_internal_node(bithenge_node_t *self,
182 const bithenge_internal_node_ops_t *ops)
183{
184 self->type = BITHENGE_NODE_INTERNAL;
185 self->refs = 1;
186 self->internal_ops = ops;
187 return EOK;
188}
189
190/** Create an internal node from a set of keys and values. This function takes
191 * ownership of a reference to the key and value nodes, and optionally the
192 * array @a nodes.
193 * @memberof bithenge_node_t
194 * @param[out] out Stores the created internal node.
195 * @param nodes The array of key-value pairs. Keys are stored at even indices
196 * and values are stored at odd indices.
197 * @param len The number of key-value pairs in the node array.
198 * @param needs_free If true, when the internal node is destroyed it will free
199 * the nodes array rather than just dereferencing each node inside it.
200 * @return EOK on success or an error code from errno.h. */
201int bithenge_new_simple_internal_node(bithenge_node_t **out,
202 bithenge_node_t **nodes, bithenge_int_t len, bool needs_free)
203{
204 int rc;
205 assert(out);
206 simple_internal_node_t *self = malloc(sizeof(*self));
207 if (!self) {
208 rc = ENOMEM;
209 goto error;
210 }
211 rc = bithenge_init_internal_node(simple_as_node(self),
212 &simple_internal_node_ops);
213 if (rc != EOK)
214 goto error;
215 self->nodes = nodes;
216 self->len = len;
217 self->needs_free = needs_free;
218 *out = simple_as_node(self);
219 return EOK;
220error:
221 for (bithenge_int_t i = 0; i < 2 * len; i++)
222 bithenge_node_dec_ref(nodes[i]);
223 if (needs_free)
224 free(nodes);
225 free(self);
226 return rc;
227}
228
229static bithenge_node_t false_node = { BITHENGE_NODE_BOOLEAN, 1, .boolean_value = false };
230static bithenge_node_t true_node = { BITHENGE_NODE_BOOLEAN, 1, .boolean_value = true };
231
232/** Create a boolean node.
233 * @memberof bithenge_node_t
234 * @param[out] out Stores the created boolean node.
235 * @param value The value for the node to hold.
236 * @return EOK on success or an error code from errno.h. */
237int bithenge_new_boolean_node(bithenge_node_t **out, bool value)
238{
239 assert(out);
240 *out = value ? &true_node : &false_node;
241 (*out)->refs++;
242 return EOK;
243}
244
245/** Create an integer node.
246 * @memberof bithenge_node_t
247 * @param[out] out Stores the created integer node.
248 * @param value The value for the node to hold.
249 * @return EOK on success or an error code from errno.h. */
250int bithenge_new_integer_node(bithenge_node_t **out, bithenge_int_t value)
251{
252 assert(out);
253 bithenge_node_t *self = malloc(sizeof(*self));
254 if (!self)
255 return ENOMEM;
256 self->type = BITHENGE_NODE_INTEGER;
257 self->refs = 1;
258 self->integer_value = value;
259 *out = self;
260 return EOK;
261}
262
263/** Create a string node.
264 * @memberof bithenge_node_t
265 * @param[out] out Stores the created string node. On error, this is unchanged.
266 * @param value The value for the node to hold.
267 * @param needs_free Whether the string should be freed when the node is
268 * destroyed.
269 * @return EOK on success or an error code from errno.h. */
270int bithenge_new_string_node(bithenge_node_t **out, const char *value, bool needs_free)
271{
272 assert(out);
273 bithenge_node_t *self = malloc(sizeof(*self));
274 if (!self) {
275 if (needs_free)
276 free((void *)value);
277 return ENOMEM;
278 }
279 self->type = BITHENGE_NODE_STRING;
280 self->refs = 1;
281 self->string_value.ptr = value;
282 self->string_value.needs_free = needs_free;
283 *out = self;
284 return EOK;
285}
286
287/** Check whether the contents of two nodes are equal. Does not yet work for
288 * internal nodes.
289 * @memberof bithenge_node_t
290 * @param a, b Nodes to compare.
291 * @return Whether the nodes are equal. If an error occurs, returns false.
292 * @todo Add support for internal nodes.
293 */
294bool bithenge_node_equal(bithenge_node_t *a, bithenge_node_t *b)
295{
296 if (a->type != b->type)
297 return false;
298 switch (a->type) {
299 case BITHENGE_NODE_INTERNAL:
300 return false;
301 case BITHENGE_NODE_BOOLEAN:
302 return a->boolean_value == b->boolean_value;
303 case BITHENGE_NODE_INTEGER:
304 return a->integer_value == b->integer_value;
305 case BITHENGE_NODE_STRING:
306 return !str_cmp(a->string_value.ptr, b->string_value.ptr);
307 case BITHENGE_NODE_BLOB:
308 return bithenge_blob_equal(bithenge_node_as_blob(a),
309 bithenge_node_as_blob(b));
310 }
311 return false;
312}
313
314/** @}
315 */
Note: See TracBrowser for help on using the repository browser.