A Discrete-Event Network Simulator
API
hash-function.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2012 Lawrence Livermore National Laboratory
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
19  */
20 
21 #ifndef HASHFUNCTION_H
22 #define HASHFUNCTION_H
23 
24 #include <cstring> // memcpy
25 #include "simple-ref-count.h"
26 
34 namespace ns3 {
35 
40 namespace Hash {
41 
47 class Implementation : public SimpleRefCount<Implementation>
48 {
49 public:
64  virtual uint32_t GetHash32 (const char * buffer, const std::size_t size) = 0;
81  virtual uint64_t GetHash64 (const char * buffer, const std::size_t size);
85  virtual void clear (void) = 0;
90  { }
94  virtual ~Implementation ()
95  { }
96 }; // Hashfunction
97 
98 
99 /*--------------------------------------
100  * Hash function implementation
101  * by function pointers and templates
102  */
103 
113 typedef uint32_t (*Hash32Function_ptr) (const char *, const std::size_t);
114 typedef uint64_t (*Hash64Function_ptr) (const char *, const std::size_t);
121 namespace Function {
122 
129 class Hash32 : public Implementation
130 {
131 public:
138  { }
139  uint32_t GetHash32 (const char * buffer, const std::size_t size)
140  {
141  return (*m_fp)(buffer, size);
142  }
143  void clear ()
144  { }
145 
146 private:
148 }; // Hash32
149 
156 class Hash64 : public Implementation
157 {
158 public:
165  { }
166  uint64_t GetHash64 (const char * buffer, const std::size_t size)
167  {
168  return (*m_fp)(buffer, size);
169  }
170  uint32_t GetHash32 (const char * buffer, const std::size_t size)
171  {
172  uint32_t hash32;
173  uint64_t hash64 = GetHash64 (buffer, size);
174 
175  memcpy (&hash32, &hash64, sizeof (hash32));
176  return hash32;
177  }
178  void clear ()
179  { }
180 
181 private:
183 }; // Hash64<Hash64Function_ptr>
184 
185 
186 } // namespace Function
187 
188 } // namespace Hash
189 
190 } // namespace ns3
191 
192 #endif /* HASHFUNCTION_H */
193 
uint32_t(* Hash32Function_ptr)(const char *, const std::size_t)
Function pointer signatures for basic hash functions.
Implementation()
Constructor.
Definition: hash-function.h:89
virtual ~Implementation()
Destructor.
Definition: hash-function.h:94
virtual uint64_t GetHash64(const char *buffer, const std::size_t size)
Compute 64-bit hash of a byte buffer.
Hash function implementation base class.
Definition: hash-function.h:47
virtual uint32_t GetHash32(const char *buffer, const std::size_t size)=0
Compute 32-bit hash of a byte buffer.
uint32_t GetHash32(const char *buffer, const std::size_t size)
Compute 32-bit hash of a byte buffer.
Hash32(Hash32Function_ptr hp)
Constructor from a 32-bit hash function pointer.
void clear()
Restore initial state.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint32_t GetHash32(const char *buffer, const std::size_t size)
Compute 32-bit hash of a byte buffer.
Template for creating a Hash::Implementation from a 32-bit hash function.
virtual void clear(void)=0
Restore initial state.
Hash32Function_ptr m_fp
The hash function.
Template for creating a Hash::Implementation from a 64-bit hash function.
Hash64Function_ptr m_fp
The hash function.
A template-based reference counting class.
void clear()
Restore initial state.
Hash64(Hash64Function_ptr hp)
Constructor from a 64-bit hash function pointer.
ns3::SimpleRefCount declaration and template implementation.
uint64_t(* Hash64Function_ptr)(const char *, const std::size_t)
Function pointer signatures for basic hash functions.
uint64_t GetHash64(const char *buffer, const std::size_t size)
Compute 64-bit hash of a byte buffer.