Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 3 | * Copyright 2015 gRPC authors. |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <grpc/support/avl.h> |
| 20 | |
| 21 | #include <assert.h> |
| 22 | #include <stdlib.h> |
| 23 | |
| 24 | #include <grpc/support/alloc.h> |
| 25 | #include <grpc/support/string_util.h> |
| 26 | #include <grpc/support/useful.h> |
| 27 | |
| 28 | gpr_avl gpr_avl_create(const gpr_avl_vtable *vtable) { |
| 29 | gpr_avl out; |
| 30 | out.vtable = vtable; |
| 31 | out.root = NULL; |
| 32 | return out; |
| 33 | } |
| 34 | |
| 35 | static gpr_avl_node *ref_node(gpr_avl_node *node) { |
| 36 | if (node) { |
| 37 | gpr_ref(&node->refs); |
| 38 | } |
| 39 | return node; |
| 40 | } |
| 41 | |
| 42 | static void unref_node(const gpr_avl_vtable *vtable, gpr_avl_node *node) { |
| 43 | if (node == NULL) { |
| 44 | return; |
| 45 | } |
| 46 | if (gpr_unref(&node->refs)) { |
| 47 | vtable->destroy_key(node->key); |
| 48 | vtable->destroy_value(node->value); |
| 49 | unref_node(vtable, node->left); |
| 50 | unref_node(vtable, node->right); |
| 51 | gpr_free(node); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | static long node_height(gpr_avl_node *node) { |
| 56 | return node == NULL ? 0 : node->height; |
| 57 | } |
| 58 | |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 59 | #ifndef NDEBUG |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 60 | static long calculate_height(gpr_avl_node *node) { |
| 61 | return node == NULL ? 0 : 1 + GPR_MAX(calculate_height(node->left), |
| 62 | calculate_height(node->right)); |
| 63 | } |
| 64 | |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 65 | static gpr_avl_node *assert_invariants(gpr_avl_node *n) { |
| 66 | if (n == NULL) return NULL; |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 67 | assert_invariants(n->left); |
| 68 | assert_invariants(n->right); |
| 69 | assert(calculate_height(n) == n->height); |
| 70 | assert(labs(node_height(n->left) - node_height(n->right)) <= 1); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 71 | return n; |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 72 | } |
Craig Tiller | 9c721ff | 2015-11-23 15:16:12 -0800 | [diff] [blame] | 73 | #else |
Craig Tiller | 22d11e1 | 2015-11-23 15:33:54 -0800 | [diff] [blame] | 74 | static gpr_avl_node *assert_invariants(gpr_avl_node *n) { return n; } |
Craig Tiller | 9c721ff | 2015-11-23 15:16:12 -0800 | [diff] [blame] | 75 | #endif |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 76 | |
| 77 | gpr_avl_node *new_node(void *key, void *value, gpr_avl_node *left, |
| 78 | gpr_avl_node *right) { |
| 79 | gpr_avl_node *node = gpr_malloc(sizeof(*node)); |
| 80 | gpr_ref_init(&node->refs, 1); |
| 81 | node->key = key; |
| 82 | node->value = value; |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 83 | node->left = assert_invariants(left); |
| 84 | node->right = assert_invariants(right); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 85 | node->height = 1 + GPR_MAX(node_height(left), node_height(right)); |
| 86 | return node; |
| 87 | } |
| 88 | |
| 89 | static gpr_avl_node *get(const gpr_avl_vtable *vtable, gpr_avl_node *node, |
| 90 | void *key) { |
| 91 | long cmp; |
| 92 | |
| 93 | if (node == NULL) { |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| 97 | cmp = vtable->compare_keys(node->key, key); |
| 98 | if (cmp == 0) { |
| 99 | return node; |
| 100 | } else if (cmp > 0) { |
| 101 | return get(vtable, node->left, key); |
| 102 | } else { |
| 103 | return get(vtable, node->right, key); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void *gpr_avl_get(gpr_avl avl, void *key) { |
| 108 | gpr_avl_node *node = get(avl.vtable, avl.root, key); |
| 109 | return node ? node->value : NULL; |
| 110 | } |
| 111 | |
Craig Tiller | 965eab3 | 2016-05-07 22:11:37 -0700 | [diff] [blame] | 112 | int gpr_avl_maybe_get(gpr_avl avl, void *key, void **value) { |
| 113 | gpr_avl_node *node = get(avl.vtable, avl.root, key); |
| 114 | if (node != NULL) { |
| 115 | *value = node->value; |
| 116 | return 1; |
| 117 | } |
| 118 | return 0; |
| 119 | } |
| 120 | |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 121 | static gpr_avl_node *rotate_left(const gpr_avl_vtable *vtable, void *key, |
| 122 | void *value, gpr_avl_node *left, |
| 123 | gpr_avl_node *right) { |
| 124 | gpr_avl_node *n = |
| 125 | new_node(vtable->copy_key(right->key), vtable->copy_value(right->value), |
| 126 | new_node(key, value, left, ref_node(right->left)), |
| 127 | ref_node(right->right)); |
| 128 | unref_node(vtable, right); |
| 129 | return n; |
| 130 | } |
| 131 | |
| 132 | static gpr_avl_node *rotate_right(const gpr_avl_vtable *vtable, void *key, |
| 133 | void *value, gpr_avl_node *left, |
| 134 | gpr_avl_node *right) { |
| 135 | gpr_avl_node *n = new_node( |
| 136 | vtable->copy_key(left->key), vtable->copy_value(left->value), |
| 137 | ref_node(left->left), new_node(key, value, ref_node(left->right), right)); |
| 138 | unref_node(vtable, left); |
| 139 | return n; |
| 140 | } |
| 141 | |
| 142 | static gpr_avl_node *rotate_left_right(const gpr_avl_vtable *vtable, void *key, |
| 143 | void *value, gpr_avl_node *left, |
| 144 | gpr_avl_node *right) { |
| 145 | /* rotate_right(..., rotate_left(left), right) */ |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 146 | gpr_avl_node *n = new_node( |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 147 | vtable->copy_key(left->right->key), |
| 148 | vtable->copy_value(left->right->value), |
| 149 | new_node(vtable->copy_key(left->key), vtable->copy_value(left->value), |
| 150 | ref_node(left->left), ref_node(left->right->left)), |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 151 | new_node(key, value, ref_node(left->right->right), right)); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 152 | unref_node(vtable, left); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 153 | return n; |
| 154 | } |
| 155 | |
| 156 | static gpr_avl_node *rotate_right_left(const gpr_avl_vtable *vtable, void *key, |
| 157 | void *value, gpr_avl_node *left, |
| 158 | gpr_avl_node *right) { |
| 159 | /* rotate_left(..., left, rotate_right(right)) */ |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 160 | gpr_avl_node *n = new_node( |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 161 | vtable->copy_key(right->left->key), |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 162 | vtable->copy_value(right->left->value), |
| 163 | new_node(key, value, left, ref_node(right->left->left)), |
Craig Tiller | ba7d4df | 2016-01-25 11:47:58 -0800 | [diff] [blame] | 164 | new_node(vtable->copy_key(right->key), vtable->copy_value(right->value), |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 165 | ref_node(right->left->right), ref_node(right->right))); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 166 | unref_node(vtable, right); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 167 | return n; |
| 168 | } |
| 169 | |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 170 | static gpr_avl_node *rebalance(const gpr_avl_vtable *vtable, void *key, |
| 171 | void *value, gpr_avl_node *left, |
| 172 | gpr_avl_node *right) { |
| 173 | switch (node_height(left) - node_height(right)) { |
| 174 | case 2: |
| 175 | if (node_height(left->left) - node_height(left->right) == -1) { |
| 176 | return assert_invariants( |
| 177 | rotate_left_right(vtable, key, value, left, right)); |
| 178 | } else { |
| 179 | return assert_invariants(rotate_right(vtable, key, value, left, right)); |
| 180 | } |
| 181 | case -2: |
| 182 | if (node_height(right->left) - node_height(right->right) == 1) { |
| 183 | return assert_invariants( |
| 184 | rotate_right_left(vtable, key, value, left, right)); |
| 185 | } else { |
| 186 | return assert_invariants(rotate_left(vtable, key, value, left, right)); |
| 187 | } |
| 188 | default: |
| 189 | return assert_invariants(new_node(key, value, left, right)); |
| 190 | } |
| 191 | } |
| 192 | |
Zoltan Kuscsik | fcb56a7 | 2017-04-19 15:47:37 +0200 | [diff] [blame] | 193 | static gpr_avl_node *add_key(const gpr_avl_vtable *vtable, gpr_avl_node *node, |
| 194 | void *key, void *value) { |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 195 | long cmp; |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 196 | if (node == NULL) { |
| 197 | return new_node(key, value, NULL, NULL); |
| 198 | } |
| 199 | cmp = vtable->compare_keys(node->key, key); |
| 200 | if (cmp == 0) { |
Craig Tiller | 22d11e1 | 2015-11-23 15:33:54 -0800 | [diff] [blame] | 201 | return new_node(key, value, ref_node(node->left), ref_node(node->right)); |
| 202 | } else if (cmp > 0) { |
Craig Tiller | 3328a58 | 2015-11-23 15:39:32 -0800 | [diff] [blame] | 203 | return rebalance( |
| 204 | vtable, vtable->copy_key(node->key), vtable->copy_value(node->value), |
Zoltan Kuscsik | fcb56a7 | 2017-04-19 15:47:37 +0200 | [diff] [blame] | 205 | add_key(vtable, node->left, key, value), ref_node(node->right)); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 206 | } else { |
Craig Tiller | 22d11e1 | 2015-11-23 15:33:54 -0800 | [diff] [blame] | 207 | return rebalance(vtable, vtable->copy_key(node->key), |
| 208 | vtable->copy_value(node->value), ref_node(node->left), |
Zoltan Kuscsik | fcb56a7 | 2017-04-19 15:47:37 +0200 | [diff] [blame] | 209 | add_key(vtable, node->right, key, value)); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 210 | } |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | gpr_avl gpr_avl_add(gpr_avl avl, void *key, void *value) { |
| 214 | gpr_avl_node *old_root = avl.root; |
Zoltan Kuscsik | fcb56a7 | 2017-04-19 15:47:37 +0200 | [diff] [blame] | 215 | avl.root = add_key(avl.vtable, avl.root, key, value); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 216 | assert_invariants(avl.root); |
| 217 | unref_node(avl.vtable, old_root); |
| 218 | return avl; |
| 219 | } |
| 220 | |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 221 | static gpr_avl_node *in_order_head(gpr_avl_node *node) { |
| 222 | while (node->left != NULL) { |
| 223 | node = node->left; |
| 224 | } |
| 225 | return node; |
| 226 | } |
| 227 | |
| 228 | static gpr_avl_node *in_order_tail(gpr_avl_node *node) { |
| 229 | while (node->right != NULL) { |
| 230 | node = node->right; |
| 231 | } |
| 232 | return node; |
| 233 | } |
| 234 | |
Zoltan Kuscsik | fcb56a7 | 2017-04-19 15:47:37 +0200 | [diff] [blame] | 235 | static gpr_avl_node *remove_key(const gpr_avl_vtable *vtable, |
| 236 | gpr_avl_node *node, void *key) { |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 237 | long cmp; |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 238 | if (node == NULL) { |
| 239 | return NULL; |
| 240 | } |
| 241 | cmp = vtable->compare_keys(node->key, key); |
| 242 | if (cmp == 0) { |
| 243 | if (node->left == NULL) { |
| 244 | return ref_node(node->right); |
| 245 | } else if (node->right == NULL) { |
| 246 | return ref_node(node->left); |
| 247 | } else if (node->left->height < node->right->height) { |
| 248 | gpr_avl_node *h = in_order_head(node->right); |
Craig Tiller | 22d11e1 | 2015-11-23 15:33:54 -0800 | [diff] [blame] | 249 | return rebalance(vtable, vtable->copy_key(h->key), |
Craig Tiller | 3328a58 | 2015-11-23 15:39:32 -0800 | [diff] [blame] | 250 | vtable->copy_value(h->value), ref_node(node->left), |
Zoltan Kuscsik | fcb56a7 | 2017-04-19 15:47:37 +0200 | [diff] [blame] | 251 | remove_key(vtable, node->right, h->key)); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 252 | } else { |
| 253 | gpr_avl_node *h = in_order_tail(node->left); |
Craig Tiller | 3328a58 | 2015-11-23 15:39:32 -0800 | [diff] [blame] | 254 | return rebalance( |
| 255 | vtable, vtable->copy_key(h->key), vtable->copy_value(h->value), |
Zoltan Kuscsik | fcb56a7 | 2017-04-19 15:47:37 +0200 | [diff] [blame] | 256 | remove_key(vtable, node->left, h->key), ref_node(node->right)); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 257 | } |
Craig Tiller | 22d11e1 | 2015-11-23 15:33:54 -0800 | [diff] [blame] | 258 | } else if (cmp > 0) { |
Zoltan Kuscsik | fcb56a7 | 2017-04-19 15:47:37 +0200 | [diff] [blame] | 259 | return rebalance( |
| 260 | vtable, vtable->copy_key(node->key), vtable->copy_value(node->value), |
| 261 | remove_key(vtable, node->left, key), ref_node(node->right)); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 262 | } else { |
Craig Tiller | 22d11e1 | 2015-11-23 15:33:54 -0800 | [diff] [blame] | 263 | return rebalance(vtable, vtable->copy_key(node->key), |
| 264 | vtable->copy_value(node->value), ref_node(node->left), |
Zoltan Kuscsik | fcb56a7 | 2017-04-19 15:47:37 +0200 | [diff] [blame] | 265 | remove_key(vtable, node->right, key)); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 266 | } |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | gpr_avl gpr_avl_remove(gpr_avl avl, void *key) { |
| 270 | gpr_avl_node *old_root = avl.root; |
Zoltan Kuscsik | fcb56a7 | 2017-04-19 15:47:37 +0200 | [diff] [blame] | 271 | avl.root = remove_key(avl.vtable, avl.root, key); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 272 | assert_invariants(avl.root); |
| 273 | unref_node(avl.vtable, old_root); |
| 274 | return avl; |
| 275 | } |
| 276 | |
| 277 | gpr_avl gpr_avl_ref(gpr_avl avl) { |
| 278 | ref_node(avl.root); |
| 279 | return avl; |
| 280 | } |
| 281 | |
| 282 | 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] | 283 | |
| 284 | int gpr_avl_is_empty(gpr_avl avl) { return avl.root == NULL; } |