/// @file ecs/sparse_set.h /// @brief Contiguous per-component storage with O(1) lookup, insert and erase. #pragma once #include #include #include #include #include "core/types.h" #include "ecs/entity.h" namespace kumo { /// @brief Type-erased interface shared by every component pool in a World. /// /// Kept minimal so World can destroy an entity across all pools without RTTI. class PoolBase { public: virtual ~PoolBase() = default; virtual bool has(Entity entity) const = 0; virtual bool remove(Entity entity) = 0; virtual void clear() = 0; virtual size_t size() const = 0; virtual const std::vector& entities() const = 0; }; /// @brief Sparse-set component storage for one component type @p T. /// /// `sparse_` maps `entity.index()` to a dense index in `dense_` / `data_`. /// `dense_` stores the full Entity handle (including generation), so stale /// handles to a reused slot never match. Removal uses swap-and-pop, keeping /// `data_` tightly packed and deterministic. template class SparseSet final : public PoolBase { public: static constexpr uint32_t kInvalid = std::numeric_limits::max(); /// @brief Adds or replaces the component for @p entity. template T& emplace(Entity entity, Args&&... args) { assert(entity.valid()); const uint32_t idx = entity.index(); if (idx >= sparse_.size()) { sparse_.resize(static_cast(idx + 1), kInvalid); } const uint32_t existing = sparse_[idx]; if (existing != kInvalid) { assert(dense_[existing] == entity); data_[existing] = T{std::forward(args)...}; return data_[existing]; } const uint32_t dense_idx = static_cast(dense_.size()); sparse_[idx] = dense_idx; dense_.push_back(entity); data_.push_back(T{std::forward(args)...}); return data_.back(); } bool has(Entity entity) const override { if (!entity.valid()) return false; const uint32_t idx = entity.index(); if (idx >= sparse_.size()) return false; const uint32_t dense_idx = sparse_[idx]; return dense_idx != kInvalid && dense_[dense_idx] == entity; } /// @brief Removes the component for @p entity via swap-and-pop. /// @return `true` if the component existed and was removed. bool remove(Entity entity) override { if (!has(entity)) return false; const uint32_t dense_idx = sparse_[entity.index()]; const uint32_t last_idx = static_cast(dense_.size() - 1); if (dense_idx != last_idx) { const Entity moved = dense_[last_idx]; dense_[dense_idx] = moved; data_[dense_idx] = std::move(data_[last_idx]); sparse_[moved.index()] = dense_idx; } dense_.pop_back(); data_.pop_back(); sparse_[entity.index()] = kInvalid; return true; } T& get(Entity entity) { assert(has(entity)); return data_[sparse_[entity.index()]]; } const T& get(Entity entity) const { assert(has(entity)); return data_[sparse_[entity.index()]]; } T* try_get(Entity entity) { return has(entity) ? &data_[sparse_[entity.index()]] : nullptr; } const T* try_get(Entity entity) const { return has(entity) ? &data_[sparse_[entity.index()]] : nullptr; } void clear() override { sparse_.clear(); dense_.clear(); data_.clear(); } size_t size() const override { return dense_.size(); } bool empty() const { return dense_.empty(); } const std::vector& entities() const override { return dense_; } std::vector& data() { return data_; } const std::vector& data() const { return data_; } private: std::vector sparse_; std::vector dense_; std::vector data_; }; } // namespace kumo