Interface to help hash various types through a hasher type.

Synopsis

Declared in <llvm/Support/HashBuilder.h>

template<
    typename HasherT,
    llvm::endianness Endianness>
class HashBuilder
    : public HashBuilderBase<HasherT>

Description

Via provided specializations of add, addRange, and addRangeElements functions, various types (e.g. ArrayRef, StringRef, etc.) can be hashed without requiring any knowledge of hashed types from the hasher type.

The only method expected from the templated hasher type HasherT is: * void update(ArrayRef<uint8_t> Data)

Additionally, the following methods will be forwarded to the hasher type: * decltype(std::declval<HasherT &>().final()) final() * decltype(std::declval<HasherT &>().result()) result()

From a user point of view, the interface provides the following: * template<typename T> add(const T &Value) The add function implements hashing of various types. * template <typename ItT> void addRange(ItT First, ItT Last) The addRange function is designed to aid hashing a range of values. It explicitly adds the size of the range in the hash. * template <typename ItT> void addRangeElements(ItT First, ItT Last) The addRangeElements function is also designed to aid hashing a range of values. In contrast to addRange, it ignores the size of the range, behaving as if elements were added one at a time with add.

User‐defined struct types can participate in this interface by providing an addHash templated function. See the associated template specialization for details.

This interface does not impose requirements on the hasher update(ArrayRef<uint8_t> Data) method. We want to avoid collisions for variable‐size types; for example for ` builder.add({1}); builder.add({2, 3}); ` and ` builder.add({1, 2}); builder.add({3}); ` . Thus, specializations of add and addHash for variable‐size types must not assume that the hasher type considers the size as part of the hash; they must explicitly add the size to the hash. See for example specializations for ArrayRef and StringRef.

Additionally, since types are eventually forwarded to the hasher's void update(ArrayRef<uint8_t>) method, endianness plays a role in the hash computation (for example when computing add((int)123)). Specifiying a non‐`native` Endianness template parameter allows to compute stable hash across platforms with different endianness.

Base Classes

Name

Description

HashBuilderBase<HasherT>

Declares the hasher member, and functions forwarding directly to the hasher.

Type Aliases

Name

Description

HasAddHashT

Detect whether addHash can be called for type T.

HasByteSwapT

Detect whether support::endian::byte_swap can be called for type T.

HashResultTy

Result type of HasherT::final().

Member Functions

Name

Description

HashBuilder [constructor]

Constructors

add

add overloads

addRange

Add a range of values, including the range size, to the hash.

addRangeElements

Add the elements of a range to the hash, ignoring the range size.

adjustForEndiannessAndAdd

Adjust Value for the target endianness and add it to the hash.

final

Forward to HasherT::final() if available.

getHasher

Return a reference to the underlying hasher.

result

Forward to HasherT::result() if available.

update

Forward to HasherT::update(ArrayRef<uint8_t>).

Non-Member Functions

Name

Description

addHash

Feed version components of VT into hash builder HBuilder.

Created with MrDocs