[11b9ad7] | 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>
|
---|
[5c925ce] | 39 | #include "blob.h"
|
---|
[da0fef6] | 40 | #include "os.h"
|
---|
[11b9ad7] | 41 | #include "tree.h"
|
---|
| 42 |
|
---|
[978ccaf1] | 43 | static void blob_destroy(bithenge_node_t *base)
|
---|
[5c925ce] | 44 | {
|
---|
[978ccaf1] | 45 | bithenge_blob_t *self = bithenge_node_as_blob(base);
|
---|
| 46 | assert(self->base.blob_ops);
|
---|
| 47 | self->base.blob_ops->destroy(self);
|
---|
[5c925ce] | 48 | }
|
---|
| 49 |
|
---|
[978ccaf1] | 50 | static void node_destroy(bithenge_node_t *self)
|
---|
[11b9ad7] | 51 | {
|
---|
[978ccaf1] | 52 | switch (bithenge_node_type(self)) {
|
---|
[5c925ce] | 53 | case BITHENGE_NODE_BLOB:
|
---|
[978ccaf1] | 54 | blob_destroy(self);
|
---|
| 55 | return;
|
---|
[11b9ad7] | 56 | case BITHENGE_NODE_STRING:
|
---|
[978ccaf1] | 57 | if (self->string_value.needs_free)
|
---|
| 58 | free((void *)self->string_value.ptr);
|
---|
[11b9ad7] | 59 | break;
|
---|
| 60 | case BITHENGE_NODE_INTERNAL:
|
---|
[978ccaf1] | 61 | self->internal_ops->destroy(self);
|
---|
| 62 | return;
|
---|
[11b9ad7] | 63 | case BITHENGE_NODE_BOOLEAN:
|
---|
[978ccaf1] | 64 | return; /* The boolean nodes are allocated statically below. */
|
---|
[11b9ad7] | 65 | case BITHENGE_NODE_INTEGER: /* pass-through */
|
---|
| 66 | break;
|
---|
| 67 | }
|
---|
[978ccaf1] | 68 | free(self);
|
---|
[11b9ad7] | 69 | }
|
---|
| 70 |
|
---|
[f2da0bb] | 71 | /** Decrement a node's reference count and free it if appropriate.
|
---|
| 72 | * @memberof bithenge_node_t
|
---|
[978ccaf1] | 73 | * @param node The node to dereference, or NULL. */
|
---|
| 74 | void bithenge_node_dec_ref(bithenge_node_t *node)
|
---|
[f2da0bb] | 75 | {
|
---|
| 76 | if (!node)
|
---|
[978ccaf1] | 77 | return;
|
---|
[f2da0bb] | 78 | if (--node->refs == 0)
|
---|
[978ccaf1] | 79 | node_destroy(node);
|
---|
[f2da0bb] | 80 | }
|
---|
| 81 |
|
---|
[e8e31d9] | 82 | typedef struct {
|
---|
| 83 | bithenge_node_t *key;
|
---|
| 84 | bithenge_node_t **out;
|
---|
| 85 | } get_for_each_data_t;
|
---|
| 86 |
|
---|
| 87 | static 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. */
|
---|
| 110 | int 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 |
|
---|
[10334c2e] | 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. */
|
---|
| 133 | int 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 |
|
---|
| 142 | static void internal_node_indestructible(bithenge_node_t *self)
|
---|
| 143 | {
|
---|
| 144 | assert(false);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | static 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 |
|
---|
| 153 | static int empty_internal_node_get(bithenge_node_t *self, bithenge_node_t *key,
|
---|
| 154 | bithenge_node_t **out)
|
---|
| 155 | {
|
---|
| 156 | return ENOENT;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | static 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 |
|
---|
| 165 | static 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. */
|
---|
| 174 | int 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 |
|
---|
[11b9ad7] | 181 | typedef struct
|
---|
| 182 | {
|
---|
[5f679702] | 183 | bithenge_node_t base;
|
---|
[11b9ad7] | 184 | bithenge_node_t **nodes;
|
---|
| 185 | bithenge_int_t len;
|
---|
| 186 | bool needs_free;
|
---|
| 187 | } simple_internal_node_t;
|
---|
| 188 |
|
---|
[5f679702] | 189 | static simple_internal_node_t *node_as_simple(bithenge_node_t *node)
|
---|
[11b9ad7] | 190 | {
|
---|
| 191 | return (simple_internal_node_t *)node;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
[f2da0bb] | 194 | static bithenge_node_t *simple_as_node(simple_internal_node_t *node)
|
---|
| 195 | {
|
---|
| 196 | return &node->base;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[8375d0eb] | 199 | static int simple_internal_node_for_each(bithenge_node_t *base,
|
---|
| 200 | bithenge_for_each_func_t func, void *data)
|
---|
[11b9ad7] | 201 | {
|
---|
| 202 | int rc;
|
---|
[978ccaf1] | 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);
|
---|
[11b9ad7] | 208 | if (rc != EOK)
|
---|
| 209 | return rc;
|
---|
| 210 | }
|
---|
| 211 | return EOK;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[978ccaf1] | 214 | static void simple_internal_node_destroy(bithenge_node_t *base)
|
---|
[5c925ce] | 215 | {
|
---|
[978ccaf1] | 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);
|
---|
[5c925ce] | 222 | }
|
---|
| 223 |
|
---|
[11b9ad7] | 224 | static bithenge_internal_node_ops_t simple_internal_node_ops = {
|
---|
[5c925ce] | 225 | .for_each = simple_internal_node_for_each,
|
---|
| 226 | .destroy = simple_internal_node_destroy,
|
---|
[11b9ad7] | 227 | };
|
---|
| 228 |
|
---|
[f2da0bb] | 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.
|
---|
[8375d0eb] | 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
|
---|
[f2da0bb] | 238 | * the nodes array rather than just dereferencing each node inside it.
|
---|
[8375d0eb] | 239 | * @return EOK on success or an error code from errno.h. */
|
---|
| 240 | int bithenge_new_simple_internal_node(bithenge_node_t **out,
|
---|
| 241 | bithenge_node_t **nodes, bithenge_int_t len, bool needs_free)
|
---|
[11b9ad7] | 242 | {
|
---|
[04a7435f] | 243 | int rc;
|
---|
[11b9ad7] | 244 | assert(out);
|
---|
[978ccaf1] | 245 | simple_internal_node_t *self = malloc(sizeof(*self));
|
---|
| 246 | if (!self) {
|
---|
[04a7435f] | 247 | rc = ENOMEM;
|
---|
| 248 | goto error;
|
---|
[f2da0bb] | 249 | }
|
---|
[978ccaf1] | 250 | rc = bithenge_init_internal_node(simple_as_node(self),
|
---|
[04a7435f] | 251 | &simple_internal_node_ops);
|
---|
| 252 | if (rc != EOK)
|
---|
| 253 | goto error;
|
---|
[978ccaf1] | 254 | self->nodes = nodes;
|
---|
| 255 | self->len = len;
|
---|
| 256 | self->needs_free = needs_free;
|
---|
| 257 | *out = simple_as_node(self);
|
---|
[11b9ad7] | 258 | return EOK;
|
---|
[04a7435f] | 259 | error:
|
---|
| 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);
|
---|
[978ccaf1] | 264 | free(self);
|
---|
[04a7435f] | 265 | return rc;
|
---|
[11b9ad7] | 266 | }
|
---|
| 267 |
|
---|
[f2da0bb] | 268 | static bithenge_node_t false_node = { BITHENGE_NODE_BOOLEAN, 1, .boolean_value = false };
|
---|
| 269 | static bithenge_node_t true_node = { BITHENGE_NODE_BOOLEAN, 1, .boolean_value = true };
|
---|
[11b9ad7] | 270 |
|
---|
[f2da0bb] | 271 | /** Create a boolean node.
|
---|
[8375d0eb] | 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. */
|
---|
[11b9ad7] | 276 | int bithenge_new_boolean_node(bithenge_node_t **out, bool value)
|
---|
| 277 | {
|
---|
| 278 | assert(out);
|
---|
[5f679702] | 279 | *out = value ? &true_node : &false_node;
|
---|
[f2da0bb] | 280 | (*out)->refs++;
|
---|
[11b9ad7] | 281 | return EOK;
|
---|
| 282 | }
|
---|
| 283 |
|
---|
[f2da0bb] | 284 | /** Create an integer node.
|
---|
[8375d0eb] | 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. */
|
---|
[11b9ad7] | 289 | int bithenge_new_integer_node(bithenge_node_t **out, bithenge_int_t value)
|
---|
| 290 | {
|
---|
| 291 | assert(out);
|
---|
[978ccaf1] | 292 | bithenge_node_t *self = malloc(sizeof(*self));
|
---|
| 293 | if (!self)
|
---|
[11b9ad7] | 294 | return ENOMEM;
|
---|
[978ccaf1] | 295 | self->type = BITHENGE_NODE_INTEGER;
|
---|
| 296 | self->refs = 1;
|
---|
| 297 | self->integer_value = value;
|
---|
| 298 | *out = self;
|
---|
[11b9ad7] | 299 | return EOK;
|
---|
| 300 | }
|
---|
| 301 |
|
---|
[f2da0bb] | 302 | /** Create a string node.
|
---|
[8375d0eb] | 303 | * @memberof bithenge_node_t
|
---|
[04a7435f] | 304 | * @param[out] out Stores the created string node. On error, this is unchanged.
|
---|
[8375d0eb] | 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. */
|
---|
[11b9ad7] | 309 | int bithenge_new_string_node(bithenge_node_t **out, const char *value, bool needs_free)
|
---|
| 310 | {
|
---|
| 311 | assert(out);
|
---|
[978ccaf1] | 312 | bithenge_node_t *self = malloc(sizeof(*self));
|
---|
[600f5d1] | 313 | if (!self) {
|
---|
| 314 | if (needs_free)
|
---|
| 315 | free((void *)value);
|
---|
[11b9ad7] | 316 | return ENOMEM;
|
---|
[600f5d1] | 317 | }
|
---|
[978ccaf1] | 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;
|
---|
[11b9ad7] | 323 | return EOK;
|
---|
| 324 | }
|
---|
[8375d0eb] | 325 |
|
---|
[d5070ef] | 326 | /** Check whether the contents of two nodes are equal. Does not yet work for
|
---|
[78d3a00] | 327 | * internal nodes. Takes ownership of nothing.
|
---|
[d5070ef] | 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 | */
|
---|
| 333 | bool 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 |
|
---|
[8375d0eb] | 353 | /** @}
|
---|
| 354 | */
|
---|