/usr/include/nih/tree.h is in libnih-dev 1.0.3-6ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | /* libnih
*
* Copyright © 2009 Scott James Remnant <scott@netsplit.com>.
* Copyright © 2009 Canonical Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef NIH_TREE_H
#define NIH_TREE_H
#include <nih/macros.h>
/**
* Provides a generic binary tree implementation. No assumption is
* made about the structure of the tree, or its rules. Instead when
* you add a node to a tree, you must specify the parent node and whether
* to add the new node to its left or right.
*
* Tree nodes may be created in one of two ways. The most common is to
* embed the NihTree structure as the first member of your own structure,
* and initialise it with nih_tree_init() after allocating the structure.
* Alternatively you may create NihTreeEntry structures with
* nih_tree_entry_new() and point at your own data from them.
*
* If you need no data for the tree root, you may use NihTree itself and
* allocate it with nih_tree_new().
*
* Nodes may be added to the tree with nih_tree_add(), passing the parent
* node, the new node and whether to add to the left or right.
*
* To remove a node from the tree, and its children, use nih_tree_remove();
* the node removed becomes the root of a new tree.
*
* Nodes may be moved between trees, or relocated within a tree, by simply
* calling nih_tree_add() - there's no need to call nih_tree_remove() first.
*
* A node may also be removed from a tree and from its children using
* nih_tree_unlink(); the node removed, and each of its children, become
* the roots of new trees.
*
* Tree-iteration may be performed non-recursively in a pre-order, in-order
* or post-order fashion; forwards or backwards. The functions
* nih_tree_next_full(), nih_tree_prev_full(), nih_tree_next_pre_full(),
* nih_tree_prev_pre_full(), nih_tree_next_post_full() and
* nih_tree_prev_post_full() all return the next or previous node, allowing
* for filtering. If you do not need to filter macros are provided that
* pass NULL, named without the _full extension.
*
* These are almost always used in a for loop, so macros are provided that
* expand to a for loop for each of the different orders;
* NIH_TREE_FOREACH_FULL(), NIH_TREE_FOREACH_PRE_FULL() and
* NIH_TREE_FOREACH_POST_FULL(). Versions which pass NULL for the filter
* are provided without the _FULL extension.
**/
/**
* NihTreeWhere:
*
* These constants define a position for one node, relative to another;
* usually for when adding a node to an existing tree.
**/
typedef enum {
NIH_TREE_LEFT = -1,
NIH_TREE_RIGHT = 1,
} NihTreeWhere;
/**
* NihTree:
* @parent: parent node in the tree,
* @left: left child node,
* @right: right child node.
*
* This structure can be used both to refer to a binary tree and can be
* placed in your own structures to use them as tree nodes.
*
* A node without any parent (root node) has @parent set to NULL, nodes
* without any children (leaf nodes) have @left and @right set to NULL.
*
* NihTree is most useful for implementing pure binary trees, where the
* properties of that structure (such as simple location or traversal) are
* desired.
*
* General trees (where each node may have zero or more nodes, beyond two)
* can be implemented using binary trees as described by Knuth (fundamentally,
* head right for siblings, left for children) or as lists of children in
* each node (such as used by nih_alloc); pick whichever suits your data
* best.
**/
typedef struct nih_tree {
struct nih_tree *parent, *left, *right;
} NihTree;
/**
* NihTreeEntry:
* @node: tree node,
* @data: data pointer,
* @str: string pointer,
* @int_data: integer value.
*
* This structure can be used as a generic NihTree node that contains
* a pointer to generic data, a string or contains an integer value.
*
* You should take care of setting the data yourself.
**/
typedef struct nih_tree_entry {
NihTree node;
union {
void *data;
char *str;
int int_data;
};
} NihTreeEntry;
/**
* NihTreeFilter:
* @data: data pointer,
* @node: node to be visited.
*
* A tree filter is a function that is called when iterating a tree to
* determine whether a particular node and its children should be ignored.
*
* Returns: TRUE if the node should be ignored, FALSE otherwise.
**/
typedef int (*NihTreeFilter) (void *data, NihTree *node);
/**
* NIH_TREE_FOREACH_FULL:
* @tree: root of the tree to iterate,
* @iter: name of iterator variable,
* @filter: filter function to test each node,
* @data: data pointer to pass to @filter.
*
* Expands to a for statement that in-order iterates over each node in @tree,
* setting @iter to each node for the block within the loop.
*
* If @filter is given, it will be called for each node visited and must
* return FALSE otherwise the node and its children will be ignored.
*
* You should not make changes to the structure of the tree while iterating,
* since the order will be relatively unpredictable.
**/
#define NIH_TREE_FOREACH_FULL(tree, iter, filter, data) \
for (NihTree *iter = nih_tree_next_full ((tree), NULL, (filter), (data)); \
iter != NULL; \
iter = nih_tree_next_full ((tree), iter, (filter), (data)))
/**
* NIH_TREE_FOREACH_PRE_FULL:
* @tree: root of the tree to iterate,
* @iter: name of iterator variable,
* @filter: filter function to test each node,
* @data: data pointer to pass to @filter.
*
* Expands to a for statement that pre-order iterates over each node in @tree,
* setting @iter to each node for the block within the loop.
*
* If @filter is given, it will be called for each node visited and must
* return FALSE otherwise the node and its children will be ignored.
*
* You should not make changes to the structure of the tree while iterating,
* since the order will be relatively unpredictable.
**/
#define NIH_TREE_FOREACH_PRE_FULL(tree, iter, filter, data) \
for (NihTree *iter = nih_tree_next_pre_full ((tree), NULL, (filter), (data)); \
iter != NULL; \
iter = nih_tree_next_pre_full ((tree), iter, (filter), (data)))
/**
* NIH_TREE_FOREACH_POST_FULL:
* @tree: root of the tree to iterate,
* @iter: name of iterator variable,
* @filter: filter function to test each node,
* @data: data pointer to pass to @filter.
*
* Expands to a for statement that post-order iterates over each node in @tree,
* setting @iter to each node for the block within the loop.
*
* If @filter is given, it will be called for each node visited and must
* return FALSE otherwise the node and its children will be ignored.
*
* You should not make changes to the structure of the tree while iterating,
* since the order will be relatively unpredictable.
**/
#define NIH_TREE_FOREACH_POST_FULL(tree, iter, filter, data) \
for (NihTree *iter = nih_tree_next_post_full ((tree), NULL, (filter), (data)); \
iter != NULL; \
iter = nih_tree_next_post_full ((tree), iter, (filter), (data)))
/**
* nih_tree_next:
* @tree: tree to iterate,
* @node: node just visited.
*
* Iterates the @tree in-order non-recursively; to obtain the first node,
* @tree should be set to the root of the tree and @node should be NULL.
* Then for subsequent nodes, @node should be the previous return value
* from this function.
*
* Returns: next in-order node within @tree or NULL if no further nodes.
**/
#define nih_tree_next(tree, node) \
nih_tree_next_full ((tree), (node), NULL, NULL)
/**
* nih_tree_prev:
* @tree: tree to iterate,
* @node: node just visited.
*
* Reverse-iterates the @tree in-order non-recursively; to obtain the last
* node, @tree should be set to the root of the tree and @node should be NULL.
* Then for subsequent nodes, @node should be the previous return value
* from this function.
*
* Returns: previous in-order node within @tree or NULL if no further nodes.
**/
#define nih_tree_prev(tree, node) \
nih_tree_prev_full ((tree), (node), NULL, NULL)
/**
* nih_tree_next_pre:
* @tree: tree to iterate,
* @node: node just visited.
*
* Iterates the @tree in-order non-recursively; to obtain the first node,
* @tree should be set to the root of the tree and @node should be NULL.
* Then for subsequent nodes, @node should be the previous return value
* from this function.
*
* Returns: next in-order node within @tree or NULL if no further nodes.
**/
#define nih_tree_next_pre(tree, node) \
nih_tree_next_pre_full ((tree), (node), NULL, NULL)
/**
* nih_tree_prev_pre:
* @tree: tree to iterate,
* @node: node just visited.
*
* Reverse-iterates the @tree in-order non-recursively; to obtain the last
* node, @tree should be set to the root of the tree and @node should be NULL.
* Then for subsequent nodes, @node should be the previous return value
* from this function.
*
* Returns: previous in-order node within @tree or NULL if no further nodes.
**/
#define nih_tree_prev_pre(tree, node) \
nih_tree_prev_pre_full ((tree), (node), NULL, NULL)
/**
* nih_tree_next_post:
* @tree: tree to iterate,
* @node: node just visited.
*
* Iterates the @tree in-order non-recursively; to obtain the first node,
* @tree should be set to the root of the tree and @node should be NULL.
* Then for subsequent nodes, @node should be the previous return value
* from this function.
*
* Returns: next in-order node within @tree or NULL if no further nodes.
**/
#define nih_tree_next_post(tree, node) \
nih_tree_next_post_full ((tree), (node), NULL, NULL)
/**
* nih_tree_prev_post:
* @tree: tree to iterate,
* @node: node just visited.
*
* Reverse-iterates the @tree in-order non-recursively; to obtain the last
* node, @tree should be set to the root of the tree and @node should be NULL.
* Then for subsequent nodes, @node should be the previous return value
* from this function.
*
* Returns: previous in-order node within @tree or NULL if no further nodes.
**/
#define nih_tree_prev_post(tree, node) \
nih_tree_prev_post_full ((tree), (node), NULL, NULL)
/**
* NIH_TREE_FOREACH:
* @tree: root of the tree to iterate,
* @iter: name of iterator variable.
*
* Expands to a for statement that in-order iterates over each node in @tree,
* setting @iter to each node for the block within the loop.
*
* You should not make changes to the structure of the tree while iterating,
* since the order will be relatively unpredictable.
**/
#define NIH_TREE_FOREACH(tree, iter) \
for (NihTree *iter = nih_tree_next ((tree), NULL); iter != NULL; \
iter = nih_tree_next ((tree), iter))
/**
* NIH_TREE_FOREACH_PRE:
* @tree: root of the tree to iterate,
* @iter: name of iterator variable.
*
* Expands to a for statement that pre-order iterates over each node in @tree,
* setting @iter to each node for the block within the loop.
*
* You should not make changes to the structure of the tree while iterating,
* since the order will be relatively unpredictable.
**/
#define NIH_TREE_FOREACH_PRE(tree, iter) \
for (NihTree *iter = nih_tree_next_pre ((tree), NULL); iter != NULL; \
iter = nih_tree_next_pre ((tree), iter))
/**
* NIH_TREE_FOREACH_POST:
* @tree: root of the tree to iterate,
* @iter: name of iterator variable.
*
* Expands to a for statement that post-order iterates over each node in @tree,
* setting @iter to each node for the block within the loop.
*
* You should not make changes to the structure of the tree while iterating,
* since the order will be relatively unpredictable.
**/
#define NIH_TREE_FOREACH_POST(tree, iter) \
for (NihTree *iter = nih_tree_next_post ((tree), NULL); iter != NULL; \
iter = nih_tree_next_post ((tree), iter))
NIH_BEGIN_EXTERN
void nih_tree_init (NihTree *tree);
NihTree * nih_tree_new (const void *parent)
__attribute__ ((warn_unused_result));
NihTreeEntry *nih_tree_entry_new (const void *parent)
__attribute__ ((warn_unused_result));
NihTree * nih_tree_add (NihTree *tree, NihTree *node,
NihTreeWhere where);
NihTree * nih_tree_remove (NihTree *node);
NihTree * nih_tree_unlink (NihTree *node);
int nih_tree_destroy (NihTree *node);
NihTree * nih_tree_next_full (NihTree *tree, NihTree *node,
NihTreeFilter filter, void *data);
NihTree * nih_tree_prev_full (NihTree *tree, NihTree *node,
NihTreeFilter filter, void *data);
NihTree * nih_tree_next_pre_full (NihTree *tree, NihTree *node,
NihTreeFilter filter, void *data);
NihTree * nih_tree_prev_pre_full (NihTree *tree, NihTree *node,
NihTreeFilter filter, void *data);
NihTree * nih_tree_next_post_full (NihTree *tree, NihTree *node,
NihTreeFilter filter, void *data);
NihTree * nih_tree_prev_post_full (NihTree *tree, NihTree *node,
NihTreeFilter filter, void *data);
NIH_END_EXTERN
#endif /* NIH_TREE_H */
|