A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
27 namespace ns3 {
28 
33 namespace Hash {
34 
40 class Implementation : public SimpleRefCount<Implementation>
41 {
42 public:
57  virtual uint32_t GetHash32 (const char * buffer, const size_t size) = 0;
74  virtual uint64_t GetHash64 (const char * buffer, const size_t size);
78  virtual void clear (void) = 0;
86  virtual ~Implementation () { };
87 }; // Hashfunction
88 
89 
90 /*--------------------------------------
91  * Hash function implementation
92  * by function pointers and templates
93  */
94 
104 typedef uint32_t (*Hash32Function_ptr) (const char *, const size_t);
105 typedef uint64_t (*Hash64Function_ptr) (const char *, const size_t);
112 namespace Function {
113 
119 class Hash32 : public Implementation
120 {
121 public:
122  Hash32 (Hash32Function_ptr hp) : m_fp (hp) { };
123  uint32_t GetHash32 (const char * buffer, const size_t size)
124  {
125  return (*m_fp) (buffer, size);
126  }
127  void clear () { };
128 private:
130 }; // Hash32
131 
137 class Hash64 : public Implementation
138 {
139 public:
140  Hash64 (Hash64Function_ptr hp) : m_fp (hp) { };
141  uint64_t GetHash64 (const char * buffer, const size_t size)
142  {
143  return (*m_fp) (buffer, size);
144  }
145  uint32_t GetHash32 (const char * buffer, const size_t size)
146  {
147  uint32_t hash32;
148  uint64_t hash64 = GetHash64 (buffer, size);
149 
150  memcpy (&hash32, &hash64, sizeof (hash32));
151  return hash32;
152  }
153  void clear () { };
154 private:
156 }; // Hash64<Hash64Function_ptr>
157 
158 
159 } // namespace Function
160 
161 } // namespace Hash
162 
163 } // namespace ns3
164 
165 #endif /* HASHFUNCTION_H */
166 
Implementation()
Constructor.
Definition: hash-function.h:82
virtual ~Implementation()
Destructor.
Definition: hash-function.h:86
uint32_t GetHash32(const char *buffer, const size_t size)
Compute 32-bit hash of a byte buffer.
uint64_t(* Hash64Function_ptr)(const char *, const size_t)
Basic hash function typedefs.
Hash function implementation base class.
Definition: hash-function.h:40
virtual uint64_t GetHash64(const char *buffer, const size_t size)
Compute 64-bit hash of a byte buffer.
Hash32(Hash32Function_ptr hp)
void clear()
Restore initial state.
Template for Hashfunctions from 32-bit hash functions.
virtual void clear(void)=0
Restore initial state.
virtual uint32_t GetHash32(const char *buffer, const size_t size)=0
Compute 32-bit hash of a byte buffer.
uint64_t GetHash64(const char *buffer, const size_t size)
Compute 64-bit hash of a byte buffer.
uint32_t(* Hash32Function_ptr)(const char *, const size_t)
Basic hash function typedefs.
Hash32Function_ptr m_fp
Template for Hashfunctions from 64-bit hash functions.
Hash64Function_ptr m_fp
A template-based reference counting class.
void clear()
Restore initial state.
Hash64(Hash64Function_ptr hp)
uint32_t GetHash32(const char *buffer, const size_t size)
Compute 32-bit hash of a byte buffer.