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> class OwnPtr;
34template<typename T> class PassOwnPtr;
35
36template<typename T> struct HashTraits;
37
38template<bool isInteger, typename T> struct GenericHashTraitsBase;
39
40template<typename T> struct GenericHashTraitsBase<false, T> {
41    // The emptyValueIsZero flag is used to optimize allocation of empty hash tables with zeroed memory.
42    static const bool emptyValueIsZero = false;
43
44    // The hasIsEmptyValueFunction flag allows the hash table to automatically generate code to check
45    // for the empty value when it can be done with the equality operator, but allows custom functions
46    // for cases like String that need them.
47    static const bool hasIsEmptyValueFunction = false;
48
49    // The starting table size. Can be overridden when we know beforehand that
50    // a hash table will have at least N entries.
51    static const int minimumTableSize = 8;
52};
53
54// Default integer traits disallow both 0 and -1 as keys (max value instead of -1 for unsigned).
55template<typename T> struct GenericHashTraitsBase<true, T> : GenericHashTraitsBase<false, T> {
56    static const bool emptyValueIsZero = true;
57    static void constructDeletedValue(T& slot) { slot = static_cast<T>(-1); }
58    static bool isDeletedValue(T value) { return value == static_cast<T>(-1); }
59};
60
61template<typename T> struct GenericHashTraits : GenericHashTraitsBase<std::is_integral<T>::value, T> {
62    typedef T TraitType;
63    typedef T EmptyValueType;
64
65    static T emptyValue() { return T(); }
66
67    // Type for return value of functions that do not transfer ownership, such as get.
68    typedef T PeekType;
69    static PeekType peek(const T& value) { return value; }
70    static T& peek(T& value) { return value; } // Overloaded to avoid copying of non-temporary values.
71};
72
73template<typename T> struct HashTraits : GenericHashTraits<T> { };
74
75template<typename T> struct FloatHashTraits : GenericHashTraits<T> {
76    static T emptyValue() { return std::numeric_limits<T>::infinity(); }
77    static void constructDeletedValue(T& slot) { slot = -std::numeric_limits<T>::infinity(); }
78    static bool isDeletedValue(T value) { return value == -std::numeric_limits<T>::infinity(); }
79};
80
81template<> struct HashTraits<float> : FloatHashTraits<float> { };
82template<> struct HashTraits<double> : FloatHashTraits<double> { };
83
84// Default unsigned traits disallow both 0 and max as keys -- use these traits to allow zero and disallow max - 1.
85template<typename T> struct UnsignedWithZeroKeyHashTraits : GenericHashTraits<T> {
86    static const bool emptyValueIsZero = false;
87    static T emptyValue() { return std::numeric_limits<T>::max(); }
88    static void constructDeletedValue(T& slot) { slot = std::numeric_limits<T>::max() - 1; }
89    static bool isDeletedValue(T value) { return value == std::numeric_limits<T>::max() - 1; }
90};
91
92template<typename P> struct HashTraits<P*> : GenericHashTraits<P*> {
93    static const bool emptyValueIsZero = true;
94    static void constructDeletedValue(P*& slot) { slot = reinterpret_cast<P*>(-1); }
95    static bool isDeletedValue(P* value) { return value == reinterpret_cast<P*>(-1); }
96};
97
98template<typename T> struct SimpleClassHashTraits : GenericHashTraits<T> {
99    static const bool emptyValueIsZero = true;
100    static void constructDeletedValue(T& slot) { new (NotNull, &slot) T(HashTableDeletedValue); }
101    static bool isDeletedValue(const T& value) { return value.isHashTableDeletedValue(); }
102};
103
104template<typename T, typename Deleter> struct HashTraits<std::unique_ptr<T, Deleter>> : SimpleClassHashTraits<std::unique_ptr<T, Deleter>> {
105    typedef std::nullptr_t EmptyValueType;
106    static EmptyValueType emptyValue() { return nullptr; }
107
108    typedef T* PeekType;
109    static T* peek(const std::unique_ptr<T, Deleter>& value) { return value.get(); }
110    static T* peek(std::nullptr_t) { return nullptr; }
111};
112
113template<typename T> struct HashTraits<OwnPtr<T>> : SimpleClassHashTraits<OwnPtr<T>> {
114    typedef std::nullptr_t EmptyValueType;
115    static EmptyValueType emptyValue() { return nullptr; }
116
117    typedef T* PeekType;
118    static T* peek(const OwnPtr<T>& value) { return value.get(); }
119    static T* peek(std::nullptr_t) { return nullptr; }
120};
121
122template<typename P> struct HashTraits<RefPtr<P>> : SimpleClassHashTraits<RefPtr<P>> {
123    static P* emptyValue() { return 0; }
124
125    typedef P* PeekType;
126    static PeekType peek(const RefPtr<P>& value) { return value.get(); }
127    static PeekType peek(P* value) { return value; }
128};
129
130template<> struct HashTraits<String> : SimpleClassHashTraits<String> {
131    static const bool hasIsEmptyValueFunction = true;
132    static bool isEmptyValue(const String&);
133};
134
135// This struct template is an implementation detail of the isHashTraitsEmptyValue function,
136// which selects either the emptyValue function or the isEmptyValue function to check for empty values.
137template<typename Traits, bool hasEmptyValueFunction> struct HashTraitsEmptyValueChecker;
138template<typename Traits> struct HashTraitsEmptyValueChecker<Traits, true> {
139    template<typename T> static bool isEmptyValue(const T& value) { return Traits::isEmptyValue(value); }
140};
141template<typename Traits> struct HashTraitsEmptyValueChecker<Traits, false> {
142    template<typename T> static bool isEmptyValue(const T& value) { return value == Traits::emptyValue(); }
143};
144template<typename Traits, typename T> inline bool isHashTraitsEmptyValue(const T& value)
145{
146    return HashTraitsEmptyValueChecker<Traits, Traits::hasIsEmptyValueFunction>::isEmptyValue(value);
147}
148
149template<typename FirstTraitsArg, typename SecondTraitsArg>
150struct PairHashTraits : GenericHashTraits<std::pair<typename FirstTraitsArg::TraitType, typename SecondTraitsArg::TraitType>> {
151    typedef FirstTraitsArg FirstTraits;
152    typedef SecondTraitsArg SecondTraits;
153    typedef std::pair<typename FirstTraits::TraitType, typename SecondTraits::TraitType> TraitType;
154    typedef std::pair<typename FirstTraits::EmptyValueType, typename SecondTraits::EmptyValueType> EmptyValueType;
155
156    static const bool emptyValueIsZero = FirstTraits::emptyValueIsZero && SecondTraits::emptyValueIsZero;
157    static EmptyValueType emptyValue() { return std::make_pair(FirstTraits::emptyValue(), SecondTraits::emptyValue()); }
158
159    static const int minimumTableSize = FirstTraits::minimumTableSize;
160
161    static void constructDeletedValue(TraitType& slot) { FirstTraits::constructDeletedValue(slot.first); }
162    static bool isDeletedValue(const TraitType& value) { return FirstTraits::isDeletedValue(value.first); }
163};
164
165template<typename First, typename Second>
166struct HashTraits<std::pair<First, Second>> : public PairHashTraits<HashTraits<First>, HashTraits<Second>> { };
167
168template<typename KeyTypeArg, typename ValueTypeArg>
169struct KeyValuePair {
170    typedef KeyTypeArg KeyType;
171
172    KeyValuePair()
173    {
174    }
175
176    template<typename K, typename V>
177    KeyValuePair(K&& key, V&& value)
178        : key(std::forward<K>(key))
179        , value(std::forward<V>(value))
180    {
181    }
182
183    template <typename OtherKeyType, typename OtherValueType>
184    KeyValuePair(KeyValuePair<OtherKeyType, OtherValueType>&& other)
185        : key(std::forward<OtherKeyType>(other.key))
186        , value(std::forward<OtherValueType>(other.value))
187    {
188    }
189
190    KeyTypeArg key;
191    ValueTypeArg value;
192};
193
194template<typename KeyTraitsArg, typename ValueTraitsArg>
195struct KeyValuePairHashTraits : GenericHashTraits<KeyValuePair<typename KeyTraitsArg::TraitType, typename ValueTraitsArg::TraitType>> {
196    typedef KeyTraitsArg KeyTraits;
197    typedef ValueTraitsArg ValueTraits;
198    typedef KeyValuePair<typename KeyTraits::TraitType, typename ValueTraits::TraitType> TraitType;
199    typedef KeyValuePair<typename KeyTraits::EmptyValueType, typename ValueTraits::EmptyValueType> EmptyValueType;
200
201    static const bool emptyValueIsZero = KeyTraits::emptyValueIsZero && ValueTraits::emptyValueIsZero;
202    static EmptyValueType emptyValue() { return KeyValuePair<typename KeyTraits::EmptyValueType, typename ValueTraits::EmptyValueType>(KeyTraits::emptyValue(), ValueTraits::emptyValue()); }
203
204    static const int minimumTableSize = KeyTraits::minimumTableSize;
205
206    static void constructDeletedValue(TraitType& slot) { KeyTraits::constructDeletedValue(slot.key); }
207    static bool isDeletedValue(const TraitType& value) { return KeyTraits::isDeletedValue(value.key); }
208};
209
210template<typename Key, typename Value>
211struct HashTraits<KeyValuePair<Key, Value>> : public KeyValuePairHashTraits<HashTraits<Key>, HashTraits<Value>> { };
212
213template<typename T>
214struct NullableHashTraits : public HashTraits<T> {
215    static const bool emptyValueIsZero = false;
216    static T emptyValue() { return reinterpret_cast<T>(1); }
217};
218
219// Useful for classes that want complete control over what is empty and what is deleted,
220// and how to construct both.
221template<typename T>
222struct CustomHashTraits : public GenericHashTraits<T> {
223    static const bool emptyValueIsZero = false;
224    static const bool hasIsEmptyValueFunction = true;
225
226    static void constructDeletedValue(T& slot)
227    {
228        new (NotNull, &slot) T(T::DeletedValue);
229    }
230
231    static bool isDeletedValue(const T& value)
232    {
233        return value.isDeletedValue();
234    }
235
236    static T emptyValue()
237    {
238        return T(T::EmptyValue);
239    }
240
241    static bool isEmptyValue(const T& value)
242    {
243        return value.isEmptyValue();
244    }
245};
246
247} // namespace WTF
248
249using WTF::HashTraits;
250using WTF::PairHashTraits;
251using WTF::NullableHashTraits;
252using WTF::SimpleClassHashTraits;
253
254#endif // WTF_HashTraits_h
255