Skip to content

Engine → Core → flat_hash_map.h

detail::hash_table

Syntax

template <typename Value, typename KeyOf, typename Hash, typename Eq> class hash_table

Constructors

AccessSignatureDescription
publichash_table() = default;
publicexplicit hash_table( usize initialCapacity);
publichash_table( const hash_table &other);
publichash_table( hash_table &&other) noexcept;

Destructors

AccessSignatureDescription
public~hash_table();

Variables

AccessNameTypeDefaultDescription
privateHASH_MASKstatic constexpr u320x7FFFFFFFu
privatemAnchorusize0
publicMAX_CAPACITYstatic constexpr usizeusize{1} << 31Largest number of buckets: the stored hash keeps 31 bits, so the home bucket is addressable for every capacity up to 2^31.
publicMAX_LOAD_FACTORstatic constexpr f320.875f
privatemCapacityusize0
privatemEqualEq{}
privatemGrowAtusize0
privatemHashHash{}
publicMIN_CAPACITYstatic constexpr usize8
privatemMaskusize0
privatemMetau32 *nullptr
privatemSizeusize0
privatemValuesValue *nullptr
privateNO_SLOTstatic constexpr usizestd::numeric_limits<usize>::max()
privateOCCUPIED_BITstatic constexpr u320x80000000u

Functions

Public

begin

[[nodiscard]] iterator begin() noexcept;
[[nodiscard]] const_iterator begin() const noexcept;

capacity

[[nodiscard]] usize capacity() const noexcept;

Buckets (a power of two, 0 before the first insertion).

clear

void clear() noexcept;

Destroys every element; keeps the table so capacity() is unchanged.

contains

[[nodiscard]] bool contains(
    const key_type &key) const noexcept;

empty

[[nodiscard]] bool empty() const noexcept;

end

[[nodiscard]] iterator end() noexcept;
[[nodiscard]] const_iterator end() const noexcept;

erase

bool erase(
    const key_type &key) noexcept;

Removes the element with key. Returns false when absent. Never rehashes.

iterator erase(
    const_iterator position) noexcept;

Removes the element at position. Returns the iterator following it, which is the next element an iteration from begin() has not visited yet: deletion only shifts elements towards the start of their probe run and no run crosses the anchor the iteration order starts at.

find

[[nodiscard]] iterator find(
    const key_type &key) noexcept;
[[nodiscard]] const_iterator find(
    const key_type &key) const noexcept;

hash_function

[[nodiscard]] const Hash &hash_function() const noexcept;

insert

std::pair<iterator, bool> insert(
    const value_type &value);
std::pair<iterator, bool> insert(
    value_type &&value);

insert_with

template <typename Builder> std::pair<iterator, bool> insert_with(
    const key_type &key,
    Builder &&build);

Inserts when the key is absent. build receives the uninitialised slot and must construct the value there. Returns the element and whether it was inserted. Arguments captured by build must not refer to elements of this table.

key_eq

[[nodiscard]] const Eq &key_eq() const noexcept;

load_factor

[[nodiscard]] f32 load_factor() const noexcept;

size() / capacity(); 0 for an empty table.

reserve

void reserve(
    usize count);

Makes room for count elements without a rehash during the next insertions. reserve(0) does nothing, so an empty table stays unallocated.

size

[[nodiscard]] usize size() const noexcept;

swap

void swap(
    hash_table &other) noexcept;

Private

allocate_table

void allocate_table(
    usize capacity);

capacity_for

[[nodiscard]] static usize capacity_for(
    usize count) noexcept;

Smallest power-of-two capacity (at least MIN_CAPACITY) whose grow threshold admits count elements.

clone_from

void clone_from(
    const hash_table &other);

Copies another table slot for slot, which keeps its iteration order.

deallocate_table

static void deallocate_table(
    Value *values,
    u32 *meta,
    usize capacity) noexcept;
void deallocate_table() noexcept;

destroy_elements

void destroy_elements() noexcept;

erase_index

void erase_index(
    usize index) noexcept;

Backward-shift deletion: later elements of the run whose home is at or before the hole move into it, so no tombstone is needed and lookups stay short after many erases. The anchor is empty and therefore never part of the run.

find_index

[[nodiscard]] usize find_index(
    const key_type &key) const noexcept;
[[nodiscard]] usize find_index(
    u64 hash,
    const key_type &key) const noexcept;

Slot of key or NO_SLOT. The probe stops at the first empty slot (there always is one: the load factor is below one); keys are only compared when the stored hash matches.

forget_table

void forget_table() noexcept;

grow_threshold

[[nodiscard]] static usize grow_threshold(
    usize capacity) noexcept;

hash_of

[[nodiscard]] u64 hash_of(
    const key_type &key) const noexcept;

home_of

[[nodiscard]] usize home_of(
    u64 hash) const noexcept;

home_of_slot

[[nodiscard]] usize home_of_slot(
    usize index) const noexcept;

insert_new

template <typename Builder> usize insert_new(
    u64 hash,
    Builder &&build);

Places a value whose key is known to be absent in the first empty slot of its probe sequence. The caller has made sure the table is below its grow threshold.

is_occupied

[[nodiscard]] bool is_occupied(
    usize index) const noexcept;

iterator_at

[[nodiscard]] iterator iterator_at(
    usize index) noexcept;
[[nodiscard]] const_iterator iterator_at(
    usize index) const noexcept;

next_empty_after

[[nodiscard]] usize next_empty_after(
    usize index) const noexcept;

The first empty slot after index in probe order. Exists because the table is never full.

rehash

void rehash(
    usize newCapacity);

relocate

static void relocate(
    Value *source,
    Value *destination) noexcept;

tag_of

[[nodiscard]] static u32 tag_of(
    u64 hash) noexcept;

What an occupied slot stores for hash. Its low bits are the home bucket, so erase can recompute homes without re-hashing keys.

Operator Overloads

Public

operator=

hash_table &operator=(
    const hash_table &other);
hash_table &operator=(
    hash_table &&other) noexcept;