splay-tree.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* A splay-tree datatype.
  2. Copyright (C) 1998-2022 Free Software Foundation, Inc.
  3. Contributed by Mark Mitchell (mark@markmitchell.com).
  4. This file is part of the GNU Offloading and Multi Processing Library
  5. (libgomp).
  6. Libgomp 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 3, or (at your option)
  9. any later version.
  10. Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. more details.
  14. Under Section 7 of GPL version 3, you are granted additional
  15. permissions described in the GCC Runtime Library Exception, version
  16. 3.1, as published by the Free Software Foundation.
  17. You should have received a copy of the GNU General Public License and
  18. a copy of the GCC Runtime Library Exception along with this program;
  19. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. <http://www.gnu.org/licenses/>. */
  21. /* The splay tree code copied from include/splay-tree.h and adjusted,
  22. so that all the data lives directly in splay_tree_node_s structure
  23. and no extra allocations are needed.
  24. Files including this header should before including it add:
  25. typedef struct splay_tree_node_s *splay_tree_node;
  26. typedef struct splay_tree_s *splay_tree;
  27. typedef struct splay_tree_key_s *splay_tree_key;
  28. define splay_tree_key_s structure, and define
  29. splay_compare inline function.
  30. Alternatively, they can define splay_tree_prefix macro before
  31. including this header and then all the above types, the
  32. splay_compare function and the splay_tree_{lookup,insert_remove}
  33. function will be prefixed by that prefix. If splay_tree_prefix
  34. macro is defined, this header must be included twice: once where
  35. you need the header file definitions, and once where you need the
  36. .c implementation routines. In the latter case, you must also
  37. define the macro splay_tree_c. See the include of splay-tree.h in
  38. priority_queue.[hc] for an example. */
  39. /* For an easily readable description of splay-trees, see:
  40. Lewis, Harry R. and Denenberg, Larry. Data Structures and Their
  41. Algorithms. Harper-Collins, Inc. 1991.
  42. The major feature of splay trees is that all basic tree operations
  43. are amortized O(log n) time for a tree with n nodes. */
  44. #ifdef splay_tree_prefix
  45. # define splay_tree_name_1(prefix, name) prefix ## _ ## name
  46. # define splay_tree_name(prefix, name) splay_tree_name_1 (prefix, name)
  47. # define splay_tree_node_s \
  48. splay_tree_name (splay_tree_prefix, splay_tree_node_s)
  49. # define splay_tree_s \
  50. splay_tree_name (splay_tree_prefix, splay_tree_s)
  51. # define splay_tree_key_s \
  52. splay_tree_name (splay_tree_prefix, splay_tree_key_s)
  53. # define splay_tree_node \
  54. splay_tree_name (splay_tree_prefix, splay_tree_node)
  55. # define splay_tree \
  56. splay_tree_name (splay_tree_prefix, splay_tree)
  57. # define splay_tree_key \
  58. splay_tree_name (splay_tree_prefix, splay_tree_key)
  59. # define splay_compare \
  60. splay_tree_name (splay_tree_prefix, splay_compare)
  61. # define splay_tree_lookup \
  62. splay_tree_name (splay_tree_prefix, splay_tree_lookup)
  63. # define splay_tree_insert \
  64. splay_tree_name (splay_tree_prefix, splay_tree_insert)
  65. # define splay_tree_remove \
  66. splay_tree_name (splay_tree_prefix, splay_tree_remove)
  67. # define splay_tree_foreach \
  68. splay_tree_name (splay_tree_prefix, splay_tree_foreach)
  69. # define splay_tree_callback \
  70. splay_tree_name (splay_tree_prefix, splay_tree_callback)
  71. #endif
  72. #ifndef splay_tree_c
  73. /* Header file definitions and prototypes. */
  74. /* The nodes in the splay tree. */
  75. struct splay_tree_node_s {
  76. struct splay_tree_key_s key;
  77. /* The left and right children, respectively. */
  78. splay_tree_node left;
  79. splay_tree_node right;
  80. };
  81. /* The splay tree. */
  82. struct splay_tree_s {
  83. splay_tree_node root;
  84. };
  85. typedef void (*splay_tree_callback) (splay_tree_key, void *);
  86. extern splay_tree_key splay_tree_lookup (splay_tree, splay_tree_key);
  87. extern void splay_tree_insert (splay_tree, splay_tree_node);
  88. extern void splay_tree_remove (splay_tree, splay_tree_key);
  89. extern void splay_tree_foreach (splay_tree, splay_tree_callback, void *);
  90. #else /* splay_tree_c */
  91. # ifdef splay_tree_prefix
  92. # include "splay-tree.c"
  93. # undef splay_tree_name_1
  94. # undef splay_tree_name
  95. # undef splay_tree_node_s
  96. # undef splay_tree_s
  97. # undef splay_tree_key_s
  98. # undef splay_tree_node
  99. # undef splay_tree
  100. # undef splay_tree_key
  101. # undef splay_compare
  102. # undef splay_tree_lookup
  103. # undef splay_tree_insert
  104. # undef splay_tree_remove
  105. # undef splay_tree_foreach
  106. # undef splay_tree_callback
  107. # undef splay_tree_c
  108. # endif
  109. #endif /* #ifndef splay_tree_c */
  110. #ifdef splay_tree_prefix
  111. # undef splay_tree_prefix
  112. #endif