hash-map-traits.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* A hash map traits.
  2. Copyright (C) 2015-2019 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 HASH_MAP_TRAITS_H
  16. #define HASH_MAP_TRAITS_H
  17. /* Bacause mem-stats.h uses default hashmap traits, we have to
  18. put the class to this separate header file. */
  19. #include "hash-traits.h"
  20. /* Implement hash_map traits for a key with hash traits H. Empty and
  21. deleted map entries are represented as empty and deleted keys. */
  22. template <typename H, typename Value>
  23. struct simple_hashmap_traits
  24. {
  25. typedef typename H::value_type key_type;
  26. static const bool maybe_mx = true;
  27. static inline hashval_t hash (const key_type &);
  28. static inline bool equal_keys (const key_type &, const key_type &);
  29. template <typename T> static inline void remove (T &);
  30. template <typename T> static inline bool is_empty (const T &);
  31. template <typename T> static inline bool is_deleted (const T &);
  32. template <typename T> static inline void mark_empty (T &);
  33. template <typename T> static inline void mark_deleted (T &);
  34. };
  35. template <typename H, typename Value>
  36. inline hashval_t
  37. simple_hashmap_traits <H, Value>::hash (const key_type &h)
  38. {
  39. return H::hash (h);
  40. }
  41. template <typename H, typename Value>
  42. inline bool
  43. simple_hashmap_traits <H, Value>::equal_keys (const key_type &k1,
  44. const key_type &k2)
  45. {
  46. return H::equal (k1, k2);
  47. }
  48. template <typename H, typename Value>
  49. template <typename T>
  50. inline void
  51. simple_hashmap_traits <H, Value>::remove (T &entry)
  52. {
  53. H::remove (entry.m_key);
  54. entry.m_value.~Value ();
  55. }
  56. template <typename H, typename Value>
  57. template <typename T>
  58. inline bool
  59. simple_hashmap_traits <H, Value>::is_empty (const T &entry)
  60. {
  61. return H::is_empty (entry.m_key);
  62. }
  63. template <typename H, typename Value>
  64. template <typename T>
  65. inline bool
  66. simple_hashmap_traits <H, Value>::is_deleted (const T &entry)
  67. {
  68. return H::is_deleted (entry.m_key);
  69. }
  70. template <typename H, typename Value>
  71. template <typename T>
  72. inline void
  73. simple_hashmap_traits <H, Value>::mark_empty (T &entry)
  74. {
  75. H::mark_empty (entry.m_key);
  76. }
  77. template <typename H, typename Value>
  78. template <typename T>
  79. inline void
  80. simple_hashmap_traits <H, Value>::mark_deleted (T &entry)
  81. {
  82. H::mark_deleted (entry.m_key);
  83. }
  84. template <typename H, typename Value>
  85. struct simple_cache_map_traits: public simple_hashmap_traits<H,Value>
  86. {
  87. static const bool maybe_mx = false;
  88. };
  89. /* Implement traits for a hash_map with values of type Value for cases
  90. in which the key cannot represent empty and deleted slots. Instead
  91. record empty and deleted entries in Value. Derived classes must
  92. implement the hash and equal_keys functions. */
  93. template <typename Value>
  94. struct unbounded_hashmap_traits
  95. {
  96. template <typename T> static inline void remove (T &);
  97. template <typename T> static inline bool is_empty (const T &);
  98. template <typename T> static inline bool is_deleted (const T &);
  99. template <typename T> static inline void mark_empty (T &);
  100. template <typename T> static inline void mark_deleted (T &);
  101. };
  102. template <typename Value>
  103. template <typename T>
  104. inline void
  105. unbounded_hashmap_traits <Value>::remove (T &entry)
  106. {
  107. default_hash_traits <Value>::remove (entry.m_value);
  108. }
  109. template <typename Value>
  110. template <typename T>
  111. inline bool
  112. unbounded_hashmap_traits <Value>::is_empty (const T &entry)
  113. {
  114. return default_hash_traits <Value>::is_empty (entry.m_value);
  115. }
  116. template <typename Value>
  117. template <typename T>
  118. inline bool
  119. unbounded_hashmap_traits <Value>::is_deleted (const T &entry)
  120. {
  121. return default_hash_traits <Value>::is_deleted (entry.m_value);
  122. }
  123. template <typename Value>
  124. template <typename T>
  125. inline void
  126. unbounded_hashmap_traits <Value>::mark_empty (T &entry)
  127. {
  128. default_hash_traits <Value>::mark_empty (entry.m_value);
  129. }
  130. template <typename Value>
  131. template <typename T>
  132. inline void
  133. unbounded_hashmap_traits <Value>::mark_deleted (T &entry)
  134. {
  135. default_hash_traits <Value>::mark_deleted (entry.m_value);
  136. }
  137. /* Implement traits for a hash_map from integer type Key to Value in
  138. cases where Key has no spare values for recording empty and deleted
  139. slots. */
  140. template <typename Key, typename Value>
  141. struct unbounded_int_hashmap_traits : unbounded_hashmap_traits <Value>
  142. {
  143. typedef Key key_type;
  144. static inline hashval_t hash (Key);
  145. static inline bool equal_keys (Key, Key);
  146. };
  147. template <typename Key, typename Value>
  148. inline hashval_t
  149. unbounded_int_hashmap_traits <Key, Value>::hash (Key k)
  150. {
  151. return k;
  152. }
  153. template <typename Key, typename Value>
  154. inline bool
  155. unbounded_int_hashmap_traits <Key, Value>::equal_keys (Key k1, Key k2)
  156. {
  157. return k1 == k2;
  158. }
  159. #endif // HASH_MAP_TRAITS_H