libubox
C utility functions for OpenWrt.
|
Go to the source code of this file.
Data Structures | |
struct | avl_node |
struct | avl_tree |
Macros | |
#define | EXPORT(sym) sym |
#define | AVL_TREE_INIT(_name, _comp, _allow_dups, _cmp_ptr) |
#define | AVL_TREE(_name, _comp, _allow_dups, _cmp_ptr) |
#define | avl_find_element(tree, key, element, node_element) ((__typeof__(*(element)) *)__avl_find_element(tree, key, offsetof(typeof(*(element)), node_element), AVL_FIND_EQUAL)) |
#define | avl_find_le_element(tree, key, element, node_element) ((__typeof__(*(element)) *)__avl_find_element(tree, key, offsetof(typeof(*(element)), node_element), AVL_FIND_LESSEQUAL)) |
#define | avl_find_ge_element(tree, key, element, node_element) ((__typeof__(*(element)) *)__avl_find_element(tree, key, offsetof(typeof(*(element)), node_element), AVL_FIND_GREATEREQUAL)) |
#define | avl_first_element(tree, element, node_member) container_of((tree)->list_head.next, __typeof__(*(element)), node_member.list) |
#define | avl_last_element(tree, element, node_member) container_of((tree)->list_head.prev, __typeof__(*(element)), node_member.list) |
#define | avl_next_element(element, node_member) container_of((&(element)->node_member.list)->next, __typeof__(*(element)), node_member.list) |
#define | avl_prev_element(element, node_member) container_of((&(element)->node_member.list)->prev, __typeof__(*(element)), node_member.list) |
#define | avl_for_element_range(first, last, element, node_member) |
#define | avl_for_element_range_reverse(first, last, element, node_member) |
#define | avl_for_each_element(tree, element, node_member) |
#define | avl_for_each_element_reverse(tree, element, node_member) |
#define | avl_for_element_to_last(tree, first, element, node_member) avl_for_element_range(first, avl_last_element(tree, element, node_member), element, node_member) |
#define | avl_for_element_to_last_reverse(tree, first, element, node_member) avl_for_element_range_reverse(first, avl_last_element(tree, element, node_member), element, node_member) |
#define | avl_for_first_to_element(tree, last, element, node_member) avl_for_element_range(avl_first_element(tree, element, node_member), last, element, node_member) |
#define | avl_for_first_to_element_reverse(tree, last, element, node_member) avl_for_element_range_reverse(avl_first_element(tree, element, node_member), last, element, node_member) |
#define | avl_for_element_range_safe(first_element, last_element, element, node_member, ptr) |
#define | avl_for_element_range_reverse_safe(first_element, last_element, element, node_member, ptr) |
#define | avl_for_each_element_safe(tree, element, node_member, ptr) |
#define | avl_for_each_element_reverse_safe(tree, element, node_member, ptr) |
#define | avl_remove_all_elements(tree, element, node_member, ptr) |
Typedefs | |
typedef int(* | avl_tree_comp) (const void *k1, const void *k2, void *ptr) |
Enumerations | |
enum | avl_find_mode { AVL_FIND_EQUAL , AVL_FIND_LESSEQUAL , AVL_FIND_GREATEREQUAL } |
Functions | |
void | avl_init (struct avl_tree *, avl_tree_comp, bool, void *) |
struct avl_node * | avl_find (const struct avl_tree *, const void *) |
struct avl_node * | avl_find_greaterequal (const struct avl_tree *tree, const void *key) |
struct avl_node * | avl_find_lessequal (const struct avl_tree *tree, const void *key) |
int | avl_insert (struct avl_tree *, struct avl_node *) |
void | avl_delete (struct avl_tree *, struct avl_node *) |
static bool | avl_is_first (struct avl_tree *tree, struct avl_node *node) |
static bool | avl_is_last (struct avl_tree *tree, struct avl_node *node) |
static bool | avl_is_empty (struct avl_tree *tree) |
static void * | __avl_find_element (const struct avl_tree *tree, const void *key, size_t offset, enum avl_find_mode mode) |
#define avl_find_element | ( | tree, | |
key, | |||
element, | |||
node_element | |||
) | ((__typeof__(*(element)) *)__avl_find_element(tree, key, offsetof(typeof(*(element)), node_element), AVL_FIND_EQUAL)) |
tree | pointer to avl-tree |
key | pointer to key |
element | pointer to a node element (don't need to be initialized) |
node_element | name of the avl_node element inside the larger struct |
#define avl_find_ge_element | ( | tree, | |
key, | |||
element, | |||
node_element | |||
) | ((__typeof__(*(element)) *)__avl_find_element(tree, key, offsetof(typeof(*(element)), node_element), AVL_FIND_GREATEREQUAL)) |
tree | pointer to avl-tree |
key | pointer to specified key |
element | pointer to a node element (don't need to be initialized) |
node_element | name of the avl_node element inside the larger struct return pointer to first tree element with greater or equal key than specified key, NULL if no element was found |
#define avl_find_le_element | ( | tree, | |
key, | |||
element, | |||
node_element | |||
) | ((__typeof__(*(element)) *)__avl_find_element(tree, key, offsetof(typeof(*(element)), node_element), AVL_FIND_LESSEQUAL)) |
tree | pointer to avl-tree |
key | pointer to specified key |
element | pointer to a node element (don't need to be initialized) |
node_element | name of the avl_node element inside the larger struct return pointer to last tree element with less or equal key than specified key, NULL if no element was found |
#define avl_first_element | ( | tree, | |
element, | |||
node_member | |||
) | container_of((tree)->list_head.next, __typeof__(*(element)), node_member.list) |
This function must not be called for an empty tree
tree | pointer to avl-tree |
element | pointer to a node element (don't need to be initialized) |
node_member | name of the avl_node element inside the larger struct |
#define avl_for_each_element | ( | tree, | |
element, | |||
node_member | |||
) |
Loop over all elements of an avl_tree, used similar to a for() command. This loop should not be used if elements are removed from the tree during the loop.
tree | pointer to avl-tree |
element | pointer to a node of the tree, this element will contain the current node of the tree during the loop |
node_member | name of the avl_node element inside the larger struct |
#define avl_for_each_element_reverse | ( | tree, | |
element, | |||
node_member | |||
) |
Loop over all elements of an avl_tree backwards, used similar to a for() command. This loop should not be used if elements are removed from the tree during the loop.
tree | pointer to avl-tree |
element | pointer to a node of the tree, this element will contain the current node of the tree during the loop |
node_member | name of the avl_node element inside the larger struct |
#define avl_for_each_element_reverse_safe | ( | tree, | |
element, | |||
node_member, | |||
ptr | |||
) |
Loop over all elements of an avl_tree backwards, used similar to a for() command. This loop can be used if the current element might be removed from the tree during the loop. Other elements should not be removed during the loop.
tree | pointer to avl-tree |
element | pointer to a node of the tree, this element will contain the current node of the tree during the loop |
node_member | name of the avl_node element inside the larger struct |
ptr | pointer to a tree element which is used to store the next node during the loop |
#define avl_for_each_element_safe | ( | tree, | |
element, | |||
node_member, | |||
ptr | |||
) |
Loop over all elements of an avl_tree, used similar to a for() command. This loop can be used if the current element might be removed from the tree during the loop. Other elements should not be removed during the loop.
tree | pointer to avl-tree |
element | pointer to a node of the tree, this element will contain the current node of the tree during the loop |
node_member | name of the avl_node element inside the larger struct |
ptr | pointer to a tree element which is used to store the next node during the loop |
#define avl_for_element_range | ( | first, | |
last, | |||
element, | |||
node_member | |||
) |
Loop over a block of elements of a tree, used similar to a for() command. This loop should not be used if elements are removed from the tree during the loop.
first | pointer to first element of loop |
last | pointer to last element of loop |
element | pointer to a node of the tree, this element will contain the current node of the list during the loop |
node_member | name of the avl_node element inside the larger struct |
#define avl_for_element_range_reverse | ( | first, | |
last, | |||
element, | |||
node_member | |||
) |
Loop over a block of elements of a tree backwards, used similar to a for() command. This loop should not be used if elements are removed from the tree during the loop.
first | pointer to first element of loop |
last | pointer to last element of loop |
element | pointer to a node of the tree, this element will contain the current node of the list during the loop |
node_member | name of the avl_node element inside the larger struct |
#define avl_for_element_range_reverse_safe | ( | first_element, | |
last_element, | |||
element, | |||
node_member, | |||
ptr | |||
) |
Loop over a block of elements of a tree backwards, used similar to a for() command. This loop can be used if the current element might be removed from the tree during the loop. Other elements should not be removed during the loop.
first_element | first element of range (will be last returned by the loop) |
last_element | last element of range (will be first returned by the loop) |
element | iterator pointer to node element struct |
node_member | name of avl_node within node element struct |
ptr | pointer to node element struct which is used to store the previous node during the loop |
#define avl_for_element_range_safe | ( | first_element, | |
last_element, | |||
element, | |||
node_member, | |||
ptr | |||
) |
Loop over a block of nodes of a tree, used similar to a for() command. This loop can be used if the current element might be removed from the tree during the loop. Other elements should not be removed during the loop.
first_element | first element of loop |
last_element | last element of loop |
element | iterator pointer to tree element struct |
node_member | name of avl_node within tree element struct |
ptr | pointer to tree element struct which is used to store the next node during the loop |
#define avl_for_element_to_last | ( | tree, | |
first, | |||
element, | |||
node_member | |||
) | avl_for_element_range(first, avl_last_element(tree, element, node_member), element, node_member) |
Loop over a block of elements of a tree, used similar to a for() command. This loop should not be used if elements are removed from the tree during the loop. The loop runs from the element 'first' to the end of the tree.
tree | pointer to avl-tree |
first | pointer to first element of loop |
element | pointer to a node of the tree, this element will contain the current node of the list during the loop |
node_member | name of the avl_node element inside the larger struct |
#define avl_for_element_to_last_reverse | ( | tree, | |
first, | |||
element, | |||
node_member | |||
) | avl_for_element_range_reverse(first, avl_last_element(tree, element, node_member), element, node_member) |
Loop over a block of elements of a tree backwards, used similar to a for() command. This loop should not be used if elements are removed from the tree during the loop. The loop runs from the element 'first' to the end of the tree.
tree | pointer to avl-tree |
first | pointer to first element of loop |
element | pointer to a node of the tree, this element will contain the current node of the list during the loop |
node_member | name of the avl_node element inside the larger struct |
#define avl_for_first_to_element | ( | tree, | |
last, | |||
element, | |||
node_member | |||
) | avl_for_element_range(avl_first_element(tree, element, node_member), last, element, node_member) |
Loop over a block of elements of a tree, used similar to a for() command. This loop should not be used if elements are removed from the tree during the loop. The loop runs from the start of the tree to the element 'last'.
tree | pointer to avl-tree |
last | pointer to last element of loop |
element | pointer to a node of the tree, this element will contain the current node of the list during the loop |
node_member | name of the avl_node element inside the larger struct |
#define avl_for_first_to_element_reverse | ( | tree, | |
last, | |||
element, | |||
node_member | |||
) | avl_for_element_range_reverse(avl_first_element(tree, element, node_member), last, element, node_member) |
Loop over a block of elements of a tree backwards, used similar to a for() command. This loop should not be used if elements are removed from the tree during the loop. The loop runs from the start of the tree to the element 'last'.
tree | pointer to avl-tree |
last | pointer to last element of loop |
element | pointer to a node of the tree, this element will contain the current node of the list during the loop |
node_member | name of the avl_node element inside the larger struct |
#define avl_last_element | ( | tree, | |
element, | |||
node_member | |||
) | container_of((tree)->list_head.prev, __typeof__(*(element)), node_member.list) |
#define avl_next_element | ( | element, | |
node_member | |||
) | container_of((&(element)->node_member.list)->next, __typeof__(*(element)), node_member.list) |
This function must not be called for the last element of an avl tree
element | pointer to a node of the tree |
node_member | name of the avl_node element inside the larger struct |
#define avl_prev_element | ( | element, | |
node_member | |||
) | container_of((&(element)->node_member.list)->prev, __typeof__(*(element)), node_member.list) |
This function must not be called for the first element of an avl tree
element | pointer to a node of the tree |
node_member | name of the avl_node element inside the larger struct |
#define avl_remove_all_elements | ( | tree, | |
element, | |||
node_member, | |||
ptr | |||
) |
A special loop that removes all elements of the tree and cleans up the tree root. The loop body is responsible to free the node elements of the tree.
This loop is much faster than a normal one for clearing the tree because it does not rebalance the tree after each removal. Do NOT use a break command inside. You can free the memory of the elements within the loop. Do NOT call avl_delete() on the elements within the loop,
tree | pointer to avl-tree |
element | pointer to a node of the tree, this element will contain the current node of the tree during the loop |
node_member | name of the avl_node element inside the larger struct |
ptr | pointer to a tree element which is used to store the next node during the loop |
#define AVL_TREE | ( | _name, | |
_comp, | |||
_allow_dups, | |||
_cmp_ptr | |||
) |
#define AVL_TREE_INIT | ( | _name, | |
_comp, | |||
_allow_dups, | |||
_cmp_ptr | |||
) |
typedef int(* avl_tree_comp) (const void *k1, const void *k2, void *ptr) |
enum avl_find_mode |
|
inlinestatic |
Internal function to support returning the element from a avl tree query
tree | pointer to avl tree |
key | pointer to key |
offset | offset of node inside the embedded struct |
mode | mode of lookup operation (less equal, equal or greater equal) |
pointer | to elemen, NULL if no fitting one was found |
Definition at line 213 of file avl.h.
Remove a node from an avl tree
tree | pointer to tree |
node | pointer to node |
Definition at line 307 of file avl.c.
Finds a node in an avl-tree with a certain key
tree | pointer to avl-tree |
key | pointer to key |
Definition at line 115 of file avl.c.
Finds the first node in an avl-tree with a key greater or equal than the specified key
tree | pointer to avl-tree |
key | pointer to specified key |
Definition at line 179 of file avl.c.
Finds the last node in an avl-tree with a key less or equal than the specified key
tree | pointer to avl-tree |
key | pointer to specified key |
Definition at line 137 of file avl.c.
void avl_init | ( | struct avl_tree * | tree, |
avl_tree_comp | comp, | ||
bool | allow_dups, | ||
void * | ptr | ||
) |
Initialize a new avl_tree struct
tree | pointer to avl-tree |
comp | pointer to comparator for the tree |
allow_dups | true if the tree allows multiple elements with the same |
ptr | custom parameter for comparator |
Definition at line 92 of file avl.c.
Inserts an avl_node into a tree
tree | pointer to tree |
new | pointer to node |
Definition at line 220 of file avl.c.
|
inlinestatic |