Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
SparseArray.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** R-Type
4** File description:
5** SparseArray.hpp
6*/
7
8/*
9** MIT License
10**
11** Copyright (c) 2025 Robin Toillon
12**
13** Permission is hereby granted, free of charge, to any person obtaining
14** a copy of this software and associated documentation files (the
15** "Software"), to deal in the Software without restriction, including
16** without limitation the rights to use, copy, modify, merge, publish,
17** distribute, sublicense, and/or sell copies of the Software, and to
18** permit persons to whom the Software is furnished to do so, subject to
19** the following conditions:
20**
21** The above copyright notice and this permission notice shall be
22** included in all copies or substantial portions of the Software.
23**
24** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
28** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31*/
32
42#ifndef RTYPE_SPARSEARRAY_HPP_
43 #define RTYPE_SPARSEARRAY_HPP_
44
45 #include "RType/Assert.hpp"
47 #include "RType/ECS/Entity.hpp"
48
49 #include <vector>
50 #include <limits>
51 #include <span>
52
53namespace rtp::ecs
54{
61 class ISparseArray { // Only for type erasure
62 public:
63 virtual ~ISparseArray() noexcept = default;
64
69 virtual void erase(Entity entity) = 0;
70
76 virtual bool has(Entity entity) const = 0;
77
81 virtual void clear(void) = 0;
82 };
83
94 template <Component T>
95 class SparseArray final : public ISparseArray {
96 public:
97 using value_type = T;
98 using container_t = std::vector<value_type>;
99
100 static constexpr std::size_t NullIndex =
101 std::numeric_limits<size_t>::max();
102
103 SparseArray() = default;
104 SparseArray(const SparseArray &) = default;
105 SparseArray(SparseArray &&) noexcept = default;
106 SparseArray &operator=(const SparseArray &) = default;
107 SparseArray &operator=(SparseArray &&) noexcept = default;
108 ~SparseArray() override = default;
109
114 void erase(Entity entity) noexcept override final;
115
121 [[nodiscard]]
122 bool has(Entity entity) const noexcept override final;
123
127 void clear(void) noexcept override final;
128
137 template <typename Self>
138 [[nodiscard]]
139 auto &&operator[](this Self &self, Entity entity) noexcept;
140
148 template <typename... Args>
149 T &emplace(Entity entity, Args &&...args);
150
155#if defined(__GNUC__) || defined(__clang__)
156 // C++23 explicit object parameter (GCC/Clang)
157 template <typename Self>
158 [[nodiscard]]
159 auto data(this Self &&self) noexcept;
160#else
161 // Traditional overloads for MSVC
162 [[nodiscard]]
163 std::span<T> data() noexcept;
164
165 [[nodiscard]]
166 std::span<const T> data() const noexcept;
167#endif
168
173#if defined(__GNUC__) || defined(__clang__)
174 // C++23 explicit object parameter (GCC/Clang)
175 template <typename Self>
176 [[nodiscard]]
177 auto entities(this Self &&self) noexcept;
178#else
179 // Traditional overloads for MSVC
180 [[nodiscard]]
181 std::span<Entity> entities() noexcept;
182
183 [[nodiscard]]
184 std::span<const Entity> entities() const noexcept;
185#endif
186
191 [[nodiscard]]
192 std::size_t size(void) const noexcept;
193
198 [[nodiscard]]
199 bool empty(void) const noexcept;
200
201 private:
202 std::vector<size_t> _sparse;
203 std::vector<Entity> _dense;
205 };
206}
207
208 #include "SparseArray.tpp" /* SparseArray implementation */
209
210#endif /* !RTYPE_SPARSEARRAY_HPP_ */
Assertion and verification macros for runtime checks.
Concepts for validating component types in the ECS.
Entity identifier for ECS architecture.
Represents an entity in the ECS (Entity-Component-System) architecture.
Definition Entity.hpp:63
Type-erased base interface for sparse arrays.
virtual ~ISparseArray() noexcept=default
virtual bool has(Entity entity) const =0
Check if an entity has a component.
virtual void erase(Entity entity)=0
Remove a component from an entity.
virtual void clear(void)=0
Remove all components.
Sparse array container for component storage.
std::vector< size_t > _sparse
The Sparse Array (The Map)
std::span< Entity > entities() noexcept
Get the entity array corresponding to components.
std::vector< Entity > _dense
The Dense Entity Array (The Reverse Lookup)
std::span< T > data() noexcept
Get the underlying dense component array.
SparseArray(const SparseArray &)=default
container_t _data
The Dense Component Array (The Cache Friendly Data)
std::vector< value_type > container_t
std::size_t size(void) const noexcept
Get the number of components stored.
bool empty(void) const noexcept
Check if the array is empty.
SparseArray(SparseArray &&) noexcept=default
Concept for valid ECS component types.
File : RenderSystem.hpp License: MIT Author : Elias Josué HAJJAR LLAUQUEN elias-josue....
STL namespace.