source: mainline/uspace/app/bithenge/tree.c@ 23db8aa

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

Bithenge: add switch transforms and sugar

  • Property mode set to 100644
File size: 10.1 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
128/** Initialize an internal node.
129 * @memberof bithenge_node_t
130 * @param[out] self The node.
131 * @param[in] ops The operations provided.
132 * @return EOK on success or an error code from errno.h. */
133int bithenge_init_internal_node(bithenge_node_t *self,
134 const bithenge_internal_node_ops_t *ops)
135{
136 self->type = BITHENGE_NODE_INTERNAL;
137 self->refs = 1;
138 self->internal_ops = ops;
139 return EOK;
140}
141
142static void internal_node_indestructible(bithenge_node_t *self)
143{
144 assert(false);
145}
146
147static int empty_internal_node_for_each(bithenge_node_t *base,
148 bithenge_for_each_func_t func, void *data)
149{
150 return EOK;
151}
152
153static int empty_internal_node_get(bithenge_node_t *self, bithenge_node_t *key,
154 bithenge_node_t **out)
155{
156 return ENOENT;
157}
158
159static const bithenge_internal_node_ops_t empty_internal_node_ops = {
160 .for_each = empty_internal_node_for_each,
161 .get = empty_internal_node_get,
162 .destroy = internal_node_indestructible,
163};
164
165static bithenge_node_t empty_internal_node = {
166 BITHENGE_NODE_INTERNAL,
167 1,
168 { .internal_ops = &empty_internal_node_ops },
169};
170
171/** Create an empty internal node.
172 * @param[out] out Holds the created node.
173 * @return EOK on success or an error code from errno.h. */
174int bithenge_new_empty_internal_node(bithenge_node_t **out)
175{
176 bithenge_node_inc_ref(&empty_internal_node);
177 *out = &empty_internal_node;
178 return EOK;
179}
180
181typedef struct
182{
183 bithenge_node_t base;
184 bithenge_node_t **nodes;
185 bithenge_int_t len;
186 bool needs_free;
187} simple_internal_node_t;
188
189static simple_internal_node_t *node_as_simple(bithenge_node_t *node)
190{
191 return (simple_internal_node_t *)node;
192}
193
194static bithenge_node_t *simple_as_node(simple_internal_node_t *node)
195{
196 return &node->base;
197}
198
199static int simple_internal_node_for_each(bithenge_node_t *base,
200 bithenge_for_each_func_t func, void *data)
201{
202 int rc;
203 simple_internal_node_t *self = node_as_simple(base);
204 for (bithenge_int_t i = 0; i < self->len; i++) {
205 bithenge_node_inc_ref(self->nodes[2*i+0]);
206 bithenge_node_inc_ref(self->nodes[2*i+1]);
207 rc = func(self->nodes[2*i+0], self->nodes[2*i+1], data);
208 if (rc != EOK)
209 return rc;
210 }
211 return EOK;
212}
213
214static void simple_internal_node_destroy(bithenge_node_t *base)
215{
216 simple_internal_node_t *self = node_as_simple(base);
217 for (bithenge_int_t i = 0; i < 2 * self->len; i++)
218 bithenge_node_dec_ref(self->nodes[i]);
219 if (self->needs_free)
220 free(self->nodes);
221 free(self);
222}
223
224static bithenge_internal_node_ops_t simple_internal_node_ops = {
225 .for_each = simple_internal_node_for_each,
226 .destroy = simple_internal_node_destroy,
227};
228
229/** Create an internal node from a set of keys and values. This function takes
230 * ownership of a reference to the key and value nodes, and optionally the
231 * array @a nodes.
232 * @memberof bithenge_node_t
233 * @param[out] out Stores the created internal node.
234 * @param nodes The array of key-value pairs. Keys are stored at even indices
235 * and values are stored at odd indices.
236 * @param len The number of key-value pairs in the node array.
237 * @param needs_free If true, when the internal node is destroyed it will free
238 * the nodes array rather than just dereferencing each node inside it.
239 * @return EOK on success or an error code from errno.h. */
240int bithenge_new_simple_internal_node(bithenge_node_t **out,
241 bithenge_node_t **nodes, bithenge_int_t len, bool needs_free)
242{
243 int rc;
244 assert(out);
245 simple_internal_node_t *self = malloc(sizeof(*self));
246 if (!self) {
247 rc = ENOMEM;
248 goto error;
249 }
250 rc = bithenge_init_internal_node(simple_as_node(self),
251 &simple_internal_node_ops);
252 if (rc != EOK)
253 goto error;
254 self->nodes = nodes;
255 self->len = len;
256 self->needs_free = needs_free;
257 *out = simple_as_node(self);
258 return EOK;
259error:
260 for (bithenge_int_t i = 0; i < 2 * len; i++)
261 bithenge_node_dec_ref(nodes[i]);
262 if (needs_free)
263 free(nodes);
264 free(self);
265 return rc;
266}
267
268static bithenge_node_t false_node = { BITHENGE_NODE_BOOLEAN, 1, .boolean_value = false };
269static bithenge_node_t true_node = { BITHENGE_NODE_BOOLEAN, 1, .boolean_value = true };
270
271/** Create a boolean node.
272 * @memberof bithenge_node_t
273 * @param[out] out Stores the created boolean node.
274 * @param value The value for the node to hold.
275 * @return EOK on success or an error code from errno.h. */
276int bithenge_new_boolean_node(bithenge_node_t **out, bool value)
277{
278 assert(out);
279 *out = value ? &true_node : &false_node;
280 (*out)->refs++;
281 return EOK;
282}
283
284/** Create an integer node.
285 * @memberof bithenge_node_t
286 * @param[out] out Stores the created integer node.
287 * @param value The value for the node to hold.
288 * @return EOK on success or an error code from errno.h. */
289int bithenge_new_integer_node(bithenge_node_t **out, bithenge_int_t value)
290{
291 assert(out);
292 bithenge_node_t *self = malloc(sizeof(*self));
293 if (!self)
294 return ENOMEM;
295 self->type = BITHENGE_NODE_INTEGER;
296 self->refs = 1;
297 self->integer_value = value;
298 *out = self;
299 return EOK;
300}
301
302/** Create a string node.
303 * @memberof bithenge_node_t
304 * @param[out] out Stores the created string node. On error, this is unchanged.
305 * @param value The value for the node to hold.
306 * @param needs_free Whether the string should be freed when the node is
307 * destroyed.
308 * @return EOK on success or an error code from errno.h. */
309int bithenge_new_string_node(bithenge_node_t **out, const char *value, bool needs_free)
310{
311 assert(out);
312 bithenge_node_t *self = malloc(sizeof(*self));
313 if (!self) {
314 if (needs_free)
315 free((void *)value);
316 return ENOMEM;
317 }
318 self->type = BITHENGE_NODE_STRING;
319 self->refs = 1;
320 self->string_value.ptr = value;
321 self->string_value.needs_free = needs_free;
322 *out = self;
323 return EOK;
324}
325
326/** Check whether the contents of two nodes are equal. Does not yet work for
327 * internal nodes. Takes ownership of nothing.
328 * @memberof bithenge_node_t
329 * @param a, b Nodes to compare.
330 * @return Whether the nodes are equal. If an error occurs, returns false.
331 * @todo Add support for internal nodes.
332 */
333bool bithenge_node_equal(bithenge_node_t *a, bithenge_node_t *b)
334{
335 if (a->type != b->type)
336 return false;
337 switch (a->type) {
338 case BITHENGE_NODE_INTERNAL:
339 return false;
340 case BITHENGE_NODE_BOOLEAN:
341 return a->boolean_value == b->boolean_value;
342 case BITHENGE_NODE_INTEGER:
343 return a->integer_value == b->integer_value;
344 case BITHENGE_NODE_STRING:
345 return !str_cmp(a->string_value.ptr, b->string_value.ptr);
346 case BITHENGE_NODE_BLOB:
347 return bithenge_blob_equal(bithenge_node_as_blob(a),
348 bithenge_node_as_blob(b));
349 }
350 return false;
351}
352
353/** @}
354 */
Note: See TracBrowser for help on using the repository browser.