splay-tree.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #ifndef SPLAY_TREE_H
  20. #define SPLAY_TREE_H 1
  21. #include <stdint.h>
  22. /* how many bytes can a splay key to index on have max. */
  23. /* typedef unsigned long long int splay_tree_key; */
  24. typedef uintptr_t splay_tree_key;
  25. /* how many bytes can a splay value have max. */
  26. /* typedef unsigned long long int splay_tree_value; */
  27. typedef uintptr_t splay_tree_value;
  28. /* Forward declaration for a tree. */
  29. typedef struct splay_tree_t *splay_tree;
  30. /* The nodes in the splay tree. */
  31. struct splay_tree_node_n
  32. {
  33. /* The key. */
  34. splay_tree_key key;
  35. /* The value. */
  36. splay_tree_value value;
  37. /* The left and right children, respectively. */
  38. struct splay_tree_node_n *left;
  39. struct splay_tree_node_n *right;
  40. };
  41. /* Forward declaration for a node in the tree. */
  42. typedef struct splay_tree_node_n *splay_tree_node;
  43. /* The type of a function which compares two splay-tree keys. The
  44. function should return values as for qsort. */
  45. typedef int (*splay_tree_compare_fn) (splay_tree_key, splay_tree_key);
  46. /* The type of a function used to deallocate any resources associated
  47. with the key. */
  48. typedef void (*splay_tree_delete_key_fn) (splay_tree_key);
  49. /* The type of a function used to deallocate any resources associated
  50. with the value. */
  51. typedef void (*splay_tree_delete_value_fn) (splay_tree_value);
  52. /* The type of a function used to iterate over the tree. */
  53. typedef int (*splay_tree_foreach_fn) (splay_tree_node, void *);
  54. /* The splay tree itself. */
  55. struct splay_tree_t
  56. {
  57. /* The root of the tree. */
  58. struct splay_tree_node_n *root;
  59. /* The comparision function. */
  60. splay_tree_compare_fn comp;
  61. /* The deallocate-key function. NULL if no cleanup is necessary. */
  62. splay_tree_delete_key_fn delete_key;
  63. /* The deallocate-value function. NULL if no cleanup is necessary. */
  64. splay_tree_delete_value_fn delete_value;
  65. };
  66. /* splay routines */
  67. extern splay_tree splay_tree_new (splay_tree_compare_fn fnc,
  68. splay_tree_delete_key_fn fndk,
  69. splay_tree_delete_value_fn fndv);
  70. extern splay_tree splay_tree_delete (splay_tree sp);
  71. extern void splay_tree_insert (splay_tree sp, splay_tree_key k,
  72. splay_tree_value v);
  73. extern void splay_tree_insert_duplicates (splay_tree sp, splay_tree_key k,
  74. splay_tree_value v);
  75. extern void splay_tree_remove (splay_tree sp, splay_tree_key key);
  76. extern splay_tree_node splay_tree_lookup (splay_tree sp, splay_tree_key k);
  77. extern int splay_tree_foreach (splay_tree sp, splay_tree_foreach_fn f,
  78. void *data);
  79. extern splay_tree_node splay_tree_min (splay_tree sp);
  80. extern splay_tree_node splay_tree_max (splay_tree sp);
  81. /* Return the immediate successor KEY, or NULL if there is no
  82. successor. KEY need not be present in the tree. */
  83. extern splay_tree_node splay_tree_successor (splay_tree sp,
  84. splay_tree_key key);
  85. /* return 1 if splay tree has data, otherwise 0 */
  86. extern int splay_tree_has_data (splay_tree sp);
  87. /* free() wrappers to free key/value */
  88. extern void splay_tree_free_value (splay_tree_value value);
  89. extern void splay_tree_free_key (splay_tree_key key);
  90. extern int splay_tree_compare_ints (splay_tree_key k1, splay_tree_key k2);
  91. extern int splay_tree_compare_pointers (splay_tree_key k1, splay_tree_key k2);
  92. extern int splay_tree_compare_strings (splay_tree_key k1, splay_tree_key k2);
  93. #endif /* _SPLAY_TREE_H */
  94. /* end */