Skip to content

Engine → Core → slot_map.h

slot_map

Syntax

template <typename T, slot_key_type Key = slot_key> class slot_map

Constructors

AccessSignatureDescription
publicslot_map() = default;
publicslot_map( const slot_map &) = default;
publicslot_map( slot_map &&other) noexcept;Takes the elements, slots and keys of other, which is left empty (no slots) and reusable. Keys issued by other address the new map.

Destructors

AccessSignatureDescription
public~slot_map() = default;

Variables

AccessNameTypeDefaultDescription
privateINVALID_INDEXstatic constexpr u32std::numeric_limits<u32>::max()
publicMAX_SLOT_COUNTstatic constexpr usizestatic_cast<usize>( std::numeric_limits<index_type>::max() < std::numeric_limits<u32>::max() ? static_cast<u64>(std::numeric_limits<index_type>::max()) + 1 : static_cast<u64>(std::numeric_limits<u32>::max()) )Largest number of slots the key's index type can address.
privatemEntriesstd::vector<entry>{}
privatemFreeCountu320
privatemFreeHeadu32INVALID_INDEX
privatemRetiredCountu320
privatemSlotsstd::vector<slot_entry>{}

Functions

Public

begin

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

capacity

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

Elements the dense storage can hold without reallocating.

cbegin

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

cend

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

clear

void clear();

Erases every element. Every outstanding key becomes stale (generations are bumped); slots and capacity are kept for reuse.

contains

[[nodiscard]] bool contains(
    Key key) const noexcept;

True when key addresses a live element.

emplace

template <typename... Args> [[nodiscard]] Key emplace(
    Args &&...args);

Constructs an element in place and returns its key. Returns the null key (and asserts) when the key's index type cannot address another slot.

empty

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

end

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

entries

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

The live {key, value} entries in dense storage order (the order iteration uses).

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

erase

bool erase(
    Key key);

Destroys the element addressed by key. Returns false for null, stale or unknown keys. The slot's generation is bumped so every outstanding copy of the key becomes stale; a generation that would wrap to 0 retires the slot for good.

iterator erase(
    const_iterator position);

Erases the element an iterator points at. Returns the iterator to the element that now occupies that storage position (or end()), so erase-while-iterating works. The iterator must point at an element of this map; end() and iterators of other maps assert and leave the map unchanged.

find

[[nodiscard]] T *find(
    Key key) noexcept;

The element addressed by key, or nullptr when the key is null, stale or unknown. Allocation-free, O(1).

[[nodiscard]] const T *find(
    Key key) const noexcept;

free_slot_count

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

Slots on the free list, reused by the next insertions.

insert

[[nodiscard]] Key insert(
    T &&value);

Moves an element in and returns its key.

[[nodiscard]] Key insert(
    const T &value);

Copies an element in and returns its key.

key_at

[[nodiscard]] Key key_at(
    usize denseIndex) const noexcept;

Key of the element at dense position denseIndex (the order iteration uses). Asserts and returns the null key when the position is out of range.

reserve

void reserve(
    usize count);

Reserves dense storage and slots for count elements.

retired_slot_count

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

Slots whose generation wrapped; they are never reused.

size

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

Number of live elements.

slot_count

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

Slots in the indirection table (live + free + retired).

Private

erase_at

void erase_at(
    u32 denseIndex);

live_slot

[[nodiscard]] const slot_entry *live_slot(
    Key key) const noexcept;

make_key

[[nodiscard]] static Key make_key(
    u32 slotIndex,
    u32 generation) noexcept;

Builds a key field by field so key types that declare their members in any order (or add more) work.

release_slot

void release_slot(
    u32 slotIndex) noexcept;

Marks a slot free (or retired when its generation wraps). The caller has already removed the element from the dense storage.

Operator Overloads

Public

operator=

slot_map &operator=(
    const slot_map &) = default;
slot_map &operator=(
    slot_map &&other) noexcept;

Replaces the contents with those of other, which is left empty (no slots) and reusable.