github.com/moontrade/nogc@v0.1.7/collections/btree/btree.h (about) 1 // Copyright 2020 Joshua J Baker. All rights reserved. 2 // Use of this source code is governed by an MIT-style 3 // license that can be found in the LICENSE file. 4 5 #ifndef BTREE_H 6 #define BTREE_H 7 8 #include <stdbool.h> 9 #include <stddef.h> 10 #include <stdint.h> 11 12 struct btree; 13 14 struct btree *btree_new(size_t elsize, size_t max_items, 15 int (*compare)(const void *a, const void *b, 16 void *udata), 17 void *udata); 18 struct btree *btree_new_with_allocator( 19 void *(*malloc)(size_t), 20 void *(*realloc)(void *, size_t), 21 void (*free)(void*), 22 size_t elsize, size_t max_items, 23 int (*compare)(const void *a, const void *b, 24 void *udata), 25 void *udata); 26 void btree_free(struct btree *btree); 27 bool btree_oom(struct btree *btree); 28 size_t btree_height(struct btree *btree); 29 size_t btree_count(struct btree *btree); 30 void *btree_set(struct btree *btree, void *item); 31 void *btree_get(struct btree *btree, void *key); 32 void *btree_delete(struct btree *btree, void *key); 33 void *btree_pop_min(struct btree *btree); 34 void *btree_pop_max(struct btree *btree); 35 void *btree_min(struct btree *btree); 36 void *btree_max(struct btree *btree); 37 void *btree_load(struct btree *btree, void *item); 38 bool btree_ascend(struct btree *btree, void *pivot, 39 bool (*iter)(const void *item, void *udata), void *udata); 40 bool btree_descend(struct btree *btree, void *pivot, 41 bool (*iter)(const void *item, void *udata), void *udata); 42 43 // btree_action is a return value for the iter callback function that is 44 // passed to the btree_action_* functions. 45 enum btree_action { 46 BTREE_STOP, // Stop the iteration 47 BTREE_NONE, // Make no change and continue iterating 48 BTREE_DELETE, // Delete item item and continue iterating 49 BTREE_UPDATE, // Update the item and continue iterating. 50 }; 51 52 void btree_action_ascend(struct btree *btree, void *pivot, 53 enum btree_action (*iter)(void *item, void *udata), 54 void *udata); 55 56 void btree_action_descend(struct btree *btree, void *pivot, 57 enum btree_action (*iter)(void *item, void *udata), 58 void *udata); 59 60 // functions that support hints 61 62 void *btree_set_hint(struct btree *btree, void *item, uint64_t *hint); 63 void *btree_get_hint(struct btree *btree, void *key, uint64_t *hint); 64 void *btree_delete_hint(struct btree *btree, void *key, uint64_t *hint); 65 bool btree_ascend_hint(struct btree *btree, void *pivot, 66 bool (*iter)(const void *item, void *udata), 67 void *udata, uint64_t *hint); 68 bool btree_descend_hint(struct btree *btree, void *pivot, 69 bool (*iter)(const void *item, void *udata), 70 void *udata, uint64_t *hint); 71 void btree_action_ascend_hint(struct btree *btree, void *pivot, 72 enum btree_action (*iter)(void *item, 73 void *udata), 74 void *udata, uint64_t *hint); 75 void btree_action_descend_hint(struct btree *btree, void *pivot, 76 enum btree_action (*iter)(void *item, 77 void *udata), 78 void *udata, uint64_t *hint); 79 80 // DEPRECATED: use `btree_new_with_allocator` 81 void btree_set_allocator(void *(malloc)(size_t), void (*free)(void*)); 82 83 84 #endif