A Discrete-Event Network Simulator
API
hash.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Lawrence Livermore National Laboratory
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
18  */
19 
20 #ifndef HASH_H
21 #define HASH_H
22 
23 #include "assert.h"
24 #include "hash-fnv.h"
25 #include "hash-function.h"
26 #include "hash-murmur3.h"
27 #include "ptr.h"
28 
29 #include <string>
30 
37 namespace ns3
38 {
39 
86 class Hasher
87 {
88  public:
92  Hasher();
113  uint32_t GetHash32(const char* buffer, const std::size_t size);
128  uint64_t GetHash64(const char* buffer, const std::size_t size);
129 
143  uint32_t GetHash32(const std::string s);
157  uint64_t GetHash64(const std::string s);
172  Hasher& clear();
173 
174  private:
176 }; // Hasher
177 
178 /*************************************************
179  ** Global functions declarations
180  ************************************************/
181 
191 uint32_t Hash32(const char* buffer, const std::size_t size);
201 uint64_t Hash64(const char* buffer, const std::size_t size);
202 
211 uint32_t Hash32(const std::string s);
220 uint64_t Hash64(const std::string s);
221 
222 } // namespace ns3
223 
224 /*************************************************
225  ** Inline implementations for rvo
226  ************************************************/
227 
228 namespace ns3
229 {
230 
231 /*************************************************
232  class Hasher implementation, inlined for rvo
233 */
234 
235 inline uint32_t
236 Hasher::GetHash32(const char* buffer, const std::size_t size)
237 {
238  NS_ASSERT(m_impl);
239  return m_impl->GetHash32(buffer, size);
240 }
241 
242 inline uint64_t
243 Hasher::GetHash64(const char* buffer, const std::size_t size)
244 {
245  NS_ASSERT(m_impl);
246  return m_impl->GetHash64(buffer, size);
247 }
248 
249 inline uint32_t
250 Hasher::GetHash32(const std::string s)
251 {
252  NS_ASSERT(m_impl);
253  return m_impl->GetHash32(s.c_str(), s.size());
254 }
255 
256 inline uint64_t
257 Hasher::GetHash64(const std::string s)
258 {
259  NS_ASSERT(m_impl);
260  return m_impl->GetHash64(s.c_str(), s.size());
261 }
262 
263 /*************************************************
264  Global hash functions, inlined for rvo
265 */
266 
272 
273 inline uint32_t
274 Hash32(const char* buffer, const std::size_t size)
275 {
276  return GetStaticHash().GetHash32(buffer, size);
277 }
278 
279 inline uint64_t
280 Hash64(const char* buffer, const std::size_t size)
281 {
282  return GetStaticHash().GetHash64(buffer, size);
283 }
284 
285 inline uint32_t
286 Hash32(const std::string s)
287 {
288  return GetStaticHash().GetHash32(s);
289 }
290 
291 inline uint64_t
292 Hash64(const std::string s)
293 {
294  return GetStaticHash().GetHash64(s);
295 }
296 
297 } // namespace ns3
298 
299 #endif /* HASH_H */
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
Generic Hash function interface.
Definition: hash.h:87
Hasher()
Constructor using the default implementation.
Definition: hash.cc:43
Ptr< Hash::Implementation > m_impl
Hash implementation.
Definition: hash.h:175
uint32_t GetHash32(const char *buffer, const std::size_t size)
Compute 32-bit hash of a byte buffer.
Definition: hash.h:236
uint64_t GetHash64(const char *buffer, const std::size_t size)
Compute 64-bit hash of a byte buffer.
Definition: hash.h:243
Hasher & clear()
Restore initial state.
Definition: hash.cc:56
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
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:280
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:274
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()
Get a reference to the static global hasher at g_hasher.
Definition: hash.cc:36
ns3::Ptr smart pointer declaration and implementation.