ucx/avl.h

Mon, 18 May 2015 18:39:19 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 18 May 2015 18:39:19 +0200
changeset 196
1b4cdafef2eb
parent 194
0c1b7676e74c
child 199
e25dc68336ec
permissions
-rw-r--r--

added free() to AVL tree implementation + use UcxAllocator

universe@192 1 /*
universe@192 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@192 3 *
universe@192 4 * Copyright 2015 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@192 36 * removal of elements.
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@193 45 #include "ucx.h"
universe@194 46 #include "allocator.h"
universe@194 47 #include <stdint.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@194 128 * ucx_strcmp() 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@196 137 * Destroys an UcxAVLTree.
universe@196 138 * @param tree the tree to destroy
universe@196 139 */
universe@196 140 void ucx_avl_free(UcxAVLTree *tree);
universe@196 141
universe@196 142 /**
universe@194 143 * Macro for initializing a new UcxAVLTree with the default allocator and a
universe@194 144 * ucx_ptrcmp() compare function.
universe@194 145 *
universe@194 146 * @return a new default UcxAVLTree object
universe@194 147 */
universe@194 148 #define ucx_avl_default_new() ucx_avl_new_a(ucx_ptrcmp, ucx_default_allocator())
universe@194 149
universe@194 150 /**
universe@193 151 * Gets the value from the tree, that is associated with the specified key.
universe@193 152 * @param tree the UcxAVLTree
universe@193 153 * @param key the key
universe@193 154 * @return the value (or <code>NULL</code>, if the key is not present)
universe@193 155 */
universe@194 156 void *ucx_avl_get(UcxAVLTree *tree, intptr_t key);
universe@193 157
universe@193 158 /**
universe@193 159 * Puts a key/value pair into the tree.
universe@193 160 * @param tree the UcxAVLTree
universe@193 161 * @param key the key
universe@193 162 * @param value the new value
universe@193 163 * @return the replaced value (or <code>NULL</code>, if the key is new to the
universe@193 164 * tree)
universe@193 165 */
universe@194 166 void* ucx_avl_put(UcxAVLTree *tree, intptr_t key, void *value);
universe@193 167
universe@193 168 /**
universe@193 169 * Removes an element from the AVL tree.
universe@193 170 * @param tree the UcxAVLTree
universe@193 171 * @param key the key
universe@193 172 * @return the removed value (or <code>NULL</code>, if the key was not present)
universe@193 173 */
universe@194 174 void* ucx_avl_remove(UcxAVLTree *tree, intptr_t key);
universe@192 175
universe@192 176
universe@192 177
universe@192 178 #ifdef __cplusplus
universe@192 179 }
universe@192 180 #endif
universe@192 181
universe@192 182 #endif /* UCX_AVL_H */
universe@192 183

mercurial