cannam@226
|
1 /*
|
cannam@226
|
2 Copyright 2011-2016 David Robillard <http://drobilla.net>
|
cannam@226
|
3
|
cannam@226
|
4 Permission to use, copy, modify, and/or distribute this software for any
|
cannam@226
|
5 purpose with or without fee is hereby granted, provided that the above
|
cannam@226
|
6 copyright notice and this permission notice appear in all copies.
|
cannam@226
|
7
|
cannam@226
|
8 THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
cannam@226
|
9 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
cannam@226
|
10 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
cannam@226
|
11 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
cannam@226
|
12 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
cannam@226
|
13 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
cannam@226
|
14 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
cannam@226
|
15 */
|
cannam@226
|
16
|
cannam@226
|
17 #ifndef ZIX_BTREE_H
|
cannam@226
|
18 #define ZIX_BTREE_H
|
cannam@226
|
19
|
cannam@226
|
20 #include <stddef.h>
|
cannam@226
|
21
|
cannam@245
|
22 #include "common.h"
|
cannam@226
|
23
|
cannam@226
|
24 #ifdef __cplusplus
|
cannam@226
|
25 extern "C" {
|
cannam@226
|
26 #else
|
cannam@226
|
27 # include <stdbool.h>
|
cannam@226
|
28 #endif
|
cannam@226
|
29
|
cannam@226
|
30 /**
|
cannam@226
|
31 @addtogroup zix
|
cannam@226
|
32 @{
|
cannam@226
|
33 @name BTree
|
cannam@226
|
34 @{
|
cannam@226
|
35 */
|
cannam@226
|
36
|
cannam@226
|
37 /**
|
cannam@226
|
38 A B-Tree.
|
cannam@226
|
39 */
|
cannam@226
|
40 typedef struct ZixBTreeImpl ZixBTree;
|
cannam@226
|
41
|
cannam@226
|
42 /**
|
cannam@226
|
43 A B-Tree node (opaque).
|
cannam@226
|
44 */
|
cannam@226
|
45 typedef struct ZixBTreeNodeImpl ZixBTreeNode;
|
cannam@226
|
46
|
cannam@226
|
47 /**
|
cannam@226
|
48 An iterator over a B-Tree.
|
cannam@226
|
49
|
cannam@226
|
50 Note that modifying the trees invalidates all iterators, so all iterators
|
cannam@226
|
51 are const iterators.
|
cannam@226
|
52 */
|
cannam@226
|
53 typedef struct ZixBTreeIterImpl ZixBTreeIter;
|
cannam@226
|
54
|
cannam@226
|
55 /**
|
cannam@226
|
56 Create a new (empty) B-Tree.
|
cannam@226
|
57 */
|
cannam@226
|
58 ZIX_API ZixBTree*
|
cannam@226
|
59 zix_btree_new(ZixComparator cmp,
|
cannam@226
|
60 void* cmp_data,
|
cannam@226
|
61 ZixDestroyFunc destroy);
|
cannam@226
|
62
|
cannam@226
|
63 /**
|
cannam@226
|
64 Free `t`.
|
cannam@226
|
65 */
|
cannam@226
|
66 ZIX_API void
|
cannam@226
|
67 zix_btree_free(ZixBTree* t);
|
cannam@226
|
68
|
cannam@226
|
69 /**
|
cannam@226
|
70 Return the number of elements in `t`.
|
cannam@226
|
71 */
|
cannam@226
|
72 ZIX_API size_t
|
cannam@226
|
73 zix_btree_size(const ZixBTree* t);
|
cannam@226
|
74
|
cannam@226
|
75 /**
|
cannam@226
|
76 Insert the element `e` into `t`.
|
cannam@226
|
77 */
|
cannam@226
|
78 ZIX_API ZixStatus
|
cannam@226
|
79 zix_btree_insert(ZixBTree* t, void* e);
|
cannam@226
|
80
|
cannam@226
|
81 /**
|
cannam@226
|
82 Remove the value `e` from `t`.
|
cannam@226
|
83
|
cannam@226
|
84 @param t Tree to remove from.
|
cannam@226
|
85
|
cannam@226
|
86 @param e Value to remove.
|
cannam@226
|
87
|
cannam@226
|
88 @param out Set to point to the removed pointer (which may not equal `e`).
|
cannam@226
|
89
|
cannam@226
|
90 @param next If non-NULL, pointed to the value following `e`. If *next is
|
cannam@226
|
91 also non-NULL, the iterator is reused, otherwise a new one is allocated. To
|
cannam@226
|
92 reuse an iterator, no items may have been added since its creation.
|
cannam@226
|
93 */
|
cannam@226
|
94 ZIX_API ZixStatus
|
cannam@226
|
95 zix_btree_remove(ZixBTree* t, const void* e, void** out, ZixBTreeIter** next);
|
cannam@226
|
96
|
cannam@226
|
97 /**
|
cannam@226
|
98 Set `ti` to an element equal to `e` in `t`.
|
cannam@226
|
99 If no such item exists, `ti` is set to NULL.
|
cannam@226
|
100 */
|
cannam@226
|
101 ZIX_API ZixStatus
|
cannam@226
|
102 zix_btree_find(const ZixBTree* t, const void* e, ZixBTreeIter** ti);
|
cannam@226
|
103
|
cannam@226
|
104 /**
|
cannam@226
|
105 Set `ti` to the smallest element in `t` that is not less than `e`.
|
cannam@226
|
106
|
cannam@226
|
107 Wildcards are supported, so if the search key `e` compares equal to many
|
cannam@226
|
108 values in the tree, `ti` will be set to the least such element. The search
|
cannam@226
|
109 key `e` is always passed as the second argument to the comparator.
|
cannam@226
|
110 */
|
cannam@226
|
111 ZIX_API ZixStatus
|
cannam@226
|
112 zix_btree_lower_bound(const ZixBTree* t, const void* e, ZixBTreeIter** ti);
|
cannam@226
|
113
|
cannam@226
|
114 /**
|
cannam@226
|
115 Return the data associated with the given tree item.
|
cannam@226
|
116 */
|
cannam@226
|
117 ZIX_API void*
|
cannam@226
|
118 zix_btree_get(const ZixBTreeIter* ti);
|
cannam@226
|
119
|
cannam@226
|
120 /**
|
cannam@226
|
121 Return an iterator to the first (smallest) element in `t`.
|
cannam@226
|
122
|
cannam@226
|
123 The returned iterator must be freed with zix_btree_iter_free().
|
cannam@226
|
124 */
|
cannam@226
|
125 ZIX_API ZixBTreeIter*
|
cannam@226
|
126 zix_btree_begin(const ZixBTree* t);
|
cannam@226
|
127
|
cannam@226
|
128 /**
|
cannam@226
|
129 Return true iff `i` is an iterator to the end of its tree.
|
cannam@226
|
130 */
|
cannam@226
|
131 ZIX_API bool
|
cannam@226
|
132 zix_btree_iter_is_end(const ZixBTreeIter* i);
|
cannam@226
|
133
|
cannam@226
|
134 /**
|
cannam@226
|
135 Increment `i` to point to the next element in the tree.
|
cannam@226
|
136 */
|
cannam@226
|
137 ZIX_API void
|
cannam@226
|
138 zix_btree_iter_increment(ZixBTreeIter* i);
|
cannam@226
|
139
|
cannam@226
|
140 /**
|
cannam@226
|
141 Free `i`.
|
cannam@226
|
142 */
|
cannam@226
|
143 ZIX_API void
|
cannam@226
|
144 zix_btree_iter_free(ZixBTreeIter* i);
|
cannam@226
|
145
|
cannam@226
|
146 /**
|
cannam@226
|
147 @}
|
cannam@226
|
148 @}
|
cannam@226
|
149 */
|
cannam@226
|
150
|
cannam@226
|
151 #ifdef __cplusplus
|
cannam@226
|
152 } /* extern "C" */
|
cannam@226
|
153 #endif
|
cannam@226
|
154
|
cannam@226
|
155 #endif /* ZIX_BTREE_H */
|