Skip to content

Engine → Core → flat_hash_map.h

flat_hash_map

Syntax

template <typename K, typename V, typename Hash = std::hash<K>, typename Eq = std::equal_to<K>> class flat_hash_map

Constructors

AccessSignatureDescription
publicflat_hash_map() = default;
publicexplicit flat_hash_map( usize initialCapacity);Pre-sizes the table for initialCapacity elements; 0 allocates nothing.
publicflat_hash_map( std::initializer_list<value_type> values);Builds the map from pairs; for duplicate keys the first one wins.
publicflat_hash_map( const flat_hash_map &) = default;
publicflat_hash_map( flat_hash_map &&) noexcept = default;

Destructors

AccessSignatureDescription
public~flat_hash_map() = default;

Variables

AccessNameTypeDefaultDescription
publicMAX_CAPACITYstatic constexpr usizetable_type::MAX_CAPACITY
publicMAX_LOAD_FACTORstatic constexpr f32table_type::MAX_LOAD_FACTOR
privatemTabletable_type{}

Functions

Public

at

[[nodiscard]] V &at(
    const K &key) noexcept;

The value for key. The key must be present: a missing key is a fatal programmer error (asserts, then terminates), never an exception.

[[nodiscard]] const V &at(
    const K &key) const noexcept;

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). The table rehashes only when size() would exceed capacity() * MAX_LOAD_FACTOR.

cbegin

[[nodiscard]] const_iterator cbegin() const noexcept;

cend

[[nodiscard]] const_iterator cend() const noexcept;

clear

void clear() noexcept;

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

contains

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

count

[[nodiscard]] usize count(
    const K &key) const noexcept;

0 or 1.

emplace

template <typename... Args> std::pair<iterator, bool> emplace(
    Args &&...args);

Constructs a pair from the arguments and inserts it when its key is absent.

empty

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

end

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

erase

bool erase(
    const K &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.

find

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

The element with key key, or end().

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

hash_function

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

insert

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

Inserts when the key is absent. Returns the element and whether it was inserted.

std::pair<iterator, bool> insert(
    value_type &&value);
template <std::input_iterator Iterator> void insert(
    Iterator first,
    Iterator last);

Inserts every element of the range that is not already present.

insert_or_assign

template <typename M> std::pair<iterator, bool> insert_or_assign(
    const K &key,
    M &&value);

Inserts or overwrites. Returns the element and true when it was newly inserted.

template <typename M> std::pair<iterator, bool> insert_or_assign(
    K &&key,
    M &&value);

key_eq

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

load_factor

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

reserve

void reserve(
    usize count);

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

size

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

swap

void swap(
    flat_hash_map &other) noexcept;

try_emplace

template <typename... Args> std::pair<iterator, bool> try_emplace(
    const K &key,
    Args &&...args);

Inserts {key, V(args...)} when key is absent; the value is only constructed in that case. Returns the element and whether it was inserted.

template <typename... Args> std::pair<iterator, bool> try_emplace(
    K &&key,
    Args &&...args);

Operator Overloads

Public

operator=

flat_hash_map &operator=(
    const flat_hash_map &) = default;
flat_hash_map &operator=(
    flat_hash_map &&) noexcept = default;

operator==

[[nodiscard]] bool operator==(
    const flat_hash_map &other) const;

Same keys with equal values, regardless of slot order.

operator[]

[[nodiscard]] V &operator[](
    const K &key);

The value for key, default-constructed and inserted when absent.

[[nodiscard]] V &operator[](
    K &&key);