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-function.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 HASHFUNCTION_H
10
#define HASHFUNCTION_H
11
12
#include "
simple-ref-count.h
"
13
14
#include <cstring>
// memcpy
15
16
/**
17
* @file
18
* @ingroup hash
19
* @brief ns3::Hash::Implementation, ns3::Hash::Function::Hash32 and
20
* ns3::Hash::Function::Hash64 declarations.
21
*/
22
23
namespace
ns3
24
{
25
26
/**
27
* @ingroup hash
28
* Hash function implementations
29
*/
30
namespace
Hash
31
{
32
33
/**
34
* @ingroup hash
35
*
36
* @brief Hash function implementation base class.
37
*/
38
class
Implementation
:
public
SimpleRefCount
<Implementation>
39
{
40
public
:
41
/**
42
* Compute 32-bit hash of a byte buffer
43
*
44
* Call clear() between calls to GetHash32() to reset the
45
* internal state and hash each buffer separately.
46
*
47
* If you don't call clear() between calls to GetHash32,
48
* you can hash successive buffers. The final return value
49
* will be the cumulative hash across all calls.
50
*
51
* @param [in] buffer Pointer to the beginning of the buffer.
52
* @param [in] size Length of the buffer, in bytes.
53
* @return 32-bit hash of the buffer.
54
*/
55
virtual
uint32_t
GetHash32
(
const
char
* buffer,
const
std::size_t size) = 0;
56
/**
57
* Compute 64-bit hash of a byte buffer.
58
*
59
* Default implementation returns 32-bit hash, with a warning.
60
*
61
* Call clear() between calls to GetHash64() to reset the
62
* internal state and hash each buffer separately.
63
*
64
* If you don't call clear() between calls to GetHash64,
65
* you can hash successive buffers. The final return value
66
* will be the cumulative hash across all calls.
67
*
68
* @param [in] buffer Pointer to the beginning of the buffer.
69
* @param [in] size Length of the buffer, in bytes.
70
* @return 64-bit hash of the buffer.
71
*/
72
virtual
uint64_t
GetHash64
(
const
char
* buffer,
const
std::size_t size);
73
/**
74
* Restore initial state.
75
*/
76
virtual
void
clear
() = 0;
77
78
/**
79
* Constructor.
80
*/
81
Implementation
()
82
{
83
}
84
85
/**
86
* Destructor.
87
*/
88
virtual
~Implementation
()
89
{
90
}
91
92
// end of class Hash::Implementation
93
};
94
95
/*--------------------------------------
96
* Hash function implementation
97
* by function pointers and templates
98
*/
99
100
/**
101
*
102
* @ingroup hash
103
*
104
* @brief Function pointer signatures for basic hash functions.
105
*
106
* See Hash::Function::Hash32 or Hash::Function::Hash64
107
* @{
108
*/
109
typedef
uint32_t
(*
Hash32Function_ptr
)(
const
char
*,
const
std::size_t);
110
typedef
uint64_t (*
Hash64Function_ptr
)(
const
char
*,
const
std::size_t);
111
112
/**@}*/
113
114
/**
115
* @ingroup hash
116
* Hash functions.
117
*/
118
namespace
Function
119
{
120
121
/**
122
* @ingroup hash
123
*
124
* @brief Template for creating a Hash::Implementation from
125
* a 32-bit hash function.
126
*/
127
class
Hash32
:
public
Implementation
128
{
129
public
:
130
/**
131
* Constructor from a 32-bit hash function pointer.
132
*
133
* @param [in] hp Function pointer to a 32-bit hash function.
134
*/
135
Hash32
(
Hash32Function_ptr
hp)
136
:
m_fp
(hp)
137
{
138
}
139
140
uint32_t
GetHash32
(
const
char
* buffer,
const
std::size_t size)
override
141
{
142
return
(*
m_fp
)(buffer, size);
143
}
144
145
void
clear
()
override
146
{
147
}
148
149
private
:
150
Hash32Function_ptr
m_fp
;
/**< The hash function. */
151
152
// end of class Hash::Function::Hash32
153
};
154
155
/**
156
* @ingroup hash
157
*
158
* @brief Template for creating a Hash::Implementation from
159
* a 64-bit hash function.
160
*/
161
class
Hash64
:
public
Implementation
162
{
163
public
:
164
/**
165
* Constructor from a 64-bit hash function pointer.
166
*
167
* @param [in] hp Function pointer to a 64-bit hash function.
168
*/
169
Hash64
(
Hash64Function_ptr
hp)
170
:
m_fp
(hp)
171
{
172
}
173
174
uint64_t
GetHash64
(
const
char
* buffer,
const
std::size_t size)
override
175
{
176
return
(*
m_fp
)(buffer, size);
177
}
178
179
uint32_t
GetHash32
(
const
char
* buffer,
const
std::size_t size)
override
180
{
181
uint32_t
hash32;
182
uint64_t hash64 =
GetHash64
(buffer, size);
183
184
memcpy(&hash32, &hash64,
sizeof
(hash32));
185
return
hash32;
186
}
187
188
void
clear
()
override
189
{
190
}
191
192
private
:
193
Hash64Function_ptr
m_fp
;
/**< The hash function. */
194
195
// end of class Hash::Function::Hash64
196
};
197
198
}
// namespace Function
199
200
}
// namespace Hash
201
202
}
// namespace ns3
203
204
#endif
/* HASHFUNCTION_H */
ns3::Hash::Function::Hash32
Template for creating a Hash::Implementation from a 32-bit hash function.
Definition
hash-function.h:128
ns3::Hash::Function::Hash32::clear
void clear() override
Restore initial state.
Definition
hash-function.h:145
ns3::Hash::Function::Hash32::Hash32
Hash32(Hash32Function_ptr hp)
Constructor from a 32-bit hash function pointer.
Definition
hash-function.h:135
ns3::Hash::Function::Hash32::GetHash32
uint32_t GetHash32(const char *buffer, const std::size_t size) override
Compute 32-bit hash of a byte buffer.
Definition
hash-function.h:140
ns3::Hash::Function::Hash32::m_fp
Hash32Function_ptr m_fp
The hash function.
Definition
hash-function.h:150
ns3::Hash::Function::Hash64
Template for creating a Hash::Implementation from a 64-bit hash function.
Definition
hash-function.h:162
ns3::Hash::Function::Hash64::clear
void clear() override
Restore initial state.
Definition
hash-function.h:188
ns3::Hash::Function::Hash64::m_fp
Hash64Function_ptr m_fp
The hash function.
Definition
hash-function.h:193
ns3::Hash::Function::Hash64::GetHash64
uint64_t GetHash64(const char *buffer, const std::size_t size) override
Compute 64-bit hash of a byte buffer.
Definition
hash-function.h:174
ns3::Hash::Function::Hash64::GetHash32
uint32_t GetHash32(const char *buffer, const std::size_t size) override
Compute 32-bit hash of a byte buffer.
Definition
hash-function.h:179
ns3::Hash::Function::Hash64::Hash64
Hash64(Hash64Function_ptr hp)
Constructor from a 64-bit hash function pointer.
Definition
hash-function.h:169
ns3::Hash::Implementation
Hash function implementation base class.
Definition
hash-function.h:39
ns3::Hash::Implementation::Implementation
Implementation()
Constructor.
Definition
hash-function.h:81
ns3::Hash::Implementation::GetHash64
virtual uint64_t GetHash64(const char *buffer, const std::size_t size)
Compute 64-bit hash of a byte buffer.
Definition
hash-function.cc:28
ns3::Hash::Implementation::~Implementation
virtual ~Implementation()
Destructor.
Definition
hash-function.h:88
ns3::Hash::Implementation::clear
virtual void clear()=0
Restore initial state.
ns3::Hash::Implementation::GetHash32
virtual uint32_t GetHash32(const char *buffer, const std::size_t size)=0
Compute 32-bit hash of a byte buffer.
ns3::SimpleRefCount
A template-based reference counting class.
Definition
simple-ref-count.h:70
uint32_t
ns3::Hash::Hash32Function_ptr
uint32_t(* Hash32Function_ptr)(const char *, const std::size_t)
Function pointer signatures for basic hash functions.
Definition
hash-function.h:109
ns3::Hash::Hash64Function_ptr
uint64_t(* Hash64Function_ptr)(const char *, const std::size_t)
Function pointer signatures for basic hash functions.
Definition
hash-function.h:110
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
simple-ref-count.h
ns3::SimpleRefCount declaration and template implementation.
src
core
model
hash-function.h
Generated on Tue Apr 29 2025 18:20:41 for ns-3 by
1.11.0