Wed, 16 May 2018 14:02:59 +0200
adds remaining documentation for the scstr functions
universe@192 | 1 | /* |
universe@192 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@192 | 3 | * |
universe@259 | 4 | * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. |
universe@192 | 5 | * |
universe@192 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@192 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@192 | 8 | * |
universe@192 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@192 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@192 | 11 | * |
universe@192 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@192 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@192 | 14 | * documentation and/or other materials provided with the distribution. |
universe@192 | 15 | * |
universe@192 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@192 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@192 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@192 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@192 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@192 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@192 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@192 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@192 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@192 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@192 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@192 | 27 | */ |
universe@192 | 28 | |
universe@192 | 29 | |
universe@192 | 30 | /** |
universe@192 | 31 | * @file avl.h |
universe@192 | 32 | * |
universe@192 | 33 | * AVL tree implementation. |
universe@192 | 34 | * |
universe@192 | 35 | * This binary search tree implementation allows average O(1) insertion and |
universe@204 | 36 | * removal of elements (excluding binary search time). |
universe@192 | 37 | * |
universe@192 | 38 | * @author Mike Becker |
universe@192 | 39 | * @author Olaf Wintermann |
universe@192 | 40 | */ |
universe@192 | 41 | |
universe@192 | 42 | #ifndef UCX_AVL_H |
universe@194 | 43 | #define UCX_AVL_H |
universe@192 | 44 | |
universe@259 | 45 | #include "ucx.h" |
universe@259 | 46 | #include "allocator.h" |
universe@218 | 47 | #include <inttypes.h> |
universe@193 | 48 | |
universe@192 | 49 | #ifdef __cplusplus |
universe@192 | 50 | extern "C" { |
universe@192 | 51 | #endif |
universe@192 | 52 | |
universe@193 | 53 | /** |
universe@193 | 54 | * UCX AVL Node type. |
universe@193 | 55 | * |
universe@193 | 56 | * @see UcxAVLNode |
universe@193 | 57 | */ |
universe@193 | 58 | typedef struct UcxAVLNode UcxAVLNode; |
universe@193 | 59 | |
universe@193 | 60 | /** |
universe@193 | 61 | * UCX AVL Node. |
universe@193 | 62 | */ |
universe@193 | 63 | struct UcxAVLNode { |
universe@193 | 64 | /** |
universe@193 | 65 | * The key for this node. |
universe@193 | 66 | */ |
universe@194 | 67 | intptr_t key; |
universe@193 | 68 | /** |
universe@193 | 69 | * Data contained by this node. |
universe@193 | 70 | */ |
universe@193 | 71 | void *value; |
universe@193 | 72 | /** |
universe@193 | 73 | * The height of this (sub)-tree. |
universe@193 | 74 | */ |
universe@193 | 75 | size_t height; |
universe@193 | 76 | /** |
universe@193 | 77 | * Parent node. |
universe@193 | 78 | */ |
universe@193 | 79 | UcxAVLNode *parent; |
universe@193 | 80 | /** |
universe@193 | 81 | * Root node of left subtree. |
universe@193 | 82 | */ |
universe@193 | 83 | UcxAVLNode *left; |
universe@193 | 84 | /** |
universe@193 | 85 | * Root node of right subtree. |
universe@193 | 86 | */ |
universe@193 | 87 | UcxAVLNode *right; |
universe@193 | 88 | }; |
universe@193 | 89 | |
universe@193 | 90 | /** |
universe@193 | 91 | * UCX AVL Tree. |
universe@193 | 92 | */ |
universe@193 | 93 | typedef struct { |
universe@193 | 94 | /** |
universe@194 | 95 | * The UcxAllocator that shall be used to manage the memory for node data. |
universe@194 | 96 | */ |
universe@194 | 97 | UcxAllocator *allocator; |
universe@194 | 98 | /** |
universe@193 | 99 | * Root node of the tree. |
universe@193 | 100 | */ |
universe@193 | 101 | UcxAVLNode *root; |
universe@193 | 102 | /** |
universe@193 | 103 | * Compare function that shall be used to compare the UcxAVLNode keys. |
universe@193 | 104 | * @see UcxAVLNode.key |
universe@193 | 105 | */ |
universe@193 | 106 | cmp_func cmpfunc; |
universe@193 | 107 | /** |
universe@193 | 108 | * Custom user data. |
universe@193 | 109 | * This data will also be provided to the cmpfunc. |
universe@193 | 110 | */ |
universe@193 | 111 | void *userdata; |
universe@193 | 112 | } UcxAVLTree; |
universe@193 | 113 | |
universe@193 | 114 | /** |
universe@194 | 115 | * Initializes a new UcxAVLTree with a default allocator. |
universe@194 | 116 | * |
universe@194 | 117 | * @param cmpfunc the compare function that shall be used |
universe@194 | 118 | * @return a new UcxAVLTree object |
universe@194 | 119 | * @see ucx_avl_new_a() |
universe@194 | 120 | */ |
universe@194 | 121 | UcxAVLTree *ucx_avl_new(cmp_func cmpfunc); |
universe@194 | 122 | |
universe@194 | 123 | /** |
universe@194 | 124 | * Initializes a new UcxAVLTree with the specified allocator. |
universe@194 | 125 | * |
universe@194 | 126 | * The cmpfunc should be capable of comparing two keys within this AVL tree. |
universe@194 | 127 | * So if you want to use null terminated strings as keys, you could use the |
universe@308 | 128 | * ucx_cmp_str() function here. |
universe@194 | 129 | * |
universe@194 | 130 | * @param cmpfunc the compare function that shall be used |
universe@194 | 131 | * @param allocator the UcxAllocator that shall be used |
universe@194 | 132 | * @return a new UcxAVLTree object |
universe@194 | 133 | */ |
universe@194 | 134 | UcxAVLTree *ucx_avl_new_a(cmp_func cmpfunc, UcxAllocator *allocator); |
universe@194 | 135 | |
universe@194 | 136 | /** |
universe@225 | 137 | * Destroys a UcxAVLTree. |
universe@287 | 138 | * |
universe@287 | 139 | * Note, that the contents are not automatically freed. |
universe@287 | 140 | * Use may use #ucx_avl_free_content() before calling this function. |
universe@287 | 141 | * |
universe@196 | 142 | * @param tree the tree to destroy |
universe@287 | 143 | * @see ucx_avl_free_content() |
universe@196 | 144 | */ |
universe@196 | 145 | void ucx_avl_free(UcxAVLTree *tree); |
universe@196 | 146 | |
universe@196 | 147 | /** |
universe@287 | 148 | * Frees the contents of a UcxAVLTree. |
universe@287 | 149 | * |
universe@287 | 150 | * This is a convenience function that iterates over the tree and passes all |
universe@287 | 151 | * values to the specified destructor function. |
universe@287 | 152 | * |
universe@287 | 153 | * If no destructor is specified (<code>NULL</code>), the free() function of |
universe@287 | 154 | * the tree's own allocator is used. |
universe@287 | 155 | * |
universe@287 | 156 | * You must ensure, that it is valid to pass each value in the map to the same |
universe@287 | 157 | * destructor function. |
universe@287 | 158 | * |
universe@287 | 159 | * You should free the entire tree afterwards, as the contents will be invalid. |
universe@287 | 160 | * |
universe@287 | 161 | * @param tree for which the contents shall be freed |
universe@287 | 162 | * @param destr optional pointer to a destructor function |
universe@287 | 163 | * @see ucx_avl_free() |
universe@287 | 164 | */ |
universe@287 | 165 | void ucx_avl_free_content(UcxAVLTree *tree, ucx_destructor destr); |
universe@287 | 166 | |
universe@287 | 167 | /** |
universe@194 | 168 | * Macro for initializing a new UcxAVLTree with the default allocator and a |
universe@312 | 169 | * ucx_cmp_ptr() compare function. |
universe@194 | 170 | * |
universe@194 | 171 | * @return a new default UcxAVLTree object |
universe@194 | 172 | */ |
universe@312 | 173 | #define ucx_avl_default_new() \ |
universe@312 | 174 | ucx_avl_new_a(ucx_cmp_ptr, ucx_default_allocator()) |
universe@194 | 175 | |
universe@194 | 176 | /** |
universe@204 | 177 | * Gets the node from the tree, that is associated with the specified key. |
universe@204 | 178 | * @param tree the UcxAVLTree |
universe@204 | 179 | * @param key the key |
universe@204 | 180 | * @return the node (or <code>NULL</code>, if the key is not present) |
universe@204 | 181 | */ |
universe@205 | 182 | UcxAVLNode *ucx_avl_get_node(UcxAVLTree *tree, intptr_t key); |
universe@204 | 183 | |
universe@204 | 184 | /** |
universe@193 | 185 | * Gets the value from the tree, that is associated with the specified key. |
universe@193 | 186 | * @param tree the UcxAVLTree |
universe@193 | 187 | * @param key the key |
universe@193 | 188 | * @return the value (or <code>NULL</code>, if the key is not present) |
universe@193 | 189 | */ |
universe@194 | 190 | void *ucx_avl_get(UcxAVLTree *tree, intptr_t key); |
universe@193 | 191 | |
universe@193 | 192 | /** |
universe@243 | 193 | * A mode for #ucx_avl_find_node() with the same behavior as |
universe@243 | 194 | * #ucx_avl_get_node(). |
universe@243 | 195 | */ |
universe@243 | 196 | #define UCX_AVL_FIND_EXACT 0 |
universe@243 | 197 | /** |
universe@243 | 198 | * A mode for #ucx_avl_find_node() finding the node whose key is at least |
universe@243 | 199 | * as large as the specified key. |
universe@243 | 200 | */ |
universe@243 | 201 | #define UCX_AVL_FIND_LOWER_BOUNDED 1 |
universe@243 | 202 | /** |
universe@243 | 203 | * A mode for #ucx_avl_find_node() finding the node whose key is at most |
universe@243 | 204 | * as large as the specified key. |
universe@243 | 205 | */ |
universe@243 | 206 | #define UCX_AVL_FIND_UPPER_BOUNDED 2 |
universe@243 | 207 | /** |
universe@243 | 208 | * A mode for #ucx_avl_find_node() finding the node with a key that is as close |
universe@243 | 209 | * to the specified key as possible. If the key is present, the behavior is |
universe@243 | 210 | * like #ucx_avl_get_node(). This mode only returns <code>NULL</code> on |
universe@243 | 211 | * empty trees. |
universe@243 | 212 | */ |
universe@243 | 213 | #define UCX_AVL_FIND_CLOSEST 3 |
universe@243 | 214 | |
universe@243 | 215 | /** |
universe@243 | 216 | * Finds a node within the tree. The following modes are supported: |
universe@243 | 217 | * <ul> |
universe@243 | 218 | * <li>#UCX_AVL_FIND_EXACT: the same behavior as #ucx_avl_get_node()</li> |
universe@243 | 219 | * <li>#UCX_AVL_FIND_LOWER_BOUNDED: finds the node whose key is at least |
universe@243 | 220 | * as large as the specified key</li> |
universe@243 | 221 | * <li>#UCX_AVL_FIND_UPPER_BOUNDED: finds the node whose key is at most |
universe@243 | 222 | * as large as the specified key</li> |
universe@243 | 223 | * <li>#UCX_AVL_FIND_CLOSEST: finds the node with a key that is as close to |
universe@243 | 224 | * the specified key as possible. If the key is present, the behavior is |
universe@243 | 225 | * like #ucx_avl_get_node(). This mode only returns <code>NULL</code> on |
universe@243 | 226 | * empty trees.</li> |
universe@243 | 227 | * </ul> |
universe@243 | 228 | * |
universe@243 | 229 | * The distance function provided MUST agree with the compare function of |
universe@243 | 230 | * the AVL tree. |
universe@243 | 231 | * |
universe@243 | 232 | * @param tree the UcxAVLTree |
universe@243 | 233 | * @param key the key |
universe@243 | 234 | * @param dfnc the distance function |
universe@243 | 235 | * @param mode the find mode |
universe@243 | 236 | * @return the node (or <code>NULL</code>, if no node can be found) |
universe@243 | 237 | */ |
universe@243 | 238 | UcxAVLNode *ucx_avl_find_node(UcxAVLTree *tree, intptr_t key, |
universe@243 | 239 | distance_func dfnc, int mode); |
universe@243 | 240 | |
universe@243 | 241 | /** |
universe@243 | 242 | * Finds a value within the tree. |
universe@243 | 243 | * See #ucx_avl_find_node() for details. |
universe@243 | 244 | * |
universe@243 | 245 | * @param tree the UcxAVLTree |
universe@243 | 246 | * @param key the key |
universe@243 | 247 | * @param dfnc the distance function |
universe@243 | 248 | * @param mode the find mode |
universe@243 | 249 | * @return the value (or <code>NULL</code>, if no value can be found) |
universe@243 | 250 | */ |
universe@243 | 251 | void *ucx_avl_find(UcxAVLTree *tree, intptr_t key, |
universe@243 | 252 | distance_func dfnc, int mode); |
universe@243 | 253 | |
universe@243 | 254 | /** |
universe@193 | 255 | * Puts a key/value pair into the tree. |
universe@204 | 256 | * |
universe@204 | 257 | * Attention: use this function only, if a possible old value does not need |
universe@204 | 258 | * to be preserved. |
universe@204 | 259 | * |
universe@193 | 260 | * @param tree the UcxAVLTree |
universe@193 | 261 | * @param key the key |
universe@193 | 262 | * @param value the new value |
universe@204 | 263 | * @return zero, if and only if the operation succeeded |
universe@193 | 264 | */ |
universe@204 | 265 | int ucx_avl_put(UcxAVLTree *tree, intptr_t key, void *value); |
universe@204 | 266 | |
universe@204 | 267 | /** |
universe@204 | 268 | * Puts a key/value pair into the tree. |
universe@204 | 269 | * |
universe@204 | 270 | * This is a secure function which saves the old value to the variable pointed |
universe@204 | 271 | * at by oldvalue. |
universe@204 | 272 | * |
universe@204 | 273 | * @param tree the UcxAVLTree |
universe@204 | 274 | * @param key the key |
universe@204 | 275 | * @param value the new value |
universe@204 | 276 | * @param oldvalue optional: a pointer to the location where a possible old |
universe@204 | 277 | * value shall be stored |
universe@204 | 278 | * @return zero, if and only if the operation succeeded |
universe@204 | 279 | */ |
universe@204 | 280 | int ucx_avl_put_s(UcxAVLTree *tree, intptr_t key, void *value, void **oldvalue); |
universe@204 | 281 | |
universe@204 | 282 | /** |
universe@204 | 283 | * Removes a node from the AVL tree. |
universe@204 | 284 | * |
universe@204 | 285 | * Note: the specified node is logically removed. The tree implementation |
universe@204 | 286 | * decides which memory area is freed. In most cases the here provided node |
universe@243 | 287 | * is freed, so its further use is generally undefined. |
universe@204 | 288 | * |
universe@204 | 289 | * @param tree the UcxAVLTree |
universe@204 | 290 | * @param node the node to remove |
universe@204 | 291 | * @return zero, if and only if an element has been removed |
universe@204 | 292 | */ |
universe@205 | 293 | int ucx_avl_remove_node(UcxAVLTree *tree, UcxAVLNode *node); |
universe@193 | 294 | |
universe@193 | 295 | /** |
universe@193 | 296 | * Removes an element from the AVL tree. |
universe@204 | 297 | * |
universe@193 | 298 | * @param tree the UcxAVLTree |
universe@193 | 299 | * @param key the key |
universe@204 | 300 | * @return zero, if and only if an element has been removed |
universe@193 | 301 | */ |
universe@204 | 302 | int ucx_avl_remove(UcxAVLTree *tree, intptr_t key); |
universe@204 | 303 | |
universe@204 | 304 | /** |
universe@204 | 305 | * Removes an element from the AVL tree. |
universe@204 | 306 | * |
universe@204 | 307 | * This is a secure function which saves the old key and value data from node |
universe@204 | 308 | * to the variables at the location of oldkey and oldvalue (if specified), so |
universe@204 | 309 | * they can be freed afterwards (if necessary). |
universe@204 | 310 | * |
universe@204 | 311 | * Note: the returned key in oldkey is possibly not the same as the provided |
universe@204 | 312 | * key for the lookup (in terms of memory location). |
universe@204 | 313 | * |
universe@204 | 314 | * @param tree the UcxAVLTree |
universe@204 | 315 | * @param key the key of the element to remove |
universe@204 | 316 | * @param oldkey optional: a pointer to the location where the old key shall be |
universe@204 | 317 | * stored |
universe@204 | 318 | * @param oldvalue optional: a pointer to the location where the old value |
universe@204 | 319 | * shall be stored |
universe@204 | 320 | * @return zero, if and only if an element has been removed |
universe@204 | 321 | */ |
universe@204 | 322 | int ucx_avl_remove_s(UcxAVLTree *tree, intptr_t key, |
universe@204 | 323 | intptr_t *oldkey, void **oldvalue); |
universe@192 | 324 | |
universe@199 | 325 | /** |
universe@199 | 326 | * Counts the nodes in the specified UcxAVLTree. |
universe@199 | 327 | * @param tree the AVL tree |
universe@199 | 328 | * @return the node count |
universe@199 | 329 | */ |
universe@199 | 330 | size_t ucx_avl_count(UcxAVLTree *tree); |
universe@192 | 331 | |
universe@245 | 332 | /** |
universe@245 | 333 | * Finds the in-order predecessor of the given node. |
universe@245 | 334 | * @param node an AVL node |
universe@245 | 335 | * @return the in-order predecessor of the given node, or <code>NULL</code> if |
universe@245 | 336 | * the given node is the in-order minimum |
universe@245 | 337 | */ |
universe@245 | 338 | UcxAVLNode* ucx_avl_pred(UcxAVLNode* node); |
universe@245 | 339 | |
universe@245 | 340 | /** |
universe@245 | 341 | * Finds the in-order successor of the given node. |
universe@245 | 342 | * @param node an AVL node |
universe@245 | 343 | * @return the in-order successor of the given node, or <code>NULL</code> if |
universe@245 | 344 | * the given node is the in-order maximum |
universe@245 | 345 | */ |
universe@245 | 346 | UcxAVLNode* ucx_avl_succ(UcxAVLNode* node); |
universe@245 | 347 | |
universe@192 | 348 | #ifdef __cplusplus |
universe@192 | 349 | } |
universe@192 | 350 | #endif |
universe@192 | 351 | |
universe@192 | 352 | #endif /* UCX_AVL_H */ |
universe@192 | 353 |