Generic Hash function interface. More...
#include "hash.h"
Public Member Functions | |
Hasher () | |
Constructor using the default implementation. | |
Hasher (Ptr< Hash::Implementation > hp) | |
Constructor using the supplied implementation. | |
Hasher & | clear () |
Restore initial state. | |
uint32_t | GetHash32 (const char *buffer, const std::size_t size) |
Compute 32-bit hash of a byte buffer. | |
uint32_t | GetHash32 (const std::string s) |
Compute 32-bit hash of a string. | |
uint64_t | GetHash64 (const char *buffer, const std::size_t size) |
Compute 64-bit hash of a byte buffer. | |
uint64_t | GetHash64 (const std::string s) |
Compute 64-bit hash of a string. | |
Private Attributes | |
Ptr< Hash::Implementation > | m_impl |
Hash implementation. | |
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 | ( | ) |
Constructor using the default implementation.
Definition at line 32 of file hash.cc.
References ns3::Create(), m_impl, and NS_ASSERT.
ns3::Hasher::Hasher | ( | Ptr< Hash::Implementation > | hp | ) |
Hasher & ns3::Hasher::clear | ( | ) |
Restore initial state.
Returning this Hasher allows code like this:
Definition at line 45 of file hash.cc.
References clear(), and m_impl.
Referenced by clear(), ns3::tests::IncrementalTestCase::DoHash(), ns3::tests::Fnv1aTestCase::DoRun(), ns3::tests::Hash32FunctionPtrTestCase::DoRun(), ns3::tests::Hash64FunctionPtrTestCase::DoRun(), ns3::tests::Murmur3TestCase::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 225 of file hash.h.
References m_impl, and NS_ASSERT.
Referenced by ns3::tests::IncrementalTestCase::DoHash(), ns3::tests::Fnv1aTestCase::DoRun(), ns3::tests::Hash32FunctionPtrTestCase::DoRun(), ns3::tests::Murmur3TestCase::DoRun(), UniqueTypeIdTestCase::DoRun(), ns3::Hash::Example::Collider::GetHash(), ns3::Hash32(), 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 232 of file hash.h.
References m_impl, and NS_ASSERT.
Referenced by ns3::tests::IncrementalTestCase::DoHash(), ns3::tests::Fnv1aTestCase::DoRun(), ns3::tests::Hash64FunctionPtrTestCase::DoRun(), ns3::tests::Murmur3TestCase::DoRun(), ns3::Hash::Example::Collider::GetHash(), ns3::Hash64(), 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 164 of file hash.h.
Referenced by Hasher(), Hasher(), clear(), GetHash32(), GetHash32(), GetHash64(), and GetHash64().