hash-set.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* A type-safe hash set.
  2. Copyright (C) 2014-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_set_h
  16. #define hash_set_h
  17. template<typename KeyId, bool Lazy = false,
  18. typename Traits = default_hash_traits<KeyId> >
  19. class hash_set
  20. {
  21. public:
  22. typedef typename Traits::value_type Key;
  23. explicit hash_set (size_t n = 13, bool ggc = false CXX_MEM_STAT_INFO)
  24. : m_table (n, ggc, GATHER_STATISTICS, HASH_SET_ORIGIN PASS_MEM_STAT) {}
  25. /* Create a hash_set in gc memory with space for at least n elements. */
  26. static hash_set *
  27. create_ggc (size_t n)
  28. {
  29. hash_set *set = ggc_alloc<hash_set> ();
  30. new (set) hash_set (n, true);
  31. return set;
  32. }
  33. /* If key k isn't already in the map add it to the map, and
  34. return false. Otherwise return true. */
  35. bool add (const Key &k)
  36. {
  37. Key *e = m_table.find_slot_with_hash (k, Traits::hash (k), INSERT);
  38. bool existed = !Traits::is_empty (*e);
  39. if (!existed)
  40. *e = k;
  41. return existed;
  42. }
  43. /* if the passed in key is in the map return its value otherwise NULL. */
  44. bool contains (const Key &k)
  45. {
  46. if (Lazy)
  47. return (m_table.find_slot_with_hash (k, Traits::hash (k), NO_INSERT)
  48. != NULL);
  49. Key &e = m_table.find_with_hash (k, Traits::hash (k));
  50. return !Traits::is_empty (e);
  51. }
  52. void remove (const Key &k)
  53. {
  54. m_table.remove_elt_with_hash (k, Traits::hash (k));
  55. }
  56. /* Call the call back on each pair of key and value with the passed in
  57. arg. */
  58. template<typename Arg, bool (*f)(const typename Traits::value_type &, Arg)>
  59. void traverse (Arg a) const
  60. {
  61. for (typename hash_table<Traits, Lazy>::iterator iter = m_table.begin ();
  62. iter != m_table.end (); ++iter)
  63. f (*iter, a);
  64. }
  65. /* Return the number of elements in the set. */
  66. size_t elements () const { return m_table.elements (); }
  67. /* Clear the hash table. */
  68. void empty () { m_table.empty (); }
  69. class iterator
  70. {
  71. public:
  72. explicit iterator (const typename hash_table<Traits,
  73. Lazy>::iterator &iter) :
  74. m_iter (iter) {}
  75. iterator &operator++ ()
  76. {
  77. ++m_iter;
  78. return *this;
  79. }
  80. Key
  81. operator* ()
  82. {
  83. return *m_iter;
  84. }
  85. bool
  86. operator != (const iterator &other) const
  87. {
  88. return m_iter != other.m_iter;
  89. }
  90. private:
  91. typename hash_table<Traits, Lazy>::iterator m_iter;
  92. };
  93. /* Standard iterator retrieval methods. */
  94. iterator begin () const { return iterator (m_table.begin ()); }
  95. iterator end () const { return iterator (m_table.end ()); }
  96. private:
  97. template<typename T, typename U>
  98. friend void gt_ggc_mx (hash_set<T, false, U> *);
  99. template<typename T, typename U>
  100. friend void gt_pch_nx (hash_set<T, false, U> *);
  101. template<typename T, typename U>
  102. friend void gt_pch_nx (hash_set<T, false, U> *, gt_pointer_operator, void *);
  103. hash_table<Traits, Lazy> m_table;
  104. };
  105. /* Generic hash_set<TYPE> debug helper.
  106. This needs to be instantiated for each hash_set<TYPE> used throughout
  107. the compiler like this:
  108. DEFINE_DEBUG_HASH_SET (TYPE)
  109. The reason we have a debug_helper() is because GDB can't
  110. disambiguate a plain call to debug(some_hash), and it must be called
  111. like debug<TYPE>(some_hash). */
  112. template<typename T>
  113. void
  114. debug_helper (hash_set<T> &ref)
  115. {
  116. for (typename hash_set<T>::iterator it = ref.begin ();
  117. it != ref.end (); ++it)
  118. {
  119. debug_slim (*it);
  120. fputc ('\n', stderr);
  121. }
  122. }
  123. #define DEFINE_DEBUG_HASH_SET(T) \
  124. template void debug_helper (hash_set<T> &); \
  125. DEBUG_FUNCTION void \
  126. debug (hash_set<T> &ref) \
  127. { \
  128. debug_helper <T> (ref); \
  129. } \
  130. DEBUG_FUNCTION void \
  131. debug (hash_set<T> *ptr) \
  132. { \
  133. if (ptr) \
  134. debug (*ptr); \
  135. else \
  136. fprintf (stderr, "<nil>\n"); \
  137. }
  138. /* ggc marking routines. */
  139. template<typename K, typename H>
  140. static inline void
  141. gt_ggc_mx (hash_set<K, false, H> *h)
  142. {
  143. gt_ggc_mx (&h->m_table);
  144. }
  145. template<typename K, typename H>
  146. static inline void
  147. gt_pch_nx (hash_set<K, false, H> *h)
  148. {
  149. gt_pch_nx (&h->m_table);
  150. }
  151. template<typename K, typename H>
  152. static inline void
  153. gt_pch_nx (hash_set<K, false, H> *h, gt_pointer_operator op, void *cookie)
  154. {
  155. op (&h->m_table.m_entries, cookie);
  156. }
  157. #endif