1/*
2 * Copyright (C) 2005, 2006, 2007, 2008, 2011, 2012 Apple Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
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
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21#ifndef WTF_HashTraits_h
22#define WTF_HashTraits_h
23
24#include <wtf/HashFunctions.h>
25#include <wtf/StdLibExtras.h>
26#include <utility>
27#include <limits>
28
29namespace WTF {
30
31class String;
32
33template<typename T> struct HashTraits;
34
35template<bool isInteger, typename T> struct GenericHashTraitsBase;
36
37template<typename T> struct GenericHashTraitsBase<false, T> {
38 // The emptyValueIsZero flag is used to optimize allocation of empty hash tables with zeroed memory.
39 static const bool emptyValueIsZero = false;
40
41 // The hasIsEmptyValueFunction flag allows the hash table to automatically generate code to check
42 // for the empty value when it can be done with the equality operator, but allows custom functions
43 // for cases like String that need them.
44 static const bool hasIsEmptyValueFunction = false;
45
46 // The starting table size. Can be overridden when we know beforehand that
47 // a hash table will have at least N entries.
48 static const unsigned minimumTableSize = 8;
49};
50
51// Default integer traits disallow both 0 and -1 as keys (max value instead of -1 for unsigned).
52template<typename T> struct GenericHashTraitsBase<true, T> : GenericHashTraitsBase<false, T> {
53 static const bool emptyValueIsZero = true;
54 static void constructDeletedValue(T& slot) { slot = static_cast<T>(-1); }
55 static bool isDeletedValue(T value) { return value == static_cast<T>(-1); }
56};
57
58template<typename T> struct GenericHashTraits : GenericHashTraitsBase<std::is_integral<T>::value, T> {
59 typedef T TraitType;
60 typedef T EmptyValueType;
61
62 static T emptyValue() { return T(); }
63
64 // Type for return value of functions that do not transfer ownership, such as get.
65 typedef T PeekType;
66 template<typename U> static U&& peek(U&& value) { return std::forward<U>(value); }
67};
68
69template<typename T> struct HashTraits : GenericHashTraits<T> { };
70
71template<typename T> struct FloatHashTraits : GenericHashTraits<T> {
72 static T emptyValue() { return std::numeric_limits<T>::infinity(); }
73 static void constructDeletedValue(T& slot) { slot = -std::numeric_limits<T>::infinity(); }
74 static bool isDeletedValue(T value) { return value == -std::numeric_limits<T>::infinity(); }
75};
76
77template<> struct HashTraits<float> : FloatHashTraits<float> { };
78template<> struct HashTraits<double> : FloatHashTraits<double> { };
79
80// Default unsigned traits disallow both 0 and max as keys -- use these traits to allow zero and disallow max - 1.
81template<typename T> struct UnsignedWithZeroKeyHashTraits : GenericHashTraits<T> {
82 static const bool emptyValueIsZero = false;
83 static T emptyValue() { return std::numeric_limits<T>::max(); }
84 static void constructDeletedValue(T& slot) { slot = std::numeric_limits<T>::max() - 1; }
85 static bool isDeletedValue(T value) { return value == std::numeric_limits<T>::max() - 1; }
86};
87
88// Can be used with strong enums, allows zero as key.
89template<typename T> struct StrongEnumHashTraits : GenericHashTraits<T> {
90 using UnderlyingType = typename std::underlying_type<T>::type;
91 static const bool emptyValueIsZero = false;
92 static T emptyValue() { return static_cast<T>(std::numeric_limits<UnderlyingType>::max()); }
93 static void constructDeletedValue(T& slot) { slot = static_cast<T>(std::numeric_limits<UnderlyingType>::max() - 1); }
94 static bool isDeletedValue(T value) { return value == static_cast<T>(std::numeric_limits<UnderlyingType>::max() - 1); }
95};
96
97template<typename P> struct HashTraits<P*> : GenericHashTraits<P*> {
98 static const bool emptyValueIsZero = true;
99 static void constructDeletedValue(P*& slot) { slot = reinterpret_cast<P*>(-1); }
100 static bool isDeletedValue(P* value) { return value == reinterpret_cast<P*>(-1); }
101};
102
103template<typename T> struct SimpleClassHashTraits : GenericHashTraits<T> {
104 static const bool emptyValueIsZero = true;
105 static void constructDeletedValue(T& slot) { new (NotNull, std::addressof(slot)) T(HashTableDeletedValue); }
106 static bool isDeletedValue(const T& value) { return value.isHashTableDeletedValue(); }
107};
108
109template<typename T, typename Deleter> struct HashTraits<std::unique_ptr<T, Deleter>> : SimpleClassHashTraits<std::unique_ptr<T, Deleter>> {
110 typedef std::nullptr_t EmptyValueType;
111 static EmptyValueType emptyValue() { return nullptr; }
112
113 static void constructDeletedValue(std::unique_ptr<T, Deleter>& slot) { new (NotNull, std::addressof(slot)) std::unique_ptr<T, Deleter> { reinterpret_cast<T*>(-1) }; }
114 static bool isDeletedValue(const std::unique_ptr<T, Deleter>& value) { return value.get() == reinterpret_cast<T*>(-1); }
115
116 typedef T* PeekType;
117 static T* peek(const std::unique_ptr<T, Deleter>& value) { return value.get(); }
118 static T* peek(std::nullptr_t) { return nullptr; }
119
120 static void customDeleteBucket(std::unique_ptr<T, Deleter>& value)
121 {
122 // The custom delete function exists to avoid a dead store before the value is destructed.
123 // The normal destruction sequence of a bucket would be:
124 // 1) Call the destructor of unique_ptr.
125 // 2) unique_ptr store a zero for its internal pointer.
126 // 3) unique_ptr destroys its value.
127 // 4) Call constructDeletedValue() to set the bucket as destructed.
128 //
129 // The problem is the call in (3) prevents the compile from eliminating the dead store in (2)
130 // becase a side effect of free() could be observing the value.
131 //
132 // This version of deleteBucket() ensures the dead 2 stores changing "value"
133 // are on the same side of the function call.
134 ASSERT(!isDeletedValue(value));
135 T* pointer = value.release();
136 constructDeletedValue(value);
137
138 // The null case happens if a caller uses std::move() to remove the pointer before calling remove()
139 // with an iterator. This is very uncommon.
140 if (LIKELY(pointer))
141 Deleter()(pointer);
142 }
143};
144
145template<typename P> struct HashTraits<RefPtr<P>> : SimpleClassHashTraits<RefPtr<P>> {
146 static P* emptyValue() { return nullptr; }
147
148 typedef P* PeekType;
149 static PeekType peek(const RefPtr<P>& value) { return value.get(); }
150 static PeekType peek(P* value) { return value; }
151
152 static void customDeleteBucket(RefPtr<P>& value)
153 {
154 // See unique_ptr's customDeleteBucket() for an explanation.
155 ASSERT(!SimpleClassHashTraits<RefPtr<P>>::isDeletedValue(value));
156 auto valueToBeDestroyed = WTFMove(value);
157 SimpleClassHashTraits<RefPtr<P>>::constructDeletedValue(value);
158 }
159};
160
161template<> struct HashTraits<String> : SimpleClassHashTraits<String> {
162 static const bool hasIsEmptyValueFunction = true;
163 static bool isEmptyValue(const String&);
164
165 static void customDeleteBucket(String&);
166};
167
168// This struct template is an implementation detail of the isHashTraitsEmptyValue function,
169// which selects either the emptyValue function or the isEmptyValue function to check for empty values.
170template<typename Traits, bool hasEmptyValueFunction> struct HashTraitsEmptyValueChecker;
171template<typename Traits> struct HashTraitsEmptyValueChecker<Traits, true> {
172 template<typename T> static bool isEmptyValue(const T& value) { return Traits::isEmptyValue(value); }
173};
174template<typename Traits> struct HashTraitsEmptyValueChecker<Traits, false> {
175 template<typename T> static bool isEmptyValue(const T& value) { return value == Traits::emptyValue(); }
176};
177template<typename Traits, typename T> inline bool isHashTraitsEmptyValue(const T& value)
178{
179 return HashTraitsEmptyValueChecker<Traits, Traits::hasIsEmptyValueFunction>::isEmptyValue(value);
180}
181
182template<typename Traits, typename T>
183struct HashTraitHasCustomDelete {
184 static T& bucketArg;
185 template<typename X> static std::true_type TestHasCustomDelete(X*, decltype(X::customDeleteBucket(bucketArg))* = nullptr);
186 static std::false_type TestHasCustomDelete(...);
187 typedef decltype(TestHasCustomDelete(static_cast<Traits*>(nullptr))) ResultType;
188 static const bool value = ResultType::value;
189};
190
191template<typename Traits, typename T>
192typename std::enable_if<HashTraitHasCustomDelete<Traits, T>::value>::type
193hashTraitsDeleteBucket(T& value)
194{
195 Traits::customDeleteBucket(value);
196}
197
198template<typename Traits, typename T>
199typename std::enable_if<!HashTraitHasCustomDelete<Traits, T>::value>::type
200hashTraitsDeleteBucket(T& value)
201{
202 value.~T();
203 Traits::constructDeletedValue(value);
204}
205
206template<typename FirstTraitsArg, typename SecondTraitsArg>
207struct PairHashTraits : GenericHashTraits<std::pair<typename FirstTraitsArg::TraitType, typename SecondTraitsArg::TraitType>> {
208 typedef FirstTraitsArg FirstTraits;
209 typedef SecondTraitsArg SecondTraits;
210 typedef std::pair<typename FirstTraits::TraitType, typename SecondTraits::TraitType> TraitType;
211 typedef std::pair<typename FirstTraits::EmptyValueType, typename SecondTraits::EmptyValueType> EmptyValueType;
212
213 static const bool emptyValueIsZero = FirstTraits::emptyValueIsZero && SecondTraits::emptyValueIsZero;
214 static EmptyValueType emptyValue() { return std::make_pair(FirstTraits::emptyValue(), SecondTraits::emptyValue()); }
215
216 static const unsigned minimumTableSize = FirstTraits::minimumTableSize;
217
218 static void constructDeletedValue(TraitType& slot) { FirstTraits::constructDeletedValue(slot.first); }
219 static bool isDeletedValue(const TraitType& value) { return FirstTraits::isDeletedValue(value.first); }
220};
221
222template<typename First, typename Second>
223struct HashTraits<std::pair<First, Second>> : public PairHashTraits<HashTraits<First>, HashTraits<Second>> { };
224
225template<typename KeyTypeArg, typename ValueTypeArg>
226struct KeyValuePair {
227 typedef KeyTypeArg KeyType;
228
229 KeyValuePair()
230 {
231 }
232
233 template<typename K, typename V>
234 KeyValuePair(K&& key, V&& value)
235 : key(std::forward<K>(key))
236 , value(std::forward<V>(value))
237 {
238 }
239
240 template <typename OtherKeyType, typename OtherValueType>
241 KeyValuePair(KeyValuePair<OtherKeyType, OtherValueType>&& other)
242 : key(std::forward<OtherKeyType>(other.key))
243 , value(std::forward<OtherValueType>(other.value))
244 {
245 }
246
247 KeyTypeArg key;
248 ValueTypeArg value;
249};
250
251template<typename KeyTraitsArg, typename ValueTraitsArg>
252struct KeyValuePairHashTraits : GenericHashTraits<KeyValuePair<typename KeyTraitsArg::TraitType, typename ValueTraitsArg::TraitType>> {
253 typedef KeyTraitsArg KeyTraits;
254 typedef ValueTraitsArg ValueTraits;
255 typedef KeyValuePair<typename KeyTraits::TraitType, typename ValueTraits::TraitType> TraitType;
256 typedef KeyValuePair<typename KeyTraits::EmptyValueType, typename ValueTraits::EmptyValueType> EmptyValueType;
257 typedef typename ValueTraitsArg::TraitType ValueType;
258
259 static const bool emptyValueIsZero = KeyTraits::emptyValueIsZero && ValueTraits::emptyValueIsZero;
260 static EmptyValueType emptyValue() { return KeyValuePair<typename KeyTraits::EmptyValueType, typename ValueTraits::EmptyValueType>(KeyTraits::emptyValue(), ValueTraits::emptyValue()); }
261
262 static const unsigned minimumTableSize = KeyTraits::minimumTableSize;
263
264 static void constructDeletedValue(TraitType& slot) { KeyTraits::constructDeletedValue(slot.key); }
265 static bool isDeletedValue(const TraitType& value) { return KeyTraits::isDeletedValue(value.key); }
266
267 static void customDeleteBucket(TraitType& value)
268 {
269 static_assert(std::is_trivially_destructible<KeyValuePair<int, int>>::value,
270 "The wrapper itself has to be trivially destructible for customDeleteBucket() to make sense, since we do not destruct the wrapper itself.");
271
272 hashTraitsDeleteBucket<KeyTraits>(value.key);
273 value.value.~ValueType();
274 }
275};
276
277template<typename Key, typename Value>
278struct HashTraits<KeyValuePair<Key, Value>> : public KeyValuePairHashTraits<HashTraits<Key>, HashTraits<Value>> { };
279
280template<typename T>
281struct NullableHashTraits : public HashTraits<T> {
282 static const bool emptyValueIsZero = false;
283 static T emptyValue() { return reinterpret_cast<T>(1); }
284};
285
286// Useful for classes that want complete control over what is empty and what is deleted,
287// and how to construct both.
288template<typename T>
289struct CustomHashTraits : public GenericHashTraits<T> {
290 static const bool emptyValueIsZero = false;
291 static const bool hasIsEmptyValueFunction = true;
292
293 static void constructDeletedValue(T& slot)
294 {
295 new (NotNull, std::addressof(slot)) T(T::DeletedValue);
296 }
297
298 static bool isDeletedValue(const T& value)
299 {
300 return value.isDeletedValue();
301 }
302
303 static T emptyValue()
304 {
305 return T(T::EmptyValue);
306 }
307
308 static bool isEmptyValue(const T& value)
309 {
310 return value.isEmptyValue();
311 }
312};
313
314} // namespace WTF
315
316using WTF::HashTraits;
317using WTF::PairHashTraits;
318using WTF::NullableHashTraits;
319using WTF::SimpleClassHashTraits;
320
321#endif // WTF_HashTraits_h
322