GRU - Generic Reusable Utilities
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
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  gru_tree_node_t *node, compare_function_t comparable, const void *other);
69 
78  gru_tree_node_t *node, compare_function_t comparable, const void *other);
79 
88  gru_tree_node_t *node, compare_function_t comparable, const void *other);
89 
98  gru_tree_node_t *node, tree_callback_fn callback, void *payload);
99 
107  gru_tree_node_t *node, tree_callback_fn callback, void *payload);
108 
116 
124 
125 #ifdef __cplusplus
126 }
127 #endif
128 
129 #endif /* GRU_TREE_H */
handle_function_t tree_callback_fn
Definition: gru_collection_callbacks.h:42
Definition: gru_list.h:36
uint32_t gru_tree_count(gru_tree_node_t *node)
Returns the number of children of a tree.
Definition: gru_tree.c:227
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
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:141
#define gru_export
Definition: gru_portable.h:19
void gru_tree_destroy(gru_tree_node_t **ptr)
Destroys a tree node and all it's descendants.
Definition: gru_tree.c:45
struct gru_tree_node_t_ gru_tree_node_t
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:108
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:234
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:188
bool(* compare_function_t)(const void *, const void *data, void *result)
Comparator function for the collections module.
Definition: gru_collection_callbacks.h:30
uint32_t gru_tree_count_children(gru_tree_node_t *node)
Returns the number of children of a node.
Definition: gru_tree.c:209
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
Definition: gru_tree.h:33
gru_list_t * children
Definition: gru_tree.h:34
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
const void * data
Definition: gru_tree.h:35