Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 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 | |
| 74 | static long calculate_height(gpr_avl_node *node) { |
| 75 | return node == NULL ? 0 : 1 + GPR_MAX(calculate_height(node->left), |
| 76 | calculate_height(node->right)); |
| 77 | } |
| 78 | |
| 79 | static void assert_invariants(gpr_avl_node *n) { |
| 80 | if (n == NULL) return; |
| 81 | assert_invariants(n->left); |
| 82 | assert_invariants(n->right); |
| 83 | assert(calculate_height(n) == n->height); |
| 84 | assert(labs(node_height(n->left) - node_height(n->right)) <= 1); |
| 85 | } |
| 86 | |
| 87 | gpr_avl_node *new_node(void *key, void *value, gpr_avl_node *left, |
| 88 | gpr_avl_node *right) { |
| 89 | gpr_avl_node *node = gpr_malloc(sizeof(*node)); |
| 90 | gpr_ref_init(&node->refs, 1); |
| 91 | node->key = key; |
| 92 | node->value = value; |
| 93 | node->left = left; |
| 94 | node->right = right; |
| 95 | node->height = 1 + GPR_MAX(node_height(left), node_height(right)); |
| 96 | return node; |
| 97 | } |
| 98 | |
| 99 | static gpr_avl_node *get(const gpr_avl_vtable *vtable, gpr_avl_node *node, |
| 100 | void *key) { |
| 101 | long cmp; |
| 102 | |
| 103 | if (node == NULL) { |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | cmp = vtable->compare_keys(node->key, key); |
| 108 | if (cmp == 0) { |
| 109 | return node; |
| 110 | } else if (cmp > 0) { |
| 111 | return get(vtable, node->left, key); |
| 112 | } else { |
| 113 | return get(vtable, node->right, key); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void *gpr_avl_get(gpr_avl avl, void *key) { |
| 118 | gpr_avl_node *node = get(avl.vtable, avl.root, key); |
| 119 | return node ? node->value : NULL; |
| 120 | } |
| 121 | |
| 122 | static gpr_avl_node *rotate_left(const gpr_avl_vtable *vtable, void *key, |
| 123 | void *value, gpr_avl_node *left, |
| 124 | gpr_avl_node *right) { |
| 125 | gpr_avl_node *n = |
| 126 | new_node(vtable->copy_key(right->key), vtable->copy_value(right->value), |
| 127 | new_node(key, value, left, ref_node(right->left)), |
| 128 | ref_node(right->right)); |
| 129 | unref_node(vtable, right); |
| 130 | return n; |
| 131 | } |
| 132 | |
| 133 | static gpr_avl_node *rotate_right(const gpr_avl_vtable *vtable, void *key, |
| 134 | void *value, gpr_avl_node *left, |
| 135 | gpr_avl_node *right) { |
| 136 | gpr_avl_node *n = new_node( |
| 137 | vtable->copy_key(left->key), vtable->copy_value(left->value), |
| 138 | ref_node(left->left), new_node(key, value, ref_node(left->right), right)); |
| 139 | unref_node(vtable, left); |
| 140 | return n; |
| 141 | } |
| 142 | |
| 143 | static gpr_avl_node *rotate_left_right(const gpr_avl_vtable *vtable, void *key, |
| 144 | void *value, gpr_avl_node *left, |
| 145 | gpr_avl_node *right) { |
| 146 | /* rotate_right(..., rotate_left(left), right) */ |
| 147 | /* TODO(ctiller): elide first allocation */ |
| 148 | gpr_avl_node *leftp = new_node( |
| 149 | vtable->copy_key(left->right->key), |
| 150 | vtable->copy_value(left->right->value), |
| 151 | new_node(vtable->copy_key(left->key), vtable->copy_value(left->value), |
| 152 | ref_node(left->left), ref_node(left->right->left)), |
| 153 | ref_node(left->right->right)); |
| 154 | gpr_avl_node *n = |
| 155 | new_node(vtable->copy_key(leftp->key), vtable->copy_value(leftp->value), |
| 156 | ref_node(leftp->left), |
| 157 | new_node(key, value, ref_node(leftp->right), right)); |
| 158 | unref_node(vtable, left); |
| 159 | unref_node(vtable, leftp); |
| 160 | return n; |
| 161 | } |
| 162 | |
| 163 | static gpr_avl_node *rotate_right_left(const gpr_avl_vtable *vtable, void *key, |
| 164 | void *value, gpr_avl_node *left, |
| 165 | gpr_avl_node *right) { |
| 166 | /* rotate_left(..., left, rotate_right(right)) */ |
| 167 | /* TODO(ctiller): elide first allocation */ |
| 168 | gpr_avl_node *rightp = new_node( |
| 169 | vtable->copy_key(right->left->key), |
| 170 | vtable->copy_value(right->left->value), ref_node(right->left->left), |
| 171 | new_node(vtable->copy_key(right->key), vtable->copy_key(right->value), |
| 172 | ref_node(right->left->right), ref_node(right->right))); |
| 173 | gpr_avl_node *n = |
| 174 | new_node(vtable->copy_key(rightp->key), vtable->copy_value(rightp->value), |
| 175 | new_node(key, value, left, ref_node(rightp->left)), |
| 176 | ref_node(rightp->right)); |
| 177 | unref_node(vtable, right); |
| 178 | unref_node(vtable, rightp); |
| 179 | return n; |
| 180 | } |
| 181 | |
| 182 | static gpr_avl_node *add(const gpr_avl_vtable *vtable, gpr_avl_node *node, |
| 183 | void *key, void *value) { |
| 184 | long cmp; |
| 185 | gpr_avl_node *l; |
| 186 | gpr_avl_node *r; |
| 187 | if (node == NULL) { |
| 188 | return new_node(key, value, NULL, NULL); |
| 189 | } |
| 190 | cmp = vtable->compare_keys(node->key, key); |
| 191 | if (cmp == 0) { |
| 192 | return new_node(key, value, NULL, NULL); |
| 193 | } |
| 194 | l = node->left; |
| 195 | r = node->right; |
| 196 | if (cmp > 0) { |
| 197 | l = add(vtable, l, key, value); |
| 198 | ref_node(r); |
| 199 | } else { |
| 200 | r = add(vtable, r, key, value); |
| 201 | ref_node(l); |
| 202 | } |
| 203 | |
| 204 | key = vtable->copy_key(node->key); |
| 205 | value = vtable->copy_value(node->value); |
| 206 | |
| 207 | switch (node_height(l) - node_height(r)) { |
| 208 | case 2: |
| 209 | if (node_height(l->left) - node_height(l->right) == 1) { |
| 210 | return rotate_right(vtable, key, value, l, r); |
| 211 | } else { |
| 212 | return rotate_left_right(vtable, key, value, l, r); |
| 213 | } |
| 214 | case -2: |
| 215 | if (node_height(r->left) - node_height(r->right) == 1) { |
| 216 | return rotate_right_left(vtable, key, value, l, r); |
| 217 | } else { |
| 218 | return rotate_left(vtable, key, value, l, r); |
| 219 | } |
| 220 | default: |
| 221 | return new_node(key, value, l, r); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | gpr_avl gpr_avl_add(gpr_avl avl, void *key, void *value) { |
| 226 | gpr_avl_node *old_root = avl.root; |
| 227 | avl.root = add(avl.vtable, avl.root, key, value); |
| 228 | assert_invariants(avl.root); |
| 229 | unref_node(avl.vtable, old_root); |
| 230 | return avl; |
| 231 | } |
| 232 | |
| 233 | void gpr_avl_destroy(gpr_avl avl) { unref_node(avl.vtable, avl.root); } |