A Discrete-Event Network Simulator
API
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 
39 namespace ns3 {
40 
87 class Hasher
88 {
89 public:
93  Hasher ();
114  uint32_t GetHash32 (const char * buffer, const std::size_t size);
129  uint64_t GetHash64 (const char * buffer, const std::size_t size);
130 
144  uint32_t GetHash32 (const std::string s);
158  uint64_t GetHash64 (const std::string s);
173  Hasher & clear (void);
174 
175 private:
177 }; // Hasher
178 
179 
180 /*************************************************
181  ** Global functions declarations
182  ************************************************/
183 
193 uint32_t Hash32 (const char * buffer, const std::size_t size);
203 uint64_t Hash64 (const char * buffer, const std::size_t size);
204 
213 uint32_t Hash32 (const std::string s);
222 uint64_t Hash64 (const std::string s);
223 
224 } // namespace ns3
225 
226 
227 /*************************************************
228  ** Inline implementations for rvo
229  ************************************************/
230 
231 namespace ns3 {
232 
233 /*************************************************
234  class Hasher implementation, inlined for rvo
235 */
236 
237 inline
238 uint32_t
239 Hasher::GetHash32 (const char * buffer, const std::size_t size)
240 {
241  NS_ASSERT (m_impl != 0);
242  return m_impl->GetHash32 (buffer, size);
243 }
244 
245 inline
246 uint64_t
247 Hasher::GetHash64 (const char * buffer, const std::size_t size)
248 {
249  NS_ASSERT (m_impl != 0);
250  return m_impl->GetHash64 (buffer, size);
251 }
252 
253 inline
254 uint32_t
255 Hasher::GetHash32 (const std::string s)
256 {
257  NS_ASSERT (m_impl != 0);
258  return m_impl->GetHash32 (s.c_str (), s.size ());
259 }
260 
261 inline
262 uint64_t
263 Hasher::GetHash64 (const std::string s)
264 {
265  NS_ASSERT (m_impl != 0);
266  return m_impl->GetHash64 (s.c_str (), s.size ());
267 }
268 
269 
270 /*************************************************
271  Global hash functions, inlined for rvo
272 */
273 
278 Hasher& GetStaticHash (void);
279 
280 inline
281 uint32_t
282 Hash32 (const char * buffer, const std::size_t size)
283 {
284  return GetStaticHash ().GetHash32 (buffer, size);
285 }
286 
287 inline
288 uint64_t
289 Hash64 (const char * buffer, const std::size_t size)
290 {
291  return GetStaticHash ().GetHash64 (buffer, size);
292 }
293 
294 inline
295 uint32_t
296 Hash32 (const std::string s)
297 {
298  return GetStaticHash ().GetHash32 (s);
299 }
300 
301 inline
302 uint64_t
303 Hash64 (const std::string s)
304 {
305  return GetStaticHash ().GetHash64 (s);
306 }
307 
308 
309 } // namespace ns3
310 
311 #endif /* HASH_H */
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
Generic Hash function interface.
Definition: hash.h:88
Hasher()
Constructor using the default implementation.
Definition: hash.cc:42
Ptr< Hash::Implementation > m_impl
Hash implementation.
Definition: hash.h:176
uint32_t GetHash32(const char *buffer, const std::size_t size)
Compute 32-bit hash of a byte buffer.
Definition: hash.h:239
uint64_t GetHash64(const char *buffer, const std::size_t size)
Compute 64-bit hash of a byte buffer.
Definition: hash.h:247
Hasher & clear(void)
Restore initial state.
Definition: hash.cc:55
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:67
uint64_t Hash64(const char *buffer, const std::size_t size)
Compute 64-bit hash of a byte buffer, using the default hash function.
Definition: hash.h:289
uint32_t Hash32(const char *buffer, const std::size_t size)
Compute 32-bit hash of a byte buffer, using the default hash function.
Definition: hash.h:282
ns3::Hash::Function::Fnv1a declaration.
ns3::Hash::Implementation, ns3::Hash::Function::Hash32 and ns3::Hash::Function::Hash64 declarations.
ns3::Hash::Function::Murmur3 declaration.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hasher & GetStaticHash(void)
Get a reference to the static global hasher at g_hasher.
Definition: hash.cc:35
ns3::Ptr smart pointer declaration and implementation.