1 #pragma once 2 #ifndef IWSTREE_H 3 #define IWSTREE_H 4 5 /* 6 Copyright (c) 2011, Willem-Hendrik Thiart 7 Copyright (c) 2012-2020 Softmotions Ltd <info@softmotions.com> 8 All rights reserved. 9 10 Redistribution and use in source and binary forms, with or without 11 modification, are permitted provided that the following conditions are met: 12 * Redistributions of source code must retain the above copyright 13 notice, this list of conditions and the following disclaimer. 14 * Redistributions in binary form must reproduce the above copyright 15 notice, this list of conditions and the following disclaimer in the 16 documentation and/or other materials provided with the distribution. 17 * The names of its contributors may not be used to endorse or promote 18 products derived from this software without specific prior written 19 permission. 20 21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 22 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 DISCLAIMED. IN NO EVENT SHALL WILLEM-HENDRIK THIART BE LIABLE FOR ANY 25 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 28 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include "basedefs.h" 34 #include <stdbool.h> 35 36 IW_EXTERN_C_START 37 38 typedef struct { 39 void *root; 40 int (*cmp)(const void *, const void *); 41 void (*kvfree)(void *, void *); 42 int count; 43 } IWSTREE; 44 45 typedef struct _IWSTREE_ITER { 46 IWSTREE *st; /**< Owner tree */ 47 int spos; /**< Position of top element stack */ 48 int slen; /**< Max number of elements in stack */ 49 void **stack; /**< Bottom of iterator stack */ 50 } IWSTREE_ITER; 51 52 typedef bool (*IWSTREE_VISITOR)(void *key, void *val, void *op, iwrc *rcp); 53 54 /** 55 * @brief Constructs new splay tree 56 * 57 * @param cmp Keys compare function. If zero address pointers will be compared. 58 * @param kvfree Optional `(key, value)` free function 59 * @return IWSTREE* or NULL if memory allocation failed 60 */ 61 IW_EXPORT IWSTREE *iwstree_create( 62 int (*cmp)(const void *, const void *), 63 void (*kvfree)(void *, void *) 64 ); 65 66 IW_EXPORT int iwstree_str_cmp(const void *o1, const void *o2); 67 68 IW_EXPORT int iwstree_uint64_cmp(const void *o1, const void *o2); 69 70 IW_EXPORT int iwstree_int64_cmp(const void *o1, const void *o2); 71 72 IW_EXPORT void iwstree_clear(IWSTREE *st); 73 74 IW_EXPORT void iwstree_destroy(IWSTREE *st); 75 76 IW_EXPORT int iwstree_is_empty(IWSTREE *st); 77 78 IW_EXPORT void *iwstree_remove(IWSTREE *st, const void *key); 79 80 IW_EXPORT void *iwstree_get(IWSTREE *st, const void *key); 81 82 IW_EXPORT int iwstree_count(IWSTREE *st); 83 84 IW_EXPORT void *iwstree_peek(IWSTREE *st); 85 86 IW_EXPORT iwrc iwstree_put(IWSTREE *st, void *key, void *value); 87 88 IW_EXPORT iwrc iwstree_put_overwrite(IWSTREE *st, void *key, void *value); 89 90 IW_EXPORT iwrc iwstree_visit(IWSTREE *st, IWSTREE_VISITOR visitor, void *op); 91 92 IW_EXPORT iwrc iwstree_iter_init(IWSTREE *st, IWSTREE_ITER *iter); 93 94 IW_EXPORT bool iwstree_iter_has_next(IWSTREE_ITER *iter); 95 96 IW_EXPORT iwrc iwstree_iter_next(IWSTREE_ITER *iter, void **key, void **val); 97 98 IW_EXPORT void iwstree_iter_close(IWSTREE_ITER *iter); 99 100 IW_EXTERN_C_END 101 #endif 102