]>
Commit | Line | Data |
---|---|---|
b37bf2e1 | 1 | /* |
9dae56ea | 2 | * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. |
b37bf2e1 A |
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 | ||
b37bf2e1 | 24 | #include "HashFunctions.h" |
ba379fdc | 25 | #include "TypeTraits.h" |
b37bf2e1 A |
26 | #include <utility> |
27 | #include <limits> | |
28 | ||
29 | namespace WTF { | |
30 | ||
31 | using std::pair; | |
32 | using std::make_pair; | |
33 | ||
b37bf2e1 A |
34 | template<typename T> struct HashTraits; |
35 | ||
36 | template<bool isInteger, typename T> struct GenericHashTraitsBase; | |
9dae56ea | 37 | |
b37bf2e1 | 38 | template<typename T> struct GenericHashTraitsBase<false, T> { |
b37bf2e1 A |
39 | static const bool emptyValueIsZero = false; |
40 | static const bool needsDestruction = true; | |
41 | }; | |
42 | ||
9dae56ea A |
43 | // Default integer traits disallow both 0 and -1 as keys (max value instead of -1 for unsigned). |
44 | template<typename T> struct GenericHashTraitsBase<true, T> { | |
45 | static const bool emptyValueIsZero = true; | |
46 | static const bool needsDestruction = false; | |
47 | static void constructDeletedValue(T& slot) { slot = static_cast<T>(-1); } | |
48 | static bool isDeletedValue(T value) { return value == static_cast<T>(-1); } | |
49 | }; | |
50 | ||
b37bf2e1 | 51 | template<typename T> struct GenericHashTraits : GenericHashTraitsBase<IsInteger<T>::value, T> { |
9dae56ea | 52 | typedef T TraitType; |
b37bf2e1 | 53 | static T emptyValue() { return T(); } |
b37bf2e1 A |
54 | }; |
55 | ||
56 | template<typename T> struct HashTraits : GenericHashTraits<T> { }; | |
57 | ||
9dae56ea A |
58 | template<typename T> struct FloatHashTraits : GenericHashTraits<T> { |
59 | static const bool needsDestruction = false; | |
b37bf2e1 | 60 | static T emptyValue() { return std::numeric_limits<T>::infinity(); } |
9dae56ea A |
61 | static void constructDeletedValue(T& slot) { slot = -std::numeric_limits<T>::infinity(); } |
62 | static bool isDeletedValue(T value) { return value == -std::numeric_limits<T>::infinity(); } | |
63 | }; | |
64 | ||
65 | template<> struct HashTraits<float> : FloatHashTraits<float> { }; | |
66 | template<> struct HashTraits<double> : FloatHashTraits<double> { }; | |
67 | ||
68 | // Default unsigned traits disallow both 0 and max as keys -- use these traits to allow zero and disallow max - 1. | |
69 | template<typename T> struct UnsignedWithZeroKeyHashTraits : GenericHashTraits<T> { | |
b37bf2e1 A |
70 | static const bool emptyValueIsZero = false; |
71 | static const bool needsDestruction = false; | |
9dae56ea A |
72 | static T emptyValue() { return std::numeric_limits<T>::max(); } |
73 | static void constructDeletedValue(T& slot) { slot = std::numeric_limits<T>::max() - 1; } | |
74 | static bool isDeletedValue(T value) { return value == std::numeric_limits<T>::max() - 1; } | |
b37bf2e1 A |
75 | }; |
76 | ||
77 | template<typename P> struct HashTraits<P*> : GenericHashTraits<P*> { | |
b37bf2e1 A |
78 | static const bool emptyValueIsZero = true; |
79 | static const bool needsDestruction = false; | |
9dae56ea A |
80 | static void constructDeletedValue(P*& slot) { slot = reinterpret_cast<P*>(-1); } |
81 | static bool isDeletedValue(P* value) { return value == reinterpret_cast<P*>(-1); } | |
b37bf2e1 A |
82 | }; |
83 | ||
14957cd0 | 84 | template<typename T> struct SimpleClassHashTraits : GenericHashTraits<T> { |
b37bf2e1 | 85 | static const bool emptyValueIsZero = true; |
14957cd0 A |
86 | static void constructDeletedValue(T& slot) { new (&slot) T(HashTableDeletedValue); } |
87 | static bool isDeletedValue(const T& value) { return value.isHashTableDeletedValue(); } | |
b37bf2e1 A |
88 | }; |
89 | ||
14957cd0 A |
90 | template<typename P> struct HashTraits<RefPtr<P> > : SimpleClassHashTraits<RefPtr<P> > { }; |
91 | ||
b37bf2e1 A |
92 | // special traits for pairs, helpful for their use in HashMap implementation |
93 | ||
b37bf2e1 | 94 | template<typename FirstTraitsArg, typename SecondTraitsArg> |
9dae56ea | 95 | struct PairHashTraits : GenericHashTraits<pair<typename FirstTraitsArg::TraitType, typename SecondTraitsArg::TraitType> > { |
b37bf2e1 A |
96 | typedef FirstTraitsArg FirstTraits; |
97 | typedef SecondTraitsArg SecondTraits; | |
98 | typedef pair<typename FirstTraits::TraitType, typename SecondTraits::TraitType> TraitType; | |
99 | ||
b37bf2e1 | 100 | static const bool emptyValueIsZero = FirstTraits::emptyValueIsZero && SecondTraits::emptyValueIsZero; |
9dae56ea | 101 | static TraitType emptyValue() { return make_pair(FirstTraits::emptyValue(), SecondTraits::emptyValue()); } |
b37bf2e1 A |
102 | |
103 | static const bool needsDestruction = FirstTraits::needsDestruction || SecondTraits::needsDestruction; | |
104 | ||
9dae56ea A |
105 | static void constructDeletedValue(TraitType& slot) { FirstTraits::constructDeletedValue(slot.first); } |
106 | static bool isDeletedValue(const TraitType& value) { return FirstTraits::isDeletedValue(value.first); } | |
b37bf2e1 A |
107 | }; |
108 | ||
109 | template<typename First, typename Second> | |
110 | struct HashTraits<pair<First, Second> > : public PairHashTraits<HashTraits<First>, HashTraits<Second> > { }; | |
111 | ||
b37bf2e1 A |
112 | } // namespace WTF |
113 | ||
114 | using WTF::HashTraits; | |
115 | using WTF::PairHashTraits; | |
116 | ||
117 | #endif // WTF_HashTraits_h |