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