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;
93  virtual ~Implementation () { };
94 }; // Hashfunction
95 
96 
97 /*--------------------------------------
98  * Hash function implementation
99  * by function pointers and templates
100  */
101 
111 typedef uint32_t (*Hash32Function_ptr) (const char *, const std::size_t);
112 typedef uint64_t (*Hash64Function_ptr) (const char *, const std::size_t);
119 namespace Function {
120 
127 class Hash32 : public Implementation
128 {
129 public:
135  Hash32 (Hash32Function_ptr hp) : m_fp (hp) { };
136  uint32_t GetHash32 (const char * buffer, const std::size_t size)
137  {
138  return (*m_fp) (buffer, size);
139  }
140  void clear () { };
141 private:
143 }; // Hash32
144 
151 class Hash64 : public Implementation
152 {
153 public:
159  Hash64 (Hash64Function_ptr hp) : m_fp (hp) { };
160  uint64_t GetHash64 (const char * buffer, const std::size_t size)
161  {
162  return (*m_fp) (buffer, size);
163  }
164  uint32_t GetHash32 (const char * buffer, const std::size_t size)
165  {
166  uint32_t hash32;
167  uint64_t hash64 = GetHash64 (buffer, size);
168 
169  memcpy (&hash32, &hash64, sizeof (hash32));
170  return hash32;
171  }
172  void clear () { };
173 private:
175 }; // Hash64<Hash64Function_ptr>
176 
177 
178 } // namespace Function
179 
180 } // namespace Hash
181 
182 } // namespace ns3
183 
184 #endif /* HASHFUNCTION_H */
185 
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:93
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.