|
|
|
@ -16,30 +16,31 @@ |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
#include <grpc/support/avl.h> |
|
|
|
|
#include "src/core/lib/avl/avl.h" |
|
|
|
|
|
|
|
|
|
#include <assert.h> |
|
|
|
|
#include <stdlib.h> |
|
|
|
|
|
|
|
|
|
#include <grpc/support/alloc.h> |
|
|
|
|
#include <grpc/support/string_util.h> |
|
|
|
|
#include <grpc/support/useful.h> |
|
|
|
|
|
|
|
|
|
gpr_avl gpr_avl_create(const gpr_avl_vtable* vtable) { |
|
|
|
|
gpr_avl out; |
|
|
|
|
#include "src/core/lib/gpr/useful.h" |
|
|
|
|
|
|
|
|
|
grpc_avl grpc_avl_create(const grpc_avl_vtable* vtable) { |
|
|
|
|
grpc_avl out; |
|
|
|
|
out.vtable = vtable; |
|
|
|
|
out.root = nullptr; |
|
|
|
|
return out; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static gpr_avl_node* ref_node(gpr_avl_node* node) { |
|
|
|
|
static grpc_avl_node* ref_node(grpc_avl_node* node) { |
|
|
|
|
if (node) { |
|
|
|
|
gpr_ref(&node->refs); |
|
|
|
|
} |
|
|
|
|
return node; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void unref_node(const gpr_avl_vtable* vtable, gpr_avl_node* node, |
|
|
|
|
static void unref_node(const grpc_avl_vtable* vtable, grpc_avl_node* node, |
|
|
|
|
void* user_data) { |
|
|
|
|
if (node == nullptr) { |
|
|
|
|
return; |
|
|
|
@ -53,18 +54,18 @@ static void unref_node(const gpr_avl_vtable* vtable, gpr_avl_node* node, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static long node_height(gpr_avl_node* node) { |
|
|
|
|
static long node_height(grpc_avl_node* node) { |
|
|
|
|
return node == nullptr ? 0 : node->height; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#ifndef NDEBUG |
|
|
|
|
static long calculate_height(gpr_avl_node* node) { |
|
|
|
|
static long calculate_height(grpc_avl_node* node) { |
|
|
|
|
return node == nullptr ? 0 |
|
|
|
|
: 1 + GPR_MAX(calculate_height(node->left), |
|
|
|
|
calculate_height(node->right)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static gpr_avl_node* assert_invariants(gpr_avl_node* n) { |
|
|
|
|
static grpc_avl_node* assert_invariants(grpc_avl_node* n) { |
|
|
|
|
if (n == nullptr) return nullptr; |
|
|
|
|
assert_invariants(n->left); |
|
|
|
|
assert_invariants(n->right); |
|
|
|
@ -73,12 +74,12 @@ static gpr_avl_node* assert_invariants(gpr_avl_node* n) { |
|
|
|
|
return n; |
|
|
|
|
} |
|
|
|
|
#else |
|
|
|
|
static gpr_avl_node* assert_invariants(gpr_avl_node* n) { return n; } |
|
|
|
|
static grpc_avl_node* assert_invariants(grpc_avl_node* n) { return n; } |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
gpr_avl_node* new_node(void* key, void* value, gpr_avl_node* left, |
|
|
|
|
gpr_avl_node* right) { |
|
|
|
|
gpr_avl_node* node = (gpr_avl_node*)gpr_malloc(sizeof(*node)); |
|
|
|
|
grpc_avl_node* new_node(void* key, void* value, grpc_avl_node* left, |
|
|
|
|
grpc_avl_node* right) { |
|
|
|
|
grpc_avl_node* node = (grpc_avl_node*)gpr_malloc(sizeof(*node)); |
|
|
|
|
gpr_ref_init(&node->refs, 1); |
|
|
|
|
node->key = key; |
|
|
|
|
node->value = value; |
|
|
|
@ -88,8 +89,8 @@ gpr_avl_node* new_node(void* key, void* value, gpr_avl_node* left, |
|
|
|
|
return node; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static gpr_avl_node* get(const gpr_avl_vtable* vtable, gpr_avl_node* node, |
|
|
|
|
void* key, void* user_data) { |
|
|
|
|
static grpc_avl_node* get(const grpc_avl_vtable* vtable, grpc_avl_node* node, |
|
|
|
|
void* key, void* user_data) { |
|
|
|
|
long cmp; |
|
|
|
|
|
|
|
|
|
if (node == nullptr) { |
|
|
|
@ -106,13 +107,13 @@ static gpr_avl_node* get(const gpr_avl_vtable* vtable, gpr_avl_node* node, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void* gpr_avl_get(gpr_avl avl, void* key, void* user_data) { |
|
|
|
|
gpr_avl_node* node = get(avl.vtable, avl.root, key, user_data); |
|
|
|
|
void* grpc_avl_get(grpc_avl avl, void* key, void* user_data) { |
|
|
|
|
grpc_avl_node* node = get(avl.vtable, avl.root, key, user_data); |
|
|
|
|
return node ? node->value : nullptr; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int gpr_avl_maybe_get(gpr_avl avl, void* key, void** value, void* user_data) { |
|
|
|
|
gpr_avl_node* node = get(avl.vtable, avl.root, key, user_data); |
|
|
|
|
int grpc_avl_maybe_get(grpc_avl avl, void* key, void** value, void* user_data) { |
|
|
|
|
grpc_avl_node* node = get(avl.vtable, avl.root, key, user_data); |
|
|
|
|
if (node != nullptr) { |
|
|
|
|
*value = node->value; |
|
|
|
|
return 1; |
|
|
|
@ -120,21 +121,21 @@ int gpr_avl_maybe_get(gpr_avl avl, void* key, void** value, void* user_data) { |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static gpr_avl_node* rotate_left(const gpr_avl_vtable* vtable, void* key, |
|
|
|
|
void* value, gpr_avl_node* left, |
|
|
|
|
gpr_avl_node* right, void* user_data) { |
|
|
|
|
gpr_avl_node* n = new_node(vtable->copy_key(right->key, user_data), |
|
|
|
|
vtable->copy_value(right->value, user_data), |
|
|
|
|
new_node(key, value, left, ref_node(right->left)), |
|
|
|
|
ref_node(right->right)); |
|
|
|
|
static grpc_avl_node* rotate_left(const grpc_avl_vtable* vtable, void* key, |
|
|
|
|
void* value, grpc_avl_node* left, |
|
|
|
|
grpc_avl_node* right, void* user_data) { |
|
|
|
|
grpc_avl_node* n = new_node(vtable->copy_key(right->key, user_data), |
|
|
|
|
vtable->copy_value(right->value, user_data), |
|
|
|
|
new_node(key, value, left, ref_node(right->left)), |
|
|
|
|
ref_node(right->right)); |
|
|
|
|
unref_node(vtable, right, user_data); |
|
|
|
|
return n; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static gpr_avl_node* rotate_right(const gpr_avl_vtable* vtable, void* key, |
|
|
|
|
void* value, gpr_avl_node* left, |
|
|
|
|
gpr_avl_node* right, void* user_data) { |
|
|
|
|
gpr_avl_node* n = |
|
|
|
|
static grpc_avl_node* rotate_right(const grpc_avl_vtable* vtable, void* key, |
|
|
|
|
void* value, grpc_avl_node* left, |
|
|
|
|
grpc_avl_node* right, void* user_data) { |
|
|
|
|
grpc_avl_node* n = |
|
|
|
|
new_node(vtable->copy_key(left->key, user_data), |
|
|
|
|
vtable->copy_value(left->value, user_data), ref_node(left->left), |
|
|
|
|
new_node(key, value, ref_node(left->right), right)); |
|
|
|
@ -142,11 +143,12 @@ static gpr_avl_node* rotate_right(const gpr_avl_vtable* vtable, void* key, |
|
|
|
|
return n; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static gpr_avl_node* rotate_left_right(const gpr_avl_vtable* vtable, void* key, |
|
|
|
|
void* value, gpr_avl_node* left, |
|
|
|
|
gpr_avl_node* right, void* user_data) { |
|
|
|
|
static grpc_avl_node* rotate_left_right(const grpc_avl_vtable* vtable, |
|
|
|
|
void* key, void* value, |
|
|
|
|
grpc_avl_node* left, |
|
|
|
|
grpc_avl_node* right, void* user_data) { |
|
|
|
|
/* rotate_right(..., rotate_left(left), right) */ |
|
|
|
|
gpr_avl_node* n = |
|
|
|
|
grpc_avl_node* n = |
|
|
|
|
new_node(vtable->copy_key(left->right->key, user_data), |
|
|
|
|
vtable->copy_value(left->right->value, user_data), |
|
|
|
|
new_node(vtable->copy_key(left->key, user_data), |
|
|
|
@ -157,11 +159,12 @@ static gpr_avl_node* rotate_left_right(const gpr_avl_vtable* vtable, void* key, |
|
|
|
|
return n; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static gpr_avl_node* rotate_right_left(const gpr_avl_vtable* vtable, void* key, |
|
|
|
|
void* value, gpr_avl_node* left, |
|
|
|
|
gpr_avl_node* right, void* user_data) { |
|
|
|
|
static grpc_avl_node* rotate_right_left(const grpc_avl_vtable* vtable, |
|
|
|
|
void* key, void* value, |
|
|
|
|
grpc_avl_node* left, |
|
|
|
|
grpc_avl_node* right, void* user_data) { |
|
|
|
|
/* rotate_left(..., left, rotate_right(right)) */ |
|
|
|
|
gpr_avl_node* n = |
|
|
|
|
grpc_avl_node* n = |
|
|
|
|
new_node(vtable->copy_key(right->left->key, user_data), |
|
|
|
|
vtable->copy_value(right->left->value, user_data), |
|
|
|
|
new_node(key, value, left, ref_node(right->left->left)), |
|
|
|
@ -172,9 +175,9 @@ static gpr_avl_node* rotate_right_left(const gpr_avl_vtable* vtable, void* key, |
|
|
|
|
return n; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static gpr_avl_node* rebalance(const gpr_avl_vtable* vtable, void* key, |
|
|
|
|
void* value, gpr_avl_node* left, |
|
|
|
|
gpr_avl_node* right, void* user_data) { |
|
|
|
|
static grpc_avl_node* rebalance(const grpc_avl_vtable* vtable, void* key, |
|
|
|
|
void* value, grpc_avl_node* left, |
|
|
|
|
grpc_avl_node* right, void* user_data) { |
|
|
|
|
switch (node_height(left) - node_height(right)) { |
|
|
|
|
case 2: |
|
|
|
|
if (node_height(left->left) - node_height(left->right) == -1) { |
|
|
|
@ -197,8 +200,9 @@ static gpr_avl_node* rebalance(const gpr_avl_vtable* vtable, void* key, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static gpr_avl_node* add_key(const gpr_avl_vtable* vtable, gpr_avl_node* node, |
|
|
|
|
void* key, void* value, void* user_data) { |
|
|
|
|
static grpc_avl_node* add_key(const grpc_avl_vtable* vtable, |
|
|
|
|
grpc_avl_node* node, void* key, void* value, |
|
|
|
|
void* user_data) { |
|
|
|
|
long cmp; |
|
|
|
|
if (node == nullptr) { |
|
|
|
|
return new_node(key, value, nullptr, nullptr); |
|
|
|
@ -219,31 +223,31 @@ static gpr_avl_node* add_key(const gpr_avl_vtable* vtable, gpr_avl_node* node, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
gpr_avl gpr_avl_add(gpr_avl avl, void* key, void* value, void* user_data) { |
|
|
|
|
gpr_avl_node* old_root = avl.root; |
|
|
|
|
grpc_avl grpc_avl_add(grpc_avl avl, void* key, void* value, void* user_data) { |
|
|
|
|
grpc_avl_node* old_root = avl.root; |
|
|
|
|
avl.root = add_key(avl.vtable, avl.root, key, value, user_data); |
|
|
|
|
assert_invariants(avl.root); |
|
|
|
|
unref_node(avl.vtable, old_root, user_data); |
|
|
|
|
return avl; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static gpr_avl_node* in_order_head(gpr_avl_node* node) { |
|
|
|
|
static grpc_avl_node* in_order_head(grpc_avl_node* node) { |
|
|
|
|
while (node->left != nullptr) { |
|
|
|
|
node = node->left; |
|
|
|
|
} |
|
|
|
|
return node; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static gpr_avl_node* in_order_tail(gpr_avl_node* node) { |
|
|
|
|
static grpc_avl_node* in_order_tail(grpc_avl_node* node) { |
|
|
|
|
while (node->right != nullptr) { |
|
|
|
|
node = node->right; |
|
|
|
|
} |
|
|
|
|
return node; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static gpr_avl_node* remove_key(const gpr_avl_vtable* vtable, |
|
|
|
|
gpr_avl_node* node, void* key, |
|
|
|
|
void* user_data) { |
|
|
|
|
static grpc_avl_node* remove_key(const grpc_avl_vtable* vtable, |
|
|
|
|
grpc_avl_node* node, void* key, |
|
|
|
|
void* user_data) { |
|
|
|
|
long cmp; |
|
|
|
|
if (node == nullptr) { |
|
|
|
|
return nullptr; |
|
|
|
@ -255,13 +259,13 @@ static gpr_avl_node* remove_key(const gpr_avl_vtable* vtable, |
|
|
|
|
} else if (node->right == nullptr) { |
|
|
|
|
return ref_node(node->left); |
|
|
|
|
} else if (node->left->height < node->right->height) { |
|
|
|
|
gpr_avl_node* h = in_order_head(node->right); |
|
|
|
|
grpc_avl_node* h = in_order_head(node->right); |
|
|
|
|
return rebalance( |
|
|
|
|
vtable, vtable->copy_key(h->key, user_data), |
|
|
|
|
vtable->copy_value(h->value, user_data), ref_node(node->left), |
|
|
|
|
remove_key(vtable, node->right, h->key, user_data), user_data); |
|
|
|
|
} else { |
|
|
|
|
gpr_avl_node* h = in_order_tail(node->left); |
|
|
|
|
grpc_avl_node* h = in_order_tail(node->left); |
|
|
|
|
return rebalance(vtable, vtable->copy_key(h->key, user_data), |
|
|
|
|
vtable->copy_value(h->value, user_data), |
|
|
|
|
remove_key(vtable, node->left, h->key, user_data), |
|
|
|
@ -280,21 +284,21 @@ static gpr_avl_node* remove_key(const gpr_avl_vtable* vtable, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
gpr_avl gpr_avl_remove(gpr_avl avl, void* key, void* user_data) { |
|
|
|
|
gpr_avl_node* old_root = avl.root; |
|
|
|
|
grpc_avl grpc_avl_remove(grpc_avl avl, void* key, void* user_data) { |
|
|
|
|
grpc_avl_node* old_root = avl.root; |
|
|
|
|
avl.root = remove_key(avl.vtable, avl.root, key, user_data); |
|
|
|
|
assert_invariants(avl.root); |
|
|
|
|
unref_node(avl.vtable, old_root, user_data); |
|
|
|
|
return avl; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
gpr_avl gpr_avl_ref(gpr_avl avl, void* user_data) { |
|
|
|
|
grpc_avl grpc_avl_ref(grpc_avl avl, void* user_data) { |
|
|
|
|
ref_node(avl.root); |
|
|
|
|
return avl; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void gpr_avl_unref(gpr_avl avl, void* user_data) { |
|
|
|
|
void grpc_avl_unref(grpc_avl avl, void* user_data) { |
|
|
|
|
unref_node(avl.vtable, avl.root, user_data); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int gpr_avl_is_empty(gpr_avl avl) { return avl.root == nullptr; } |
|
|
|
|
int grpc_avl_is_empty(grpc_avl avl) { return avl.root == nullptr; } |