typed-splay-tree.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* A typesafe wrapper around libiberty's splay-tree.h.
  2. Copyright (C) 2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef GCC_TYPED_SPLAY_TREE_H
  16. #define GCC_TYPED_SPLAY_TREE_H
  17. #include "splay-tree.h"
  18. /* Typesafe wrapper around libiberty's splay-tree.h. */
  19. template <typename KEY_TYPE, typename VALUE_TYPE>
  20. class typed_splay_tree
  21. {
  22. public:
  23. typedef KEY_TYPE key_type;
  24. typedef VALUE_TYPE value_type;
  25. typedef int (*compare_fn) (key_type, key_type);
  26. typedef void (*delete_key_fn) (key_type);
  27. typedef void (*delete_value_fn) (value_type);
  28. typed_splay_tree (compare_fn,
  29. delete_key_fn,
  30. delete_value_fn);
  31. ~typed_splay_tree ();
  32. value_type lookup (key_type k);
  33. value_type predecessor (key_type k);
  34. value_type successor (key_type k);
  35. void insert (key_type k, value_type v);
  36. private:
  37. static value_type node_to_value (splay_tree_node node);
  38. private:
  39. ::splay_tree m_inner;
  40. };
  41. /* Constructor for typed_splay_tree <K, V>. */
  42. template <typename KEY_TYPE, typename VALUE_TYPE>
  43. inline typed_splay_tree<KEY_TYPE, VALUE_TYPE>::
  44. typed_splay_tree (compare_fn compare_fn,
  45. delete_key_fn delete_key_fn,
  46. delete_value_fn delete_value_fn)
  47. {
  48. m_inner = splay_tree_new ((splay_tree_compare_fn)compare_fn,
  49. (splay_tree_delete_key_fn)delete_key_fn,
  50. (splay_tree_delete_value_fn)delete_value_fn);
  51. }
  52. /* Destructor for typed_splay_tree <K, V>. */
  53. template <typename KEY_TYPE, typename VALUE_TYPE>
  54. inline typed_splay_tree<KEY_TYPE, VALUE_TYPE>::
  55. ~typed_splay_tree ()
  56. {
  57. splay_tree_delete (m_inner);
  58. }
  59. /* Lookup KEY, returning a value if present, and NULL
  60. otherwise. */
  61. template <typename KEY_TYPE, typename VALUE_TYPE>
  62. inline VALUE_TYPE
  63. typed_splay_tree<KEY_TYPE, VALUE_TYPE>::lookup (key_type key)
  64. {
  65. splay_tree_node node = splay_tree_lookup (m_inner, (splay_tree_key)key);
  66. return node_to_value (node);
  67. }
  68. /* Return the immediate predecessor of KEY, or NULL if there is no
  69. predecessor. KEY need not be present in the tree. */
  70. template <typename KEY_TYPE, typename VALUE_TYPE>
  71. inline VALUE_TYPE
  72. typed_splay_tree<KEY_TYPE, VALUE_TYPE>::predecessor (key_type key)
  73. {
  74. splay_tree_node node = splay_tree_predecessor (m_inner, (splay_tree_key)key);
  75. return node_to_value (node);
  76. }
  77. /* Return the immediate successor of KEY, or NULL if there is no
  78. successor. KEY need not be present in the tree. */
  79. template <typename KEY_TYPE, typename VALUE_TYPE>
  80. inline VALUE_TYPE
  81. typed_splay_tree<KEY_TYPE, VALUE_TYPE>::successor (key_type k)
  82. {
  83. splay_tree_node node = splay_tree_successor (m_inner, (splay_tree_key)k);
  84. return node_to_value (node);
  85. }
  86. /* Insert a new node (associating KEY with VALUE). If a
  87. previous node with the indicated KEY exists, its data is replaced
  88. with the new value. */
  89. template <typename KEY_TYPE, typename VALUE_TYPE>
  90. inline void
  91. typed_splay_tree<KEY_TYPE, VALUE_TYPE>::insert (key_type key,
  92. value_type value)
  93. {
  94. splay_tree_insert (m_inner,
  95. (splay_tree_key)key,
  96. (splay_tree_value)value);
  97. }
  98. /* Internal function for converting from splay_tree_node to
  99. VALUE_TYPE. */
  100. template <typename KEY_TYPE, typename VALUE_TYPE>
  101. inline VALUE_TYPE
  102. typed_splay_tree<KEY_TYPE, VALUE_TYPE>::node_to_value (splay_tree_node node)
  103. {
  104. if (node)
  105. return (value_type)node->value;
  106. else
  107. return 0;
  108. }
  109. #endif /* GCC_TYPED_SPLAY_TREE_H */