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
};
// Hasher
166
167
/*************************************************
168
** Global functions declarations
169
************************************************/
170
171
/**
172
* \ingroup hash
173
*
174
* Compute 32-bit hash of a byte buffer, using the default hash function.
175
*
176
* \param [in] buffer Pointer to the beginning of the buffer.
177
* \param [in] size Length of the buffer, in bytes.
178
* \return 32-bit hash of the buffer.
179
*/
180
uint32_t
Hash32
(
const
char
* buffer,
const
std::size_t size);
181
/**
182
* \ingroup hash
183
*
184
* Compute 64-bit hash of a byte buffer, using the default hash function.
185
*
186
* \param [in] buffer Pointer to the beginning of the buffer.
187
* \param [in] size Length of the buffer, in bytes.
188
* \return 64-bit hash of the buffer.
189
*/
190
uint64_t
Hash64
(
const
char
* buffer,
const
std::size_t size);
191
192
/**
193
* \ingroup hash
194
*
195
* Compute 32-bit hash of a string, using the default hash function.
196
*
197
* \param [in] s String to hash.
198
* \return 32-bit hash of the string.
199
*/
200
uint32_t
Hash32
(
const
std::string s);
201
/**
202
* \ingroup hash
203
*
204
* Compute 64-bit hash of a string, using the default hash function.
205
*
206
* \param [in] s String to hash.
207
* \return 64-bit hash of the string.
208
*/
209
uint64_t
Hash64
(
const
std::string s);
210
211
}
// namespace ns3
212
213
/*************************************************
214
** Inline implementations for rvo
215
************************************************/
216
217
namespace
ns3
218
{
219
220
/*************************************************
221
class Hasher implementation, inlined for rvo
222
*/
223
224
inline
uint32_t
225
Hasher::GetHash32
(
const
char
* buffer,
const
std::size_t size)
226
{
227
NS_ASSERT
(
m_impl
);
228
return
m_impl
->GetHash32(buffer, size);
229
}
230
231
inline
uint64_t
232
Hasher::GetHash64
(
const
char
* buffer,
const
std::size_t size)
233
{
234
NS_ASSERT
(
m_impl
);
235
return
m_impl
->GetHash64(buffer, size);
236
}
237
238
inline
uint32_t
239
Hasher::GetHash32
(
const
std::string s)
240
{
241
NS_ASSERT
(
m_impl
);
242
return
m_impl
->GetHash32(s.c_str(), s.size());
243
}
244
245
inline
uint64_t
246
Hasher::GetHash64
(
const
std::string s)
247
{
248
NS_ASSERT
(
m_impl
);
249
return
m_impl
->GetHash64(s.c_str(), s.size());
250
}
251
252
/*************************************************
253
Global hash functions, inlined for rvo
254
*/
255
256
/**
257
* \brief Get a reference to the static global hasher at g_hasher
258
* \return Reference to the static Hasher instance.
259
*/
260
Hasher
&
GetStaticHash
();
261
262
inline
uint32_t
263
Hash32
(
const
char
* buffer,
const
std::size_t size)
264
{
265
return
GetStaticHash
().
GetHash32
(buffer, size);
266
}
267
268
inline
uint64_t
269
Hash64
(
const
char
* buffer,
const
std::size_t size)
270
{
271
return
GetStaticHash
().
GetHash64
(buffer, size);
272
}
273
274
inline
uint32_t
275
Hash32
(
const
std::string s)
276
{
277
return
GetStaticHash
().
GetHash32
(s);
278
}
279
280
inline
uint64_t
281
Hash64
(
const
std::string s)
282
{
283
return
GetStaticHash
().
GetHash64
(s);
284
}
285
286
}
// namespace ns3
287
288
#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:225
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:232
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:269
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:263
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 Fri Nov 8 2024 13:58:59 for ns-3 by
1.11.0