GRU - Generic Reusable Utilities
gru_tree.h
Go to the documentation of this file.
1 /*
2  Copyright 2016 Otavio Rodolfo Piske
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16 #ifndef GRU_TREE_H
17 #define GRU_TREE_H
18 
20 #include "gru_list.h"
21 #include "gru_node.h"
22 
23 #include "common/gru_portable.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 /*
30  * An extremely simple unbalanced tree implementation
31  */
32 
33 typedef struct gru_tree_node_t_ {
35  const void *data;
37 
43 gru_export gru_tree_node_t *gru_tree_new(const void *data);
44 
50 
59 
68  compare_function_t comparable,
69  const void *other);
70 
79  compare_function_t comparable,
80  const void *other);
81 
90  compare_function_t comparable,
91  const void *other);
92 
101  gru_tree_for_each(gru_tree_node_t *node, tree_callback_fn callback, void *payload);
102 
110  tree_callback_fn callback,
111  void *payload);
112 
120 
128 
129 #ifdef __cplusplus
130 }
131 #endif
132 
133 #endif /* GRU_TREE_H */
bool(* compare_function_t)(const void *, const void *data, void *result)
Comparator function for the collections module.
Definition: gru_collection_callbacks.h:30
handle_function_t tree_callback_fn
Definition: gru_collection_callbacks.h:42
#define gru_export
Definition: gru_portable.h:19
gru_export uint32_t gru_tree_count(gru_tree_node_t *node)
Returns the number of children of a tree.
Definition: gru_tree.c:230
gru_export uint32_t gru_tree_count_children(gru_tree_node_t *node)
Returns the number of children of a node.
Definition: gru_tree.c:212
gru_export void gru_tree_for_each_child(gru_tree_node_t *node, tree_callback_fn callback, void *payload)
Executes an operation on each child of the given node.
Definition: gru_tree.c:190
gru_export gru_tree_node_t * gru_tree_add_child(gru_tree_node_t *node, const void *data)
Adds a child node to a given node.
Definition: gru_tree.c:61
gru_export bool gru_tree_remove_child(gru_tree_node_t *node, compare_function_t comparable, const void *other)
Removes a direct descendant of a node.
Definition: gru_tree.c:109
gru_export void gru_tree_destroy(gru_tree_node_t **ptr)
Destroys a tree node and all it's descendants.
Definition: gru_tree.c:45
gru_export const gru_tree_node_t * gru_tree_search(gru_tree_node_t *node, compare_function_t comparable, const void *other)
Searches the tree using DFS.
Definition: gru_tree.c:78
gru_export const gru_tree_node_t * gru_tree_search_child(gru_tree_node_t *node, compare_function_t comparable, const void *other)
Searches only the immediate children of the node.
Definition: gru_tree.c:237
gru_export const gru_tree_node_t * gru_tree_for_each(gru_tree_node_t *node, tree_callback_fn callback, void *payload)
Traverses the tree executing a set of operations.
Definition: gru_tree.c:144
struct gru_tree_node_t_ gru_tree_node_t
gru_export gru_tree_node_t * gru_tree_new(const void *data)
Creates a new tree and returns a pointer to the root node.
Definition: gru_tree.c:18
Definition: gru_list.h:41
Definition: gru_tree.h:33
gru_list_t * children
Definition: gru_tree.h:34
const void * data
Definition: gru_tree.h:35