83 using Slot = std::variant<T, impl::slot_map::FreeNode>;
85 static_assert(std::ranges::forward_range<Container<Slot>&>);
86 static_assert(std::ranges::forward_range<
const Container<Slot>&>);
87 static_assert(std::ranges::sized_range<
const Container<Slot>&>);
88 static_assert(impl::slot_map::Indexable<Container<Slot>&>);
89 static_assert(impl::slot_map::Indexable<
const Container<Slot>&>);
102 template <
std::input_iterator It,
std::sentinel_for<It> Sentinel>
104 insert_range(std::ranges::subrange(std::move(first), std::move(last)));
107 SlotMap(
const SlotMap&) =
default;
108 SlotMap& operator=(
const SlotMap&) =
default;
112 : slots_(std::move(other.slots_)),
113 free_list_head_(std::exchange(other.free_list_head_, impl::slot_map::kFreeListEnd)),
114 free_list_size_(std::exchange(other.free_list_size_, 0))
116 other.slots_ = Container<Slot>{};
121 if (
this != &other) {
122 slots_ = std::move(other.slots_);
123 free_list_head_ = std::exchange(other.free_list_head_, impl::slot_map::kFreeListEnd);
124 free_list_size_ = std::exchange(other.free_list_size_, 0);
125 other.slots_ = Container<Slot>{};
130 ~SlotMap() =
default;
138 template <
typename... Args>
140 if (
const auto entry = TryPopFromFreeList(); entry.slot !=
nullptr) {
142 T& element = entry.slot->
template emplace<T>(std::forward<Args>(args)...);
143 return {element, entry.index};
145 EraseAndPushToFreeList(entry);
150 const std::size_t index = std::ranges::size(slots_);
151 Slot& slot = slots_.emplace_back(std::in_place_type<T>, std::forward<Args>(args)...);
152 auto*
const value = std::get_if<T>(&slot);
154 return {*value, index};
177 template <
std::
ranges::input_range Range>
179 for (
auto&& elem : std::forward<Range>(range)) {
180 emplace(std::forward<
decltype(elem)>(elem));
185 [[nodiscard]] std::size_t
size()
const noexcept {
return std::ranges::size(slots_) - free_list_size_; }
193 [[nodiscard]] std::size_t
capacity()
const noexcept {
194 if constexpr (impl::slot_map::HasCapacity<
const Container<Slot>&>) {
195 return slots_.capacity();
197 return std::ranges::size(slots_);
205 slots_.reserve(capacity);
211 T&
operator[](std::size_t index)
noexcept USERVER_IMPL_LIFETIME_BOUND {
212 UASSERT(index < std::ranges::size(slots_));
213 auto*
const value = std::get_if<T>(&slots_[index]);
219 const T&
operator[](std::size_t index)
const noexcept USERVER_IMPL_LIFETIME_BOUND {
220 UASSERT(index < std::ranges::size(slots_));
221 const auto*
const value = std::get_if<T>(&slots_[index]);
235 std::size_t
erase(std::size_t index) {
236 UASSERT(index < std::ranges::size(slots_));
237 auto& slot = slots_[index];
238 if (std::get_if<T>(&slot) ==
nullptr) {
241 EraseAndPushToFreeList({.slot = &slot, .index = index});
250 return std::ranges::ref_view(slots_) | std::views::filter(IsLive{}) | std::views::transform(ToValue{});
255 return std::ranges::ref_view(slots_) | std::views::filter(IsLive{}) | std::views::transform(ToConstValue{});
260 bool operator()(
const Slot& slot)
const noexcept {
return std::holds_alternative<T>(slot); }
264 T& operator()(Slot& slot)
const noexcept {
265 auto*
const value = std::get_if<T>(&slot);
271 struct ToConstValue {
272 const T& operator()(
const Slot& slot)
const noexcept {
273 const auto*
const value = std::get_if<T>(&slot);
279 struct FreeListEntry
final {
284 FreeListEntry TryPopFromFreeList()
noexcept {
285 const auto index = free_list_head_;
287 if (index == impl::slot_map::kFreeListEnd) {
288 return {.slot =
nullptr, .index = impl::slot_map::kFreeListEnd};
291 Slot& slot = slots_[index];
292 const auto*
const node = std::get_if<impl::slot_map::FreeNode>(&slot);
294 free_list_head_ = node->next_index;
296 return {.slot = &slot, .index = index};
299 void EraseAndPushToFreeList(FreeListEntry entry)
noexcept {
300 UASSERT(entry.slot !=
nullptr);
301 UASSERT(entry.slot == &slots_[entry.index]);
302 entry.slot->
template emplace<impl::slot_map::FreeNode>(free_list_head_);
303 free_list_head_ = entry.index;
307 Container<Slot> slots_;
308 std::size_t free_list_head_{impl::slot_map::kFreeListEnd};
309 std::size_t free_list_size_{0};