10 #include "cgds/safe_alloc.h"
11 #include "cgds/types.h"
18 * @brief Tree node, containing some generic data.
20 typedef struct TreeNode
{
21 void* data
; ///< Generic data contained in this node.
22 struct TreeNode
* parent
; ///< Pointer to parent node (NULL if node is root).
23 struct TreeNode
* firstChild
; ///< Pointer to the first child (if any).
24 struct TreeNode
* lastChild
; ///< Pointer to the last child (if any).
25 struct TreeNode
* prev
; ///< Pointer to the previous sibling (on the left).
26 struct TreeNode
* next
; ///< Pointer to the next sibling (on the right).
30 * @brief Generic multi-ary tree.
33 TreeNode
* root
; ///< Root node of the tree.
34 size_t dataSize
; ///< Size of *data at a tree node, in bytes.
35 UInt size
; ///< Count nodes in the tree.
39 * @brief Initialize an empty tree.
42 Tree
* tree
, ///< "this" pointer.
43 size_t dataSize
///< Size in bytes of a tree element.
47 * @brief Return an allocated and initialized tree.
50 size_t dataSize
///< Size in bytes of a tree node element.
54 * @brief Return an allocated and initialized tree.
55 * @param type Type at a tree node (int, char*, ...).
57 * Usage: Tree* tree_new(<Type> type)
59 #define tree_new(type) \
60 _tree_new(sizeof(type))
63 * @brief Copy constructor (works well if items do not have allocated sub-pointers).
66 Tree
* tree
///< "this" pointer.
70 * @brief Check if the tree is empty.
73 Tree
* tree
///< "this" pointer.
77 * @brief return current size of the tree (counting nodes).
80 Tree
* tree
///< "this" pointer.
84 * @brief Auxiliary function to get tree height.
86 UInt
_tree_height_rekursiv(
87 TreeNode
* treeNode
///< Pointer to a node in the "this" tree.
91 * @brief Return tree height (max depth from root to leaves).
94 Tree
* tree
///< "this" pointer.
98 * @brief Check if a sub-tree is a leaf (without children).
101 TreeNode
* treeNode
///< Pointer to a node in the "this" tree.
105 * @brief Initialize tree root when the tree is empty.
108 Tree
* tree
, ///< "this" pointer.
109 void* data
///< Pointer to data to be assigned.
113 * @brief Initialize tree root when the tree is empty.
114 * @param tree "this" pointer.
115 * @param data Data to be assigned.
117 * Usage: void tree_set_root(Tree* tree, void data)
119 #define tree_set_root(tree, data) \
121 typeof((data)) tmp = data; \
122 _tree_set_root(tree, &tmp); \
126 * @brief Return data contained in a given tree node.
129 TreeNode
* treeNode
///< Pointer to a node in the "this" tree.
133 * @brief Retrieve data contained in a given tree node.
134 * @param treeNode Pointer to a node in the "this" tree.
135 * @param data Data to be assigned.
137 * Usage: void tree_get(TreeNode* treeNode, void data)
139 #define tree_get(treeNode, data) \
141 void* pData = _tree_get(treeNode); \
142 data = *((typeof(&data))pData); \
146 * @brief Set (alter) data at tree node passed as argument.
149 Tree
* tree
, ///< "this" pointer.
150 TreeNode
* treeNode
, ///< Pointer to a node in the "this" tree.
151 void* data
///< Pointer to data to be assigned.
155 * @brief Set (alter) data at tree node passed as argument.
156 * @param tree "this" pointer.
157 * @param treeNode Pointer to a node in the "this" tree.
158 * @param data Data to be assigned.
160 * Usage: void tree_set(Tree* tree, TreeNode* treeNode, void data)
162 #define tree_set(tree, treeNode, data) \
164 typeof((data)) tmp = data; \
165 _tree_set(tree,treeNode,&tmp); \
169 * @brief Add a child to current node children.
171 void _tree_add_child(
172 Tree
* tree
, ///< "this" pointer.
173 TreeNode
* treeNode
, ///< Pointer to a node in the "this" tree.
174 void* data
///< Pointer to data to be added.
178 * @brief Add a child to current node children.
179 * @param tree "this" pointer.
180 * @param treeNode Pointer to a node in the "this" tree.
181 * @param data Data to be added.
183 * Usage: void tree_add_child(Tree* tree, TreeNode* treeNode, void data)
185 #define tree_add_child(tree,treeNode,data) \
187 typeof((data)) tmp = data; \
188 _tree_add_child(tree,treeNode,&tmp); \
192 * @brief Add a sibling to current node (after it on the right).
194 void _tree_add_sibling(
195 Tree
* tree
, ///< "this" pointer.
196 TreeNode
* treeNode
, ///< Pointer to a node in the "this" tree.
197 void* data
///< Pointer to data to be added.
201 * @brief Add a sibling to current node (after it on the right).
202 * @param tree "this" pointer.
203 * @param treeNode Pointer to a node in the "this" tree.
204 * @param data Data to be added.
206 * Usage: void tree_add_sibling(Tree* tree, TreeNode* treeNode, void data)
208 #define tree_add_sibling(tree, treeNode, data) \
210 typeof((data)) tmp = data; \
211 _tree_add_sibling(tree, treeNode, &tmp); \
215 * @brief Auxiliary to remove a subtree.
217 void _tree_remove_rekursiv(
218 Tree
* tree
, ///< "this" pointer.
219 TreeNode
* treeNode
///< Pointer to a node in the "this" tree.
223 * @brief Remove the whole subtree rooted at 'treeNode'.
226 Tree
* tree
, ///< "this" pointer.
227 TreeNode
* treeNode
///< Pointer to a node in the "this" tree.
231 * @brief Remove children of the tree node given as argument.
234 Tree
* tree
, ///< "this" pointer.
235 TreeNode
* treeNode
///< Pointer to a node in the "this" tree.
239 * @brief Clear the entire tree.
242 Tree
* tree
///< "this" pointer.
246 * @brief Destroy the tree: clear it, and free 'tree' pointer.
249 Tree
* tree
///< "this" pointer.
257 * @brief Type of tree search: depth first or breadth first walk.
259 typedef enum TreeIteratorMode
{
260 IN_DEPTH
= 0, ///< Depth first.
261 IN_BREADTH
= 1 ///< Breadth first.
265 * @brief Iterator on a tree object.
267 typedef struct TreeIterator
{
268 Tree
* tree
; ///< Pointer to the tree to iterate over.
269 TreeNode
* current
; ///< Current iterator position.
270 TreeIteratorMode mode
; ///< Mode of iteration (in depth or in breadth).
274 * @brief Obtain an iterator object, starting at tree root.
276 TreeIterator
* tree_get_iterator(
277 Tree
* tree
, ///< Pointer to the tree to iterate over.
278 TreeIteratorMode mode
///< Mode of iteration (in depth or in breadth).
282 * @brief (Re)set current position inside tree to root.
285 TreeIterator
* treeI
///< "this" pointer.
289 * @brief Tell if there is some data at the current index.
292 TreeIterator
* treeI
///< "this" pointer.
296 * @brief Return current tree node.
298 TreeNode
* treeI_get_raw(
299 TreeIterator
* treeI
///< "this" pointer.
303 * @brief Get data at current tree node.
304 * @param treeI "this" pointer.
305 * @param data Data to be assigned.
307 * Usage: void treeI_get(TreeIterator* treeI, void data)
309 #define treeI_get(treeI, data) \
310 tree_get(treeI->current, data)
313 * @brief Set (alter) data at current tree node.
314 * @param treeI "this" pointer.
315 * @param data Data to be assigned.
317 * Usage: void treeI_set(TreeIterator* treeI, void data)
319 #define treeI_set(treeI, data) \
320 tree_set(treeI->tree, treeI->current, data)
323 * @brief Move current iterator position forward (toward leaves).
325 void treeI_move_next(
326 TreeIterator
* treeI
///< "this" pointer.
330 * @brief Free memory allocated for the iterator.
333 TreeIterator
* treeI
///< "this" pointer.