A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ns3::Hasher Class Reference

Generic Hash function interface. More...

#include "hash.h"

+ Collaboration diagram for ns3::Hasher:

Public Member Functions

 Hasher ()
 Constructor using the default implementation.
 
 Hasher (Ptr< Hash::Implementation > hp)
 Constructor using the supplied implementation.
 
Hasherclear ()
 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::Implementationm_impl
 Hash implementation.
 

Detailed Description

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

Hasher hasher = Hasher ( Create<Hash::Function::Fnv1a> () );
Generic Hash function interface.
Definition: hash.h:87
Hasher()
Constructor using the default implementation.
Definition: hash.cc:43
uint32_t GetHash32(const char *buffer, const std::size_t size)
Compute 32-bit hash of a byte buffer.
Definition: hash.h:236
uint8_t data[writeSize]

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.

Internal:

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.

Definition at line 86 of file hash.h.

Constructor & Destructor Documentation

◆ Hasher() [1/2]

ns3::Hasher::Hasher ( )

Constructor using the default implementation.

Definition at line 43 of file hash.cc.

References m_impl, and NS_ASSERT.

◆ Hasher() [2/2]

ns3::Hasher::Hasher ( Ptr< Hash::Implementation hp)

Constructor using the supplied implementation.

Parameters
[in]hpPtr<Hash::Implementation> to the desired implementation.

Definition at line 49 of file hash.cc.

References m_impl, and NS_ASSERT.

Member Function Documentation

◆ clear()

Hasher & ns3::Hasher::clear ( )

Restore initial state.

Returning this Hasher allows code like this:

h.GetHash32 (...);
...
h.clear ().GetHash64 (...);
Returns
This hasher.

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().

+ Here is the caller graph for this function:

◆ GetHash32() [1/2]

uint32_t ns3::Hasher::GetHash32 ( const char *  buffer,
const std::size_t  size 
)
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.

Parameters
[in]bufferPointer to the beginning of the buffer.
[in]sizeLength of the buffer, in bytes.
Returns
32-bit hash of the buffer..

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().

+ Here is the caller graph for this function:

◆ GetHash32() [2/2]

uint32_t ns3::Hasher::GetHash32 ( const std::string  s)
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.

Parameters
[in]sString to hash.
Returns
32-bit hash of the string.

Definition at line 250 of file hash.h.

References m_impl, and NS_ASSERT.

◆ GetHash64() [1/2]

uint64_t ns3::Hasher::GetHash64 ( const char *  buffer,
const std::size_t  size 
)
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.

Parameters
[in]bufferPointer to the beginning of the buffer.
[in]sizeLength of the buffer, in bytes.
Returns
64-bit hash of the buffer.

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().

+ Here is the caller graph for this function:

◆ GetHash64() [2/2]

uint64_t ns3::Hasher::GetHash64 ( const std::string  s)
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.

Parameters
[in]sString to hash.
Returns
64-bit hash of the string.

Definition at line 257 of file hash.h.

References m_impl, and NS_ASSERT.

Member Data Documentation

◆ m_impl

Ptr<Hash::Implementation> ns3::Hasher::m_impl
private

Hash implementation.

Definition at line 175 of file hash.h.

Referenced by Hasher(), clear(), GetHash32(), and GetHash64().


The documentation for this class was generated from the following files: