[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 <bool.h>
|
---|
| 42 | #include <inttypes.h>
|
---|
| 43 | #include <sys/types.h>
|
---|
| 44 |
|
---|
| 45 | #ifdef INTMAX_MAX
|
---|
| 46 | typedef intmax_t bithenge_int_t;
|
---|
| 47 | #define BITHENGE_PRId PRIdMAX
|
---|
| 48 | #else
|
---|
| 49 | typedef int64_t bithenge_int_t;
|
---|
| 50 | #define BITHENGE_PRId PRId64
|
---|
| 51 | #endif
|
---|
| 52 |
|
---|
[8375d0eb] | 53 | /** Indicates the type of a tree node. */
|
---|
[11b9ad7] | 54 | typedef enum {
|
---|
[8375d0eb] | 55 | /** An internal node with labelled edges to other nodes. */
|
---|
| 56 | BITHENGE_NODE_INTERNAL = 1,
|
---|
| 57 | /** A leaf node holding a boolean value. */
|
---|
[11b9ad7] | 58 | BITHENGE_NODE_BOOLEAN,
|
---|
[8375d0eb] | 59 | /** A leaf node holding an integer. */
|
---|
[11b9ad7] | 60 | BITHENGE_NODE_INTEGER,
|
---|
[8375d0eb] | 61 | /** A leaf node holding a string. */
|
---|
[11b9ad7] | 62 | BITHENGE_NODE_STRING,
|
---|
[8375d0eb] | 63 | /** A leaf node holding a binary blob. */
|
---|
[5c925ce] | 64 | BITHENGE_NODE_BLOB,
|
---|
[11b9ad7] | 65 | } bithenge_node_type_t;
|
---|
| 66 |
|
---|
| 67 | typedef struct bithenge_node_t {
|
---|
[8375d0eb] | 68 | /** @privatesection */
|
---|
[11b9ad7] | 69 | bithenge_node_type_t type;
|
---|
[5f679702] | 70 | union {
|
---|
[5c925ce] | 71 | const struct bithenge_internal_node_ops_t *internal_ops;
|
---|
[5f679702] | 72 | bool boolean_value;
|
---|
| 73 | bithenge_int_t integer_value;
|
---|
| 74 | struct {
|
---|
| 75 | const char *ptr;
|
---|
| 76 | bool needs_free;
|
---|
| 77 | } string_value;
|
---|
[5c925ce] | 78 | const struct bithenge_random_access_blob_ops_t *blob_ops;
|
---|
[5f679702] | 79 | };
|
---|
[11b9ad7] | 80 | } bithenge_node_t;
|
---|
| 81 |
|
---|
[8375d0eb] | 82 | /** A callback function used to iterate over a node's children.
|
---|
| 83 | * @memberof bithenge_node_t
|
---|
| 84 | * @param key The key.
|
---|
| 85 | * @param value The value.
|
---|
| 86 | * @param data Data provided to @a bithenge_node_t::bithenge_node_for_each.
|
---|
| 87 | * @return EOK on success or an error code from errno.h. */
|
---|
[11b9ad7] | 88 | typedef int (*bithenge_for_each_func_t)(bithenge_node_t *key, bithenge_node_t *value, void *data);
|
---|
| 89 |
|
---|
[8375d0eb] | 90 | /** Operations providing access to an internal node. */
|
---|
[11b9ad7] | 91 | typedef struct bithenge_internal_node_ops_t {
|
---|
[8375d0eb] | 92 | /** @copydoc bithenge_node_t::bithenge_node_for_each */
|
---|
[5f679702] | 93 | int (*for_each)(bithenge_node_t *node, bithenge_for_each_func_t func, void *data);
|
---|
[8375d0eb] | 94 | /** @copydoc bithenge_node_t::bithenge_node_destroy */
|
---|
[5c925ce] | 95 | int (*destroy)(bithenge_node_t *node);
|
---|
[11b9ad7] | 96 | } bithenge_internal_node_ops_t;
|
---|
| 97 |
|
---|
[8375d0eb] | 98 | /** Find the type of a node.
|
---|
| 99 | * @memberof bithenge_node_t
|
---|
| 100 | * @param node The node.
|
---|
| 101 | * @return The type of the node. */
|
---|
| 102 | static inline bithenge_node_type_t bithenge_node_type(const bithenge_node_t *node)
|
---|
| 103 | {
|
---|
| 104 | return node->type;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | /** Iterate over a node's children.
|
---|
| 108 | * @memberof bithenge_node_t
|
---|
| 109 | * @param node The internal node to iterate over.
|
---|
| 110 | * @param func The callback function.
|
---|
| 111 | * @param data Data to provide to the callback function.
|
---|
| 112 | * @return EOK on success or an error code from errno.h. */
|
---|
[5f679702] | 113 | static inline int bithenge_node_for_each(bithenge_node_t *node, bithenge_for_each_func_t func, void *data)
|
---|
[11b9ad7] | 114 | {
|
---|
[5c925ce] | 115 | assert(node->type == BITHENGE_NODE_INTERNAL);
|
---|
[5f679702] | 116 | return node->internal_ops->for_each(node, func, data);
|
---|
[11b9ad7] | 117 | }
|
---|
| 118 |
|
---|
[8375d0eb] | 119 | /** Get the value of a boolean node.
|
---|
| 120 | * @memberof bithenge_node_t
|
---|
| 121 | * @param node The boolean node.
|
---|
| 122 | * @return The node's value. */
|
---|
[5f679702] | 123 | static inline bool bithenge_boolean_node_value(bithenge_node_t *node)
|
---|
[11b9ad7] | 124 | {
|
---|
[5c925ce] | 125 | assert(node->type == BITHENGE_NODE_BOOLEAN);
|
---|
[5f679702] | 126 | return node->boolean_value;
|
---|
[11b9ad7] | 127 | }
|
---|
| 128 |
|
---|
[8375d0eb] | 129 | /** Get the value of an integer node.
|
---|
| 130 | * @memberof bithenge_node_t
|
---|
| 131 | * @param node The integer node.
|
---|
| 132 | * @return The node's value. */
|
---|
[5f679702] | 133 | static inline bithenge_int_t bithenge_integer_node_value(bithenge_node_t *node)
|
---|
[11b9ad7] | 134 | {
|
---|
[5c925ce] | 135 | assert(node->type == BITHENGE_NODE_INTEGER);
|
---|
[5f679702] | 136 | return node->integer_value;
|
---|
[11b9ad7] | 137 | }
|
---|
| 138 |
|
---|
[8375d0eb] | 139 | /** Get the value of an string node.
|
---|
| 140 | * @memberof bithenge_node_t
|
---|
| 141 | * @param node The string node.
|
---|
| 142 | * @return The node's value. */
|
---|
[5f679702] | 143 | static inline const char *bithenge_string_node_value(bithenge_node_t *node)
|
---|
[11b9ad7] | 144 | {
|
---|
[5c925ce] | 145 | assert(node->type == BITHENGE_NODE_STRING);
|
---|
[5f679702] | 146 | return node->string_value.ptr;
|
---|
[11b9ad7] | 147 | }
|
---|
| 148 |
|
---|
[8375d0eb] | 149 | int bithenge_new_simple_internal_node(bithenge_node_t **, bithenge_node_t **,
|
---|
| 150 | bithenge_int_t, bool needs_free);
|
---|
[11b9ad7] | 151 | int bithenge_new_boolean_node(bithenge_node_t **, bool);
|
---|
| 152 | int bithenge_new_integer_node(bithenge_node_t **, bithenge_int_t);
|
---|
| 153 | int bithenge_new_string_node(bithenge_node_t **, const char *, bool);
|
---|
| 154 | int bithenge_node_destroy(bithenge_node_t *);
|
---|
| 155 |
|
---|
| 156 | #endif
|
---|
| 157 |
|
---|
| 158 | /** @}
|
---|
| 159 | */
|
---|