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