A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
hash.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012 Lawrence Livermore National Laboratory
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
18 */
19
20#ifndef HASH_H
21#define HASH_H
22
23#include "assert.h"
24#include "hash-fnv.h"
25#include "hash-function.h"
26#include "hash-murmur3.h"
27#include "ptr.h"
28
29#include <string>
30
31/**
32 * \file
33 * \ingroup hash
34 * \brief ns3::Hasher, ns3::Hash32() and ns3::Hash64() function declarations.
35 */
36
37namespace ns3
38{
39
40/**
41 * \ingroup core
42 * \defgroup hash Hash Functions
43 *
44 * \brief Generic Hash function interface.
45 *
46 * See \ref Hasher for main entry point.
47 * See \ref hash-example.cc for example usage.
48 */
49/**
50 * \ingroup hash
51 *
52 * \brief Generic Hash function interface.
53 *
54 * This class provides a generic interface for computing hashes
55 * of buffers. Various getters return hashes of different lengths.
56 *
57 * Call clear() between calls to the getter to reset the
58 * internal state and hash each buffer separately.
59 *
60 * If you don't call clear() between calls to the getter
61 * you can hash successive buffers. The final return value
62 * will be the cumulative hash across all calls.
63 *
64 * The choice of hash function can be made at construction by
65 * \code
66 * Hasher hasher = Hasher ( Create<Hash::Function::Fnv1a> () );
67 * uint32_t hash = Hasher.GetHash32 (data);
68 * \endcode
69 *
70 * The available implementations are documented in \ref hash.
71 * The default implementation is Murmur3. FNV1a is also available.
72 *
73 * In addition to this class interface, global functions are
74 * defined which use the default hash implementation.
75 *
76 * \internal
77 *
78 * Would be nice to offer longer hashes. \c uint128_t looks doable,
79 * except that our fallback \c int64x64_t implementation doesn't
80 * offer \c unsigned.
81 *
82 * Longer hashes require returning a byte buffer of some sort,
83 * but our \ref Buffer class seems a bit overkill for this case.
84 *
85 */
86class Hasher
87{
88 public:
89 /**
90 * Constructor using the default implementation.
91 */
92 Hasher();
93 /**
94 * Constructor using the supplied implementation.
95 *
96 * \param [in] hp Ptr<Hash::Implementation> to the desired implementation.
97 */
99 /**
100 * Compute 32-bit hash of a byte buffer.
101 *
102 * Call clear () between calls to GetHash32() to reset the
103 * internal state and hash each buffer separately.
104 *
105 * If you don't call clear() between calls to GetHash32,
106 * you can hash successive buffers. The final return value
107 * will be the cumulative hash across all calls.
108 *
109 * \param [in] buffer Pointer to the beginning of the buffer.
110 * \param [in] size Length of the buffer, in bytes.
111 * \return 32-bit hash of the buffer..
112 */
113 uint32_t GetHash32(const char* buffer, const std::size_t size);
114 /**
115 * Compute 64-bit hash of a byte buffer.
116 *
117 * Call clear () between calls to GetHash64() to reset the
118 * internal state and hash each buffer separately.
119 *
120 * If you don't call clear() between calls to GetHash64,
121 * you can hash successive buffers. The final return value
122 * will be the cumulative hash across all calls.
123 *
124 * \param [in] buffer Pointer to the beginning of the buffer.
125 * \param [in] size Length of the buffer, in bytes.
126 * \return 64-bit hash of the buffer.
127 */
128 uint64_t GetHash64(const char* buffer, const std::size_t size);
129
130 /**
131 * Compute 32-bit hash of a string.
132 *
133 * Call clear () between calls to GetHash32() to reset the
134 * internal state and hash each string separately.
135 *
136 * If you don't call clear() between calls to GetHash32,
137 * you can hash successive strings. The final return value
138 * will be the cumulative hash across all calls.
139 *
140 * \param [in] s String to hash.
141 * \return 32-bit hash of the string.
142 */
143 uint32_t GetHash32(const std::string s);
144 /**
145 * Compute 64-bit hash of a string.
146 *
147 * Call clear () between calls to GetHash64() to reset the
148 * internal state and hash each string separately.
149 *
150 * If you don't call clear() between calls to GetHash64,
151 * you can hash successive strings. The final return value
152 * will be the cumulative hash across all calls.
153 *
154 * \param [in] s String to hash.
155 * \return 64-bit hash of the string.
156 */
157 uint64_t GetHash64(const std::string s);
158 /**
159 * Restore initial state.
160 *
161 * Returning this Hasher allows code like this:
162 *
163 * \code
164 * Hasher h;
165 * h.GetHash32 (...);
166 * ...
167 * h.clear ().GetHash64 (...);
168 * \endcode
169 *
170 * \return This hasher.
171 */
172 Hasher& clear();
173
174 private:
175 Ptr<Hash::Implementation> m_impl; /**< Hash implementation. */
176}; // Hasher
177
178/*************************************************
179 ** Global functions declarations
180 ************************************************/
181
182/**
183 * \ingroup hash
184 *
185 * Compute 32-bit hash of a byte buffer, using the default hash function.
186 *
187 * \param [in] buffer Pointer to the beginning of the buffer.
188 * \param [in] size Length of the buffer, in bytes.
189 * \return 32-bit hash of the buffer.
190 */
191uint32_t Hash32(const char* buffer, const std::size_t size);
192/**
193 * \ingroup hash
194 *
195 * Compute 64-bit hash of a byte buffer, using the default hash function.
196 *
197 * \param [in] buffer Pointer to the beginning of the buffer.
198 * \param [in] size Length of the buffer, in bytes.
199 * \return 64-bit hash of the buffer.
200 */
201uint64_t Hash64(const char* buffer, const std::size_t size);
202
203/**
204 * \ingroup hash
205 *
206 * Compute 32-bit hash of a string, using the default hash function.
207 *
208 * \param [in] s String to hash.
209 * \return 32-bit hash of the string.
210 */
211uint32_t Hash32(const std::string s);
212/**
213 * \ingroup hash
214 *
215 * Compute 64-bit hash of a string, using the default hash function.
216 *
217 * \param [in] s String to hash.
218 * \return 64-bit hash of the string.
219 */
220uint64_t Hash64(const std::string s);
221
222} // namespace ns3
223
224/*************************************************
225 ** Inline implementations for rvo
226 ************************************************/
227
228namespace ns3
229{
230
231/*************************************************
232 class Hasher implementation, inlined for rvo
233*/
234
235inline uint32_t
236Hasher::GetHash32(const char* buffer, const std::size_t size)
237{
239 return m_impl->GetHash32(buffer, size);
240}
241
242inline uint64_t
243Hasher::GetHash64(const char* buffer, const std::size_t size)
244{
246 return m_impl->GetHash64(buffer, size);
247}
248
249inline uint32_t
250Hasher::GetHash32(const std::string s)
251{
253 return m_impl->GetHash32(s.c_str(), s.size());
254}
255
256inline uint64_t
257Hasher::GetHash64(const std::string s)
258{
260 return m_impl->GetHash64(s.c_str(), s.size());
261}
262
263/*************************************************
264 Global hash functions, inlined for rvo
265*/
266
267/**
268 * \brief Get a reference to the static global hasher at g_hasher
269 * \return Reference to the static Hasher instance.
270 */
272
273inline uint32_t
274Hash32(const char* buffer, const std::size_t size)
275{
276 return GetStaticHash().GetHash32(buffer, size);
277}
278
279inline uint64_t
280Hash64(const char* buffer, const std::size_t size)
281{
282 return GetStaticHash().GetHash64(buffer, size);
283}
284
285inline uint32_t
286Hash32(const std::string s)
287{
288 return GetStaticHash().GetHash32(s);
289}
290
291inline uint64_t
292Hash64(const std::string s)
293{
294 return GetStaticHash().GetHash64(s);
295}
296
297} // namespace ns3
298
299#endif /* HASH_H */
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
Generic Hash function interface.
Definition: hash.h:87
Hasher()
Constructor using the default implementation.
Definition: hash.cc:43
Ptr< Hash::Implementation > m_impl
Hash implementation.
Definition: hash.h:175
uint32_t GetHash32(const char *buffer, const std::size_t size)
Compute 32-bit hash of a byte buffer.
Definition: hash.h:236
uint64_t GetHash64(const char *buffer, const std::size_t size)
Compute 64-bit hash of a byte buffer.
Definition: hash.h:243
Hasher & clear()
Restore initial state.
Definition: hash.cc:56
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
uint64_t Hash64(const char *buffer, const std::size_t size)
Compute 64-bit hash of a byte buffer, using the default hash function.
Definition: hash.h:280
uint32_t Hash32(const char *buffer, const std::size_t size)
Compute 32-bit hash of a byte buffer, using the default hash function.
Definition: hash.h:274
ns3::Hash::Function::Fnv1a declaration.
ns3::Hash::Implementation, ns3::Hash::Function::Hash32 and ns3::Hash::Function::Hash64 declarations.
ns3::Hash::Function::Murmur3 declaration.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hasher & GetStaticHash()
Get a reference to the static global hasher at g_hasher.
Definition: hash.cc:36
ns3::Ptr smart pointer declaration and implementation.