Code

Use stdbool.h's bool type instead of _Bool.
[sysdb.git] / src / include / utils / avltree.h
1 /*
2  * SysDB - src/include/utils/avltree.h
3  * Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #ifndef SDB_UTILS_AVLTREE_H
29 #define SDB_UTILS_AVLTREE_H 1
31 #include "core/object.h"
33 #include <stdbool.h>
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
39 /*
40  * An AVL tree implements Adelson-Velskii and Landis' self-balancing search
41  * tree. It supports search, insert, and delete operations in average and
42  * worst-case time-complexity O(log n).
43  */
44 struct sdb_avltree;
45 typedef struct sdb_avltree sdb_avltree_t;
47 struct sdb_avltree_iter;
48 typedef struct sdb_avltree_iter sdb_avltree_iter_t;
50 /*
51  * sdb_avltree_create:
52  * Creates an AVL tree. Objects will be compared by their names.
53  */
54 sdb_avltree_t *
55 sdb_avltree_create(void);
57 /*
58  * sdb_avltree_destroy:
59  * Destroy the specified AVL tree and release all included objects (decrement
60  * the ref-count).
61  */
62 void
63 sdb_avltree_destroy(sdb_avltree_t *tree);
65 /*
66  * sdb_avltree_clear:
67  * Remove all nodes from the tree, releasing the included objects (decrement
68  * the ref-count).
69  */
70 void
71 sdb_avltree_clear(sdb_avltree_t *tree);
73 /*
74  * sdb_avltree_insert:
75  * Insert a new node into the tree. Each object must be unique. This operation
76  * may change the structure of the tree by rebalancing subtrees which no
77  * longer comply with the rules of AVL.
78  *
79  * Returns:
80  *  - 0 on success
81  *  - a negative value else
82  */
83 int
84 sdb_avltree_insert(sdb_avltree_t *tree, sdb_object_t *obj);
86 /*
87  * sdb_avltree_lookup:
88  * Lookup an object from a tree by name.
89  *
90  * Returns:
91  *  - the requested object
92  *  - NULL if no such object exists
93  */
94 sdb_object_t *
95 sdb_avltree_lookup(sdb_avltree_t *tree, const char *name);
97 /*
98  * sdb_avltree_get_iter, sdb_avltree_iter_has_next, sdb_avltree_iter_get_next,
99  * sdb_avltree_iter_destroy:
100  * Iterate through all nodes of the tree. The iterator will start at the
101  * smallest element (based on the name) and then iterate through the sorted
102  * sequence of all nodes.
103  *
104  * sdb_avltree_iter_get_next returns NULL if there is no next element.
105  */
106 sdb_avltree_iter_t *
107 sdb_avltree_get_iter(sdb_avltree_t *tree);
108 void
109 sdb_avltree_iter_destroy(sdb_avltree_iter_t *iter);
111 bool
112 sdb_avltree_iter_has_next(sdb_avltree_iter_t *iter);
113 sdb_object_t *
114 sdb_avltree_iter_get_next(sdb_avltree_iter_t *iter);
116 /*
117  * sdb_avltree_size:
118  * Returns the number of nodes in the tree.
119  */
120 size_t
121 sdb_avltree_size(sdb_avltree_t *tree);
123 /*
124  * sdb_avltree_valid:
125  * Validate a tree, checking if all rules of AVL are met. All errors will be
126  * reported through the logging sub-system. This function is mainly intended
127  * for debugging and (unit) testing.
128  *
129  * Returns:
130  *  - true if the tree is valid
131  *  - false else
132  */
133 bool
134 sdb_avltree_valid(sdb_avltree_t *tree);
136 #ifdef __cplusplus
137 } /* extern "C" */
138 #endif
140 #endif /* ! SDB_UTILS_AVLTREE_H */
142 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */