#include <userver/utils/slot_map.hpp>
A minimalistic slot-map container adaptor.
Each slot holds either a live value of type T or a free-list node. Erased slots are recycled so that emplace reuses them before allocating new storage.
Guarantees:
operator[](std::size_t) is O(1);emplace and insert are amortized O(1) (assuming that Container provides this guarantee);erase is O(1);size() and empty() are O(1);AliveItems() ARE invalidated by emplace, insert, insert_range, and erase, regardless of the underlying Container.If Container is std::deque or another reference-stable container, then inserted elements have reference stability; otherwise elements may move around on emplace and insert*, and T should be movable.
Container template is instantiated with an internal value type; the resulting type must:
std::ranges::forward_range and std::ranges::sized_range;operator[](std::size_t) with computational complexity O(1);emplace_back, forwarding arbitrary Args&&... to the value;std::ranges::size with computational complexity O(1).For example, std::deque, std::vector, boost::small_vector satisfy those requirements.
Definition at line 81 of file slot_map.hpp.
Classes | |
| struct | InsertionResult |
Result of an emplace or insert call. More... | |
Public Member Functions | |
| SlotMap ()=default | |
| Constructs an empty SlotMap. | |
| template<std::input_iterator It, std::sentinel_for< It > Sentinel> | |
| SlotMap (It first, Sentinel last) | |
Constructs a SlotMap from the elements in [first, last). | |
| SlotMap (const SlotMap &)=default | |
| SlotMap & | operator= (const SlotMap &)=default |
| SlotMap (SlotMap &&) noexcept=default | |
| SlotMap & | operator= (SlotMap &&) noexcept=default |
| template<typename... Args> | |
| InsertionResult | emplace (Args &&... args) |
| Constructs a new element in-place and returns a reference to it and its stable index. | |
| InsertionResult | insert (const T &value) |
| Inserts a copy of value and returns a reference to it and its stable index. | |
| InsertionResult | insert (T &&value) |
| Inserts value by move and returns a reference to it and its stable index. | |
| template<std::ranges::input_range Range> | |
| void | insert_range (Range &&range) |
| Inserts all elements from range. | |
| std::size_t | size () const noexcept |
| Returns the number of live elements. | |
| bool | empty () const noexcept |
| Returns true if there are no live elements. | |
| std::size_t | capacity () const noexcept |
| Returns the total number of allocated slots (live + free). | |
| void | reserve (std::size_t capacity) |
Calls reserve on the underlying container. | |
| T & | operator[] (std::size_t index) noexcept |
| Returns a reference to the live element at index. | |
| const T & | operator[] (std::size_t index) const noexcept |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
| std::size_t | erase (std::size_t index) |
| Destroys the element at index and recycles the slot. | |
| std::ranges::forward_range auto | AliveItems () noexcept |
Returns a view over live (non-erased) elements, yielding T& references. | |
| std::ranges::forward_range auto | AliveItems () const noexcept |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
|
inline |
Constructs a SlotMap from the elements in [first, last).
Definition at line 102 of file slot_map.hpp.
|
inlinenodiscardnoexcept |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 236 of file slot_map.hpp.
|
inlinenodiscardnoexcept |
Returns a view over live (non-erased) elements, yielding T& references.
emplace, insert, insert_range, and erase. References to elements remain valid. Definition at line 231 of file slot_map.hpp.
|
inlinenodiscardnoexcept |
Returns the total number of allocated slots (live + free).
This is the size of the backing storage. It never shrinks.
Definition at line 175 of file slot_map.hpp.
|
inline |
Constructs a new element in-place and returns a reference to it and its stable index.
If there are free (erased) slots, one is reused; otherwise new storage is allocated.
AliveItems(). Does not invalidate references to existing elements if the underlying container supports it, e.g. std::deque. Definition at line 121 of file slot_map.hpp.
|
inlinenodiscardnoexcept |
Returns true if there are no live elements.
Definition at line 170 of file slot_map.hpp.
|
inline |
Destroys the element at index and recycles the slot.
If the slot at index is already free (was previously erased), this is a no-op.
1 if a live element was erased, 0 if the slot was already free.AliveItems(). Does NOT invalidate references to other live elements. Definition at line 217 of file slot_map.hpp.
|
inline |
Inserts a copy of value and returns a reference to it and its stable index.
AliveItems(). Does not invalidate references to existing elements if the underlying container supports it, e.g. std::deque. Definition at line 143 of file slot_map.hpp.
|
inline |
Inserts value by move and returns a reference to it and its stable index.
AliveItems(). Does not invalidate references to existing elements if the underlying container supports it, e.g. std::deque. Definition at line 149 of file slot_map.hpp.
|
inline |
Inserts all elements from range.
Provides a basic exception guarantee: if an element's constructor throws, all previously inserted elements remain valid and the map is left in a consistent state.
AliveItems(). Does not invalidate references to existing elements if the underlying container supports it, e.g. std::deque. Definition at line 160 of file slot_map.hpp.
|
inlinenoexcept |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 201 of file slot_map.hpp.
|
inlinenoexcept |
Returns a reference to the live element at index.
Precondition: the slot at index holds a live element (was not erased).
Definition at line 193 of file slot_map.hpp.
|
inline |
Calls reserve on the underlying container.
Definition at line 184 of file slot_map.hpp.
|
inlinenodiscardnoexcept |
Returns the number of live elements.
Definition at line 167 of file slot_map.hpp.