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
34namespace ns3 {
35
40namespace Hash {
41
47class Implementation : public SimpleRefCount<Implementation>
48{
49public:
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
113typedef uint32_t (*Hash32Function_ptr) (const char *, const std::size_t);
114typedef uint64_t (*Hash64Function_ptr) (const char *, const std::size_t);
121namespace Function {
122
129class Hash32 : public Implementation
130{
131public:
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
146private:
148}; // Hash32
149
156class Hash64 : public Implementation
157{
158public:
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
181private:
183}; // Hash64<Hash64Function_ptr>
184
185
186} // namespace Function
187
188} // namespace Hash
189
190} // namespace ns3
191
192#endif /* HASHFUNCTION_H */
193
Template for creating a Hash::Implementation from a 32-bit hash function.
Hash32(Hash32Function_ptr hp)
Constructor from a 32-bit hash function pointer.
Hash32Function_ptr m_fp
The hash function.
uint32_t GetHash32(const char *buffer, const std::size_t size)
Compute 32-bit hash of a byte buffer.
void clear()
Restore initial state.
Template for creating a Hash::Implementation from a 64-bit hash function.
Hash64Function_ptr m_fp
The hash function.
uint32_t GetHash32(const char *buffer, const std::size_t size)
Compute 32-bit hash of a byte buffer.
void clear()
Restore initial state.
uint64_t GetHash64(const char *buffer, const std::size_t size)
Compute 64-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:48
Implementation()
Constructor.
Definition: hash-function.h:89
virtual void clear(void)=0
Restore initial state.
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:94
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.