Thu, 03 May 2018 10:09:49 +0200
adds distance functions for integers as utils
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ /** * @file avl.h * * AVL tree implementation. * * This binary search tree implementation allows average O(1) insertion and * removal of elements (excluding binary search time). * * @author Mike Becker * @author Olaf Wintermann */ #ifndef UCX_AVL_H #define UCX_AVL_H #include "ucx.h" #include "allocator.h" #include <inttypes.h> #ifdef __cplusplus extern "C" { #endif /** * UCX AVL Node type. * * @see UcxAVLNode */ typedef struct UcxAVLNode UcxAVLNode; /** * UCX AVL Node. */ struct UcxAVLNode { /** * The key for this node. */ intptr_t key; /** * Data contained by this node. */ void *value; /** * The height of this (sub)-tree. */ size_t height; /** * Parent node. */ UcxAVLNode *parent; /** * Root node of left subtree. */ UcxAVLNode *left; /** * Root node of right subtree. */ UcxAVLNode *right; }; /** * UCX AVL Tree. */ typedef struct { /** * The UcxAllocator that shall be used to manage the memory for node data. */ UcxAllocator *allocator; /** * Root node of the tree. */ UcxAVLNode *root; /** * Compare function that shall be used to compare the UcxAVLNode keys. * @see UcxAVLNode.key */ cmp_func cmpfunc; /** * Custom user data. * This data will also be provided to the cmpfunc. */ void *userdata; } UcxAVLTree; /** * Initializes a new UcxAVLTree with a default allocator. * * @param cmpfunc the compare function that shall be used * @return a new UcxAVLTree object * @see ucx_avl_new_a() */ UcxAVLTree *ucx_avl_new(cmp_func cmpfunc); /** * Initializes a new UcxAVLTree with the specified allocator. * * The cmpfunc should be capable of comparing two keys within this AVL tree. * So if you want to use null terminated strings as keys, you could use the * ucx_strcmp() function here. * * @param cmpfunc the compare function that shall be used * @param allocator the UcxAllocator that shall be used * @return a new UcxAVLTree object */ UcxAVLTree *ucx_avl_new_a(cmp_func cmpfunc, UcxAllocator *allocator); /** * Destroys a UcxAVLTree. * @param tree the tree to destroy */ void ucx_avl_free(UcxAVLTree *tree); /** * Macro for initializing a new UcxAVLTree with the default allocator and a * ucx_ptrcmp() compare function. * * @return a new default UcxAVLTree object */ #define ucx_avl_default_new() ucx_avl_new_a(ucx_ptrcmp, ucx_default_allocator()) /** * Gets the node from the tree, that is associated with the specified key. * @param tree the UcxAVLTree * @param key the key * @return the node (or <code>NULL</code>, if the key is not present) */ UcxAVLNode *ucx_avl_get_node(UcxAVLTree *tree, intptr_t key); /** * Gets the value from the tree, that is associated with the specified key. * @param tree the UcxAVLTree * @param key the key * @return the value (or <code>NULL</code>, if the key is not present) */ void *ucx_avl_get(UcxAVLTree *tree, intptr_t key); /** * A mode for #ucx_avl_find_node() with the same behavior as * #ucx_avl_get_node(). */ #define UCX_AVL_FIND_EXACT 0 /** * A mode for #ucx_avl_find_node() finding the node whose key is at least * as large as the specified key. */ #define UCX_AVL_FIND_LOWER_BOUNDED 1 /** * A mode for #ucx_avl_find_node() finding the node whose key is at most * as large as the specified key. */ #define UCX_AVL_FIND_UPPER_BOUNDED 2 /** * A mode for #ucx_avl_find_node() finding the node with a key that is as close * to the specified key as possible. If the key is present, the behavior is * like #ucx_avl_get_node(). This mode only returns <code>NULL</code> on * empty trees. */ #define UCX_AVL_FIND_CLOSEST 3 /** * Finds a node within the tree. The following modes are supported: * <ul> * <li>#UCX_AVL_FIND_EXACT: the same behavior as #ucx_avl_get_node()</li> * <li>#UCX_AVL_FIND_LOWER_BOUNDED: finds the node whose key is at least * as large as the specified key</li> * <li>#UCX_AVL_FIND_UPPER_BOUNDED: finds the node whose key is at most * as large as the specified key</li> * <li>#UCX_AVL_FIND_CLOSEST: finds the node with a key that is as close to * the specified key as possible. If the key is present, the behavior is * like #ucx_avl_get_node(). This mode only returns <code>NULL</code> on * empty trees.</li> * </ul> * * The distance function provided MUST agree with the compare function of * the AVL tree. * * @param tree the UcxAVLTree * @param key the key * @param dfnc the distance function * @param mode the find mode * @return the node (or <code>NULL</code>, if no node can be found) */ UcxAVLNode *ucx_avl_find_node(UcxAVLTree *tree, intptr_t key, distance_func dfnc, int mode); /** * Finds a value within the tree. * See #ucx_avl_find_node() for details. * * @param tree the UcxAVLTree * @param key the key * @param dfnc the distance function * @param mode the find mode * @return the value (or <code>NULL</code>, if no value can be found) */ void *ucx_avl_find(UcxAVLTree *tree, intptr_t key, distance_func dfnc, int mode); /** * Puts a key/value pair into the tree. * * Attention: use this function only, if a possible old value does not need * to be preserved. * * @param tree the UcxAVLTree * @param key the key * @param value the new value * @return zero, if and only if the operation succeeded */ int ucx_avl_put(UcxAVLTree *tree, intptr_t key, void *value); /** * Puts a key/value pair into the tree. * * This is a secure function which saves the old value to the variable pointed * at by oldvalue. * * @param tree the UcxAVLTree * @param key the key * @param value the new value * @param oldvalue optional: a pointer to the location where a possible old * value shall be stored * @return zero, if and only if the operation succeeded */ int ucx_avl_put_s(UcxAVLTree *tree, intptr_t key, void *value, void **oldvalue); /** * Removes a node from the AVL tree. * * Note: the specified node is logically removed. The tree implementation * decides which memory area is freed. In most cases the here provided node * is freed, so its further use is generally undefined. * * @param tree the UcxAVLTree * @param node the node to remove * @return zero, if and only if an element has been removed */ int ucx_avl_remove_node(UcxAVLTree *tree, UcxAVLNode *node); /** * Removes an element from the AVL tree. * * @param tree the UcxAVLTree * @param key the key * @return zero, if and only if an element has been removed */ int ucx_avl_remove(UcxAVLTree *tree, intptr_t key); /** * Removes an element from the AVL tree. * * This is a secure function which saves the old key and value data from node * to the variables at the location of oldkey and oldvalue (if specified), so * they can be freed afterwards (if necessary). * * Note: the returned key in oldkey is possibly not the same as the provided * key for the lookup (in terms of memory location). * * @param tree the UcxAVLTree * @param key the key of the element to remove * @param oldkey optional: a pointer to the location where the old key shall be * stored * @param oldvalue optional: a pointer to the location where the old value * shall be stored * @return zero, if and only if an element has been removed */ int ucx_avl_remove_s(UcxAVLTree *tree, intptr_t key, intptr_t *oldkey, void **oldvalue); /** * Counts the nodes in the specified UcxAVLTree. * @param tree the AVL tree * @return the node count */ size_t ucx_avl_count(UcxAVLTree *tree); /** * Finds the in-order predecessor of the given node. * @param node an AVL node * @return the in-order predecessor of the given node, or <code>NULL</code> if * the given node is the in-order minimum */ UcxAVLNode* ucx_avl_pred(UcxAVLNode* node); /** * Finds the in-order successor of the given node. * @param node an AVL node * @return the in-order successor of the given node, or <code>NULL</code> if * the given node is the in-order maximum */ UcxAVLNode* ucx_avl_succ(UcxAVLNode* node); #ifdef __cplusplus } #endif #endif /* UCX_AVL_H */