libdebian-installer
Data Structures | Functions
Di_tree

Data Structures

struct  di_tree
 Tree. More...
 
struct  di_tree_node
 Node of a tree. More...
 

Functions

di_treedi_tree_new (di_compare_func key_compare_func)
 
di_treedi_tree_new_full (di_compare_func key_compare_func, di_destroy_notify key_destroy_func, di_destroy_notify value_destroy_func)
 
void di_tree_destroy (di_tree *tree)
 
void di_tree_insert (di_tree *tree, void *key, void *value)
 
void * di_tree_lookup (di_tree *tree, const void *key)
 
void di_tree_foreach (di_tree *tree, di_hfunc *func, void *user_data)
 
di_ksize_t di_tree_size (di_tree *tree)
 

Detailed Description

Function Documentation

◆ di_tree_destroy()

void di_tree_destroy ( di_tree tree)

Destroys the di_tree. If keys and/or values are dynamically allocated, you should either free them first or create the di_tree using di_tree_new_full. In the latter case the destroy functions you supplied will be called on all keys and values before destroying the di_hash_table.

Parameters
treea di_tree.

References di_error, di_free(), di_new, di_tree_node::key, key_compare_func, di_tree_node::left, nnodes, di_tree_node::right, root, and di_tree_node::value.

97 {
98  di_tree_node_destroy (tree, tree->root);
99  di_free (tree);
100 }
di_tree_node * root
Definition: tree.c:43
void di_free(void *mem)
Definition: mem.c:60

◆ di_tree_foreach()

void di_tree_foreach ( di_tree tree,
di_hfunc *  func,
void *  user_data 
)

Calls the given function for each of the key/value pairs in the di_tree. The function is passed the key and value of each pair, and the given user_data parameter.

Postcondition
The hash table may not be modified while iterating over it (you can't add/remove items).
Parameters
treea di_tree.
functhe function to call for each key/value pair.
user_datauser data to pass to the function.
271 {
272  di_tree_node_foreach (tree->root, func, user_data);
273 }
di_tree_node * root
Definition: tree.c:43

◆ di_tree_insert()

void di_tree_insert ( di_tree tree,
void *  key,
void *  value 
)

Inserts a new key and value into a di_tree.

If the key already exists in the di_tree its current value is replaced with the new value. If you supplied a value_destroy_func when creating the di_tree, the old value is freed using that function. If you supplied a key_destroy_func when creating the di_tree, the passed key is freed using that function.

Parameters
treea di_tree.
keya key to insert.
valuethe value to associate with the key.

References nnodes, and root.

251 {
252  if (!tree->root)
253  {
254  tree->root = di_tree_node_new (key, value);
255  tree->nnodes++;
256  }
257  else
258  tree->root = di_tree_node_insert (tree, tree->root, key, value);
259 }
di_tree_node * root
Definition: tree.c:43
size_t nnodes
Definition: tree.c:42

◆ di_tree_lookup()

void* di_tree_lookup ( di_tree tree,
const void *  key 
)

Looks up a key in a di_tree.

Parameters
treea di_tree.,
keythe key to look up.
Returns
the associated value, or NULL if the key is not found.

References root.

298 {
299  return di_tree_node_lookup (tree, tree->root, key);
300 }
di_tree_node * root
Definition: tree.c:43

◆ di_tree_new()

di_tree* di_tree_new ( di_compare_func  key_compare_func)

Creates a new di_tree.

Parameters
key_compare_funca function to compare two keys. This is used when looking up keys in the di_tree.
Returns
a new di_tree.

References di_tree_new_full().

65 {
66  return di_tree_new_full (key_compare_func, NULL, NULL);
67 }
di_tree * di_tree_new_full(di_compare_func key_compare_func, di_destroy_notify key_destroy_func, di_destroy_notify value_destroy_func)
Definition: tree.c:69

◆ di_tree_new_full()

di_tree* di_tree_new_full ( di_compare_func  key_compare_func,
di_destroy_notify  key_destroy_func,
di_destroy_notify  value_destroy_func 
)

Creates a new di_tree like di_tree_new and allows to specify functions to free the memory allocated for the key and value that get called when removing the entry from the di_tree

Parameters
key_compare_funca function to check two keys for equality. This is used when looking up keys in the di_tree.
key_destroy_funca function to free the memory allocated for the key used when removing the entry from the di_tree or NULL if you don't want to supply such a function.
value_destroy_funca function to free the memory allocated for the value used when removing the entry from the di_tree or NULL if you don't want to supply such a function.
Returns
a new di_tree.

References di_free(), di_new, di_tree_node::key, key_compare_func, key_destroy_func, di_tree_node::left, nnodes, di_tree_node::right, root, di_tree_node::value, and value_destroy_func.

Referenced by di_tree_new().

70 {
71  di_tree *tree;
72 
73  tree = di_new (di_tree, 1);
74  tree->nnodes = 0;
75  tree->key_compare_func = key_compare_func;
76  tree->key_destroy_func = key_destroy_func;
77  tree->value_destroy_func = value_destroy_func;
78  tree->root = 0;
79 
80  return tree;
81 }
di_compare_func * key_compare_func
Definition: tree.c:44
di_destroy_notify * key_destroy_func
Definition: tree.c:45
Tree.
Definition: tree.c:40
di_tree_node * root
Definition: tree.c:43
size_t nnodes
Definition: tree.c:42
#define di_new(struct_type, n_structs)
Definition: mem.h:73
di_destroy_notify * value_destroy_func
Definition: tree.c:46

◆ di_tree_size()

di_ksize_t di_tree_size ( di_tree tree)

Returns the number of elements contained in the di_tree.

Parameters
hash_tablea di_tree.
Returns
the number of key/value pairs.

References di_tree_node::key, key_compare_func, di_tree_node::left, nnodes, di_tree_node::right, and di_tree_node::value.

276 {
277  return tree->nnodes;
278 }
size_t nnodes
Definition: tree.c:42