A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
hash-murmur3.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012 Lawrence Livermore National Laboratory
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
7 */
8
9#ifndef HASH_MURMUR3_H
10#define HASH_MURMUR3_H
11
12#include "hash-function.h"
13
14/**
15 * \file
16 * \ingroup hash
17 * \brief ns3::Hash::Function::Murmur3 declaration.
18 */
19
20namespace ns3
21{
22
23namespace Hash
24{
25
26namespace Function
27{
28
29/**
30 * \ingroup hash
31 *
32 * \brief Murmur3 hash function implementation
33 *
34 * Adapted from http://code.google.com/p/smhasher/
35 *
36 * MurmurHash3 was written by Austin Appleby, and is placed in the public
37 * domain. The author hereby disclaims copyright to this source code.
38
39 * Note - The x86 and x64 versions do _not_ produce the same results, as the
40 * algorithms are optimized for their respective platforms. You can still
41 * compile and run any of them on any platform, but your performance with the
42 * non-native version will be less than optimal.
43 */
44class Murmur3 : public Implementation
45{
46 public:
47 /**
48 * Constructor, clears internal state
49 */
50 Murmur3();
51 /**
52 * Compute 32-bit hash of a byte buffer
53 *
54 * Call clear () between calls to GetHash32() to reset the
55 * internal state and hash each buffer separately.
56 *
57 * If you don't call clear() between calls to GetHash32,
58 * you can hash successive buffers. The final return value
59 * will be the cumulative hash across all calls.
60 *
61 * \param [in] buffer pointer to the beginning of the buffer
62 * \param [in] size length of the buffer, in bytes
63 * \return 32-bit hash of the buffer
64 */
65 uint32_t GetHash32(const char* buffer, const std::size_t size) override;
66 /**
67 * Compute 64-bit hash of a byte buffer.
68 *
69 * Call clear () between calls to GetHash64() to reset the
70 * internal state and hash each buffer separately.
71 *
72 * If you don't call clear() between calls to GetHash64,
73 * you can hash successive buffers. The final return value
74 * will be the cumulative hash across all calls.
75 *
76 * \param [in] buffer pointer to the beginning of the buffer
77 * \param [in] size length of the buffer, in bytes
78 * \return 64-bit hash of the buffer
79 */
80 uint64_t GetHash64(const char* buffer, const std::size_t size) override;
81 /**
82 * Restore initial state
83 */
84 void clear() override;
85
86 private:
87 /**
88 * Seed value
89 *
90 * This has to be a constant for all MPI ranks to generate
91 * the same hash from the same string.
92 */
93 static constexpr auto SEED{0x8BADF00D}; // Ate bad food
94
95 /**
96 * Cache last hash value, and total bytes hashed (needed to finalize),
97 * for incremental hashing
98 */
99 /**@{*/
101 std::size_t m_size32;
102 /**@}*/
103
104 /** murmur3 produces 128-bit hash and state; we use just the first 64-bits. */
105 /**@{*/
106 uint64_t m_hash64[2];
107 std::size_t m_size64;
108 /**@}*/
109
110}; // class Murmur3
111
112} // namespace Function
113
114} // namespace Hash
115
116} // namespace ns3
117
118#endif /* HASH_MURMUR3_H */
Murmur3 hash function implementation.
void clear() override
Restore initial state.
Murmur3()
Constructor, clears internal state.
std::size_t m_size32
Cache last hash value, and total bytes hashed (needed to finalize), for incremental hashing.
uint64_t m_hash64[2]
murmur3 produces 128-bit hash and state; we use just the first 64-bits.
uint32_t GetHash32(const char *buffer, const std::size_t size) override
Compute 32-bit hash of a byte buffer.
uint32_t m_hash32
Cache last hash value, and total bytes hashed (needed to finalize), for incremental hashing.
static constexpr auto SEED
Seed value.
uint64_t GetHash64(const char *buffer, const std::size_t size) override
Compute 64-bit hash of a byte buffer.
std::size_t m_size64
murmur3 produces 128-bit hash and state; we use just the first 64-bits.
Hash function implementation base class.
ns3::Hash::Implementation, ns3::Hash::Function::Hash32 and ns3::Hash::Function::Hash64 declarations.
Every class exported by the ns3 library is enclosed in the ns3 namespace.