improve construction of the original schedule
[gcc.git] / gcc / hash-set.h
1 /* A type-safe hash set.
2 Copyright (C) 2014-2015 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20
21 #ifndef hash_set_h
22 #define hash_set_h
23
24 template<typename Key, typename Traits = default_hash_traits<Key> >
25 class hash_set
26 {
27 public:
28 explicit hash_set (size_t n = 13, bool ggc = false CXX_MEM_STAT_INFO)
29 : m_table (n, ggc, true, HASH_SET_ORIGIN PASS_MEM_STAT) {}
30
31 /* Create a hash_set in gc memory with space for at least n elements. */
32
33 static hash_set *
34 create_ggc (size_t n)
35 {
36 hash_set *set = ggc_alloc<hash_set> ();
37 new (set) hash_set (n, true);
38 return set;
39 }
40
41 /* If key k isn't already in the map add it to the map, and
42 return false. Otherwise return true. */
43
44 bool add (const Key &k)
45 {
46 Key *e = m_table.find_slot_with_hash (k, Traits::hash (k), INSERT);
47 bool existed = !Traits::is_empty (*e);
48 if (!existed)
49 *e = k;
50
51 return existed;
52 }
53
54 /* if the passed in key is in the map return its value otherwise NULL. */
55
56 bool contains (const Key &k)
57 {
58 Key &e = m_table.find_with_hash (k, Traits::hash (k));
59 return !Traits::is_empty (e);
60 }
61
62 void remove (const Key &k)
63 {
64 m_table.remove_elt_with_hash (k, Traits::hash (k));
65 }
66
67 /* Call the call back on each pair of key and value with the passed in
68 arg. */
69
70 template<typename Arg, bool (*f)(const Key &, Arg)>
71 void traverse (Arg a) const
72 {
73 for (typename hash_table<Traits>::iterator iter = m_table.begin ();
74 iter != m_table.end (); ++iter)
75 f (*iter, a);
76 }
77
78 /* Return the number of elements in the set. */
79
80 size_t elements () const { return m_table.elements (); }
81
82 class iterator
83 {
84 public:
85 explicit iterator (const typename hash_table<Traits>::iterator &iter) :
86 m_iter (iter) {}
87
88 iterator &operator++ ()
89 {
90 ++m_iter;
91 return *this;
92 }
93
94 Key
95 operator* ()
96 {
97 return *m_iter;
98 }
99
100 bool
101 operator != (const iterator &other) const
102 {
103 return m_iter != other.m_iter;
104 }
105
106 private:
107 typename hash_table<Traits>::iterator m_iter;
108 };
109
110 /* Standard iterator retrieval methods. */
111
112 iterator begin () const { return iterator (m_table.begin ()); }
113 iterator end () const { return iterator (m_table.end ()); }
114
115
116 private:
117
118 template<typename T, typename U> friend void gt_ggc_mx (hash_set<T, U> *);
119 template<typename T, typename U> friend void gt_pch_nx (hash_set<T, U> *);
120 template<typename T, typename U> friend void gt_pch_nx (hash_set<T, U> *, gt_pointer_operator, void *);
121
122 hash_table<Traits> m_table;
123 };
124
125 /* ggc marking routines. */
126
127 template<typename K, typename H>
128 static inline void
129 gt_ggc_mx (hash_set<K, H> *h)
130 {
131 gt_ggc_mx (&h->m_table);
132 }
133
134 template<typename K, typename H>
135 static inline void
136 gt_pch_nx (hash_set<K, H> *h)
137 {
138 gt_pch_nx (&h->m_table);
139 }
140
141 template<typename K, typename H>
142 static inline void
143 gt_pch_nx (hash_set<K, H> *h, gt_pointer_operator op, void *cookie)
144 {
145 op (&h->m_table.m_entries, cookie);
146 }
147
148 #endif