1 /*
2   Copyright 2011-2019 David Robillard <http://drobilla.net>
3 
4   Permission to use, copy, modify, and/or distribute this software for any
5   purpose with or without fee is hereby granted, provided that the above
6   copyright notice and this permission notice appear in all copies.
7 
8   THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9   WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10   MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11   ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16 
17 #ifndef ZIX_TREE_H
18 #define ZIX_TREE_H
19 
20 #include "zix/common.h"
21 
22 #include <stdbool.h>
23 #include <stddef.h>
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 /**
30    @addtogroup zix
31    @{
32    @name Tree
33    @{
34 */
35 
36 /**
37    A balanced binary search tree.
38 */
39 typedef struct ZixTreeImpl ZixTree;
40 
41 /**
42    An iterator over a ZixTree.
43 */
44 typedef struct ZixTreeNodeImpl ZixTreeIter;
45 
46 /**
47    Create a new (empty) tree.
48 */
49 ZIX_API ZixTree*
50 zix_tree_new(bool           allow_duplicates,
51              ZixComparator  cmp,
52              void*          cmp_data,
53              ZixDestroyFunc destroy);
54 
55 /**
56    Free `t`.
57 */
58 ZIX_API void
59 zix_tree_free(ZixTree* t);
60 
61 /**
62    Return the number of elements in `t`.
63 */
64 ZIX_API size_t
65 zix_tree_size(const ZixTree* t);
66 
67 /**
68    Insert the element `e` into `t` and point `ti` at the new element.
69 */
70 ZIX_API ZixStatus
71 zix_tree_insert(ZixTree* t, void* e, ZixTreeIter** ti);
72 
73 /**
74    Remove the item pointed at by `ti` from `t`.
75 */
76 ZIX_API ZixStatus
77 zix_tree_remove(ZixTree* t, ZixTreeIter* ti);
78 
79 /**
80    Set `ti` to an element equal to `e` in `t`.
81    If no such item exists, `ti` is set to NULL.
82 */
83 ZIX_API ZixStatus
84 zix_tree_find(const ZixTree* t, const void* e, ZixTreeIter** ti);
85 
86 /**
87    Return the data associated with the given tree item.
88 */
89 ZIX_API void*
90 zix_tree_get(const ZixTreeIter* ti);
91 
92 /**
93    Return an iterator to the first (smallest) element in `t`.
94 */
95 ZIX_API ZixTreeIter*
96 zix_tree_begin(ZixTree* t);
97 
98 /**
99    Return an iterator the the element one past the last element in `t`.
100 */
101 ZIX_API ZixTreeIter*
102 zix_tree_end(ZixTree* t);
103 
104 /**
105    Return true iff `i` is an iterator to the end of its tree.
106 */
107 ZIX_API bool
108 zix_tree_iter_is_end(const ZixTreeIter* i);
109 
110 /**
111    Return an iterator to the last (largest) element in `t`.
112 */
113 ZIX_API ZixTreeIter*
114 zix_tree_rbegin(ZixTree* t);
115 
116 /**
117    Return an iterator the the element one before the first element in `t`.
118 */
119 ZIX_API ZixTreeIter*
120 zix_tree_rend(ZixTree* t);
121 
122 /**
123    Return true iff `i` is an iterator to the reverse end of its tree.
124 */
125 ZIX_API bool
126 zix_tree_iter_is_rend(const ZixTreeIter* i);
127 
128 /**
129    Return an iterator that points to the element one past `i`.
130 */
131 ZIX_API ZixTreeIter*
132 zix_tree_iter_next(ZixTreeIter* i);
133 
134 /**
135    Return an iterator that points to the element one before `i`.
136 */
137 ZIX_API ZixTreeIter*
138 zix_tree_iter_prev(ZixTreeIter* i);
139 
140 /**
141    @}
142    @}
143 */
144 
145 #ifdef __cplusplus
146 }  /* extern "C" */
147 #endif
148 
149 #endif  /* ZIX_TREE_H */
150