Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(271)

Side by Side Diff: cc/base/hash_pair.h

Issue 17094004: Move hash_pair.h from cc/base into base/. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Clean up empty lines and add reference to danakj@. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/containers/hash_tables_unittest.cc ('k') | cc/base/hash_pair_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // Defines methods for hashing a pair of integers.
6
7 #ifndef CC_BASE_HASH_PAIR_H_
8 #define CC_BASE_HASH_PAIR_H_
9
10 #include <utility>
11
12 #include "base/basictypes.h"
13 #include "base/containers/hash_tables.h"
14
15 #if defined(COMPILER_MSVC)
16
17 #define DEFINE_PAIR_HASH_FUNCTION_START(type1, type2) \
18 template<> \
19 inline std::size_t hash_value<std::pair<type1, type2> >( \
20 const std::pair<type1, type2>& value)
21
22 #define DEFINE_PAIR_HASH_FUNCTION_END()
23
24 #elif defined(COMPILER_GCC)
25
26 #define DEFINE_PAIR_HASH_FUNCTION_START(type1, type2) \
27 template<> \
28 struct hash<std::pair<type1, type2> > { \
29 std::size_t operator()(std::pair<type1, type2> value) const
30
31 #define DEFINE_PAIR_HASH_FUNCTION_END() \
32 };
33
34 #else
35 #error define DEFINE_PAIR_HASH_FUNCTION_START for your compiler
36 #endif // COMPILER
37
38 namespace BASE_HASH_NAMESPACE {
39
40 // Implement hashing for pairs of at-most 32 bit integer values.
41 // When size_t is 32 bits, we turn the 64-bit hash code into 32 bits by using
42 // multiply-add hashing. This algorithm, as described in
43 // Theorem 4.3.3 of the thesis "Über die Komplexität der Multiplikation in
44 // eingeschränkten Branchingprogrammmodellen" by Woelfel, is:
45 //
46 // h32(x32, y32) = (h64(x32, y32) * rand_odd64 + rand16 * 2^16) % 2^64 / 2^32
47
48 #define DEFINE_32BIT_PAIR_HASH(type1, type2) \
49 DEFINE_PAIR_HASH_FUNCTION_START(type1, type2) { \
50 uint64 first = value.first; \
51 uint32 second = value.second; \
52 uint64 hash64 = (first << 32) | second; \
53 \
54 if (sizeof(std::size_t) >= sizeof(uint64)) \
55 return static_cast<std::size_t>(hash64); \
56 \
57 uint64 odd_random = 481046412LL << 32 | 1025306954LL; \
58 uint32 shift_random = 10121U << 16; \
59 \
60 hash64 = hash64 * odd_random + shift_random; \
61 std::size_t high_bits = static_cast<std::size_t>( \
62 hash64 >> (sizeof(uint64) - sizeof(std::size_t))); \
63 return high_bits; \
64 } \
65 DEFINE_PAIR_HASH_FUNCTION_END();
66
67 DEFINE_32BIT_PAIR_HASH(int16, int16);
68 DEFINE_32BIT_PAIR_HASH(int16, uint16);
69 DEFINE_32BIT_PAIR_HASH(int16, int32);
70 DEFINE_32BIT_PAIR_HASH(int16, uint32);
71 DEFINE_32BIT_PAIR_HASH(uint16, int16);
72 DEFINE_32BIT_PAIR_HASH(uint16, uint16);
73 DEFINE_32BIT_PAIR_HASH(uint16, int32);
74 DEFINE_32BIT_PAIR_HASH(uint16, uint32);
75 DEFINE_32BIT_PAIR_HASH(int32, int16);
76 DEFINE_32BIT_PAIR_HASH(int32, uint16);
77 DEFINE_32BIT_PAIR_HASH(int32, int32);
78 DEFINE_32BIT_PAIR_HASH(int32, uint32);
79 DEFINE_32BIT_PAIR_HASH(uint32, int16);
80 DEFINE_32BIT_PAIR_HASH(uint32, uint16);
81 DEFINE_32BIT_PAIR_HASH(uint32, int32);
82 DEFINE_32BIT_PAIR_HASH(uint32, uint32);
83
84 #undef DEFINE_32BIT_PAIR_HASH
85
86 // Implement hashing for pairs of up-to 64-bit integer values.
87 // We use the compound integer hash method to produce a 64-bit hash code, by
88 // breaking the two 64-bit inputs into 4 32-bit values:
89 // http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECT ION00832000000000000000
90 // Then we reduce our result to 32 bits if required, similar to above.
91
92 #define DEFINE_64BIT_PAIR_HASH(type1, type2) \
93 DEFINE_PAIR_HASH_FUNCTION_START(type1, type2) { \
94 uint32 short_random1 = 842304669U; \
95 uint32 short_random2 = 619063811U; \
96 uint32 short_random3 = 937041849U; \
97 uint32 short_random4 = 3309708029U; \
98 \
99 uint64 value1 = value.first; \
100 uint64 value2 = value.second; \
101 uint32 value1a = static_cast<uint32>(value1 & 0xffffffff); \
102 uint32 value1b = static_cast<uint32>((value1 >> 32) & 0xffffffff); \
103 uint32 value2a = static_cast<uint32>(value2 & 0xffffffff); \
104 uint32 value2b = static_cast<uint32>((value2 >> 32) & 0xffffffff); \
105 \
106 uint64 product1 = static_cast<uint64>(value1a) * short_random1; \
107 uint64 product2 = static_cast<uint64>(value1b) * short_random2; \
108 uint64 product3 = static_cast<uint64>(value2a) * short_random3; \
109 uint64 product4 = static_cast<uint64>(value2b) * short_random4; \
110 \
111 uint64 hash64 = product1 + product2 + product3 + product4; \
112 \
113 if (sizeof(std::size_t) >= sizeof(uint64)) \
114 return static_cast<std::size_t>(hash64); \
115 \
116 uint64 odd_random = 1578233944LL << 32 | 194370989LL; \
117 uint32 shift_random = 20591U << 16; \
118 \
119 hash64 = hash64 * odd_random + shift_random; \
120 std::size_t high_bits = static_cast<std::size_t>( \
121 hash64 >> (sizeof(uint64) - sizeof(std::size_t))); \
122 return high_bits; \
123 } \
124 DEFINE_PAIR_HASH_FUNCTION_END();
125
126 DEFINE_64BIT_PAIR_HASH(int16, int64);
127 DEFINE_64BIT_PAIR_HASH(int16, uint64);
128 DEFINE_64BIT_PAIR_HASH(uint16, int64);
129 DEFINE_64BIT_PAIR_HASH(uint16, uint64);
130 DEFINE_64BIT_PAIR_HASH(int32, int64);
131 DEFINE_64BIT_PAIR_HASH(int32, uint64);
132 DEFINE_64BIT_PAIR_HASH(uint32, int64);
133 DEFINE_64BIT_PAIR_HASH(uint32, uint64);
134 DEFINE_64BIT_PAIR_HASH(int64, int16);
135 DEFINE_64BIT_PAIR_HASH(int64, uint16);
136 DEFINE_64BIT_PAIR_HASH(int64, int32);
137 DEFINE_64BIT_PAIR_HASH(int64, uint32);
138 DEFINE_64BIT_PAIR_HASH(int64, int64);
139 DEFINE_64BIT_PAIR_HASH(int64, uint64);
140 DEFINE_64BIT_PAIR_HASH(uint64, int16);
141 DEFINE_64BIT_PAIR_HASH(uint64, uint16);
142 DEFINE_64BIT_PAIR_HASH(uint64, int32);
143 DEFINE_64BIT_PAIR_HASH(uint64, uint32);
144 DEFINE_64BIT_PAIR_HASH(uint64, int64);
145 DEFINE_64BIT_PAIR_HASH(uint64, uint64);
146
147 #undef DEFINE_64BIT_PAIR_HASH
148 }
149
150 #undef DEFINE_PAIR_HASH_FUNCTION_START
151 #undef DEFINE_PAIR_HASH_FUNCTION_END
152
153 #endif // CC_BASE_HASH_PAIR_H_
OLDNEW
« no previous file with comments | « base/containers/hash_tables_unittest.cc ('k') | cc/base/hash_pair_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698