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
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 */
109typedef uint32_t (*Hash32Function_ptr)(const char*, const std::size_t);
110typedef uint64_t (*Hash64Function_ptr)(const char*, const std::size_t);
111
112/**@}*/
113
114/**
115 * @ingroup hash
116 * Hash functions.
117 */
118namespace Function
119{
120
121/**
122 * @ingroup hash
123 *
124 * @brief Template for creating a Hash::Implementation from
125 * a 32-bit hash function.
126 */
127class 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 */
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 */
161class 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 */
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 */
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.