Skip to content

Engine → Core → flat_map.h

flat_map

Syntax

template <typename K, typename V, typename Compare = std::less<K>> class flat_map

Constructors

AccessSignatureDescription
publicflat_map() = default;
publicexplicit flat_map( Compare compare);
publicflat_map( std::initializer_list<value_type> values);Builds the map from pairs; for duplicate keys the first one wins.
publicflat_map( const flat_map &) = default;
publicflat_map( flat_map &&) noexcept = default;

Destructors

AccessSignatureDescription
public~flat_map() = default;

Variables

AccessNameTypeDefaultDescription
privatemCompareCompare{}
privatemEntriesstorage_type{}

Functions

Public

at

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

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;
template <typename KeyLike> requires detail::transparent_compare<Compare> [[nodiscard]] V &at(
    const KeyLike &key);
template <typename KeyLike> requires detail::transparent_compare<Compare> [[nodiscard]] const V &at(
    const KeyLike &key) const;

begin

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

capacity

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

cbegin

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

cend

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

clear

void clear() noexcept;

contains

[[nodiscard]] bool contains(
    const K &key) const;
template <typename KeyLike> requires detail::transparent_compare<Compare> [[nodiscard]] bool contains(
    const KeyLike &key) const;

count

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

0 or 1.

template <typename KeyLike> requires detail::transparent_compare<Compare> [[nodiscard]] usize count(
    const KeyLike &key) const;

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;

entries

[[nodiscard]] std::span<const value_type> entries() const noexcept;

The sorted pairs as a contiguous read-only view.

erase

bool erase(
    const K &key);

Removes the element with key. Returns false when absent.

template <typename KeyLike> requires detail::transparent_compare<Compare> && (!std::is_convertible_v<const KeyLike &, const_iterator>) && (!std::is_convertible_v<const KeyLike &, iterator>) bool erase(
    const KeyLike &key);

Removes the element whose key compares equal to key (transparent comparators only). Returns false when absent. Iterators are excluded so that they reach erase(position).

iterator erase(
    const_iterator position);

Removes the element at position. Returns the iterator following it.

find

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

The element with key key, or end().

[[nodiscard]] const_iterator find(
    const K &key) const;
template <typename KeyLike> requires detail::transparent_compare<Compare> [[nodiscard]] iterator find(
    const KeyLike &key);

Heterogeneous lookup, available when Compare declares is_transparent (e.g. finding by std::string_view in a std::string keyed map).

template <typename KeyLike> requires detail::transparent_compare<Compare> [[nodiscard]] const_iterator find(
    const KeyLike &key) const;

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_comp

[[nodiscard]] key_compare key_comp() const;

lower_bound

[[nodiscard]] iterator lower_bound(
    const K &key);

First element whose key is not less than key.

[[nodiscard]] const_iterator lower_bound(
    const K &key) const;
template <typename KeyLike> requires detail::transparent_compare<Compare> [[nodiscard]] iterator lower_bound(
    const KeyLike &key);
template <typename KeyLike> requires detail::transparent_compare<Compare> [[nodiscard]] const_iterator lower_bound(
    const KeyLike &key) const;

rbegin

[[nodiscard]] reverse_iterator rbegin() noexcept;
[[nodiscard]] const_reverse_iterator rbegin() const noexcept;

rend

[[nodiscard]] reverse_iterator rend() noexcept;
[[nodiscard]] const_reverse_iterator rend() const noexcept;

reserve

void reserve(
    usize count);

shrink_to_fit

void shrink_to_fit();

size

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

swap

void swap(
    flat_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);

upper_bound

[[nodiscard]] iterator upper_bound(
    const K &key);

First element whose key is greater than key.

[[nodiscard]] const_iterator upper_bound(
    const K &key) const;
template <typename KeyLike> requires detail::transparent_compare<Compare> [[nodiscard]] iterator upper_bound(
    const KeyLike &key);
template <typename KeyLike> requires detail::transparent_compare<Compare> [[nodiscard]] const_iterator upper_bound(
    const KeyLike &key) const;

Private

find_in

template <typename Storage, typename KeyLike> [[nodiscard]] auto find_in(
    Storage &storage,
    const KeyLike &key) const;

lower_bound_in

template <typename Storage, typename KeyLike> [[nodiscard]] auto lower_bound_in(
    Storage &storage,
    const KeyLike &key) const;

upper_bound_in

template <typename Storage, typename KeyLike> [[nodiscard]] auto upper_bound_in(
    Storage &storage,
    const KeyLike &key) const;

Operator Overloads

Public

operator=

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

operator==

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

operator[]

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

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

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