Generic Hash function interface. More...
#include "hash.h"
Public Member Functions | |
Hasher () | |
Constructor using the default implementation. More... | |
Hasher (Ptr< Hash::Implementation > hp) | |
Constructor using the supplied implementation. More... | |
Hasher & | clear () |
Restore initial state. More... | |
uint32_t | GetHash32 (const char *buffer, const std::size_t size) |
Compute 32-bit hash of a byte buffer. More... | |
uint32_t | GetHash32 (const std::string s) |
Compute 32-bit hash of a string. More... | |
uint64_t | GetHash64 (const char *buffer, const std::size_t size) |
Compute 64-bit hash of a byte buffer. More... | |
uint64_t | GetHash64 (const std::string s) |
Compute 64-bit hash of a string. More... | |
Private Attributes | |
Ptr< Hash::Implementation > | m_impl |
Hash implementation. More... | |
Generic Hash function interface.
This class provides a generic interface for computing hashes of buffers. Various getters return hashes of different lengths.
Call clear() between calls to the getter to reset the internal state and hash each buffer separately.
If you don't call clear() between calls to the getter you can hash successive buffers. The final return value will be the cumulative hash across all calls.
The choice of hash function can be made at construction by
The available implementations are documented in Hash Functions. The default implementation is Murmur3. FNV1a is also available.
In addition to this class interface, global functions are defined which use the default hash implementation.
Would be nice to offer longer hashes. uint128_t
looks doable, except that our fallback int64x64_t
implementation doesn't offer unsigned
.
Longer hashes require returning a byte buffer of some sort, but our Buffer class seems a bit overkill for this case.
ns3::Hasher::Hasher | ( | ) |
ns3::Hasher::Hasher | ( | Ptr< Hash::Implementation > | hp | ) |
Hasher & ns3::Hasher::clear | ( | ) |
Restore initial state.
Returning this Hasher allows code like this:
Definition at line 56 of file hash.cc.
References m_impl.
Referenced by ns3::tests::IncrementalTestCase::DoHash(), ns3::tests::Fnv1aTestCase::DoRun(), ns3::tests::Murmur3TestCase::DoRun(), ns3::tests::Hash32FunctionPtrTestCase::DoRun(), ns3::tests::Hash64FunctionPtrTestCase::DoRun(), UniqueTypeIdTestCase::DoRun(), ns3::Hash::Example::Collider::GetHash(), ns3::GetStaticHash(), ns3::IidManager::Hasher(), and ns3::Hash::Example::Dictionary::TimeOne().
|
inline |
Compute 32-bit hash of a byte buffer.
Call clear () between calls to GetHash32() to reset the internal state and hash each buffer separately.
If you don't call clear() between calls to GetHash32, you can hash successive buffers. The final return value will be the cumulative hash across all calls.
[in] | buffer | Pointer to the beginning of the buffer. |
[in] | size | Length of the buffer, in bytes. |
Definition at line 236 of file hash.h.
References m_impl, and NS_ASSERT.
Referenced by ns3::tests::IncrementalTestCase::DoHash(), ns3::tests::Fnv1aTestCase::DoRun(), ns3::tests::Murmur3TestCase::DoRun(), ns3::tests::Hash32FunctionPtrTestCase::DoRun(), UniqueTypeIdTestCase::DoRun(), ns3::Hash::Example::Collider::GetHash(), ns3::Hash32(), ns3::IidManager::Hasher(), and ns3::Hash::Example::Dictionary::TimeOne().
|
inline |
Compute 32-bit hash of a string.
Call clear () between calls to GetHash32() to reset the internal state and hash each string separately.
If you don't call clear() between calls to GetHash32, you can hash successive strings. The final return value will be the cumulative hash across all calls.
[in] | s | String to hash. |
|
inline |
Compute 64-bit hash of a byte buffer.
Call clear () between calls to GetHash64() to reset the internal state and hash each buffer separately.
If you don't call clear() between calls to GetHash64, you can hash successive buffers. The final return value will be the cumulative hash across all calls.
[in] | buffer | Pointer to the beginning of the buffer. |
[in] | size | Length of the buffer, in bytes. |
Definition at line 243 of file hash.h.
References m_impl, and NS_ASSERT.
Referenced by ns3::tests::IncrementalTestCase::DoHash(), ns3::tests::Fnv1aTestCase::DoRun(), ns3::tests::Murmur3TestCase::DoRun(), ns3::tests::Hash64FunctionPtrTestCase::DoRun(), ns3::Hash::Example::Collider::GetHash(), and ns3::Hash64().
|
inline |
Compute 64-bit hash of a string.
Call clear () between calls to GetHash64() to reset the internal state and hash each string separately.
If you don't call clear() between calls to GetHash64, you can hash successive strings. The final return value will be the cumulative hash across all calls.
[in] | s | String to hash. |
|
private |
Hash implementation.
Definition at line 175 of file hash.h.
Referenced by Hasher(), clear(), GetHash32(), and GetHash64().