A Discrete-Event Network Simulator
API
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 
34 namespace ns3
35 {
36 
41 namespace Hash
42 {
43 
49 class Implementation : public SimpleRefCount<Implementation>
50 {
51  public:
66  virtual uint32_t GetHash32(const char* buffer, const std::size_t size) = 0;
83  virtual uint64_t GetHash64(const char* buffer, const std::size_t size);
87  virtual void clear() = 0;
88 
93  {
94  }
95 
99  virtual ~Implementation()
100  {
101  }
102 }; // Hashfunction
103 
104 /*--------------------------------------
105  * Hash function implementation
106  * by function pointers and templates
107  */
108 
118 typedef uint32_t (*Hash32Function_ptr)(const char*, const std::size_t);
119 typedef uint64_t (*Hash64Function_ptr)(const char*, const std::size_t);
120 
127 namespace Function
128 {
129 
136 class Hash32 : public Implementation
137 {
138  public:
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:
160 }; // Hash32
161 
168 class Hash64 : public Implementation
169 {
170  public:
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:
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.