hash-set.h 4.6 KB

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