1 | /******************************************************************** |
2 | * Copyright (c) 2013 - 2014, Pivotal Inc. |
3 | * All rights reserved. |
4 | * |
5 | * Author: Zhanwei Wang |
6 | ********************************************************************/ |
7 | /******************************************************************** |
8 | * 2014 - |
9 | * open source under Apache License Version 2.0 |
10 | ********************************************************************/ |
11 | /** |
12 | * Licensed to the Apache Software Foundation (ASF) under one |
13 | * or more contributor license agreements. See the NOTICE file |
14 | * distributed with this work for additional information |
15 | * regarding copyright ownership. The ASF licenses this file |
16 | * to you under the Apache License, Version 2.0 (the |
17 | * "License"); you may not use this file except in compliance |
18 | * with the License. You may obtain a copy of the License at |
19 | * |
20 | * http://www.apache.org/licenses/LICENSE-2.0 |
21 | * |
22 | * Unless required by applicable law or agreed to in writing, software |
23 | * distributed under the License is distributed on an "AS IS" BASIS, |
24 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
25 | * See the License for the specific language governing permissions and |
26 | * limitations under the License. |
27 | */ |
28 | #ifndef _HDFS_LIBHDFS3_COMMON_HASH_H_ |
29 | #define _HDFS_LIBHDFS3_COMMON_HASH_H_ |
30 | |
31 | #include "platform.h" |
32 | |
33 | #include <string> |
34 | #include <vector> |
35 | |
36 | #ifdef NEED_BOOST |
37 | |
38 | #include <boost/functional/hash.hpp> |
39 | |
40 | namespace Hdfs { |
41 | namespace Internal { |
42 | |
43 | /** |
44 | * A hash function object used to hash a boolean value. |
45 | */ |
46 | extern boost::hash<bool> BoolHasher; |
47 | |
48 | /** |
49 | * A hash function object used to hash an int value. |
50 | */ |
51 | extern boost::hash<int> Int32Hasher; |
52 | |
53 | /** |
54 | * A hash function object used to hash an 64 bit int value. |
55 | */ |
56 | extern boost::hash<int64_t> Int64Hasher; |
57 | |
58 | /** |
59 | * A hash function object used to hash a size_t value. |
60 | */ |
61 | extern boost::hash<size_t> SizeHasher; |
62 | |
63 | /** |
64 | * A hash function object used to hash a std::string object. |
65 | */ |
66 | extern boost::hash<std::string> StringHasher; |
67 | |
68 | } |
69 | } |
70 | |
71 | #define HDFS_HASH_DEFINE(TYPE) \ |
72 | namespace boost{ \ |
73 | template<> \ |
74 | struct hash<TYPE> { \ |
75 | std::size_t operator()(const TYPE & key) const { \ |
76 | return key.hash_value(); \ |
77 | } \ |
78 | }; \ |
79 | } |
80 | |
81 | #else |
82 | |
83 | #include <functional> |
84 | |
85 | namespace Hdfs { |
86 | namespace Internal { |
87 | |
88 | /** |
89 | * A hash function object used to hash a boolean value. |
90 | */ |
91 | extern std::hash<bool> BoolHasher; |
92 | |
93 | /** |
94 | * A hash function object used to hash an int value. |
95 | */ |
96 | extern std::hash<int> Int32Hasher; |
97 | |
98 | /** |
99 | * A hash function object used to hash an 64 bit int value. |
100 | */ |
101 | extern std::hash<int64_t> Int64Hasher; |
102 | |
103 | /** |
104 | * A hash function object used to hash a size_t value. |
105 | */ |
106 | extern std::hash<size_t> SizeHasher; |
107 | |
108 | /** |
109 | * A hash function object used to hash a std::string object. |
110 | */ |
111 | extern std::hash<std::string> StringHasher; |
112 | |
113 | } |
114 | } |
115 | |
116 | #define HDFS_HASH_DEFINE(TYPE) \ |
117 | namespace std{ \ |
118 | template<> \ |
119 | struct hash<TYPE> { \ |
120 | std::size_t operator()(const TYPE & key) const { \ |
121 | return key.hash_value(); \ |
122 | } \ |
123 | }; \ |
124 | } |
125 | |
126 | #endif |
127 | |
128 | namespace Hdfs { |
129 | namespace Internal { |
130 | |
131 | /** |
132 | * A hash function used to hash a vector of size_t values. |
133 | * @param vec The vector's reference which items are to be hashed. |
134 | * @param size The size of vec. |
135 | * @return The hash value. |
136 | * @throw nothrow |
137 | */ |
138 | static inline size_t CombineHasher(const size_t * vec, size_t size) { |
139 | size_t value = 0; |
140 | |
141 | for (size_t i = 0; i < size; ++i) { |
142 | value ^= SizeHasher(vec[i]) << 1; |
143 | } |
144 | |
145 | return value; |
146 | } |
147 | |
148 | } |
149 | } |
150 | |
151 | #endif /* _HDFS_LIBHDFS3_COMMON_HASH_H_ */ |
152 | |