[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 | #ifndef BITHENGE_TREE_H_
|
---|
| 38 | #define BITHENGE_TREE_H_
|
---|
| 39 |
|
---|
| 40 | #include <assert.h>
|
---|
| 41 | #include <sys/types.h>
|
---|
[da0fef6] | 42 | #include "os.h"
|
---|
[11b9ad7] | 43 |
|
---|
[8375d0eb] | 44 | /** Indicates the type of a tree node. */
|
---|
[11b9ad7] | 45 | typedef enum {
|
---|
[8375d0eb] | 46 | /** An internal node with labelled edges to other nodes. */
|
---|
| 47 | BITHENGE_NODE_INTERNAL = 1,
|
---|
| 48 | /** A leaf node holding a boolean value. */
|
---|
[11b9ad7] | 49 | BITHENGE_NODE_BOOLEAN,
|
---|
[8375d0eb] | 50 | /** A leaf node holding an integer. */
|
---|
[11b9ad7] | 51 | BITHENGE_NODE_INTEGER,
|
---|
[8375d0eb] | 52 | /** A leaf node holding a string. */
|
---|
[11b9ad7] | 53 | BITHENGE_NODE_STRING,
|
---|
[8375d0eb] | 54 | /** A leaf node holding a binary blob. */
|
---|
[5c925ce] | 55 | BITHENGE_NODE_BLOB,
|
---|
[11b9ad7] | 56 | } bithenge_node_type_t;
|
---|
| 57 |
|
---|
| 58 | typedef struct bithenge_node_t {
|
---|
[8375d0eb] | 59 | /** @privatesection */
|
---|
[11b9ad7] | 60 | bithenge_node_type_t type;
|
---|
[f2da0bb] | 61 | unsigned int refs;
|
---|
[5f679702] | 62 | union {
|
---|
[5c925ce] | 63 | const struct bithenge_internal_node_ops_t *internal_ops;
|
---|
[5f679702] | 64 | bool boolean_value;
|
---|
| 65 | bithenge_int_t integer_value;
|
---|
| 66 | struct {
|
---|
| 67 | const char *ptr;
|
---|
| 68 | bool needs_free;
|
---|
| 69 | } string_value;
|
---|
[5c925ce] | 70 | const struct bithenge_random_access_blob_ops_t *blob_ops;
|
---|
[5f679702] | 71 | };
|
---|
[11b9ad7] | 72 | } bithenge_node_t;
|
---|
| 73 |
|
---|
[04a7435f] | 74 | /** A callback function used to iterate over a node's children. It takes
|
---|
| 75 | * ownership of a reference to both the key and the value.
|
---|
[8375d0eb] | 76 | * @memberof bithenge_node_t
|
---|
| 77 | * @param key The key.
|
---|
| 78 | * @param value The value.
|
---|
| 79 | * @param data Data provided to @a bithenge_node_t::bithenge_node_for_each.
|
---|
| 80 | * @return EOK on success or an error code from errno.h. */
|
---|
[11b9ad7] | 81 | typedef int (*bithenge_for_each_func_t)(bithenge_node_t *key, bithenge_node_t *value, void *data);
|
---|
| 82 |
|
---|
[8375d0eb] | 83 | /** Operations providing access to an internal node. */
|
---|
[11b9ad7] | 84 | typedef struct bithenge_internal_node_ops_t {
|
---|
[8375d0eb] | 85 | /** @copydoc bithenge_node_t::bithenge_node_for_each */
|
---|
[978ccaf1] | 86 | int (*for_each)(bithenge_node_t *self, bithenge_for_each_func_t func, void *data);
|
---|
[e8e31d9] | 87 | /** @copydoc bithenge_node_t::bithenge_node_get */
|
---|
| 88 | int (*get)(bithenge_node_t *self, bithenge_node_t *key,
|
---|
| 89 | bithenge_node_t **out);
|
---|
[978ccaf1] | 90 | /** Destroys the internal node.
|
---|
| 91 | * @param self The node to destroy. */
|
---|
| 92 | void (*destroy)(bithenge_node_t *self);
|
---|
[11b9ad7] | 93 | } bithenge_internal_node_ops_t;
|
---|
| 94 |
|
---|
[8375d0eb] | 95 | /** Find the type of a node.
|
---|
| 96 | * @memberof bithenge_node_t
|
---|
| 97 | * @param node The node.
|
---|
| 98 | * @return The type of the node. */
|
---|
| 99 | static inline bithenge_node_type_t bithenge_node_type(const bithenge_node_t *node)
|
---|
| 100 | {
|
---|
| 101 | return node->type;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[f2da0bb] | 104 | /** Increment a node's reference count.
|
---|
| 105 | * @memberof bithenge_node_t
|
---|
[978ccaf1] | 106 | * @param node The node to reference. */
|
---|
| 107 | static inline void bithenge_node_inc_ref(bithenge_node_t *node)
|
---|
[f2da0bb] | 108 | {
|
---|
| 109 | assert(node);
|
---|
| 110 | node->refs++;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
[978ccaf1] | 113 | void bithenge_node_dec_ref(bithenge_node_t *node);
|
---|
[f2da0bb] | 114 |
|
---|
[8375d0eb] | 115 | /** Iterate over a node's children.
|
---|
| 116 | * @memberof bithenge_node_t
|
---|
[978ccaf1] | 117 | * @param self The internal node to iterate over.
|
---|
[8375d0eb] | 118 | * @param func The callback function.
|
---|
| 119 | * @param data Data to provide to the callback function.
|
---|
| 120 | * @return EOK on success or an error code from errno.h. */
|
---|
[978ccaf1] | 121 | static inline int bithenge_node_for_each(bithenge_node_t *self,
|
---|
| 122 | bithenge_for_each_func_t func, void *data)
|
---|
[11b9ad7] | 123 | {
|
---|
[978ccaf1] | 124 | assert(self->type == BITHENGE_NODE_INTERNAL);
|
---|
| 125 | return self->internal_ops->for_each(self, func, data);
|
---|
[11b9ad7] | 126 | }
|
---|
| 127 |
|
---|
[e8e31d9] | 128 | int bithenge_node_get(bithenge_node_t *, bithenge_node_t *,
|
---|
| 129 | bithenge_node_t **);
|
---|
| 130 |
|
---|
[8375d0eb] | 131 | /** Get the value of a boolean node.
|
---|
| 132 | * @memberof bithenge_node_t
|
---|
[978ccaf1] | 133 | * @param self The boolean node.
|
---|
[8375d0eb] | 134 | * @return The node's value. */
|
---|
[978ccaf1] | 135 | static inline bool bithenge_boolean_node_value(bithenge_node_t *self)
|
---|
[11b9ad7] | 136 | {
|
---|
[978ccaf1] | 137 | assert(self->type == BITHENGE_NODE_BOOLEAN);
|
---|
| 138 | return self->boolean_value;
|
---|
[11b9ad7] | 139 | }
|
---|
| 140 |
|
---|
[8375d0eb] | 141 | /** Get the value of an integer node.
|
---|
| 142 | * @memberof bithenge_node_t
|
---|
[978ccaf1] | 143 | * @param self The integer node.
|
---|
[8375d0eb] | 144 | * @return The node's value. */
|
---|
[978ccaf1] | 145 | static inline bithenge_int_t bithenge_integer_node_value(bithenge_node_t *self)
|
---|
[11b9ad7] | 146 | {
|
---|
[978ccaf1] | 147 | assert(self->type == BITHENGE_NODE_INTEGER);
|
---|
| 148 | return self->integer_value;
|
---|
[11b9ad7] | 149 | }
|
---|
| 150 |
|
---|
[8375d0eb] | 151 | /** Get the value of an string node.
|
---|
| 152 | * @memberof bithenge_node_t
|
---|
[978ccaf1] | 153 | * @param self The string node.
|
---|
[8375d0eb] | 154 | * @return The node's value. */
|
---|
[978ccaf1] | 155 | static inline const char *bithenge_string_node_value(bithenge_node_t *self)
|
---|
[11b9ad7] | 156 | {
|
---|
[978ccaf1] | 157 | assert(self->type == BITHENGE_NODE_STRING);
|
---|
| 158 | return self->string_value.ptr;
|
---|
[11b9ad7] | 159 | }
|
---|
| 160 |
|
---|
[04a7435f] | 161 | int bithenge_init_internal_node(bithenge_node_t *,
|
---|
| 162 | const bithenge_internal_node_ops_t *);
|
---|
[10334c2e] | 163 | int bithenge_new_empty_internal_node(bithenge_node_t **);
|
---|
[8375d0eb] | 164 | int bithenge_new_simple_internal_node(bithenge_node_t **, bithenge_node_t **,
|
---|
| 165 | bithenge_int_t, bool needs_free);
|
---|
[11b9ad7] | 166 | int bithenge_new_boolean_node(bithenge_node_t **, bool);
|
---|
| 167 | int bithenge_new_integer_node(bithenge_node_t **, bithenge_int_t);
|
---|
| 168 | int bithenge_new_string_node(bithenge_node_t **, const char *, bool);
|
---|
[d5070ef] | 169 | bool bithenge_node_equal(bithenge_node_t *, bithenge_node_t *);
|
---|
[11b9ad7] | 170 |
|
---|
| 171 | #endif
|
---|
| 172 |
|
---|
| 173 | /** @}
|
---|
| 174 | */
|
---|