| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2006, 2007, 2008, 2012, 2013 Apple Inc. All rights reserved | 2 * Copyright (C) 2006, 2007, 2008, 2012, 2013 Apple Inc. All rights reserved |
| 3 * Copyright (C) Research In Motion Limited 2009. All rights reserved. | 3 * Copyright (C) Research In Motion Limited 2009. All rights reserved. |
| 4 * | 4 * |
| 5 * This library is free software; you can redistribute it and/or | 5 * This library is free software; you can redistribute it and/or |
| 6 * modify it under the terms of the GNU Library General Public | 6 * modify it under the terms of the GNU Library General Public |
| 7 * License as published by the Free Software Foundation; either | 7 * License as published by the Free Software Foundation; either |
| 8 * version 2 of the License, or (at your option) any later version. | 8 * version 2 of the License, or (at your option) any later version. |
| 9 * | 9 * |
| 10 * This library is distributed in the hope that it will be useful, | 10 * This library is distributed in the hope that it will be useful, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #ifndef StringHash_h | 22 #ifndef StringHash_h |
| 23 #define StringHash_h | 23 #define StringHash_h |
| 24 | 24 |
| 25 #include "wtf/Allocator.h" | 25 #include "wtf/Allocator.h" |
| 26 #include "wtf/HashTraits.h" | 26 #include "wtf/HashTraits.h" |
| 27 #include "wtf/StringHasher.h" | 27 #include "wtf/StringHasher.h" |
| 28 #include "wtf/text/AtomicString.h" | 28 #include "wtf/text/AtomicString.h" |
| 29 | 29 |
| 30 namespace WTF { | 30 namespace WTF { |
| 31 | 31 |
| 32 inline bool HashTraits<String>::isEmptyValue(const String& value) | 32 inline bool HashTraits<String>::isEmptyValue(const String& value) { |
| 33 { | 33 return value.isNull(); |
| 34 return value.isNull(); | |
| 35 } | 34 } |
| 36 | 35 |
| 37 // The hash() functions on StringHash and CaseFoldingHash do not support null | 36 // The hash() functions on StringHash and CaseFoldingHash do not support null |
| 38 // strings. get(), contains(), and add() on HashMap<String,..., StringHash> | 37 // strings. get(), contains(), and add() on HashMap<String,..., StringHash> |
| 39 // cause a null-pointer dereference when passed null strings. | 38 // cause a null-pointer dereference when passed null strings. |
| 40 | 39 |
| 41 // FIXME: We should really figure out a way to put the computeHash function | 40 // FIXME: We should really figure out a way to put the computeHash function |
| 42 // that's currently a member function of StringImpl into this file so we can be | 41 // that's currently a member function of StringImpl into this file so we can be |
| 43 // a little closer to having all the nearly-identical hash functions in one | 42 // a little closer to having all the nearly-identical hash functions in one |
| 44 // place. | 43 // place. |
| 45 | 44 |
| 46 struct StringHash { | 45 struct StringHash { |
| 47 STATIC_ONLY(StringHash); | 46 STATIC_ONLY(StringHash); |
| 48 static unsigned hash(StringImpl* key) { return key->hash(); } | 47 static unsigned hash(StringImpl* key) { return key->hash(); } |
| 49 static inline bool equal(const StringImpl* a, const StringImpl* b) | 48 static inline bool equal(const StringImpl* a, const StringImpl* b) { |
| 50 { | 49 return equalNonNull(a, b); |
| 51 return equalNonNull(a, b); | 50 } |
| 52 } | |
| 53 | 51 |
| 54 static unsigned hash(const RefPtr<StringImpl>& key) { return key->hash(); } | 52 static unsigned hash(const RefPtr<StringImpl>& key) { return key->hash(); } |
| 55 static bool equal(const RefPtr<StringImpl>& a, const RefPtr<StringImpl>& b) | 53 static bool equal(const RefPtr<StringImpl>& a, const RefPtr<StringImpl>& b) { |
| 56 { | 54 return equal(a.get(), b.get()); |
| 57 return equal(a.get(), b.get()); | 55 } |
| 58 } | |
| 59 | 56 |
| 60 static unsigned hash(const String& key) { return key.impl()->hash(); } | 57 static unsigned hash(const String& key) { return key.impl()->hash(); } |
| 61 static bool equal(const String& a, const String& b) | 58 static bool equal(const String& a, const String& b) { |
| 62 { | 59 return equal(a.impl(), b.impl()); |
| 63 return equal(a.impl(), b.impl()); | 60 } |
| 64 } | |
| 65 | 61 |
| 66 static const bool safeToCompareToEmptyOrDeleted = false; | 62 static const bool safeToCompareToEmptyOrDeleted = false; |
| 67 }; | 63 }; |
| 68 | 64 |
| 69 class CaseFoldingHash { | 65 class CaseFoldingHash { |
| 70 STATIC_ONLY(CaseFoldingHash); | 66 STATIC_ONLY(CaseFoldingHash); |
| 71 public: | |
| 72 static unsigned hash(const UChar* data, unsigned length) | |
| 73 { | |
| 74 return StringHasher::computeHashAndMaskTop8Bits<UChar, foldCase<UChar>>(
data, length); | |
| 75 } | |
| 76 | 67 |
| 77 static unsigned hash(StringImpl* str) | 68 public: |
| 78 { | 69 static unsigned hash(const UChar* data, unsigned length) { |
| 79 if (str->is8Bit()) | 70 return StringHasher::computeHashAndMaskTop8Bits<UChar, foldCase<UChar>>( |
| 80 return hash(str->characters8(), str->length()); | 71 data, length); |
| 81 return hash(str->characters16(), str->length()); | 72 } |
| 82 } | |
| 83 | 73 |
| 84 static unsigned hash(const LChar* data, unsigned length) | 74 static unsigned hash(StringImpl* str) { |
| 85 { | 75 if (str->is8Bit()) |
| 86 return StringHasher::computeHashAndMaskTop8Bits<LChar, foldCase<LChar>>(
data, length); | 76 return hash(str->characters8(), str->length()); |
| 87 } | 77 return hash(str->characters16(), str->length()); |
| 78 } |
| 88 | 79 |
| 89 static inline unsigned hash(const char* data, unsigned length) | 80 static unsigned hash(const LChar* data, unsigned length) { |
| 90 { | 81 return StringHasher::computeHashAndMaskTop8Bits<LChar, foldCase<LChar>>( |
| 91 return CaseFoldingHash::hash(reinterpret_cast<const LChar*>(data), lengt
h); | 82 data, length); |
| 92 } | 83 } |
| 93 | 84 |
| 94 static inline bool equal(const StringImpl* a, const StringImpl* b) | 85 static inline unsigned hash(const char* data, unsigned length) { |
| 95 { | 86 return CaseFoldingHash::hash(reinterpret_cast<const LChar*>(data), length); |
| 96 return equalIgnoringCaseNonNull(a, b); | 87 } |
| 97 } | |
| 98 | 88 |
| 99 static unsigned hash(const RefPtr<StringImpl>& key) | 89 static inline bool equal(const StringImpl* a, const StringImpl* b) { |
| 100 { | 90 return equalIgnoringCaseNonNull(a, b); |
| 101 return hash(key.get()); | 91 } |
| 102 } | |
| 103 | 92 |
| 104 static bool equal(const RefPtr<StringImpl>& a, const RefPtr<StringImpl>& b) | 93 static unsigned hash(const RefPtr<StringImpl>& key) { |
| 105 { | 94 return hash(key.get()); |
| 106 return equal(a.get(), b.get()); | 95 } |
| 107 } | |
| 108 | 96 |
| 109 static unsigned hash(const String& key) | 97 static bool equal(const RefPtr<StringImpl>& a, const RefPtr<StringImpl>& b) { |
| 110 { | 98 return equal(a.get(), b.get()); |
| 111 return hash(key.impl()); | 99 } |
| 112 } | |
| 113 static unsigned hash(const AtomicString& key) | |
| 114 { | |
| 115 return hash(key.impl()); | |
| 116 } | |
| 117 static bool equal(const String& a, const String& b) | |
| 118 { | |
| 119 return equal(a.impl(), b.impl()); | |
| 120 } | |
| 121 static bool equal(const AtomicString& a, const AtomicString& b) | |
| 122 { | |
| 123 return (a == b) || equal(a.impl(), b.impl()); | |
| 124 } | |
| 125 | 100 |
| 126 static const bool safeToCompareToEmptyOrDeleted = false; | 101 static unsigned hash(const String& key) { return hash(key.impl()); } |
| 102 static unsigned hash(const AtomicString& key) { return hash(key.impl()); } |
| 103 static bool equal(const String& a, const String& b) { |
| 104 return equal(a.impl(), b.impl()); |
| 105 } |
| 106 static bool equal(const AtomicString& a, const AtomicString& b) { |
| 107 return (a == b) || equal(a.impl(), b.impl()); |
| 108 } |
| 127 | 109 |
| 128 private: | 110 static const bool safeToCompareToEmptyOrDeleted = false; |
| 129 // Private so no one uses this in the belief that it will return the | 111 |
| 130 // correctly-folded code point in all cases (see comment below). | 112 private: |
| 131 template<typename T> static inline UChar foldCase(T ch) | 113 // Private so no one uses this in the belief that it will return the |
| 132 { | 114 // correctly-folded code point in all cases (see comment below). |
| 133 if (std::is_same<T, LChar>::value) | 115 template <typename T> |
| 134 return StringImpl::latin1CaseFoldTable[ch]; | 116 static inline UChar foldCase(T ch) { |
| 135 // It's possible for WTF::Unicode::foldCase() to return a 32-bit value | 117 if (std::is_same<T, LChar>::value) |
| 136 // that's not representable as a UChar. However, since this is rare and | 118 return StringImpl::latin1CaseFoldTable[ch]; |
| 137 // deterministic, and the result of this is merely used for hashing, go | 119 // It's possible for WTF::Unicode::foldCase() to return a 32-bit value |
| 138 // ahead and clamp the value. | 120 // that's not representable as a UChar. However, since this is rare and |
| 139 return static_cast<UChar>(WTF::Unicode::foldCase(ch)); | 121 // deterministic, and the result of this is merely used for hashing, go |
| 140 } | 122 // ahead and clamp the value. |
| 123 return static_cast<UChar>(WTF::Unicode::foldCase(ch)); |
| 124 } |
| 141 }; | 125 }; |
| 142 | 126 |
| 143 // This hash can be used in cases where the key is a hash of a string, but we | 127 // This hash can be used in cases where the key is a hash of a string, but we |
| 144 // don't want to store the string. It's not really specific to string hashing, | 128 // don't want to store the string. It's not really specific to string hashing, |
| 145 // but all our current uses of it are for strings. | 129 // but all our current uses of it are for strings. |
| 146 struct AlreadyHashed : IntHash<unsigned> { | 130 struct AlreadyHashed : IntHash<unsigned> { |
| 147 STATIC_ONLY(AlreadyHashed); | 131 STATIC_ONLY(AlreadyHashed); |
| 148 static unsigned hash(unsigned key) { return key; } | 132 static unsigned hash(unsigned key) { return key; } |
| 149 | 133 |
| 150 // To use a hash value as a key for a hash table, we need to eliminate the | 134 // To use a hash value as a key for a hash table, we need to eliminate the |
| 151 // "deleted" value, which is negative one. That could be done by changing | 135 // "deleted" value, which is negative one. That could be done by changing |
| 152 // the string hash function to never generate negative one, but this works | 136 // the string hash function to never generate negative one, but this works |
| 153 // and is still relatively efficient. | 137 // and is still relatively efficient. |
| 154 static unsigned avoidDeletedValue(unsigned hash) | 138 static unsigned avoidDeletedValue(unsigned hash) { |
| 155 { | 139 ASSERT(hash); |
| 156 ASSERT(hash); | 140 unsigned newHash = hash | (!(hash + 1) << 31); |
| 157 unsigned newHash = hash | (!(hash + 1) << 31); | 141 ASSERT(newHash); |
| 158 ASSERT(newHash); | 142 ASSERT(newHash != 0xFFFFFFFF); |
| 159 ASSERT(newHash != 0xFFFFFFFF); | 143 return newHash; |
| 160 return newHash; | 144 } |
| 161 } | |
| 162 }; | 145 }; |
| 163 | |
| 164 } | 146 } |
| 165 | 147 |
| 166 using WTF::AlreadyHashed; | 148 using WTF::AlreadyHashed; |
| 167 using WTF::CaseFoldingHash; | 149 using WTF::CaseFoldingHash; |
| 168 using WTF::StringHash; | 150 using WTF::StringHash; |
| 169 | 151 |
| 170 #endif | 152 #endif |
| OLD | NEW |