OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * This library is free software; you can redistribute it and/or | 4 * This library is free software; you can redistribute it and/or |
5 * modify it under the terms of the GNU Library General Public | 5 * modify it under the terms of the GNU Library General Public |
6 * License as published by the Free Software Foundation; either | 6 * License as published by the Free Software Foundation; either |
7 * version 2 of the License, or (at your option) any later version. | 7 * version 2 of the License, or (at your option) any later version. |
8 * | 8 * |
9 * This library is distributed in the hope that it will be useful, | 9 * This library is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 * Library General Public License for more details. | 12 * Library General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU Library General Public License | 14 * You should have received a copy of the GNU Library General Public License |
15 * along with this library; see the file COPYING.LIB. If not, write to | 15 * along with this library; see the file COPYING.LIB. If not, write to |
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
17 * Boston, MA 02110-1301, USA. | 17 * Boston, MA 02110-1301, USA. |
18 * | 18 * |
19 */ | 19 */ |
20 | 20 |
21 #ifndef WTF_HashCountedSet_h | 21 #ifndef WTF_HashCountedSet_h |
22 #define WTF_HashCountedSet_h | 22 #define WTF_HashCountedSet_h |
23 | 23 |
24 #include "wtf/Assertions.h" | 24 #include "wtf/Assertions.h" |
25 #include "wtf/HashMap.h" | 25 #include "wtf/HashMap.h" |
26 #include "wtf/Vector.h" | 26 #include "wtf/Vector.h" |
27 | 27 |
28 namespace WTF { | 28 namespace WTF { |
29 | 29 |
30 template<typename Value, typename HashFunctions = typename DefaultHash<Value >::Hash, | 30 // An unordered hash set that keeps track of how many times you added an |
31 typename Traits = HashTraits<Value> > class HashCountedSet { | 31 // item to the set. The iterators have fields ->key and ->value that return |
32 WTF_MAKE_FAST_ALLOCATED; | 32 // the set members and their counts, respectively. |
33 template< | |
34 typename Value, | |
35 typename HashFunctions = typename DefaultHash<Value>::Hash, | |
36 typename Traits = HashTraits<Value>, | |
37 typename Allocator = DefaultAllocator > class HashCountedSet { | |
38 WTF_USE_ALLOCATOR(HashCountedSet, Allocator); | |
33 private: | 39 private: |
34 typedef HashMap<Value, unsigned, HashFunctions, Traits, HashTraits<unsig ned>, DefaultAllocator> ImplType; | 40 typedef HashMap<Value, unsigned, HashFunctions, Traits, HashTraits<unsig ned>, Allocator> ImplType; |
35 public: | 41 public: |
36 typedef Value ValueType; | 42 typedef Value ValueType; |
37 typedef typename ImplType::iterator iterator; | 43 typedef typename ImplType::iterator iterator; |
38 typedef typename ImplType::const_iterator const_iterator; | 44 typedef typename ImplType::const_iterator const_iterator; |
39 typedef typename ImplType::AddResult AddResult; | 45 typedef typename ImplType::AddResult AddResult; |
40 | 46 |
41 HashCountedSet() {} | 47 HashCountedSet() {} |
42 | 48 |
43 void swap(HashCountedSet&); | 49 void swap(HashCountedSet& other) { m_impl.swap(other.m_impl); } |
44 | 50 |
45 unsigned size() const; | 51 unsigned size() const { return m_impl.size(); } |
46 unsigned capacity() const; | 52 unsigned capacity() const { return m_impl.capacity(); } |
47 bool isEmpty() const; | 53 bool isEmpty() const { return m_impl.isEmpty(); } |
48 | 54 |
49 // Iterators iterate over pairs of values and counts. | 55 // Iterators iterate over pairs of values (called key) and counts (calle d value). |
50 iterator begin(); | 56 iterator begin() { return m_impl.begin(); } |
51 iterator end(); | 57 iterator end() { return m_impl.end(); } |
52 const_iterator begin() const; | 58 const_iterator begin() const { return m_impl.begin(); } |
53 const_iterator end() const; | 59 const_iterator end() const { return m_impl.end(); } |
54 | 60 |
55 iterator find(const ValueType&); | 61 iterator find(const ValueType& value) { return m_impl.find(value); } |
56 const_iterator find(const ValueType&) const; | 62 const_iterator find(const ValueType& value) const { return m_impl.find(v alue); } |
57 bool contains(const ValueType&) const; | 63 bool contains(const ValueType& value ) const { return m_impl.contains(va lue); } |
58 unsigned count(const ValueType&) const; | 64 unsigned count(const ValueType& value ) const { return m_impl.get(value) ; } |
59 | 65 |
60 // Increases the count if an equal value is already present | 66 // Increases the count if an equal value is already present |
61 // the return value is a pair of an iterator to the new value's | 67 // the return value is a pair of an iterator to the new value's |
62 // location, and a bool that is true if an new entry was added. | 68 // location, and a bool that is true if an new entry was added. |
63 AddResult add(const ValueType&); | 69 AddResult add(const ValueType&); |
64 | 70 |
65 // Reduces the count of the value, and removes it if count | 71 // Reduces the count of the value, and removes it if count |
66 // goes down to zero, returns true if the value is removed. | 72 // goes down to zero, returns true if the value is removed. |
67 bool remove(const ValueType&); | 73 bool remove(const ValueType& value) { return remove(find(value)); } |
68 bool remove(iterator); | 74 bool remove(iterator); |
69 | 75 |
70 // Removes the value, regardless of its count. | 76 // Removes the value, regardless of its count. |
77 void removeAll(const ValueType& value) { removeAll(find(value)); } | |
71 void removeAll(iterator); | 78 void removeAll(iterator); |
72 void removeAll(const ValueType&); | |
73 | 79 |
74 // Clears the whole set. | 80 // Clears the whole set. |
75 void clear(); | 81 void clear() { m_impl.clear(); } |
82 | |
83 void trace(typename Allocator::Visitor* visitor) { m_impl.trace(visitor) ; } | |
76 | 84 |
77 private: | 85 private: |
78 ImplType m_impl; | 86 ImplType m_impl; |
79 }; | 87 }; |
80 | 88 |
81 template<typename Value, typename HashFunctions, typename Traits> | 89 template<typename T, typename U, typename V, typename W> |
82 inline void HashCountedSet<Value, HashFunctions, Traits>::swap(HashCountedSe t& other) | 90 inline typename HashCountedSet<T, U, V, W>::AddResult HashCountedSet<T, U, V , W>::add(const ValueType &value) |
Mikhail
2014/04/30 14:00:35
nit: const ValueType&
| |
83 { | |
84 m_impl.swap(other.m_impl); | |
85 } | |
86 | |
87 template<typename Value, typename HashFunctions, typename Traits> | |
88 inline unsigned HashCountedSet<Value, HashFunctions, Traits>::size() const | |
89 { | |
90 return m_impl.size(); | |
91 } | |
92 | |
93 template<typename Value, typename HashFunctions, typename Traits> | |
94 inline unsigned HashCountedSet<Value, HashFunctions, Traits>::capacity() con st | |
95 { | |
96 return m_impl.capacity(); | |
97 } | |
98 | |
99 template<typename Value, typename HashFunctions, typename Traits> | |
100 inline bool HashCountedSet<Value, HashFunctions, Traits>::isEmpty() const | |
101 { | |
102 return size() == 0; | |
103 } | |
104 | |
105 template<typename Value, typename HashFunctions, typename Traits> | |
106 inline typename HashCountedSet<Value, HashFunctions, Traits>::iterator HashC ountedSet<Value, HashFunctions, Traits>::begin() | |
107 { | |
108 return m_impl.begin(); | |
109 } | |
110 | |
111 template<typename Value, typename HashFunctions, typename Traits> | |
112 inline typename HashCountedSet<Value, HashFunctions, Traits>::iterator HashC ountedSet<Value, HashFunctions, Traits>::end() | |
113 { | |
114 return m_impl.end(); | |
115 } | |
116 | |
117 template<typename Value, typename HashFunctions, typename Traits> | |
118 inline typename HashCountedSet<Value, HashFunctions, Traits>::const_iterator HashCountedSet<Value, HashFunctions, Traits>::begin() const | |
119 { | |
120 return m_impl.begin(); | |
121 } | |
122 | |
123 template<typename Value, typename HashFunctions, typename Traits> | |
124 inline typename HashCountedSet<Value, HashFunctions, Traits>::const_iterator HashCountedSet<Value, HashFunctions, Traits>::end() const | |
125 { | |
126 return m_impl.end(); | |
127 } | |
128 | |
129 template<typename Value, typename HashFunctions, typename Traits> | |
130 inline typename HashCountedSet<Value, HashFunctions, Traits>::iterator HashC ountedSet<Value, HashFunctions, Traits>::find(const ValueType& value) | |
131 { | |
132 return m_impl.find(value); | |
133 } | |
134 | |
135 template<typename Value, typename HashFunctions, typename Traits> | |
136 inline typename HashCountedSet<Value, HashFunctions, Traits>::const_iterator HashCountedSet<Value, HashFunctions, Traits>::find(const ValueType& value) cons t | |
137 { | |
138 return m_impl.find(value); | |
139 } | |
140 | |
141 template<typename Value, typename HashFunctions, typename Traits> | |
142 inline bool HashCountedSet<Value, HashFunctions, Traits>::contains(const Val ueType& value) const | |
143 { | |
144 return m_impl.contains(value); | |
145 } | |
146 | |
147 template<typename Value, typename HashFunctions, typename Traits> | |
148 inline unsigned HashCountedSet<Value, HashFunctions, Traits>::count(const Va lueType& value) const | |
149 { | |
150 return m_impl.get(value); | |
151 } | |
152 | |
153 template<typename Value, typename HashFunctions, typename Traits> | |
154 inline typename HashCountedSet<Value, HashFunctions, Traits>::AddResult Hash CountedSet<Value, HashFunctions, Traits>::add(const ValueType &value) | |
155 { | 91 { |
156 AddResult result = m_impl.add(value, 0); | 92 AddResult result = m_impl.add(value, 0); |
157 ++result.storedValue->value; | 93 ++result.storedValue->value; |
158 return result; | 94 return result; |
159 } | 95 } |
160 | 96 |
161 template<typename Value, typename HashFunctions, typename Traits> | 97 template<typename T, typename U, typename V, typename W> |
162 inline bool HashCountedSet<Value, HashFunctions, Traits>::remove(const Value Type& value) | 98 inline bool HashCountedSet<T, U, V, W>::remove(iterator it) |
163 { | |
164 return remove(find(value)); | |
165 } | |
166 | |
167 template<typename Value, typename HashFunctions, typename Traits> | |
168 inline bool HashCountedSet<Value, HashFunctions, Traits>::remove(iterator it ) | |
169 { | 99 { |
170 if (it == end()) | 100 if (it == end()) |
171 return false; | 101 return false; |
172 | 102 |
173 unsigned oldVal = it->value; | 103 unsigned oldVal = it->value; |
174 ASSERT(oldVal); | 104 ASSERT(oldVal); |
175 unsigned newVal = oldVal - 1; | 105 unsigned newVal = oldVal - 1; |
176 if (newVal) { | 106 if (newVal) { |
177 it->value = newVal; | 107 it->value = newVal; |
178 return false; | 108 return false; |
179 } | 109 } |
180 | 110 |
181 m_impl.remove(it); | 111 m_impl.remove(it); |
182 return true; | 112 return true; |
183 } | 113 } |
184 | 114 |
185 template<typename Value, typename HashFunctions, typename Traits> | 115 template<typename T, typename U, typename V, typename W> |
186 inline void HashCountedSet<Value, HashFunctions, Traits>::removeAll(const Va lueType& value) | 116 inline void HashCountedSet<T, U, V, W>::removeAll(iterator it) |
187 { | |
188 removeAll(find(value)); | |
189 } | |
190 | |
191 template<typename Value, typename HashFunctions, typename Traits> | |
192 inline void HashCountedSet<Value, HashFunctions, Traits>::removeAll(iterator it) | |
193 { | 117 { |
194 if (it == end()) | 118 if (it == end()) |
195 return; | 119 return; |
196 | 120 |
197 m_impl.remove(it); | 121 m_impl.remove(it); |
198 } | 122 } |
199 | 123 |
200 template<typename Value, typename HashFunctions, typename Traits> | 124 template<typename T, typename U, typename V, typename W, typename VectorType > |
201 inline void HashCountedSet<Value, HashFunctions, Traits>::clear() | 125 inline void copyToVector(const HashCountedSet<T, U, V, W>& collection, Vecto rType& vector) |
202 { | 126 { |
203 m_impl.clear(); | 127 typedef typename HashCountedSet<T, U, V, W>::const_iterator iterator; |
204 } | |
205 | |
206 template<typename Value, typename HashFunctions, typename Traits, typename V ectorType> | |
207 inline void copyToVector(const HashCountedSet<Value, HashFunctions, Traits>& collection, VectorType& vector) | |
208 { | |
209 typedef typename HashCountedSet<Value, HashFunctions, Traits>::const_ite rator iterator; | |
210 | 128 |
211 vector.resize(collection.size()); | 129 vector.resize(collection.size()); |
212 | 130 |
213 iterator it = collection.begin(); | 131 iterator it = collection.begin(); |
214 iterator end = collection.end(); | 132 iterator end = collection.end(); |
215 for (unsigned i = 0; it != end; ++it, ++i) | 133 for (unsigned i = 0; it != end; ++it, ++i) |
216 vector[i] = *it; | 134 vector[i] = *it; |
217 } | 135 } |
218 | 136 |
219 template<typename Value, typename HashFunctions, typename Traits> | 137 template<typename Value, typename HashFunctions, typename Traits, typename A llocator, size_t inlineCapacity, typename VectorAllocator> |
220 inline void copyToVector(const HashCountedSet<Value, HashFunctions, Traits>& collection, Vector<Value>& vector) | 138 inline void copyToVector(const HashCountedSet<Value, HashFunctions, Traits, Allocator>& collection, Vector<Value, inlineCapacity, VectorAllocator>& vector) |
221 { | 139 { |
222 typedef typename HashCountedSet<Value, HashFunctions, Traits>::const_ite rator iterator; | 140 typedef typename HashCountedSet<Value, HashFunctions, Traits, Allocator> ::const_iterator iterator; |
223 | 141 |
224 vector.resize(collection.size()); | 142 vector.resize(collection.size()); |
225 | 143 |
226 iterator it = collection.begin(); | 144 iterator it = collection.begin(); |
227 iterator end = collection.end(); | 145 iterator end = collection.end(); |
228 for (unsigned i = 0; it != end; ++it, ++i) | 146 for (unsigned i = 0; it != end; ++it, ++i) |
229 vector[i] = (*it).key; | 147 vector[i] = (*it).key; |
230 } | 148 } |
231 | 149 |
232 | 150 |
233 } // namespace WTF | 151 } // namespace WTF |
234 | 152 |
235 using WTF::HashCountedSet; | 153 using WTF::HashCountedSet; |
236 | 154 |
237 #endif /* WTF_HashCountedSet_h */ | 155 #endif /* WTF_HashCountedSet_h */ |
OLD | NEW |