Structures and function definitions to handle a tree structure.
More...
Go to the source code of this file.
|
#define | LEAF_FIELDS(X) |
|
#define | LEAF_ROOT 0 |
|
#define | iter_childs(ROOT, LEAF) |
|
#define | free_childs(ROOT, LEAF, FREE) |
|
#define | set_leaf(LEAF, PARENT, NEXT, FIRST_CHILD, LEVEL) |
|
#define | init_leaf(LEAF) set_leaf(LEAF, NULL, NULL, NULL, -1) |
|
#define | set_childless(LEAF) (LEAF)->first_child = NULL |
|
#define | add_child(PARENT, CHILD) |
|
#define | mk_recursive(func, type) |
|
#define | mk_tail_recursive(func, type) |
|
#define | _TREE_H_ |
|
Structures and function definitions to handle a tree structure.
Definition in file tree.h.
Value:X *parent; \
X *next; \
X *first_child; \
int level; \
< These fields are included as the first fields of any structure that we want to convert into leaves of a tree. For people familiar with object orientation, this is equivalent to defining a class leaf and make other structures derive from it. The idea here is that we can share the code for the management of the tree structure of the grids both for the poisson and the cdr parts.
Definition at line 15 of file tree.h.
Iterate over all childs of a node. Simply write
leaf_t leaf;
iter_childs (node, leaf) { do_something (leaf); }
Definition at line 21 of file tree.h.
#define iter_childs |
( |
|
ROOT, |
|
|
|
LEAF |
|
) |
| |
Value:for (LEAF = ROOT->first_child; LEAF; \
LEAF = LEAF->next)
iter_child may not work if in the loop body we make free(LEAF) (though this will go unnoticed most of the time). So when freeing grids, the following macro has to be used instead:
Definition at line 31 of file tree.h.
#define free_childs |
( |
|
ROOT, |
|
|
|
LEAF, |
|
|
|
FREE |
|
) |
| |
Value:
for (LEAF = ROOT->first_child; LEAF; LEAF = (typeof(LEAF)) next__) { \
next__ = ((
grid_t *) LEAF)->next; \
FREE (LEAF); \
} \
} while(0)
This is used to put into a leaf any set of values.
Definition at line 41 of file tree.h.
#define set_leaf |
( |
|
LEAF, |
|
|
|
PARENT, |
|
|
|
NEXT, |
|
|
|
FIRST_CHILD, |
|
|
|
LEVEL |
|
) |
| |
Value:do { \
LEAF->parent = PARENT; \
LEAF->next = NEXT; \
LEAF->first_child = FIRST_CHILD; \
LEAF->level = LEVEL; \
} while(0)
Definition at line 58 of file tree.h.
#define init_leaf |
( |
|
LEAF | ) |
set_leaf(LEAF, NULL, NULL, NULL, -1) |
Sometimes we have to remove all of a leaf's children. After freeing them with free_childs, call this one.
Definition at line 65 of file tree.h.
#define set_childless |
( |
|
LEAF | ) |
(LEAF)->first_child = NULL |
Adds a child to a given leaf.
Definition at line 72 of file tree.h.
#define add_child |
( |
|
PARENT, |
|
|
|
CHILD |
|
) |
| |
Value:do { \
set_leaf(CHILD, PARENT, PARENT->first_child, CHILD->first_child, \
PARENT->level + 1); \
PARENT->first_child = CHILD; \
} while(0)
Given a function name, creates a function with the same name plus `_r' that (tail- in the second case) recursively calls the first. Can only be applied on functions that receive as single parameter a grid of type `type'.
Definition at line 77 of file tree.h.
#define mk_recursive |
( |
|
func, |
|
|
|
type |
|
) |
| |
Value:void func ## _r (type *grid_) \
{ \
type *child_; \
func (grid_); \
iter_childs (grid_, child_) { \
func ## _r (child_); \
} \
}
Definition at line 94 of file tree.h.
#define mk_tail_recursive |
( |
|
func, |
|
|
|
type |
|
) |
| |
Value:void func ## _r (type *grid_) \
{ \
type *child_; \
iter_childs (grid_, child_) { \
func ## _r (child_); \
} \
func (grid_); \
}
Definition at line 104 of file tree.h.