GRPC Core  4.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
avl.h
Go to the documentation of this file.
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 #ifndef GRPC_SUPPORT_AVL_H
35 #define GRPC_SUPPORT_AVL_H
36 
37 #include <grpc/support/sync.h>
38 
40 typedef struct gpr_avl_node {
42  void *key;
43  void *value;
44  struct gpr_avl_node *left;
46  long height;
47 } gpr_avl_node;
48 
49 typedef struct gpr_avl_vtable {
51  void (*destroy_key)(void *key);
53  void *(*copy_key)(void *key);
56  long (*compare_keys)(void *key1, void *key2);
58  void (*destroy_value)(void *value);
60  void *(*copy_value)(void *value);
62 
66 typedef struct gpr_avl {
69 } gpr_avl;
70 
78 GPRAPI void gpr_avl_unref(gpr_avl avl);
83 GPRAPI gpr_avl gpr_avl_add(gpr_avl avl, void *key, void *value);
86 GPRAPI gpr_avl gpr_avl_remove(gpr_avl avl, void *key);
90 GPRAPI void *gpr_avl_get(gpr_avl avl, void *key);
93 GPRAPI int gpr_avl_maybe_get(gpr_avl avl, void *key, void **value);
96 
97 #endif /* GRPC_SUPPORT_AVL_H */
Definition: avl.h:49
struct gpr_avl_node * right
Definition: avl.h:45
GPRAPI void * gpr_avl_get(gpr_avl avl, void *key)
lookup key, and return the associated value.
Definition: sync_generic.h:47
GPRAPI int gpr_avl_is_empty(gpr_avl avl)
Return 1 if avl is empty, 0 otherwise.
"pointer" to an AVL tree - this is a reference counted object - use gpr_avl_ref to add a reference...
Definition: avl.h:66
long(* compare_keys)(void *key1, void *key2)
compare key1, key2; return <0 if key1 < key2, >0 if key1 > key2, 0 if key1 == key2 ...
Definition: avl.h:56
void * value
Definition: avl.h:43
#define GPRAPI
Definition: port_platform.h:416
GPRAPI gpr_avl gpr_avl_create(const gpr_avl_vtable *vtable)
create an immutable AVL tree
struct gpr_avl gpr_avl
"pointer" to an AVL tree - this is a reference counted object - use gpr_avl_ref to add a reference...
struct gpr_avl_vtable gpr_avl_vtable
GPRAPI gpr_avl gpr_avl_add(gpr_avl avl, void *key, void *value)
return a new tree with (key, value) added to avl.
gpr_refcount refs
Definition: avl.h:41
gpr_avl_node * root
Definition: avl.h:68
long height
Definition: avl.h:46
void(* destroy_value)(void *value)
destroy a value
Definition: avl.h:58
GPRAPI int gpr_avl_maybe_get(gpr_avl avl, void *key, void **value)
Return 1 if avl contains key, 0 otherwise; if it has the key, sets *value to its value.
const gpr_avl_vtable * vtable
Definition: avl.h:67
struct gpr_avl_node gpr_avl_node
internal node of an AVL tree
GPRAPI void gpr_avl_unref(gpr_avl avl)
remove a reference to a tree - destroying it if there are no references left
void(* destroy_key)(void *key)
destroy a key
Definition: avl.h:51
GPRAPI gpr_avl gpr_avl_remove(gpr_avl avl, void *key)
return a new tree with key deleted implicitly unrefs avl to allow easy chaining.
void * key
Definition: avl.h:42
internal node of an AVL tree
Definition: avl.h:40
struct gpr_avl_node * left
Definition: avl.h:44
GPRAPI gpr_avl gpr_avl_ref(gpr_avl avl)
add a reference to an existing tree - returns the tree as a convenience