annotate gcc/hash-set.h @ 16:04ced10e8804

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