A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
hash.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 HASH_H
22 #define HASH_H
23 
24 #include <string>
25 
26 #include "assert.h"
27 #include "ptr.h"
28 
29 #include "hash-function.h"
30 #include "hash-murmur3.h"
31 #include "hash-fnv.h"
32 
33 namespace ns3 {
34 
78 class Hasher
79 {
80 public:
84  Hasher ();
105  uint32_t GetHash32 (const char * buffer, const size_t size);
120  uint64_t GetHash64 (const char * buffer, const size_t size);
121 
135  uint32_t GetHash32 (const std::string s);
149  uint64_t GetHash64 (const std::string s);
155  Hasher & clear (void);
156 
157 private:
159 }; // Hasher
160 
161 
162 /*************************************************
163  ** Global functions declarations
164  ************************************************/
165 
175 uint32_t Hash32 (const char * buffer, const size_t size);
185 uint64_t Hash64 (const char * buffer, const size_t size);
186 
195 uint32_t Hash32 (const std::string s);
204 uint64_t Hash64 (const std::string s);
205 
206 } // namespace ns3
207 
208 
209 /*************************************************
210  ** Inline implementations for rvo
211  ************************************************/
212 
213 namespace ns3 {
214 
215 /*************************************************
216  class Hasher implementation, inlined for rvo
217 */
218 
219 inline
220 uint32_t
221 Hasher::GetHash32 (const char * buffer, const size_t size)
222 {
223  NS_ASSERT (m_impl != 0);
224  return m_impl->GetHash32 (buffer, size);
225 }
226 
227 inline
228 uint64_t
229 Hasher::GetHash64 (const char * buffer, const size_t size)
230 {
231  NS_ASSERT (m_impl != 0);
232  return m_impl->GetHash64 (buffer, size);
233 }
234 
235 inline
236 uint32_t
237 Hasher::GetHash32 (const std::string s)
238 {
239  NS_ASSERT (m_impl != 0);
240  return m_impl->GetHash32 (s.c_str (), s.size ());
241 }
242 
243 inline
244 uint64_t
245 Hasher::GetHash64 (const std::string s)
246 {
247  NS_ASSERT (m_impl != 0);
248  return m_impl->GetHash64 (s.c_str (), s.size ());
249 }
250 
251 
252 /*************************************************
253  Global hash functions, inlined for rvo
254 */
255 
256 inline
257 uint32_t
258 Hash32 (const char * buffer, const size_t size)
259 {
260  return Hasher ().GetHash32 (buffer, size);
261 }
262 
263 inline
264 uint64_t
265 Hash64 (const char * buffer, const size_t size)
266 {
267  return Hasher ().GetHash64 (buffer, size);
268 }
269 
270 inline
271 uint32_t
272 Hash32 (const std::string s)
273 {
274  return Hasher ().GetHash32 (s);
275 }
276 
277 inline
278 uint64_t
279 Hash64 (const std::string s)
280 {
281  return Hasher ().GetHash64 (s);
282 }
283 
284 
285 } // namespace ns3
286 
287 #endif /* HASH_H */
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
virtual uint64_t GetHash64(const char *buffer, const size_t size)
Compute 64-bit hash of a byte buffer.
Hasher()
Constructor using the default implementation.
Definition: hash.cc:29
Ptr< SampleEmitter > s
uint32_t GetHash32(const char *buffer, const size_t size)
Compute 32-bit hash of a byte buffer.
Definition: hash.h:221
uint32_t Hash32(const char *buffer, const size_t size)
Compute 32-bit hash of a byte buffer, using the default hash function.
Definition: hash.h:258
Hasher & clear(void)
Restore initial state.
Definition: hash.cc:42
virtual uint32_t GetHash32(const char *buffer, const size_t size)=0
Compute 32-bit hash of a byte buffer.
uint64_t Hash64(const char *buffer, const size_t size)
Compute 64-bit hash of a byte buffer, using the default hash function.
Definition: hash.h:265
Ptr< Hash::Implementation > m_impl
Definition: hash.h:158
uint64_t GetHash64(const char *buffer, const size_t size)
Compute 64-bit hash of a byte buffer.
Definition: hash.h:229
Generic Hash function interface.
Definition: hash.h:78