Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include <grpc/support/avl.h> |
| 35 | |
| 36 | #include <assert.h> |
| 37 | #include <stdlib.h> |
| 38 | |
| 39 | #include <grpc/support/alloc.h> |
| 40 | #include <grpc/support/string_util.h> |
| 41 | #include <grpc/support/useful.h> |
| 42 | |
| 43 | gpr_avl gpr_avl_create(const gpr_avl_vtable *vtable) { |
| 44 | gpr_avl out; |
| 45 | out.vtable = vtable; |
| 46 | out.root = NULL; |
| 47 | return out; |
| 48 | } |
| 49 | |
| 50 | static gpr_avl_node *ref_node(gpr_avl_node *node) { |
| 51 | if (node) { |
| 52 | gpr_ref(&node->refs); |
| 53 | } |
| 54 | return node; |
| 55 | } |
| 56 | |
| 57 | static void unref_node(const gpr_avl_vtable *vtable, gpr_avl_node *node) { |
| 58 | if (node == NULL) { |
| 59 | return; |
| 60 | } |
| 61 | if (gpr_unref(&node->refs)) { |
| 62 | vtable->destroy_key(node->key); |
| 63 | vtable->destroy_value(node->value); |
| 64 | unref_node(vtable, node->left); |
| 65 | unref_node(vtable, node->right); |
| 66 | gpr_free(node); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | static long node_height(gpr_avl_node *node) { |
| 71 | return node == NULL ? 0 : node->height; |
| 72 | } |
| 73 | |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 74 | #ifndef NDEBUG |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 75 | static long calculate_height(gpr_avl_node *node) { |
| 76 | return node == NULL ? 0 : 1 + GPR_MAX(calculate_height(node->left), |
| 77 | calculate_height(node->right)); |
| 78 | } |
| 79 | |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 80 | static gpr_avl_node *assert_invariants(gpr_avl_node *n) { |
| 81 | if (n == NULL) return NULL; |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 82 | assert_invariants(n->left); |
| 83 | assert_invariants(n->right); |
| 84 | assert(calculate_height(n) == n->height); |
| 85 | assert(labs(node_height(n->left) - node_height(n->right)) <= 1); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 86 | return n; |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 87 | } |
Craig Tiller | 9c721ff | 2015-11-23 15:16:12 -0800 | [diff] [blame] | 88 | #else |
Craig Tiller | 22d11e1 | 2015-11-23 15:33:54 -0800 | [diff] [blame] | 89 | static gpr_avl_node *assert_invariants(gpr_avl_node *n) { return n; } |
Craig Tiller | 9c721ff | 2015-11-23 15:16:12 -0800 | [diff] [blame] | 90 | #endif |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 91 | |
| 92 | gpr_avl_node *new_node(void *key, void *value, gpr_avl_node *left, |
| 93 | gpr_avl_node *right) { |
| 94 | gpr_avl_node *node = gpr_malloc(sizeof(*node)); |
| 95 | gpr_ref_init(&node->refs, 1); |
| 96 | node->key = key; |
| 97 | node->value = value; |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 98 | node->left = assert_invariants(left); |
| 99 | node->right = assert_invariants(right); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 100 | node->height = 1 + GPR_MAX(node_height(left), node_height(right)); |
| 101 | return node; |
| 102 | } |
| 103 | |
| 104 | static gpr_avl_node *get(const gpr_avl_vtable *vtable, gpr_avl_node *node, |
| 105 | void *key) { |
| 106 | long cmp; |
| 107 | |
| 108 | if (node == NULL) { |
| 109 | return NULL; |
| 110 | } |
| 111 | |
| 112 | cmp = vtable->compare_keys(node->key, key); |
| 113 | if (cmp == 0) { |
| 114 | return node; |
| 115 | } else if (cmp > 0) { |
| 116 | return get(vtable, node->left, key); |
| 117 | } else { |
| 118 | return get(vtable, node->right, key); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void *gpr_avl_get(gpr_avl avl, void *key) { |
| 123 | gpr_avl_node *node = get(avl.vtable, avl.root, key); |
| 124 | return node ? node->value : NULL; |
| 125 | } |
| 126 | |
| 127 | static gpr_avl_node *rotate_left(const gpr_avl_vtable *vtable, void *key, |
| 128 | void *value, gpr_avl_node *left, |
| 129 | gpr_avl_node *right) { |
| 130 | gpr_avl_node *n = |
| 131 | new_node(vtable->copy_key(right->key), vtable->copy_value(right->value), |
| 132 | new_node(key, value, left, ref_node(right->left)), |
| 133 | ref_node(right->right)); |
| 134 | unref_node(vtable, right); |
| 135 | return n; |
| 136 | } |
| 137 | |
| 138 | static gpr_avl_node *rotate_right(const gpr_avl_vtable *vtable, void *key, |
| 139 | void *value, gpr_avl_node *left, |
| 140 | gpr_avl_node *right) { |
| 141 | gpr_avl_node *n = new_node( |
| 142 | vtable->copy_key(left->key), vtable->copy_value(left->value), |
| 143 | ref_node(left->left), new_node(key, value, ref_node(left->right), right)); |
| 144 | unref_node(vtable, left); |
| 145 | return n; |
| 146 | } |
| 147 | |
| 148 | static gpr_avl_node *rotate_left_right(const gpr_avl_vtable *vtable, void *key, |
| 149 | void *value, gpr_avl_node *left, |
| 150 | gpr_avl_node *right) { |
| 151 | /* rotate_right(..., rotate_left(left), right) */ |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 152 | gpr_avl_node *n = new_node( |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 153 | vtable->copy_key(left->right->key), |
| 154 | vtable->copy_value(left->right->value), |
| 155 | new_node(vtable->copy_key(left->key), vtable->copy_value(left->value), |
| 156 | ref_node(left->left), ref_node(left->right->left)), |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 157 | new_node(key, value, ref_node(left->right->right), right)); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 158 | unref_node(vtable, left); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 159 | return n; |
| 160 | } |
| 161 | |
| 162 | static gpr_avl_node *rotate_right_left(const gpr_avl_vtable *vtable, void *key, |
| 163 | void *value, gpr_avl_node *left, |
| 164 | gpr_avl_node *right) { |
| 165 | /* rotate_left(..., left, rotate_right(right)) */ |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 166 | gpr_avl_node *n = new_node( |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 167 | vtable->copy_key(right->left->key), |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 168 | vtable->copy_value(right->left->value), |
| 169 | new_node(key, value, left, ref_node(right->left->left)), |
Craig Tiller | ba7d4df | 2016-01-25 11:47:58 -0800 | [diff] [blame] | 170 | new_node(vtable->copy_key(right->key), vtable->copy_value(right->value), |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 171 | ref_node(right->left->right), ref_node(right->right))); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 172 | unref_node(vtable, right); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 173 | return n; |
| 174 | } |
| 175 | |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 176 | static gpr_avl_node *rebalance(const gpr_avl_vtable *vtable, void *key, |
| 177 | void *value, gpr_avl_node *left, |
| 178 | gpr_avl_node *right) { |
| 179 | switch (node_height(left) - node_height(right)) { |
| 180 | case 2: |
| 181 | if (node_height(left->left) - node_height(left->right) == -1) { |
| 182 | return assert_invariants( |
| 183 | rotate_left_right(vtable, key, value, left, right)); |
| 184 | } else { |
| 185 | return assert_invariants(rotate_right(vtable, key, value, left, right)); |
| 186 | } |
| 187 | case -2: |
| 188 | if (node_height(right->left) - node_height(right->right) == 1) { |
| 189 | return assert_invariants( |
| 190 | rotate_right_left(vtable, key, value, left, right)); |
| 191 | } else { |
| 192 | return assert_invariants(rotate_left(vtable, key, value, left, right)); |
| 193 | } |
| 194 | default: |
| 195 | return assert_invariants(new_node(key, value, left, right)); |
| 196 | } |
| 197 | } |
| 198 | |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 199 | static gpr_avl_node *add(const gpr_avl_vtable *vtable, gpr_avl_node *node, |
| 200 | void *key, void *value) { |
| 201 | long cmp; |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 202 | if (node == NULL) { |
| 203 | return new_node(key, value, NULL, NULL); |
| 204 | } |
| 205 | cmp = vtable->compare_keys(node->key, key); |
| 206 | if (cmp == 0) { |
Craig Tiller | 22d11e1 | 2015-11-23 15:33:54 -0800 | [diff] [blame] | 207 | return new_node(key, value, ref_node(node->left), ref_node(node->right)); |
| 208 | } else if (cmp > 0) { |
Craig Tiller | 3328a58 | 2015-11-23 15:39:32 -0800 | [diff] [blame] | 209 | return rebalance( |
| 210 | vtable, vtable->copy_key(node->key), vtable->copy_value(node->value), |
| 211 | add(vtable, node->left, key, value), ref_node(node->right)); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 212 | } else { |
Craig Tiller | 22d11e1 | 2015-11-23 15:33:54 -0800 | [diff] [blame] | 213 | return rebalance(vtable, vtable->copy_key(node->key), |
| 214 | vtable->copy_value(node->value), ref_node(node->left), |
| 215 | add(vtable, node->right, key, value)); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 216 | } |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | gpr_avl gpr_avl_add(gpr_avl avl, void *key, void *value) { |
| 220 | gpr_avl_node *old_root = avl.root; |
| 221 | avl.root = add(avl.vtable, avl.root, key, value); |
| 222 | assert_invariants(avl.root); |
| 223 | unref_node(avl.vtable, old_root); |
| 224 | return avl; |
| 225 | } |
| 226 | |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 227 | static gpr_avl_node *in_order_head(gpr_avl_node *node) { |
| 228 | while (node->left != NULL) { |
| 229 | node = node->left; |
| 230 | } |
| 231 | return node; |
| 232 | } |
| 233 | |
| 234 | static gpr_avl_node *in_order_tail(gpr_avl_node *node) { |
| 235 | while (node->right != NULL) { |
| 236 | node = node->right; |
| 237 | } |
| 238 | return node; |
| 239 | } |
| 240 | |
| 241 | static gpr_avl_node *remove(const gpr_avl_vtable *vtable, gpr_avl_node *node, |
| 242 | void *key) { |
| 243 | long cmp; |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 244 | if (node == NULL) { |
| 245 | return NULL; |
| 246 | } |
| 247 | cmp = vtable->compare_keys(node->key, key); |
| 248 | if (cmp == 0) { |
| 249 | if (node->left == NULL) { |
| 250 | return ref_node(node->right); |
| 251 | } else if (node->right == NULL) { |
| 252 | return ref_node(node->left); |
| 253 | } else if (node->left->height < node->right->height) { |
| 254 | gpr_avl_node *h = in_order_head(node->right); |
Craig Tiller | 22d11e1 | 2015-11-23 15:33:54 -0800 | [diff] [blame] | 255 | return rebalance(vtable, vtable->copy_key(h->key), |
Craig Tiller | 3328a58 | 2015-11-23 15:39:32 -0800 | [diff] [blame] | 256 | vtable->copy_value(h->value), ref_node(node->left), |
| 257 | remove(vtable, node->right, h->key)); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 258 | } else { |
| 259 | gpr_avl_node *h = in_order_tail(node->left); |
Craig Tiller | 3328a58 | 2015-11-23 15:39:32 -0800 | [diff] [blame] | 260 | return rebalance( |
| 261 | vtable, vtable->copy_key(h->key), vtable->copy_value(h->value), |
| 262 | remove(vtable, node->left, h->key), ref_node(node->right)); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 263 | } |
Craig Tiller | 22d11e1 | 2015-11-23 15:33:54 -0800 | [diff] [blame] | 264 | } else if (cmp > 0) { |
| 265 | return rebalance(vtable, vtable->copy_key(node->key), |
| 266 | vtable->copy_value(node->value), |
| 267 | remove(vtable, node->left, key), ref_node(node->right)); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 268 | } else { |
Craig Tiller | 22d11e1 | 2015-11-23 15:33:54 -0800 | [diff] [blame] | 269 | return rebalance(vtable, vtable->copy_key(node->key), |
| 270 | vtable->copy_value(node->value), ref_node(node->left), |
| 271 | remove(vtable, node->right, key)); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 272 | } |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | gpr_avl gpr_avl_remove(gpr_avl avl, void *key) { |
| 276 | gpr_avl_node *old_root = avl.root; |
| 277 | avl.root = remove(avl.vtable, avl.root, key); |
| 278 | assert_invariants(avl.root); |
| 279 | unref_node(avl.vtable, old_root); |
| 280 | return avl; |
| 281 | } |
| 282 | |
| 283 | gpr_avl gpr_avl_ref(gpr_avl avl) { |
| 284 | ref_node(avl.root); |
| 285 | return avl; |
| 286 | } |
| 287 | |
| 288 | void gpr_avl_unref(gpr_avl avl) { unref_node(avl.vtable, avl.root); } |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame^] | 289 | |
| 290 | int gpr_avl_is_empty(gpr_avl avl) { return avl.root == NULL; } |