| 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>
|
|---|
| 42 | #include "os.h"
|
|---|
| 43 |
|
|---|
| 44 | /** Indicates the type of a tree node. */
|
|---|
| 45 | typedef enum {
|
|---|
| 46 | /** An internal node with labelled edges to other nodes. */
|
|---|
| 47 | BITHENGE_NODE_INTERNAL = 1,
|
|---|
| 48 | /** A leaf node holding a boolean value. */
|
|---|
| 49 | BITHENGE_NODE_BOOLEAN,
|
|---|
| 50 | /** A leaf node holding an integer. */
|
|---|
| 51 | BITHENGE_NODE_INTEGER,
|
|---|
| 52 | /** A leaf node holding a string. */
|
|---|
| 53 | BITHENGE_NODE_STRING,
|
|---|
| 54 | /** A leaf node holding a binary blob. */
|
|---|
| 55 | BITHENGE_NODE_BLOB,
|
|---|
| 56 | } bithenge_node_type_t;
|
|---|
| 57 |
|
|---|
| 58 | typedef struct bithenge_node_t {
|
|---|
| 59 | /** @privatesection */
|
|---|
| 60 | bithenge_node_type_t type;
|
|---|
| 61 | unsigned int refs;
|
|---|
| 62 | union {
|
|---|
| 63 | const struct bithenge_internal_node_ops_t *internal_ops;
|
|---|
| 64 | bool boolean_value;
|
|---|
| 65 | bithenge_int_t integer_value;
|
|---|
| 66 | struct {
|
|---|
| 67 | const char *ptr;
|
|---|
| 68 | bool needs_free;
|
|---|
| 69 | } string_value;
|
|---|
| 70 | const struct bithenge_random_access_blob_ops_t *blob_ops;
|
|---|
| 71 | };
|
|---|
| 72 | } bithenge_node_t;
|
|---|
| 73 |
|
|---|
| 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.
|
|---|
| 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. */
|
|---|
| 81 | typedef int (*bithenge_for_each_func_t)(bithenge_node_t *key, bithenge_node_t *value, void *data);
|
|---|
| 82 |
|
|---|
| 83 | /** Operations providing access to an internal node. */
|
|---|
| 84 | typedef struct bithenge_internal_node_ops_t {
|
|---|
| 85 | /** @copydoc bithenge_node_t::bithenge_node_for_each */
|
|---|
| 86 | int (*for_each)(bithenge_node_t *self, bithenge_for_each_func_t func, void *data);
|
|---|
| 87 | /** @copydoc bithenge_node_t::bithenge_node_get */
|
|---|
| 88 | int (*get)(bithenge_node_t *self, bithenge_node_t *key,
|
|---|
| 89 | bithenge_node_t **out);
|
|---|
| 90 | /** Destroys the internal node.
|
|---|
| 91 | * @param self The node to destroy. */
|
|---|
| 92 | void (*destroy)(bithenge_node_t *self);
|
|---|
| 93 | } bithenge_internal_node_ops_t;
|
|---|
| 94 |
|
|---|
| 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 |
|
|---|
| 104 | /** Increment a node's reference count.
|
|---|
| 105 | * @memberof bithenge_node_t
|
|---|
| 106 | * @param node The node to reference. */
|
|---|
| 107 | static inline void bithenge_node_inc_ref(bithenge_node_t *node)
|
|---|
| 108 | {
|
|---|
| 109 | assert(node);
|
|---|
| 110 | node->refs++;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | void bithenge_node_dec_ref(bithenge_node_t *node);
|
|---|
| 114 |
|
|---|
| 115 | /** Iterate over a node's children.
|
|---|
| 116 | * @memberof bithenge_node_t
|
|---|
| 117 | * @param self The internal node to iterate over.
|
|---|
| 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. */
|
|---|
| 121 | static inline int bithenge_node_for_each(bithenge_node_t *self,
|
|---|
| 122 | bithenge_for_each_func_t func, void *data)
|
|---|
| 123 | {
|
|---|
| 124 | assert(self->type == BITHENGE_NODE_INTERNAL);
|
|---|
| 125 | return self->internal_ops->for_each(self, func, data);
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | int bithenge_node_get(bithenge_node_t *, bithenge_node_t *,
|
|---|
| 129 | bithenge_node_t **);
|
|---|
| 130 |
|
|---|
| 131 | /** Get the value of a boolean node.
|
|---|
| 132 | * @memberof bithenge_node_t
|
|---|
| 133 | * @param self The boolean node.
|
|---|
| 134 | * @return The node's value. */
|
|---|
| 135 | static inline bool bithenge_boolean_node_value(bithenge_node_t *self)
|
|---|
| 136 | {
|
|---|
| 137 | assert(self->type == BITHENGE_NODE_BOOLEAN);
|
|---|
| 138 | return self->boolean_value;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | /** Get the value of an integer node.
|
|---|
| 142 | * @memberof bithenge_node_t
|
|---|
| 143 | * @param self The integer node.
|
|---|
| 144 | * @return The node's value. */
|
|---|
| 145 | static inline bithenge_int_t bithenge_integer_node_value(bithenge_node_t *self)
|
|---|
| 146 | {
|
|---|
| 147 | assert(self->type == BITHENGE_NODE_INTEGER);
|
|---|
| 148 | return self->integer_value;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | /** Get the value of an string node.
|
|---|
| 152 | * @memberof bithenge_node_t
|
|---|
| 153 | * @param self The string node.
|
|---|
| 154 | * @return The node's value. */
|
|---|
| 155 | static inline const char *bithenge_string_node_value(bithenge_node_t *self)
|
|---|
| 156 | {
|
|---|
| 157 | assert(self->type == BITHENGE_NODE_STRING);
|
|---|
| 158 | return self->string_value.ptr;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | int bithenge_init_internal_node(bithenge_node_t *,
|
|---|
| 162 | const bithenge_internal_node_ops_t *);
|
|---|
| 163 | int bithenge_new_empty_internal_node(bithenge_node_t **);
|
|---|
| 164 | int bithenge_new_simple_internal_node(bithenge_node_t **, bithenge_node_t **,
|
|---|
| 165 | bithenge_int_t, bool needs_free);
|
|---|
| 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);
|
|---|
| 169 | bool bithenge_node_equal(bithenge_node_t *, bithenge_node_t *);
|
|---|
| 170 |
|
|---|
| 171 | #endif
|
|---|
| 172 |
|
|---|
| 173 | /** @}
|
|---|
| 174 | */
|
|---|