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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
18 */
19
20#ifndef HASHFUNCTION_H
21#define HASHFUNCTION_H
22
23#include "simple-ref-count.h"
24
25#include <cstring> // memcpy
26
27/**
28 * \file
29 * \ingroup hash
30 * \brief ns3::Hash::Implementation, ns3::Hash::Function::Hash32 and
31 * ns3::Hash::Function::Hash64 declarations.
32 */
33
34namespace ns3
35{
36
37/**
38 * \ingroup hash
39 * Hash function implementations
40 */
41namespace Hash
42{
43
44/**
45 * \ingroup hash
46 *
47 * \brief Hash function implementation base class.
48 */
49class Implementation : public SimpleRefCount<Implementation>
50{
51 public:
52 /**
53 * Compute 32-bit hash of a byte buffer
54 *
55 * Call clear() between calls to GetHash32() to reset the
56 * internal state and hash each buffer separately.
57 *
58 * If you don't call clear() between calls to GetHash32,
59 * you can hash successive buffers. The final return value
60 * will be the cumulative hash across all calls.
61 *
62 * \param [in] buffer Pointer to the beginning of the buffer.
63 * \param [in] size Length of the buffer, in bytes.
64 * \return 32-bit hash of the buffer.
65 */
66 virtual uint32_t GetHash32(const char* buffer, const std::size_t size) = 0;
67 /**
68 * Compute 64-bit hash of a byte buffer.
69 *
70 * Default implementation returns 32-bit hash, with a warning.
71 *
72 * Call clear() between calls to GetHash64() to reset the
73 * internal state and hash each buffer separately.
74 *
75 * If you don't call clear() between calls to GetHash64,
76 * you can hash successive buffers. The final return value
77 * will be the cumulative hash across all calls.
78 *
79 * \param [in] buffer Pointer to the beginning of the buffer.
80 * \param [in] size Length of the buffer, in bytes.
81 * \return 64-bit hash of the buffer.
82 */
83 virtual uint64_t GetHash64(const char* buffer, const std::size_t size);
84 /**
85 * Restore initial state.
86 */
87 virtual void clear() = 0;
88
89 /**
90 * Constructor.
91 */
93 {
94 }
95
96 /**
97 * Destructor.
98 */
100 {
101 }
102}; // Hashfunction
103
104/*--------------------------------------
105 * Hash function implementation
106 * by function pointers and templates
107 */
108
109/**
110 *
111 * \ingroup hash
112 *
113 * \brief Function pointer signatures for basic hash functions.
114 *
115 * See Hash::Function::Hash32 or Hash::Function::Hash64
116 * @{
117 */
118typedef uint32_t (*Hash32Function_ptr)(const char*, const std::size_t);
119typedef uint64_t (*Hash64Function_ptr)(const char*, const std::size_t);
120
121/**@}*/
122
123/**
124 * \ingroup hash
125 * Hash functions.
126 */
127namespace Function
128{
129
130/**
131 * \ingroup hash
132 *
133 * \brief Template for creating a Hash::Implementation from
134 * a 32-bit hash function.
135 */
136class Hash32 : public Implementation
137{
138 public:
139 /**
140 * Constructor from a 32-bit hash function pointer.
141 *
142 * \param [in] hp Function pointer to a 32-bit hash function.
143 */
145 : m_fp(hp)
146 {
147 }
148
149 uint32_t GetHash32(const char* buffer, const std::size_t size) override
150 {
151 return (*m_fp)(buffer, size);
152 }
153
154 void clear() override
155 {
156 }
157
158 private:
159 Hash32Function_ptr m_fp; /**< The hash function. */
160}; // Hash32
161
162/**
163 * \ingroup hash
164 *
165 * \brief Template for creating a Hash::Implementation from
166 * a 64-bit hash function.
167 */
168class Hash64 : public Implementation
169{
170 public:
171 /**
172 * Constructor from a 64-bit hash function pointer.
173 *
174 * \param [in] hp Function pointer to a 64-bit hash function.
175 */
177 : m_fp(hp)
178 {
179 }
180
181 uint64_t GetHash64(const char* buffer, const std::size_t size) override
182 {
183 return (*m_fp)(buffer, size);
184 }
185
186 uint32_t GetHash32(const char* buffer, const std::size_t size) override
187 {
188 uint32_t hash32;
189 uint64_t hash64 = GetHash64(buffer, size);
190
191 memcpy(&hash32, &hash64, sizeof(hash32));
192 return hash32;
193 }
194
195 void clear() override
196 {
197 }
198
199 private:
200 Hash64Function_ptr m_fp; /**< The hash function. */
201}; // Hash64<Hash64Function_ptr>
202
203} // namespace Function
204
205} // namespace Hash
206
207} // namespace ns3
208
209#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.
Definition: hash-function.h:50
Implementation()
Constructor.
Definition: hash-function.h:92
virtual uint64_t GetHash64(const char *buffer, const std::size_t size)
Compute 64-bit hash of a byte buffer.
virtual ~Implementation()
Destructor.
Definition: hash-function.h:99
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.