MentOS  0.8.0
The Mentoring Operating System
ndtree.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 // ============================================================================
9 // Opaque types.
10 
12 typedef struct ndtree_node_t ndtree_node_t;
14 typedef struct ndtree_t ndtree_t;
16 typedef struct ndtree_iter_t ndtree_iter_t;
17 
18 // ============================================================================
19 // Comparison functions.
20 
22 typedef int (*ndtree_tree_cmp_f)(ndtree_t *tree, void *lhs, void *rhs);
24 typedef void (*ndtree_tree_node_f)(ndtree_t *tree, ndtree_node_t *node);
25 
26 // ============================================================================
27 // Node management functions.
28 
32 
36 ndtree_node_t *ndtree_node_create(void *value);
37 
42 ndtree_node_t *ndtree_node_init(ndtree_node_t *node, void *value);
43 
47 void ndtree_node_set_value(ndtree_node_t *node, void *value);
48 
53 
57 void ndtree_set_root(ndtree_t *tree, ndtree_node_t *node);
58 
63 ndtree_node_t *ndtree_create_root(ndtree_t *tree, void *value);
64 
69 
75 
82 
86 unsigned int ndtree_node_count_children(ndtree_node_t *node);
87 
91 
92 // ============================================================================
93 // Tree management functions.
94 
98 
103 
107 void ndtree_tree_dealloc(ndtree_t *tree, ndtree_tree_node_f node_cb);
108 
114 
120 ndtree_node_t *ndtree_tree_find(ndtree_t *tree, ndtree_tree_cmp_f cmp, void *value);
121 
129 
133 unsigned int ndtree_tree_size(ndtree_t *tree);
134 
145 
155 int ndtree_tree_remove_with_cb(ndtree_t *tree, void *value, ndtree_tree_node_f node_cb);
156 
157 // ============================================================================
158 // Iterators.
159 
163 
167 
173 
179 
184 
189 
190 // ============================================================================
191 // Tree visiting functions.
192 
197 void ndtree_tree_visitor(ndtree_t *tree, ndtree_tree_node_f enter_fun, ndtree_tree_node_f exit_fun);
ndtree_node_t * ndtree_create_root(ndtree_t *tree, void *value)
Creates a new node and assigns it as root of the tree.
Definition: ndtree.c:107
ndtree_node_t * ndtree_iter_last(ndtree_node_t *node, ndtree_iter_t *iter)
Initializes the iterator the the last child of the node.
Definition: ndtree.c:346
ndtree_t * ndtree_tree_create(ndtree_tree_cmp_f cmp)
Allocate memory for a tree and sets the function used to compare nodes.
Definition: ndtree.c:159
void ndtree_tree_dealloc(ndtree_t *tree, ndtree_tree_node_f node_cb)
Deallocate a node.
Definition: ndtree.c:197
int ndtree_tree_remove_node_with_cb(ndtree_t *tree, ndtree_node_t *node, ndtree_tree_node_f node_cb)
Removes the node from the given tree.
Definition: ndtree.c:271
void ndtree_set_root(ndtree_t *tree, ndtree_node_t *node)
Sets the given node as root of the tree.
Definition: ndtree.c:101
ndtree_node_t * ndtree_iter_first(ndtree_node_t *node, ndtree_iter_t *iter)
Initializes the iterator the the first child of the node.
Definition: ndtree.c:334
int ndtree_tree_remove_with_cb(ndtree_t *tree, void *value, ndtree_tree_node_f node_cb)
Removes the node from the given tree.
Definition: ndtree.c:308
void ndtree_node_dealloc(ndtree_node_t *node)
Deallocate a node.
Definition: ndtree.c:145
void ndtree_node_set_value(ndtree_node_t *node, void *value)
Sets the value of the given node.
Definition: ndtree.c:86
ndtree_node_t * ndtree_node_init(ndtree_node_t *node, void *value)
Initializes the already allocated node.
Definition: ndtree.c:75
ndtree_node_t * ndtree_node_alloc(void)
Allocate memory for a node.
Definition: ndtree.c:63
unsigned int ndtree_node_count_children(ndtree_node_t *node)
Counts the number of children of the given node.
Definition: ndtree.c:133
void ndtree_add_child_to_node(ndtree_t *tree, ndtree_node_t *parent, ndtree_node_t *child)
Adds the given child as child of parent.
Definition: ndtree.c:119
ndtree_node_t * ndtree_iter_next(ndtree_iter_t *iter)
Moves the iterator to the next element.
Definition: ndtree.c:358
int(* ndtree_tree_cmp_f)(ndtree_t *tree, void *lhs, void *rhs)
Function for comparing elements in the tree.
Definition: ndtree.h:22
void ndtree_tree_visitor(ndtree_t *tree, ndtree_tree_node_f enter_fun, ndtree_tree_node_f exit_fun)
Run a visit of the tree (DFS).
Definition: ndtree.c:407
ndtree_t * ndtree_tree_alloc(void)
Allocate memory for a tree.
Definition: ndtree.c:154
ndtree_iter_t * ndtree_iter_alloc(void)
Allocate the memory for the iterator.
Definition: ndtree.c:319
ndtree_node_t * ndtree_get_root(ndtree_t *tree)
Provides access to the root of the tree.
Definition: ndtree.c:114
ndtree_node_t * ndtree_node_create(void *value)
Allocate memory for a node and sets its value.
Definition: ndtree.c:68
void ndtree_iter_dealloc(ndtree_iter_t *iter)
Deallocate the memory for the iterator.
Definition: ndtree.c:327
void(* ndtree_tree_node_f)(ndtree_t *tree, ndtree_node_t *node)
Callback to call on elements of the tree.
Definition: ndtree.h:24
ndtree_node_t * ndtree_tree_find(ndtree_t *tree, ndtree_tree_cmp_f cmp, void *value)
Searches the node inside the tree with the given value.
Definition: ndtree.c:230
ndtree_node_t * ndtree_create_child_of_node(ndtree_t *tree, ndtree_node_t *parent, void *value)
Creates a new node and sets it as child of parent.
Definition: ndtree.c:126
ndtree_node_t * ndtree_iter_prev(ndtree_iter_t *iter)
Moves the iterator to the previous element.
Definition: ndtree.c:369
ndtree_node_t * ndtree_node_find(ndtree_t *tree, ndtree_node_t *node, ndtree_tree_cmp_f cmp, void *value)
Searches the given value among the children of node.
Definition: ndtree.c:238
unsigned int ndtree_tree_size(ndtree_t *tree)
Returns the size of the tree.
Definition: ndtree.c:263
void * ndtree_node_get_value(ndtree_node_t *node)
Provides access to the value associated to a node.
Definition: ndtree.c:93
ndtree_t * ndtree_tree_init(ndtree_t *tree, ndtree_tree_cmp_f cmp)
Initializes the tree.
Definition: ndtree.c:164
Stores data about an NDTree iterator.
Definition: ndtree.c:40
Stores data about an NDTree node.
Definition: ndtree.c:16
Stores data about an NDTree.
Definition: ndtree.c:28