hash-map-traits.h 5.2 KB

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