A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
26
namespace
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
*/
75
class
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
*/
87
Hasher
(
Ptr<Hash::Implementation>
hp);
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
*/
182
uint32_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
*/
192
uint64_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
*/
202
uint32_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
*/
211
uint64_t
Hash64
(
const
std::string s);
212
213
}
// namespace ns3
214
215
/*************************************************
216
** Inline implementations for rvo
217
************************************************/
218
219
namespace
ns3
220
{
221
222
/*************************************************
223
class Hasher implementation, inlined for rvo
224
*/
225
226
inline
uint32_t
227
Hasher::GetHash32
(
const
char
* buffer,
const
std::size_t size)
228
{
229
NS_ASSERT
(
m_impl
);
230
return
m_impl
->GetHash32(buffer, size);
231
}
232
233
inline
uint64_t
234
Hasher::GetHash64
(
const
char
* buffer,
const
std::size_t size)
235
{
236
NS_ASSERT
(
m_impl
);
237
return
m_impl
->GetHash64(buffer, size);
238
}
239
240
inline
uint32_t
241
Hasher::GetHash32
(
const
std::string s)
242
{
243
NS_ASSERT
(
m_impl
);
244
return
m_impl
->GetHash32(s.c_str(), s.size());
245
}
246
247
inline
uint64_t
248
Hasher::GetHash64
(
const
std::string s)
249
{
250
NS_ASSERT
(
m_impl
);
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
*/
262
Hasher
&
GetStaticHash
();
263
264
inline
uint32_t
265
Hash32
(
const
char
* buffer,
const
std::size_t size)
266
{
267
return
GetStaticHash
().
GetHash32
(buffer, size);
268
}
269
270
inline
uint64_t
271
Hash64
(
const
char
* buffer,
const
std::size_t size)
272
{
273
return
GetStaticHash
().
GetHash64
(buffer, size);
274
}
275
276
inline
uint32_t
277
Hash32
(
const
std::string s)
278
{
279
return
GetStaticHash
().
GetHash32
(s);
280
}
281
282
inline
uint64_t
283
Hash64
(
const
std::string s)
284
{
285
return
GetStaticHash
().
GetHash64
(s);
286
}
287
288
}
// namespace ns3
289
290
#endif
/* HASH_H */
assert.h
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
ns3::Hasher
Generic Hash function interface.
Definition
hash.h:76
ns3::Hasher::Hasher
Hasher()
Constructor using the default implementation.
Definition
hash.cc:32
ns3::Hasher::m_impl
Ptr< Hash::Implementation > m_impl
Hash implementation.
Definition
hash.h:164
ns3::Hasher::GetHash32
uint32_t GetHash32(const char *buffer, const std::size_t size)
Compute 32-bit hash of a byte buffer.
Definition
hash.h:227
ns3::Hasher::GetHash64
uint64_t GetHash64(const char *buffer, const std::size_t size)
Compute 64-bit hash of a byte buffer.
Definition
hash.h:234
ns3::Hasher::clear
Hasher & clear()
Restore initial state.
Definition
hash.cc:45
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition
mpi-test-fixtures.h:37
uint32_t
NS_ASSERT
#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
ns3::Hash64
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
ns3::Hash32
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
hash-fnv.h
ns3::Hash::Function::Fnv1a declaration.
hash-function.h
ns3::Hash::Implementation, ns3::Hash::Function::Hash32 and ns3::Hash::Function::Hash64 declarations.
hash-murmur3.h
ns3::Hash::Function::Murmur3 declaration.
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::GetStaticHash
Hasher & GetStaticHash()
Get a reference to the static global hasher at g_hasher.
Definition
hash.cc:25
ptr.h
ns3::Ptr smart pointer declaration and implementation.
src
core
model
hash.h
Generated on Tue Apr 29 2025 18:20:41 for ns-3 by
1.11.0