1 /* 2 Copyright 2011-2014 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 <stddef.h> 21 22 #include "zix/common.h" 23 24 #ifdef __cplusplus 25 extern "C" { 26 #endif 27 28 /** 29 @addtogroup zix 30 @{ 31 @name Tree 32 @{ 33 */ 34 35 /** 36 A balanced binary search tree. 37 */ 38 typedef struct ZixTreeImpl ZixTree; 39 40 /** 41 An iterator over a @ref ZixTree. 42 */ 43 typedef struct ZixTreeNodeImpl ZixTreeIter; 44 45 /** 46 Create a new (empty) tree. 47 */ 48 ZIX_API ZixTree* 49 zix_tree_new(bool allow_duplicates, 50 ZixComparator cmp, 51 void* cmp_data, 52 ZixDestroyFunc destroy); 53 54 /** 55 Free `t`. 56 */ 57 ZIX_API void 58 zix_tree_free(ZixTree* t); 59 60 /** 61 Return the number of elements in `t`. 62 */ 63 ZIX_API size_t 64 zix_tree_size(const ZixTree* t); 65 66 /** 67 Insert the element `e` into `t` and point `ti` at the new element. 68 */ 69 ZIX_API ZixStatus 70 zix_tree_insert(ZixTree* t, void* e, ZixTreeIter** ti); 71 72 /** 73 Remove the item pointed at by `ti` from `t`. 74 */ 75 ZIX_API ZixStatus 76 zix_tree_remove(ZixTree* t, ZixTreeIter* ti); 77 78 /** 79 Set `ti` to an element equal to `e` in `t`. 80 If no such item exists, `ti` is set to NULL. 81 */ 82 ZIX_API ZixStatus 83 zix_tree_find(const ZixTree* t, const void* e, ZixTreeIter** ti); 84 85 /** 86 Return the data associated with the given tree item. 87 */ 88 ZIX_API void* 89 zix_tree_get(const ZixTreeIter* ti); 90 91 /** 92 Return an iterator to the first (smallest) element in `t`. 93 */ 94 ZIX_API ZixTreeIter* 95 zix_tree_begin(ZixTree* t); 96 97 /** 98 Return an iterator the the element one past the last element in `t`. 99 */ 100 ZIX_API ZixTreeIter* 101 zix_tree_end(ZixTree* t); 102 103 /** 104 Return true iff `i` is an iterator to the end of its tree. 105 */ 106 ZIX_API bool 107 zix_tree_iter_is_end(const ZixTreeIter* i); 108 109 /** 110 Return an iterator to the last (largest) element in `t`. 111 */ 112 ZIX_API ZixTreeIter* 113 zix_tree_rbegin(ZixTree* t); 114 115 /** 116 Return an iterator the the element one before the first element in `t`. 117 */ 118 ZIX_API ZixTreeIter* 119 zix_tree_rend(ZixTree* t); 120 121 /** 122 Return true iff `i` is an iterator to the reverse end of its tree. 123 */ 124 ZIX_API bool 125 zix_tree_iter_is_rend(const ZixTreeIter* i); 126 127 /** 128 Return an iterator that points to the element one past `i`. 129 */ 130 ZIX_API ZixTreeIter* 131 zix_tree_iter_next(ZixTreeIter* i); 132 133 /** 134 Return an iterator that points to the element one before `i`. 135 */ 136 ZIX_API ZixTreeIter* 137 zix_tree_iter_prev(ZixTreeIter* i); 138 139 /** 140 @} 141 @} 142 */ 143 144 #ifdef __cplusplus 145 } /* extern "C" */ 146 #endif 147 148 #endif /* ZIX_TREE_H */ 149