GRU - Generic Reusable Utilities
Data Structures | Typedefs | Functions
gru_tree.h File Reference
#include "gru_collection_callbacks.h"
#include "gru_list.h"
#include "gru_node.h"
#include "common/gru_portable.h"

Go to the source code of this file.

Data Structures

struct  gru_tree_node_t_
 

Typedefs

typedef struct gru_tree_node_t_ gru_tree_node_t
 

Functions

gru_export gru_tree_node_tgru_tree_new (const void *data)
 Creates a new tree and returns a pointer to the root node. More...
 
gru_export void gru_tree_destroy (gru_tree_node_t **ptr)
 Destroys a tree node and all it's descendants. More...
 
gru_export gru_tree_node_tgru_tree_add_child (gru_tree_node_t *node, const void *data)
 Adds a child node to a given node. More...
 
gru_export const gru_tree_node_tgru_tree_search (gru_tree_node_t *node, compare_function_t comparable, const void *other)
 Searches the tree using DFS. More...
 
gru_export const gru_tree_node_tgru_tree_search_child (gru_tree_node_t *node, compare_function_t comparable, const void *other)
 Searches only the immediate children of the node. More...
 
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. More...
 
gru_export const gru_tree_node_tgru_tree_for_each (gru_tree_node_t *node, tree_callback_fn callback, void *payload)
 Traverses the tree executing a set of operations. More...
 
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. More...
 
gru_export uint32_t gru_tree_count_children (gru_tree_node_t *node)
 Returns the number of children of a node. More...
 
gru_export uint32_t gru_tree_count (gru_tree_node_t *node)
 Returns the number of children of a tree. More...
 

Typedef Documentation

◆ gru_tree_node_t

Function Documentation

◆ gru_tree_add_child()

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.

Parameters
nodeThe node to add the child to
dataThe data to add to the child
Returns
A pointer to the newly added node. This node does NOT need to be free'd. It will be free'd along with its parent

◆ gru_tree_count()

gru_export uint32_t gru_tree_count ( gru_tree_node_t node)

Returns the number of children of a tree.

Parameters
nodeThe node to count the number of children
Returns
The number of items
Note
The node must not be NULL, otherwise it returns 0

◆ gru_tree_count_children()

gru_export uint32_t gru_tree_count_children ( gru_tree_node_t node)

Returns the number of children of a node.

Parameters
nodeThe node to count the number of children
Returns
The number of items
Note
The node must not be NULL, otherwise it returns 0

◆ gru_tree_destroy()

gru_export void gru_tree_destroy ( gru_tree_node_t **  ptr)

Destroys a tree node and all it's descendants.

Parameters
ptrA pointer-to-pointer for the node to destroy

◆ gru_tree_for_each()

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.

Parameters
nodeThe starting node for the search
callbackA callback function to be executed for all the nodes
payloadPayload data to be passed to the passed to the callback
Returns
A pointer to the node or NULL if not found

◆ gru_tree_for_each_child()

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.

Parameters
nodeThe starting node for the search
callbackA callback function to be executed for all the nodes
payloadPayload data to be passed to the passed to the callback

◆ gru_tree_new()

gru_export gru_tree_node_t* gru_tree_new ( const void *  data)

Creates a new tree and returns a pointer to the root node.

Parameters
dataThe data for the root node
Returns
A pointer to the root node

◆ gru_tree_remove_child()

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.

Parameters
nodeThe parent node
comparableA comparator function
otherThe data to compare to
Returns
true if removed or false otherwise

◆ gru_tree_search()

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.

Parameters
nodeThe starting node for the search
comparableA comparator function
otherThe data to compare to
Returns
A pointer to the node or NULL if not found

◆ gru_tree_search_child()

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.

Parameters
nodeThe starting node for the search
comparableA comparator function
otherThe data to compare to
Returns
A pointer to the node or NULL if not found