A Discrete-Event Network Simulator
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
23namespace ns3
24{
25
26/**
27 * \ingroup hash
28 * Hash function implementations
29 */
30namespace Hash
31{
32
33/**
34 * \ingroup hash
35 *
36 * \brief Hash function implementation base class.
37 */
38class 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 */
82 {
83 }
84
85 /**
86 * Destructor.
87 */
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 */
107typedef uint32_t (*Hash32Function_ptr)(const char*, const std::size_t);
108typedef uint64_t (*Hash64Function_ptr)(const char*, const std::size_t);
109
110/**@}*/
111
112/**
113 * \ingroup hash
114 * Hash functions.
115 */
116namespace Function
117{
118
119/**
120 * \ingroup hash
121 *
122 * \brief Template for creating a Hash::Implementation from
123 * a 32-bit hash function.
124 */
125class 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 */
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 */
157class 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 */
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 */
Template for creating a Hash::Implementation from a 32-bit hash function.
void clear() override
Restore initial state.
Hash32(Hash32Function_ptr hp)
Constructor from a 32-bit hash function pointer.
uint32_t GetHash32(const char *buffer, const std::size_t size) override
Compute 32-bit hash of a byte buffer.
Hash32Function_ptr m_fp
The hash function.
Template for creating a Hash::Implementation from a 64-bit hash function.
void clear() override
Restore initial state.
Hash64Function_ptr m_fp
The hash function.
uint64_t GetHash64(const char *buffer, const std::size_t size) override
Compute 64-bit hash of a byte buffer.
uint32_t GetHash32(const char *buffer, const std::size_t size) override
Compute 32-bit hash of a byte buffer.
Hash64(Hash64Function_ptr hp)
Constructor from a 64-bit hash function pointer.
Hash function implementation base class.
virtual uint64_t GetHash64(const char *buffer, const std::size_t size)
Compute 64-bit hash of a byte buffer.
virtual ~Implementation()
Destructor.
virtual void clear()=0
Restore initial state.
virtual uint32_t GetHash32(const char *buffer, const std::size_t size)=0
Compute 32-bit hash of a byte buffer.
A template-based reference counting class.
uint32_t(* Hash32Function_ptr)(const char *, const std::size_t)
Function pointer signatures for basic hash functions.
uint64_t(* Hash64Function_ptr)(const char *, const std::size_t)
Function pointer signatures for basic hash functions.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::SimpleRefCount declaration and template implementation.