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

Generic Hash function interface.
Definition hash.h:76
Hasher()
Constructor using the default implementation.
Definition hash.cc:32
uint32_t GetHash32(const char *buffer, const std::size_t size)
Compute 32-bit hash of a byte buffer.
Definition hash.h:225
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
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 75 of file hash.h.

Constructor & Destructor Documentation

◆ Hasher() [1/2]

ns3::Hasher::Hasher ( )

Constructor using the default implementation.

Definition at line 32 of file hash.cc.

References ns3::Create(), m_impl, and NS_ASSERT.

+ Here is the call graph for this function:

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

+ Here is the call graph for this function:
+ 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 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().

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

+ 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 246 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 164 of file hash.h.

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


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