splay-tree.h 5.4 KB

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