splay-tree.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * This program is free software: you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation, either version 3 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. *
  15. * SPDX-License-Identifier: GPL-3.0+
  16. * License-Filename: LICENSE
  17. *
  18. */
  19. /**
  20. * @file: splay-tree.h
  21. */
  22. #ifndef SPLAY_TREE_H
  23. #define SPLAY_TREE_H 1
  24. #undef __BEGIN_DECLS
  25. #undef __END_DECLS
  26. #ifdef __cplusplus
  27. #define __BEGIN_DECLS extern "C" {
  28. #define __END_DECLS }
  29. #else
  30. #define __BEGIN_DECLS /* empty */
  31. #define __END_DECLS /* empty */
  32. #endif
  33. /* needed for type unintptr_t or use long long int */
  34. #include <stdint.h>
  35. /* how many bytes can a splay key to index on have max. */
  36. /* typedef unsigned long long int splay_tree_key; */
  37. typedef uintptr_t splay_tree_key;
  38. /* how many bytes can a splay value have max. */
  39. /* typedef unsigned long long int splay_tree_value; */
  40. typedef uintptr_t splay_tree_value;
  41. /* Forward declaration for a tree. */
  42. typedef struct splay_tree_t *splay_tree;
  43. /* The nodes in the splay tree. */
  44. struct splay_tree_node_n {
  45. /* The key. */
  46. splay_tree_key key;
  47. /* The value. */
  48. splay_tree_value value;
  49. /* The left and right children, respectively. */
  50. struct splay_tree_node_n *left;
  51. struct splay_tree_node_n *right;
  52. };
  53. /* Forward declaration for a node in the tree. */
  54. typedef struct splay_tree_node_n *splay_tree_node;
  55. /* The type of a function which compares two splay-tree keys. The
  56. function should return values as for qsort. */
  57. typedef int (*splay_tree_compare_fn)(splay_tree_key, splay_tree_key);
  58. /* The type of a function used to deallocate any resources associated
  59. with the key. */
  60. typedef void (*splay_tree_delete_key_fn)(splay_tree_key);
  61. /* The type of a function used to deallocate any resources associated
  62. with the value. */
  63. typedef void (*splay_tree_delete_value_fn)(splay_tree_value);
  64. /* The type of a function used to iterate over the tree. */
  65. typedef int (*splay_tree_foreach_fn)(splay_tree_node, void *);
  66. /* The splay tree itself. */
  67. struct splay_tree_t {
  68. /* The root of the tree. */
  69. struct splay_tree_node_n *root;
  70. /* The comparision function. */
  71. splay_tree_compare_fn comp;
  72. /* The deallocate-key function. NULL if no cleanup is necessary. */
  73. splay_tree_delete_key_fn delete_key;
  74. /* The deallocate-value function. NULL if no cleanup is necessary. */
  75. splay_tree_delete_value_fn delete_value;
  76. };
  77. /* splay routines */
  78. extern splay_tree splay_tree_new(splay_tree_compare_fn fnc, splay_tree_delete_key_fn fndk, splay_tree_delete_value_fn fndv);
  79. extern splay_tree splay_tree_delete(splay_tree sp);
  80. extern void splay_tree_insert(splay_tree sp, splay_tree_key k, splay_tree_value v);
  81. extern void splay_tree_remove(splay_tree sp, splay_tree_key key);
  82. extern splay_tree_node splay_tree_lookup(splay_tree sp, splay_tree_key k);
  83. extern int splay_tree_foreach(splay_tree sp, splay_tree_foreach_fn f, void *data);
  84. extern splay_tree_node splay_tree_min(splay_tree sp);
  85. extern splay_tree_node splay_tree_max(splay_tree sp);
  86. /* Return the immediate successor KEY, or NULL if there is no
  87. successor. KEY need not be present in the tree. */
  88. extern splay_tree_node splay_tree_successor(splay_tree sp, splay_tree_key key);
  89. /* return 1 if splay tree has data, otherwise 0 */
  90. extern int splay_tree_has_data(splay_tree sp);
  91. /* free() wrappers to free key/value */
  92. extern void splay_tree_free_value(splay_tree_value value);
  93. extern void splay_tree_free_key(splay_tree_key key);
  94. extern int splay_tree_compare_ints(splay_tree_key k1, splay_tree_key k2);
  95. extern int splay_tree_compare_pointers(splay_tree_key k1, splay_tree_key k2);
  96. extern int splay_tree_compare_strings(splay_tree_key k1, splay_tree_key k2);
  97. #endif /* _SPLAY_TREE_H */
  98. /* end. */