C/C++ API Reference
Loading...
Searching...
No Matches
pw::HashState Class Reference

Overview

A stateful helper class for accumulating and mixing hash values.

HashState encapsulates the bitwise operations required to combine multiple values into a single hash. It uses move semantics to enforce a linear chain of operations, ensuring that intermediate states are not accidentally reused.

Public Member Functions

 HashState (size_t seed=17)
 
 HashState (const HashState &)=delete
 
HashStateoperator= (const HashState &)=delete
 
 HashState (HashState &&)=default
 
HashStateoperator= (HashState &&)=default
 
size_t finalize ()
 Finalizes the hash calculation and returns the result.
 

Static Public Member Functions

static HashState mix (HashState &&h, size_t val)
 
template<typename T >
static HashState combine (HashState &&h, const T &val)
 
template<typename T , typename... Ts>
static HashState combine (HashState &&h, const T &first, const Ts &... rest)
 
template<typename T >
static HashState combine_contiguous (HashState &&h, const T *data, size_t size)
 
template<typename InputIt >
static HashState combine_unordered (HashState &&h, InputIt first, InputIt last)
 

Constructor & Destructor Documentation

◆ HashState()

pw::HashState::HashState ( size_t  seed = 17)
inlineexplicit

Initializes the hash state with a seed.

Parameters
seedAn initial value to randomize the hash. Defaults to 17.

Member Function Documentation

◆ combine() [1/2]

template<typename T , typename... Ts>
static HashState pw::HashState::combine ( HashState &&  h,
const T &  first,
const Ts &...  rest 
)
inlinestatic

Recursively combines multiple values into the hash state.

This is the primary utility for users implementing PwHashValue. It allows you to hash all members of a struct in a single call.

Example:

class MyType {
public:
// Defined as an inline friend (Hidden Friend idiom).
friend HashState PwHashValue(HashState&& h, const MyType& v) {
return HashState::combine(std::move(h), v.x_, v.y_, v.z_);
}
private:
int x_, y_, z_;
};
Definition: functional.h:100
H PwHashValue(H h, const T &val)=delete

◆ combine() [2/2]

template<typename T >
static HashState pw::HashState::combine ( HashState &&  h,
const T &  val 
)
inlinestatic

Combines a single value into the hash state.

This function acts as the gateway to the hash_value_dispatcher. It automatically detects if val should be hashed via a custom PwHashValue function, as a pointer, or via std::hash.

◆ combine_contiguous()

template<typename T >
static HashState pw::HashState::combine_contiguous ( HashState &&  h,
const T *  data,
size_t  size 
)
inlinestatic

Combines a contiguous range of elements into the hash state.

This is intended for arrays or buffers where the order of elements matters.

Note
This implementation currently iterates through the range and combines elements one-by-one. It is NOT yet optimized for bulk hashing (e.g., using CRC32 or SIMD) even for trivially copyable types like uint8_t.
Parameters
hThe current state (passed by move).
dataA pointer to the first element in the range.
sizeThe number of elements to hash.
Returns
The updated HashState.

◆ combine_unordered()

template<typename InputIt >
static HashState pw::HashState::combine_unordered ( HashState &&  h,
InputIt  first,
InputIt  last 
)
inlinestatic

Combines a range of elements where the order does not affect the output.

This is intended for unordered containers like sets or maps. It works by hashing each element independently and combining them using commutative operations (addition), ensuring that {A, B} results in the same hash as {B, A}.

Parameters
hThe current state (passed by move).
firstThe starting iterator of the range.
lastThe ending iterator of the range.
Returns
The updated HashState including the combined range and its size.

◆ mix()

static HashState pw::HashState::mix ( HashState &&  h,
size_t  val 
)
inlinestatic

Mixes a raw integer value into the current hash state.

This implementation is based on boost::hash_combine.

Parameters
hThe current state (passed by move).
valThe raw integer value to mix in.
Returns
The new updated HashState.

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