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
};
// Hashfunction
92
93
/*--------------------------------------
94
* Hash function implementation
95
* by function pointers and templates
96
*/
97
98
/**
99
*
100
* \ingroup hash
101
*
102
* \brief Function pointer signatures for basic hash functions.
103
*
104
* See Hash::Function::Hash32 or Hash::Function::Hash64
105
* @{
106
*/
107
typedef
uint32_t
(*
Hash32Function_ptr
)(
const
char
*,
const
std::size_t);
108
typedef
uint64_t (*
Hash64Function_ptr
)(
const
char
*,
const
std::size_t);
109
110
/**@}*/
111
112
/**
113
* \ingroup hash
114
* Hash functions.
115
*/
116
namespace
Function
117
{
118
119
/**
120
* \ingroup hash
121
*
122
* \brief Template for creating a Hash::Implementation from
123
* a 32-bit hash function.
124
*/
125
class
Hash32
:
public
Implementation
126
{
127
public
:
128
/**
129
* Constructor from a 32-bit hash function pointer.
130
*
131
* \param [in] hp Function pointer to a 32-bit hash function.
132
*/
133
Hash32
(
Hash32Function_ptr
hp)
134
:
m_fp
(hp)
135
{
136
}
137
138
uint32_t
GetHash32
(
const
char
* buffer,
const
std::size_t size)
override
139
{
140
return
(*
m_fp
)(buffer, size);
141
}
142
143
void
clear
()
override
144
{
145
}
146
147
private
:
148
Hash32Function_ptr
m_fp
;
/**< The hash function. */
149
};
// Hash32
150
151
/**
152
* \ingroup hash
153
*
154
* \brief Template for creating a Hash::Implementation from
155
* a 64-bit hash function.
156
*/
157
class
Hash64
:
public
Implementation
158
{
159
public
:
160
/**
161
* Constructor from a 64-bit hash function pointer.
162
*
163
* \param [in] hp Function pointer to a 64-bit hash function.
164
*/
165
Hash64
(
Hash64Function_ptr
hp)
166
:
m_fp
(hp)
167
{
168
}
169
170
uint64_t
GetHash64
(
const
char
* buffer,
const
std::size_t size)
override
171
{
172
return
(*
m_fp
)(buffer, size);
173
}
174
175
uint32_t
GetHash32
(
const
char
* buffer,
const
std::size_t size)
override
176
{
177
uint32_t
hash32;
178
uint64_t hash64 =
GetHash64
(buffer, size);
179
180
memcpy(&hash32, &hash64,
sizeof
(hash32));
181
return
hash32;
182
}
183
184
void
clear
()
override
185
{
186
}
187
188
private
:
189
Hash64Function_ptr
m_fp
;
/**< The hash function. */
190
};
// Hash64<Hash64Function_ptr>
191
192
}
// namespace Function
193
194
}
// namespace Hash
195
196
}
// namespace ns3
197
198
#endif
/* HASHFUNCTION_H */
ns3::Hash::Function::Hash32
Template for creating a Hash::Implementation from a 32-bit hash function.
Definition
hash-function.h:126
ns3::Hash::Function::Hash32::clear
void clear() override
Restore initial state.
Definition
hash-function.h:143
ns3::Hash::Function::Hash32::Hash32
Hash32(Hash32Function_ptr hp)
Constructor from a 32-bit hash function pointer.
Definition
hash-function.h:133
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:138
ns3::Hash::Function::Hash32::m_fp
Hash32Function_ptr m_fp
The hash function.
Definition
hash-function.h:148
ns3::Hash::Function::Hash64
Template for creating a Hash::Implementation from a 64-bit hash function.
Definition
hash-function.h:158
ns3::Hash::Function::Hash64::clear
void clear() override
Restore initial state.
Definition
hash-function.h:184
ns3::Hash::Function::Hash64::m_fp
Hash64Function_ptr m_fp
The hash function.
Definition
hash-function.h:189
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:170
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:175
ns3::Hash::Function::Hash64::Hash64
Hash64(Hash64Function_ptr hp)
Constructor from a 64-bit hash function pointer.
Definition
hash-function.h:165
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:107
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:108
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 Wed Oct 9 2024 01:03:22 for ns-3 by
1.11.0