source: mainline/uspace/app/bithenge/tree.h@ 842ed146

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

Bithenge: add bithenge_node_get

  • Property mode set to 100644
File size: 5.9 KB
RevLine 
[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 <inttypes.h>
42#include <sys/types.h>
[da0fef6]43#include "os.h"
[11b9ad7]44
45#ifdef INTMAX_MAX
46typedef intmax_t bithenge_int_t;
47#define BITHENGE_PRId PRIdMAX
48#else
49typedef int64_t bithenge_int_t;
50#define BITHENGE_PRId PRId64
51#endif
52
[8375d0eb]53/** Indicates the type of a tree node. */
[11b9ad7]54typedef 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
67typedef struct bithenge_node_t {
[8375d0eb]68 /** @privatesection */
[11b9ad7]69 bithenge_node_type_t type;
[f2da0bb]70 unsigned int refs;
[5f679702]71 union {
[5c925ce]72 const struct bithenge_internal_node_ops_t *internal_ops;
[5f679702]73 bool boolean_value;
74 bithenge_int_t integer_value;
75 struct {
76 const char *ptr;
77 bool needs_free;
78 } string_value;
[5c925ce]79 const struct bithenge_random_access_blob_ops_t *blob_ops;
[5f679702]80 };
[11b9ad7]81} bithenge_node_t;
82
[04a7435f]83/** A callback function used to iterate over a node's children. It takes
84 * ownership of a reference to both the key and the value.
[8375d0eb]85 * @memberof bithenge_node_t
86 * @param key The key.
87 * @param value The value.
88 * @param data Data provided to @a bithenge_node_t::bithenge_node_for_each.
89 * @return EOK on success or an error code from errno.h. */
[11b9ad7]90typedef int (*bithenge_for_each_func_t)(bithenge_node_t *key, bithenge_node_t *value, void *data);
91
[8375d0eb]92/** Operations providing access to an internal node. */
[11b9ad7]93typedef struct bithenge_internal_node_ops_t {
[8375d0eb]94 /** @copydoc bithenge_node_t::bithenge_node_for_each */
[978ccaf1]95 int (*for_each)(bithenge_node_t *self, bithenge_for_each_func_t func, void *data);
[e8e31d9]96 /** @copydoc bithenge_node_t::bithenge_node_get */
97 int (*get)(bithenge_node_t *self, bithenge_node_t *key,
98 bithenge_node_t **out);
[978ccaf1]99 /** Destroys the internal node.
100 * @param self The node to destroy. */
101 void (*destroy)(bithenge_node_t *self);
[11b9ad7]102} bithenge_internal_node_ops_t;
103
[8375d0eb]104/** Find the type of a node.
105 * @memberof bithenge_node_t
106 * @param node The node.
107 * @return The type of the node. */
108static inline bithenge_node_type_t bithenge_node_type(const bithenge_node_t *node)
109{
110 return node->type;
111}
112
[f2da0bb]113/** Increment a node's reference count.
114 * @memberof bithenge_node_t
[978ccaf1]115 * @param node The node to reference. */
116static inline void bithenge_node_inc_ref(bithenge_node_t *node)
[f2da0bb]117{
118 assert(node);
119 node->refs++;
120}
121
[978ccaf1]122void bithenge_node_dec_ref(bithenge_node_t *node);
[f2da0bb]123
[8375d0eb]124/** Iterate over a node's children.
125 * @memberof bithenge_node_t
[978ccaf1]126 * @param self The internal node to iterate over.
[8375d0eb]127 * @param func The callback function.
128 * @param data Data to provide to the callback function.
129 * @return EOK on success or an error code from errno.h. */
[978ccaf1]130static inline int bithenge_node_for_each(bithenge_node_t *self,
131 bithenge_for_each_func_t func, void *data)
[11b9ad7]132{
[978ccaf1]133 assert(self->type == BITHENGE_NODE_INTERNAL);
134 return self->internal_ops->for_each(self, func, data);
[11b9ad7]135}
136
[e8e31d9]137int bithenge_node_get(bithenge_node_t *, bithenge_node_t *,
138 bithenge_node_t **);
139
[8375d0eb]140/** Get the value of a boolean node.
141 * @memberof bithenge_node_t
[978ccaf1]142 * @param self The boolean node.
[8375d0eb]143 * @return The node's value. */
[978ccaf1]144static inline bool bithenge_boolean_node_value(bithenge_node_t *self)
[11b9ad7]145{
[978ccaf1]146 assert(self->type == BITHENGE_NODE_BOOLEAN);
147 return self->boolean_value;
[11b9ad7]148}
149
[8375d0eb]150/** Get the value of an integer node.
151 * @memberof bithenge_node_t
[978ccaf1]152 * @param self The integer node.
[8375d0eb]153 * @return The node's value. */
[978ccaf1]154static inline bithenge_int_t bithenge_integer_node_value(bithenge_node_t *self)
[11b9ad7]155{
[978ccaf1]156 assert(self->type == BITHENGE_NODE_INTEGER);
157 return self->integer_value;
[11b9ad7]158}
159
[8375d0eb]160/** Get the value of an string node.
161 * @memberof bithenge_node_t
[978ccaf1]162 * @param self The string node.
[8375d0eb]163 * @return The node's value. */
[978ccaf1]164static inline const char *bithenge_string_node_value(bithenge_node_t *self)
[11b9ad7]165{
[978ccaf1]166 assert(self->type == BITHENGE_NODE_STRING);
167 return self->string_value.ptr;
[11b9ad7]168}
169
[04a7435f]170int bithenge_init_internal_node(bithenge_node_t *,
171 const bithenge_internal_node_ops_t *);
[8375d0eb]172int bithenge_new_simple_internal_node(bithenge_node_t **, bithenge_node_t **,
173 bithenge_int_t, bool needs_free);
[11b9ad7]174int bithenge_new_boolean_node(bithenge_node_t **, bool);
175int bithenge_new_integer_node(bithenge_node_t **, bithenge_int_t);
176int bithenge_new_string_node(bithenge_node_t **, const char *, bool);
[d5070ef]177bool bithenge_node_equal(bithenge_node_t *, bithenge_node_t *);
[11b9ad7]178
179#endif
180
181/** @}
182 */
Note: See TracBrowser for help on using the repository browser.