avl.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  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
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  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.
  16. *
  17. */
  18. #ifndef GRPC_SUPPORT_AVL_H
  19. #define GRPC_SUPPORT_AVL_H
  20. #include <grpc/support/sync.h>
  21. /** internal node of an AVL tree */
  22. typedef struct gpr_avl_node {
  23. gpr_refcount refs;
  24. void *key;
  25. void *value;
  26. struct gpr_avl_node *left;
  27. struct gpr_avl_node *right;
  28. long height;
  29. } gpr_avl_node;
  30. /** vtable for the AVL tree
  31. * The optional user_data is propagated from the top level gpr_avl_XXX API.
  32. * From the same API call, multiple vtable functions may be called multiple
  33. * times.
  34. */
  35. typedef struct gpr_avl_vtable {
  36. /** destroy a key */
  37. void (*destroy_key)(void *key, void *user_data);
  38. /** copy a key, returning new value */
  39. void *(*copy_key)(void *key, void *user_data);
  40. /** compare key1, key2; return <0 if key1 < key2,
  41. >0 if key1 > key2, 0 if key1 == key2 */
  42. long (*compare_keys)(void *key1, void *key2, void *user_data);
  43. /** destroy a value */
  44. void (*destroy_value)(void *value, void *user_data);
  45. /** copy a value */
  46. void *(*copy_value)(void *value, void *user_data);
  47. } gpr_avl_vtable;
  48. /** "pointer" to an AVL tree - this is a reference
  49. counted object - use gpr_avl_ref to add a reference,
  50. gpr_avl_unref when done with a reference */
  51. typedef struct gpr_avl {
  52. const gpr_avl_vtable *vtable;
  53. gpr_avl_node *root;
  54. } gpr_avl;
  55. /** Create an immutable AVL tree. */
  56. GPRAPI gpr_avl gpr_avl_create(const gpr_avl_vtable *vtable);
  57. /** Add a reference to an existing tree - returns
  58. the tree as a convenience. The optional user_data will be passed to vtable
  59. functions. */
  60. GPRAPI gpr_avl gpr_avl_ref(gpr_avl avl, void *user_data);
  61. /** Remove a reference to a tree - destroying it if there
  62. are no references left. The optional user_data will be passed to vtable
  63. functions. */
  64. GPRAPI void gpr_avl_unref(gpr_avl avl, void *user_data);
  65. /** Return a new tree with (key, value) added to avl.
  66. implicitly unrefs avl to allow easy chaining.
  67. if key exists in avl, the new tree's key entry updated
  68. (i.e. a duplicate is not created). The optional user_data will be passed to
  69. vtable functions. */
  70. GPRAPI gpr_avl gpr_avl_add(gpr_avl avl, void *key, void *value,
  71. void *user_data);
  72. /** Return a new tree with key deleted
  73. implicitly unrefs avl to allow easy chaining. The optional user_data will be
  74. passed to vtable functions. */
  75. GPRAPI gpr_avl gpr_avl_remove(gpr_avl avl, void *key, void *user_data);
  76. /** Lookup key, and return the associated value.
  77. Does not mutate avl.
  78. Returns NULL if key is not found. The optional user_data will be passed to
  79. vtable functions.*/
  80. GPRAPI void *gpr_avl_get(gpr_avl avl, void *key, void *user_data);
  81. /** Return 1 if avl contains key, 0 otherwise; if it has the key, sets *value to
  82. its value. THe optional user_data will be passed to vtable functions. */
  83. GPRAPI int gpr_avl_maybe_get(gpr_avl avl, void *key, void **value,
  84. void *user_data);
  85. /** Return 1 if avl is empty, 0 otherwise */
  86. GPRAPI int gpr_avl_is_empty(gpr_avl avl);
  87. #endif /* GRPC_SUPPORT_AVL_H */