security/sandbox/chromium/base/containers/hash_tables.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4 //
michael@0 5
michael@0 6 //
michael@0 7 // Deal with the differences between Microsoft and GNU implemenations
michael@0 8 // of hash_map. Allows all platforms to use |base::hash_map| and
michael@0 9 // |base::hash_set|.
michael@0 10 // eg:
michael@0 11 // base::hash_map<int> my_map;
michael@0 12 // base::hash_set<int> my_set;
michael@0 13 //
michael@0 14 // NOTE: It is an explicit non-goal of this class to provide a generic hash
michael@0 15 // function for pointers. If you want to hash a pointers to a particular class,
michael@0 16 // please define the template specialization elsewhere (for example, in its
michael@0 17 // header file) and keep it specific to just pointers to that class. This is
michael@0 18 // because identity hashes are not desirable for all types that might show up
michael@0 19 // in containers as pointers.
michael@0 20
michael@0 21 #ifndef BASE_CONTAINERS_HASH_TABLES_H_
michael@0 22 #define BASE_CONTAINERS_HASH_TABLES_H_
michael@0 23
michael@0 24 #include <utility>
michael@0 25
michael@0 26 #include "base/basictypes.h"
michael@0 27 #include "base/strings/string16.h"
michael@0 28 #include "build/build_config.h"
michael@0 29
michael@0 30 #if defined(COMPILER_MSVC)
michael@0 31 #include <hash_map>
michael@0 32 #include <hash_set>
michael@0 33
michael@0 34 #define BASE_HASH_NAMESPACE stdext
michael@0 35
michael@0 36 #elif defined(COMPILER_GCC)
michael@0 37 #if defined(OS_ANDROID)
michael@0 38 #define BASE_HASH_NAMESPACE std
michael@0 39 #else
michael@0 40 #define BASE_HASH_NAMESPACE __gnu_cxx
michael@0 41 #endif
michael@0 42
michael@0 43 // This is a hack to disable the gcc 4.4 warning about hash_map and hash_set
michael@0 44 // being deprecated. We can get rid of this when we upgrade to VS2008 and we
michael@0 45 // can use <tr1/unordered_map> and <tr1/unordered_set>.
michael@0 46 #ifdef __DEPRECATED
michael@0 47 #define CHROME_OLD__DEPRECATED __DEPRECATED
michael@0 48 #undef __DEPRECATED
michael@0 49 #endif
michael@0 50
michael@0 51 #if defined(OS_ANDROID)
michael@0 52 #include <hash_map>
michael@0 53 #include <hash_set>
michael@0 54 #else
michael@0 55 #include <ext/hash_map>
michael@0 56 #include <ext/hash_set>
michael@0 57 #endif
michael@0 58
michael@0 59 #include <string>
michael@0 60
michael@0 61 #ifdef CHROME_OLD__DEPRECATED
michael@0 62 #define __DEPRECATED CHROME_OLD__DEPRECATED
michael@0 63 #undef CHROME_OLD__DEPRECATED
michael@0 64 #endif
michael@0 65
michael@0 66 namespace BASE_HASH_NAMESPACE {
michael@0 67
michael@0 68 #if !defined(OS_ANDROID)
michael@0 69 // The GNU C++ library provides identity hash functions for many integral types,
michael@0 70 // but not for |long long|. This hash function will truncate if |size_t| is
michael@0 71 // narrower than |long long|. This is probably good enough for what we will
michael@0 72 // use it for.
michael@0 73
michael@0 74 #define DEFINE_TRIVIAL_HASH(integral_type) \
michael@0 75 template<> \
michael@0 76 struct hash<integral_type> { \
michael@0 77 std::size_t operator()(integral_type value) const { \
michael@0 78 return static_cast<std::size_t>(value); \
michael@0 79 } \
michael@0 80 }
michael@0 81
michael@0 82 DEFINE_TRIVIAL_HASH(long long);
michael@0 83 DEFINE_TRIVIAL_HASH(unsigned long long);
michael@0 84
michael@0 85 #undef DEFINE_TRIVIAL_HASH
michael@0 86 #endif // !defined(OS_ANDROID)
michael@0 87
michael@0 88 // Implement string hash functions so that strings of various flavors can
michael@0 89 // be used as keys in STL maps and sets. The hash algorithm comes from the
michael@0 90 // GNU C++ library, in <tr1/functional>. It is duplicated here because GCC
michael@0 91 // versions prior to 4.3.2 are unable to compile <tr1/functional> when RTTI
michael@0 92 // is disabled, as it is in our build.
michael@0 93
michael@0 94 #define DEFINE_STRING_HASH(string_type) \
michael@0 95 template<> \
michael@0 96 struct hash<string_type> { \
michael@0 97 std::size_t operator()(const string_type& s) const { \
michael@0 98 std::size_t result = 0; \
michael@0 99 for (string_type::const_iterator i = s.begin(); i != s.end(); ++i) \
michael@0 100 result = (result * 131) + *i; \
michael@0 101 return result; \
michael@0 102 } \
michael@0 103 }
michael@0 104
michael@0 105 DEFINE_STRING_HASH(std::string);
michael@0 106 DEFINE_STRING_HASH(string16);
michael@0 107
michael@0 108 #undef DEFINE_STRING_HASH
michael@0 109
michael@0 110 } // namespace BASE_HASH_NAMESPACE
michael@0 111
michael@0 112 #else // COMPILER
michael@0 113 #error define BASE_HASH_NAMESPACE for your compiler
michael@0 114 #endif // COMPILER
michael@0 115
michael@0 116 namespace base {
michael@0 117 using BASE_HASH_NAMESPACE::hash_map;
michael@0 118 using BASE_HASH_NAMESPACE::hash_multimap;
michael@0 119 using BASE_HASH_NAMESPACE::hash_multiset;
michael@0 120 using BASE_HASH_NAMESPACE::hash_set;
michael@0 121
michael@0 122 // Implement hashing for pairs of at-most 32 bit integer values.
michael@0 123 // When size_t is 32 bits, we turn the 64-bit hash code into 32 bits by using
michael@0 124 // multiply-add hashing. This algorithm, as described in
michael@0 125 // Theorem 4.3.3 of the thesis "Über die Komplexität der Multiplikation in
michael@0 126 // eingeschränkten Branchingprogrammmodellen" by Woelfel, is:
michael@0 127 //
michael@0 128 // h32(x32, y32) = (h64(x32, y32) * rand_odd64 + rand16 * 2^16) % 2^64 / 2^32
michael@0 129 //
michael@0 130 // Contact danakj@chromium.org for any questions.
michael@0 131 inline std::size_t HashInts32(uint32 value1, uint32 value2) {
michael@0 132 uint64 value1_64 = value1;
michael@0 133 uint64 hash64 = (value1_64 << 32) | value2;
michael@0 134
michael@0 135 if (sizeof(std::size_t) >= sizeof(uint64))
michael@0 136 return static_cast<std::size_t>(hash64);
michael@0 137
michael@0 138 uint64 odd_random = 481046412LL << 32 | 1025306955LL;
michael@0 139 uint32 shift_random = 10121U << 16;
michael@0 140
michael@0 141 hash64 = hash64 * odd_random + shift_random;
michael@0 142 std::size_t high_bits = static_cast<std::size_t>(
michael@0 143 hash64 >> (sizeof(uint64) - sizeof(std::size_t)));
michael@0 144 return high_bits;
michael@0 145 }
michael@0 146
michael@0 147 // Implement hashing for pairs of up-to 64-bit integer values.
michael@0 148 // We use the compound integer hash method to produce a 64-bit hash code, by
michael@0 149 // breaking the two 64-bit inputs into 4 32-bit values:
michael@0 150 // http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECTION00832000000000000000
michael@0 151 // Then we reduce our result to 32 bits if required, similar to above.
michael@0 152 inline std::size_t HashInts64(uint64 value1, uint64 value2) {
michael@0 153 uint32 short_random1 = 842304669U;
michael@0 154 uint32 short_random2 = 619063811U;
michael@0 155 uint32 short_random3 = 937041849U;
michael@0 156 uint32 short_random4 = 3309708029U;
michael@0 157
michael@0 158 uint32 value1a = static_cast<uint32>(value1 & 0xffffffff);
michael@0 159 uint32 value1b = static_cast<uint32>((value1 >> 32) & 0xffffffff);
michael@0 160 uint32 value2a = static_cast<uint32>(value2 & 0xffffffff);
michael@0 161 uint32 value2b = static_cast<uint32>((value2 >> 32) & 0xffffffff);
michael@0 162
michael@0 163 uint64 product1 = static_cast<uint64>(value1a) * short_random1;
michael@0 164 uint64 product2 = static_cast<uint64>(value1b) * short_random2;
michael@0 165 uint64 product3 = static_cast<uint64>(value2a) * short_random3;
michael@0 166 uint64 product4 = static_cast<uint64>(value2b) * short_random4;
michael@0 167
michael@0 168 uint64 hash64 = product1 + product2 + product3 + product4;
michael@0 169
michael@0 170 if (sizeof(std::size_t) >= sizeof(uint64))
michael@0 171 return static_cast<std::size_t>(hash64);
michael@0 172
michael@0 173 uint64 odd_random = 1578233944LL << 32 | 194370989LL;
michael@0 174 uint32 shift_random = 20591U << 16;
michael@0 175
michael@0 176 hash64 = hash64 * odd_random + shift_random;
michael@0 177 std::size_t high_bits = static_cast<std::size_t>(
michael@0 178 hash64 >> (sizeof(uint64) - sizeof(std::size_t)));
michael@0 179 return high_bits;
michael@0 180 }
michael@0 181
michael@0 182 #define DEFINE_32BIT_PAIR_HASH(Type1, Type2) \
michael@0 183 inline std::size_t HashPair(Type1 value1, Type2 value2) { \
michael@0 184 return HashInts32(value1, value2); \
michael@0 185 }
michael@0 186
michael@0 187 DEFINE_32BIT_PAIR_HASH(int16, int16);
michael@0 188 DEFINE_32BIT_PAIR_HASH(int16, uint16);
michael@0 189 DEFINE_32BIT_PAIR_HASH(int16, int32);
michael@0 190 DEFINE_32BIT_PAIR_HASH(int16, uint32);
michael@0 191 DEFINE_32BIT_PAIR_HASH(uint16, int16);
michael@0 192 DEFINE_32BIT_PAIR_HASH(uint16, uint16);
michael@0 193 DEFINE_32BIT_PAIR_HASH(uint16, int32);
michael@0 194 DEFINE_32BIT_PAIR_HASH(uint16, uint32);
michael@0 195 DEFINE_32BIT_PAIR_HASH(int32, int16);
michael@0 196 DEFINE_32BIT_PAIR_HASH(int32, uint16);
michael@0 197 DEFINE_32BIT_PAIR_HASH(int32, int32);
michael@0 198 DEFINE_32BIT_PAIR_HASH(int32, uint32);
michael@0 199 DEFINE_32BIT_PAIR_HASH(uint32, int16);
michael@0 200 DEFINE_32BIT_PAIR_HASH(uint32, uint16);
michael@0 201 DEFINE_32BIT_PAIR_HASH(uint32, int32);
michael@0 202 DEFINE_32BIT_PAIR_HASH(uint32, uint32);
michael@0 203
michael@0 204 #undef DEFINE_32BIT_PAIR_HASH
michael@0 205
michael@0 206 #define DEFINE_64BIT_PAIR_HASH(Type1, Type2) \
michael@0 207 inline std::size_t HashPair(Type1 value1, Type2 value2) { \
michael@0 208 return HashInts64(value1, value2); \
michael@0 209 }
michael@0 210
michael@0 211 DEFINE_64BIT_PAIR_HASH(int16, int64);
michael@0 212 DEFINE_64BIT_PAIR_HASH(int16, uint64);
michael@0 213 DEFINE_64BIT_PAIR_HASH(uint16, int64);
michael@0 214 DEFINE_64BIT_PAIR_HASH(uint16, uint64);
michael@0 215 DEFINE_64BIT_PAIR_HASH(int32, int64);
michael@0 216 DEFINE_64BIT_PAIR_HASH(int32, uint64);
michael@0 217 DEFINE_64BIT_PAIR_HASH(uint32, int64);
michael@0 218 DEFINE_64BIT_PAIR_HASH(uint32, uint64);
michael@0 219 DEFINE_64BIT_PAIR_HASH(int64, int16);
michael@0 220 DEFINE_64BIT_PAIR_HASH(int64, uint16);
michael@0 221 DEFINE_64BIT_PAIR_HASH(int64, int32);
michael@0 222 DEFINE_64BIT_PAIR_HASH(int64, uint32);
michael@0 223 DEFINE_64BIT_PAIR_HASH(int64, int64);
michael@0 224 DEFINE_64BIT_PAIR_HASH(int64, uint64);
michael@0 225 DEFINE_64BIT_PAIR_HASH(uint64, int16);
michael@0 226 DEFINE_64BIT_PAIR_HASH(uint64, uint16);
michael@0 227 DEFINE_64BIT_PAIR_HASH(uint64, int32);
michael@0 228 DEFINE_64BIT_PAIR_HASH(uint64, uint32);
michael@0 229 DEFINE_64BIT_PAIR_HASH(uint64, int64);
michael@0 230 DEFINE_64BIT_PAIR_HASH(uint64, uint64);
michael@0 231
michael@0 232 #undef DEFINE_64BIT_PAIR_HASH
michael@0 233 } // namespace base
michael@0 234
michael@0 235 namespace BASE_HASH_NAMESPACE {
michael@0 236
michael@0 237 // Implement methods for hashing a pair of integers, so they can be used as
michael@0 238 // keys in STL containers.
michael@0 239
michael@0 240 #if defined(COMPILER_MSVC)
michael@0 241
michael@0 242 template<typename Type1, typename Type2>
michael@0 243 inline std::size_t hash_value(const std::pair<Type1, Type2>& value) {
michael@0 244 return base::HashPair(value.first, value.second);
michael@0 245 }
michael@0 246
michael@0 247 #elif defined(COMPILER_GCC)
michael@0 248 template<typename Type1, typename Type2>
michael@0 249 struct hash<std::pair<Type1, Type2> > {
michael@0 250 std::size_t operator()(std::pair<Type1, Type2> value) const {
michael@0 251 return base::HashPair(value.first, value.second);
michael@0 252 }
michael@0 253 };
michael@0 254
michael@0 255 #else
michael@0 256 #error define hash<std::pair<Type1, Type2> > for your compiler
michael@0 257 #endif // COMPILER
michael@0 258
michael@0 259 }
michael@0 260
michael@0 261 #undef DEFINE_PAIR_HASH_FUNCTION_START
michael@0 262 #undef DEFINE_PAIR_HASH_FUNCTION_END
michael@0 263
michael@0 264 #endif // BASE_CONTAINERS_HASH_TABLES_H_

mercurial