splay-tree.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* A splay-tree datatype.
  2. Copyright (C) 1998-2019 Free Software Foundation, Inc.
  3. Contributed by Mark Mitchell (mark@markmitchell.com).
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING. If not, write to
  15. the Free Software Foundation, 51 Franklin Street - Fifth Floor,
  16. Boston, MA 02110-1301, USA. */
  17. /* For an easily readable description of splay-trees, see:
  18. Lewis, Harry R. and Denenberg, Larry. Data Structures and Their
  19. Algorithms. Harper-Collins, Inc. 1991.
  20. The major feature of splay trees is that all basic tree operations
  21. are amortized O(log n) time for a tree with n nodes. */
  22. #ifndef _SPLAY_TREE_H
  23. #define _SPLAY_TREE_H
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif /* __cplusplus */
  27. #include "ansidecl.h"
  28. #ifdef HAVE_STDINT_H
  29. #include <stdint.h>
  30. #endif
  31. #ifdef HAVE_INTTYPES_H
  32. #include <inttypes.h>
  33. #endif
  34. /* Use typedefs for the key and data types to facilitate changing
  35. these types, if necessary. These types should be sufficiently wide
  36. that any pointer or scalar can be cast to these types, and then
  37. cast back, without loss of precision. */
  38. typedef uintptr_t splay_tree_key;
  39. typedef uintptr_t splay_tree_value;
  40. /* Forward declaration for a node in the tree. */
  41. typedef struct splay_tree_node_s *splay_tree_node;
  42. /* The type of a function which compares two splay-tree keys. The
  43. function should return values as for qsort. */
  44. typedef int (*splay_tree_compare_fn) (splay_tree_key, splay_tree_key);
  45. /* The type of a function used to deallocate any resources associated
  46. with the key. If you provide this function, the splay tree
  47. will take the ownership of the memory of the splay_tree_key arg
  48. of splay_tree_insert. This function is called to release the keys
  49. present in the tree when calling splay_tree_delete or splay_tree_remove.
  50. If splay_tree_insert is called with a key equal to a key already
  51. present in the tree, the old key and old value will be released. */
  52. typedef void (*splay_tree_delete_key_fn) (splay_tree_key);
  53. /* The type of a function used to deallocate any resources associated
  54. with the value. If you provide this function, the memory of the
  55. splay_tree_value arg of splay_tree_insert is managed similarly to
  56. the splay_tree_key memory: see splay_tree_delete_key_fn. */
  57. typedef void (*splay_tree_delete_value_fn) (splay_tree_value);
  58. /* The type of a function used to iterate over the tree. */
  59. typedef int (*splay_tree_foreach_fn) (splay_tree_node, void*);
  60. /* The type of a function used to allocate memory for tree root and
  61. node structures. The first argument is the number of bytes needed;
  62. the second is a data pointer the splay tree functions pass through
  63. to the allocator. This function must never return zero. */
  64. typedef void *(*splay_tree_allocate_fn) (int, void *);
  65. /* The type of a function used to free memory allocated using the
  66. corresponding splay_tree_allocate_fn. The first argument is the
  67. memory to be freed; the latter is a data pointer the splay tree
  68. functions pass through to the freer. */
  69. typedef void (*splay_tree_deallocate_fn) (void *, void *);
  70. /* The nodes in the splay tree. */
  71. struct splay_tree_node_s {
  72. /* The key. */
  73. splay_tree_key key;
  74. /* The value. */
  75. splay_tree_value value;
  76. /* The left and right children, respectively. */
  77. splay_tree_node left;
  78. splay_tree_node right;
  79. };
  80. /* The splay tree itself. */
  81. struct splay_tree_s {
  82. /* The root of the tree. */
  83. splay_tree_node root;
  84. /* The comparision function. */
  85. splay_tree_compare_fn comp;
  86. /* The deallocate-key function. NULL if no cleanup is necessary. */
  87. splay_tree_delete_key_fn delete_key;
  88. /* The deallocate-value function. NULL if no cleanup is necessary. */
  89. splay_tree_delete_value_fn delete_value;
  90. /* Node allocate function. Takes allocate_data as a parameter. */
  91. splay_tree_allocate_fn allocate;
  92. /* Free function for nodes and trees. Takes allocate_data as a parameter. */
  93. splay_tree_deallocate_fn deallocate;
  94. /* Parameter for allocate/free functions. */
  95. void *allocate_data;
  96. };
  97. typedef struct splay_tree_s *splay_tree;
  98. extern splay_tree splay_tree_new (splay_tree_compare_fn,
  99. splay_tree_delete_key_fn,
  100. splay_tree_delete_value_fn);
  101. extern splay_tree splay_tree_new_with_allocator (splay_tree_compare_fn,
  102. splay_tree_delete_key_fn,
  103. splay_tree_delete_value_fn,
  104. splay_tree_allocate_fn,
  105. splay_tree_deallocate_fn,
  106. void *);
  107. extern splay_tree splay_tree_new_typed_alloc (splay_tree_compare_fn,
  108. splay_tree_delete_key_fn,
  109. splay_tree_delete_value_fn,
  110. splay_tree_allocate_fn,
  111. splay_tree_allocate_fn,
  112. splay_tree_deallocate_fn,
  113. void *);
  114. extern void splay_tree_delete (splay_tree);
  115. extern splay_tree_node splay_tree_insert (splay_tree,
  116. splay_tree_key,
  117. splay_tree_value);
  118. extern void splay_tree_remove (splay_tree, splay_tree_key);
  119. extern splay_tree_node splay_tree_lookup (splay_tree, splay_tree_key);
  120. extern splay_tree_node splay_tree_predecessor (splay_tree, splay_tree_key);
  121. extern splay_tree_node splay_tree_successor (splay_tree, splay_tree_key);
  122. extern splay_tree_node splay_tree_max (splay_tree);
  123. extern splay_tree_node splay_tree_min (splay_tree);
  124. extern int splay_tree_foreach (splay_tree, splay_tree_foreach_fn, void*);
  125. extern int splay_tree_compare_ints (splay_tree_key, splay_tree_key);
  126. extern int splay_tree_compare_pointers (splay_tree_key, splay_tree_key);
  127. extern int splay_tree_compare_strings (splay_tree_key, splay_tree_key);
  128. extern void splay_tree_delete_pointers (splay_tree_value);
  129. #ifdef __cplusplus
  130. }
  131. #endif /* __cplusplus */
  132. #endif /* _SPLAY_TREE_H */