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 | |
yang-g | d66ee1a | 2017-07-18 08:11:10 -0700 | [diff] [blame^] | 35 | static gpr_avl_node *ref_node(gpr_avl_node *node) { |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 36 | if (node) { |
| 37 | gpr_ref(&node->refs); |
| 38 | } |
| 39 | return node; |
| 40 | } |
| 41 | |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 42 | static void unref_node(const gpr_avl_vtable *vtable, gpr_avl_node *node, |
| 43 | void *user_data) { |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 44 | if (node == NULL) { |
| 45 | return; |
| 46 | } |
| 47 | if (gpr_unref(&node->refs)) { |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 48 | vtable->destroy_key(node->key, user_data); |
| 49 | vtable->destroy_value(node->value, user_data); |
| 50 | unref_node(vtable, node->left, user_data); |
| 51 | unref_node(vtable, node->right, user_data); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 52 | gpr_free(node); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | static long node_height(gpr_avl_node *node) { |
| 57 | return node == NULL ? 0 : node->height; |
| 58 | } |
| 59 | |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 60 | #ifndef NDEBUG |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 61 | static long calculate_height(gpr_avl_node *node) { |
| 62 | return node == NULL ? 0 : 1 + GPR_MAX(calculate_height(node->left), |
| 63 | calculate_height(node->right)); |
| 64 | } |
| 65 | |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 66 | static gpr_avl_node *assert_invariants(gpr_avl_node *n) { |
| 67 | if (n == NULL) return NULL; |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 68 | assert_invariants(n->left); |
| 69 | assert_invariants(n->right); |
| 70 | assert(calculate_height(n) == n->height); |
| 71 | assert(labs(node_height(n->left) - node_height(n->right)) <= 1); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 72 | return n; |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 73 | } |
Craig Tiller | 9c721ff | 2015-11-23 15:16:12 -0800 | [diff] [blame] | 74 | #else |
Craig Tiller | 22d11e1 | 2015-11-23 15:33:54 -0800 | [diff] [blame] | 75 | static gpr_avl_node *assert_invariants(gpr_avl_node *n) { return n; } |
Craig Tiller | 9c721ff | 2015-11-23 15:16:12 -0800 | [diff] [blame] | 76 | #endif |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 77 | |
| 78 | gpr_avl_node *new_node(void *key, void *value, gpr_avl_node *left, |
| 79 | gpr_avl_node *right) { |
Craig Tiller | ed38016 | 2017-07-11 08:34:26 -0700 | [diff] [blame] | 80 | gpr_avl_node *node = (gpr_avl_node *)gpr_malloc(sizeof(*node)); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 81 | gpr_ref_init(&node->refs, 1); |
| 82 | node->key = key; |
| 83 | node->value = value; |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 84 | node->left = assert_invariants(left); |
| 85 | node->right = assert_invariants(right); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 86 | node->height = 1 + GPR_MAX(node_height(left), node_height(right)); |
| 87 | return node; |
| 88 | } |
| 89 | |
| 90 | static gpr_avl_node *get(const gpr_avl_vtable *vtable, gpr_avl_node *node, |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 91 | void *key, void *user_data) { |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 92 | long cmp; |
| 93 | |
| 94 | if (node == NULL) { |
| 95 | return NULL; |
| 96 | } |
| 97 | |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 98 | cmp = vtable->compare_keys(node->key, key, user_data); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 99 | if (cmp == 0) { |
| 100 | return node; |
| 101 | } else if (cmp > 0) { |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 102 | return get(vtable, node->left, key, user_data); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 103 | } else { |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 104 | return get(vtable, node->right, key, user_data); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 108 | void *gpr_avl_get(gpr_avl avl, void *key, void *user_data) { |
| 109 | gpr_avl_node *node = get(avl.vtable, avl.root, key, user_data); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 110 | return node ? node->value : NULL; |
| 111 | } |
| 112 | |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 113 | int gpr_avl_maybe_get(gpr_avl avl, void *key, void **value, void *user_data) { |
| 114 | gpr_avl_node *node = get(avl.vtable, avl.root, key, user_data); |
Craig Tiller | 965eab3 | 2016-05-07 22:11:37 -0700 | [diff] [blame] | 115 | if (node != NULL) { |
| 116 | *value = node->value; |
| 117 | return 1; |
| 118 | } |
| 119 | return 0; |
| 120 | } |
| 121 | |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 122 | static gpr_avl_node *rotate_left(const gpr_avl_vtable *vtable, void *key, |
| 123 | void *value, gpr_avl_node *left, |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 124 | gpr_avl_node *right, void *user_data) { |
yang-g | d66ee1a | 2017-07-18 08:11:10 -0700 | [diff] [blame^] | 125 | gpr_avl_node *n = new_node(vtable->copy_key(right->key, user_data), |
| 126 | vtable->copy_value(right->value, user_data), |
| 127 | new_node(key, value, left, ref_node(right->left)), |
| 128 | ref_node(right->right)); |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 129 | unref_node(vtable, right, user_data); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 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, |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 135 | gpr_avl_node *right, void *user_data) { |
| 136 | gpr_avl_node *n = |
| 137 | new_node(vtable->copy_key(left->key, user_data), |
yang-g | d66ee1a | 2017-07-18 08:11:10 -0700 | [diff] [blame^] | 138 | vtable->copy_value(left->value, user_data), ref_node(left->left), |
| 139 | new_node(key, value, ref_node(left->right), right)); |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 140 | unref_node(vtable, left, user_data); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 141 | return n; |
| 142 | } |
| 143 | |
| 144 | static gpr_avl_node *rotate_left_right(const gpr_avl_vtable *vtable, void *key, |
| 145 | void *value, gpr_avl_node *left, |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 146 | gpr_avl_node *right, void *user_data) { |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 147 | /* rotate_right(..., rotate_left(left), right) */ |
yang-g | d66ee1a | 2017-07-18 08:11:10 -0700 | [diff] [blame^] | 148 | gpr_avl_node *n = |
| 149 | new_node(vtable->copy_key(left->right->key, user_data), |
| 150 | vtable->copy_value(left->right->value, user_data), |
| 151 | new_node(vtable->copy_key(left->key, user_data), |
| 152 | vtable->copy_value(left->value, user_data), |
| 153 | ref_node(left->left), ref_node(left->right->left)), |
| 154 | new_node(key, value, ref_node(left->right->right), right)); |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 155 | unref_node(vtable, left, user_data); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 156 | return n; |
| 157 | } |
| 158 | |
| 159 | static gpr_avl_node *rotate_right_left(const gpr_avl_vtable *vtable, void *key, |
| 160 | void *value, gpr_avl_node *left, |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 161 | gpr_avl_node *right, void *user_data) { |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 162 | /* rotate_left(..., left, rotate_right(right)) */ |
yang-g | d66ee1a | 2017-07-18 08:11:10 -0700 | [diff] [blame^] | 163 | gpr_avl_node *n = |
| 164 | new_node(vtable->copy_key(right->left->key, user_data), |
| 165 | vtable->copy_value(right->left->value, user_data), |
| 166 | new_node(key, value, left, ref_node(right->left->left)), |
| 167 | new_node(vtable->copy_key(right->key, user_data), |
| 168 | vtable->copy_value(right->value, user_data), |
| 169 | ref_node(right->left->right), ref_node(right->right))); |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 170 | unref_node(vtable, right, user_data); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 171 | return n; |
| 172 | } |
| 173 | |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 174 | static gpr_avl_node *rebalance(const gpr_avl_vtable *vtable, void *key, |
| 175 | void *value, gpr_avl_node *left, |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 176 | gpr_avl_node *right, void *user_data) { |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 177 | switch (node_height(left) - node_height(right)) { |
| 178 | case 2: |
| 179 | if (node_height(left->left) - node_height(left->right) == -1) { |
| 180 | return assert_invariants( |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 181 | rotate_left_right(vtable, key, value, left, right, user_data)); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 182 | } else { |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 183 | return assert_invariants( |
| 184 | rotate_right(vtable, key, value, left, right, user_data)); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 185 | } |
| 186 | case -2: |
| 187 | if (node_height(right->left) - node_height(right->right) == 1) { |
| 188 | return assert_invariants( |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 189 | rotate_right_left(vtable, key, value, left, right, user_data)); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 190 | } else { |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 191 | return assert_invariants( |
| 192 | rotate_left(vtable, key, value, left, right, user_data)); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 193 | } |
| 194 | default: |
| 195 | return assert_invariants(new_node(key, value, left, right)); |
| 196 | } |
| 197 | } |
| 198 | |
Zoltan Kuscsik | fcb56a7 | 2017-04-19 15:47:37 +0200 | [diff] [blame] | 199 | static gpr_avl_node *add_key(const gpr_avl_vtable *vtable, gpr_avl_node *node, |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 200 | void *key, void *value, void *user_data) { |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 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 | } |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 205 | cmp = vtable->compare_keys(node->key, key, user_data); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 206 | if (cmp == 0) { |
yang-g | d66ee1a | 2017-07-18 08:11:10 -0700 | [diff] [blame^] | 207 | return new_node(key, value, ref_node(node->left), ref_node(node->right)); |
Craig Tiller | 22d11e1 | 2015-11-23 15:33:54 -0800 | [diff] [blame] | 208 | } else if (cmp > 0) { |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 209 | return rebalance(vtable, vtable->copy_key(node->key, user_data), |
| 210 | vtable->copy_value(node->value, user_data), |
| 211 | add_key(vtable, node->left, key, value, user_data), |
yang-g | d66ee1a | 2017-07-18 08:11:10 -0700 | [diff] [blame^] | 212 | ref_node(node->right), user_data); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 213 | } else { |
yang-g | d66ee1a | 2017-07-18 08:11:10 -0700 | [diff] [blame^] | 214 | return rebalance( |
| 215 | vtable, vtable->copy_key(node->key, user_data), |
| 216 | vtable->copy_value(node->value, user_data), ref_node(node->left), |
| 217 | add_key(vtable, node->right, key, value, user_data), user_data); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 218 | } |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 219 | } |
| 220 | |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 221 | gpr_avl gpr_avl_add(gpr_avl avl, void *key, void *value, void *user_data) { |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 222 | gpr_avl_node *old_root = avl.root; |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 223 | avl.root = add_key(avl.vtable, avl.root, key, value, user_data); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 224 | assert_invariants(avl.root); |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 225 | unref_node(avl.vtable, old_root, user_data); |
Craig Tiller | fba79f2 | 2015-11-23 11:06:55 -0800 | [diff] [blame] | 226 | return avl; |
| 227 | } |
| 228 | |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 229 | static gpr_avl_node *in_order_head(gpr_avl_node *node) { |
| 230 | while (node->left != NULL) { |
| 231 | node = node->left; |
| 232 | } |
| 233 | return node; |
| 234 | } |
| 235 | |
| 236 | static gpr_avl_node *in_order_tail(gpr_avl_node *node) { |
| 237 | while (node->right != NULL) { |
| 238 | node = node->right; |
| 239 | } |
| 240 | return node; |
| 241 | } |
| 242 | |
Zoltan Kuscsik | fcb56a7 | 2017-04-19 15:47:37 +0200 | [diff] [blame] | 243 | static gpr_avl_node *remove_key(const gpr_avl_vtable *vtable, |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 244 | gpr_avl_node *node, void *key, |
| 245 | void *user_data) { |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 246 | long cmp; |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 247 | if (node == NULL) { |
| 248 | return NULL; |
| 249 | } |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 250 | cmp = vtable->compare_keys(node->key, key, user_data); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 251 | if (cmp == 0) { |
| 252 | if (node->left == NULL) { |
yang-g | d66ee1a | 2017-07-18 08:11:10 -0700 | [diff] [blame^] | 253 | return ref_node(node->right); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 254 | } else if (node->right == NULL) { |
yang-g | d66ee1a | 2017-07-18 08:11:10 -0700 | [diff] [blame^] | 255 | return ref_node(node->left); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 256 | } else if (node->left->height < node->right->height) { |
| 257 | gpr_avl_node *h = in_order_head(node->right); |
yang-g | d66ee1a | 2017-07-18 08:11:10 -0700 | [diff] [blame^] | 258 | return rebalance( |
| 259 | vtable, vtable->copy_key(h->key, user_data), |
| 260 | vtable->copy_value(h->value, user_data), ref_node(node->left), |
| 261 | remove_key(vtable, node->right, h->key, user_data), user_data); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 262 | } else { |
| 263 | gpr_avl_node *h = in_order_tail(node->left); |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 264 | return rebalance(vtable, vtable->copy_key(h->key, user_data), |
| 265 | vtable->copy_value(h->value, user_data), |
| 266 | remove_key(vtable, node->left, h->key, user_data), |
yang-g | d66ee1a | 2017-07-18 08:11:10 -0700 | [diff] [blame^] | 267 | ref_node(node->right), user_data); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 268 | } |
Craig Tiller | 22d11e1 | 2015-11-23 15:33:54 -0800 | [diff] [blame] | 269 | } else if (cmp > 0) { |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 270 | return rebalance(vtable, vtable->copy_key(node->key, user_data), |
| 271 | vtable->copy_value(node->value, user_data), |
| 272 | remove_key(vtable, node->left, key, user_data), |
yang-g | d66ee1a | 2017-07-18 08:11:10 -0700 | [diff] [blame^] | 273 | ref_node(node->right), user_data); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 274 | } else { |
yang-g | d66ee1a | 2017-07-18 08:11:10 -0700 | [diff] [blame^] | 275 | return rebalance( |
| 276 | vtable, vtable->copy_key(node->key, user_data), |
| 277 | vtable->copy_value(node->value, user_data), ref_node(node->left), |
| 278 | remove_key(vtable, node->right, key, user_data), user_data); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 279 | } |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 280 | } |
| 281 | |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 282 | gpr_avl gpr_avl_remove(gpr_avl avl, void *key, void *user_data) { |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 283 | gpr_avl_node *old_root = avl.root; |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 284 | avl.root = remove_key(avl.vtable, avl.root, key, user_data); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 285 | assert_invariants(avl.root); |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 286 | unref_node(avl.vtable, old_root, user_data); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 287 | return avl; |
| 288 | } |
| 289 | |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 290 | gpr_avl gpr_avl_ref(gpr_avl avl, void *user_data) { |
yang-g | d66ee1a | 2017-07-18 08:11:10 -0700 | [diff] [blame^] | 291 | ref_node(avl.root); |
Craig Tiller | 0593622 | 2015-11-23 14:56:46 -0800 | [diff] [blame] | 292 | return avl; |
| 293 | } |
| 294 | |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 295 | void gpr_avl_unref(gpr_avl avl, void *user_data) { |
| 296 | unref_node(avl.vtable, avl.root, user_data); |
| 297 | } |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 298 | |
| 299 | int gpr_avl_is_empty(gpr_avl avl) { return avl.root == NULL; } |