-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbagel.h
More file actions
316 lines (284 loc) · 8.04 KB
/
Copy pathbagel.h
File metadata and controls
316 lines (284 loc) · 8.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright (C) 2025 Moshe Sulamy
// bagel.h file
#pragma once
#include <cstdint>
#include <cstring>
#include <algorithm>
#include <type_traits>
#include <algorithm>
namespace bagel
{
struct Bagel
{
bool DynamicResize = false;
int IdBagSize = 5;
int InitialEntities = 10;
int InitialPackedSize = 5;
int MaxComponents = 10;
};
template <class T> struct Storage;
template <class T> class PackedStorage;
template <class T> class SparseStorage;
template <class T> class TaggedStorage;
#if __has_include("bagel_cfg.h")
#define BAGEL_STORAGE(C,T) template <> struct Storage<C> { using type = T<C>; };
#include "bagel_cfg.h"
#undef BAGEL_STORAGE
#else
constexpr Bagel Params{};
#endif
using id_type = int;
struct ent_type { id_type id; };
using size_type = int;
using index_type = int;
using mask_type =
std::conditional_t<Params.MaxComponents<=8, std::uint_fast8_t,
std::conditional_t<Params.MaxComponents<=16, std::uint_fast16_t,
std::conditional_t<Params.MaxComponents<=32, std::uint_fast32_t,
std::uint_fast64_t>>>;
constexpr inline size_type BitsetWidth = sizeof(mask_type)*8;
class NoInstance { NoInstance() = delete; };
struct NoCopy {
NoCopy() = default;
NoCopy(const NoCopy&) = delete;
void operator=(const NoCopy&) = delete;
};
template <class T, int N>
class DynamicBag : NoCopy
{
public:
void push(const T& t) {
if (_size == _capacity) {
_capacity *= 2;
_arr = static_cast<T*>(
realloc(_arr, sizeof(T)*_capacity));
}
_arr[_size] = t;
++_size;
}
void ensure(size_type s) {
if (_capacity < s) {
_capacity = std::max(s, _capacity*2);
_arr = static_cast<T*>(
realloc(_arr, sizeof(T)*_capacity));
}
}
T pop() { return _arr[--_size]; }
T& operator[](index_type i) { return _arr[i]; }
const T& operator[](index_type i) const { return _arr[i]; }
void clear() { _size = 0; }
size_type size() const { return _size; }
size_type capacity() const { return _capacity; }
~DynamicBag() { free(_arr); }
private:
T* _arr = static_cast<T*>(malloc(sizeof(T) * N));
size_type _size = 0;
size_type _capacity = N;
};
template <class T, int N>
class StaticBag
{
public:
void push(const T& t) { _arr[_size++] = t; }
T pop() { return _arr[--_size]; }
T& operator[](index_type i) { return _arr[i]; }
const T& operator[](index_type i) const { return _arr[i]; }
void clear() { _size = 0; }
size_type size() const { return _size; }
static constexpr size_type capacity() { return N; }
static void ensure(size_type) {}
private:
T _arr[N];
size_type _size = 0;
};
template <class T, int N>
using Bag = std::conditional_t<Params.DynamicResize, DynamicBag<T, N>, StaticBag<T,N>>;
template <class T>
class SparseStorage final : NoInstance
{
public:
static void add(ent_type e, const T& t) {
_bag.ensure(e.id);
_bag[e.id] = t;
}
static void del(ent_type) {}
static T& get(ent_type e) { return _bag[e.id]; }
private:
static inline Bag<T,Params.InitialEntities> _bag;
};
template <class T>
class PackedStorage final : NoInstance
{
public:
static void add(ent_type e, const T& t) {
_entToComp.ensure(e.id);
_entToComp[e.id] = _comps.size();
_comps.push(t);
_compToEnt.push(e);
}
static void del(ent_type e) {
index_type ent_comp_idx = _entToComp[e.id];
ent_type last_ent = _compToEnt.pop();
_comps[ent_comp_idx] = _comps.pop();
_compToEnt[ent_comp_idx] = last_ent;
_entToComp[last_ent.id] = ent_comp_idx;
}
static T& get(ent_type e) {
return _comps[_entToComp[e.id]];
}
static int size() { return _comps.size(); }
static T& get(index_type idx) {
return _comps[idx];
}
static ent_type entity(index_type idx) {
return _compToEnt[idx];
}
private:
static inline Bag<T,Params.InitialPackedSize> _comps;
static inline Bag<index_type,Params.InitialEntities> _entToComp;
static inline Bag<ent_type,Params.InitialPackedSize> _compToEnt;
};
template <class T>
class TaggedStorage final : NoInstance
{
public:
static void add(ent_type, const T&) {}
static void del(ent_type) {}
static T& get(ent_type) = delete;
};
template <class T>
struct Storage final : NoInstance {
using type = SparseStorage<T>;
};
class SingleMask final
{
public:
using bit_type = mask_type;
static constexpr bit_type bit(index_type idx) { return 1<<idx; }
void set(const bit_type b) { _mask |= b; }
void clear(const bit_type b) { _mask &= ~b; }
void clear() { _mask = 0; }
bool test(const bit_type b) const { return _mask & b; }
bool test(const SingleMask m) const { return (_mask & m._mask) == m._mask; }
private:
mask_type _mask{0};
};
class MultiMask final
{
public:
using bit_type = struct {
const index_type index;
const mask_type mask;
};
static constexpr bit_type bit(index_type idx) {
return {idx/BitsetWidth, static_cast<mask_type>(1<<(idx%BitsetWidth))};
}
void set(const bit_type& b) { _masks[b.index] |= b.mask; }
void clear(const bit_type& b) { _masks[b.index] &= ~b.mask; }
void clear() { memset(_masks, 0, sizeof(_masks)); }
bool test(const bit_type& b) const { return _masks[b.index] & b.mask; }
bool test(const MultiMask& m) const {
for (index_type i = 0; i < Size; ++i)
if ((_masks[i] & m._masks[i]) != m._masks[i])
return false;
return true;
}
private:
static constexpr size_type Size = (Params.MaxComponents-1)/BitsetWidth + 1;
mask_type _masks[Size] ={};
};
using Mask = std::conditional_t<Params.MaxComponents<=BitsetWidth, SingleMask, MultiMask>;
static inline index_type compCounter = -1;
template <class>
struct Component final : NoInstance
{
static inline const index_type Index = ++compCounter;
static inline const Mask::bit_type Bit = Mask::bit(Index);
};
class World final : NoInstance
{
public:
static ent_type createEntity() {
if (_ids.size() > 0)
return _ids.pop();
_masks.push(Mask{});
return {++_maxId.id};
}
static void destroyEntity(ent_type ent) {
_masks[ent.id].clear();
_ids.push(ent);
}
static const Mask& mask(ent_type e) {
return _masks[e.id];
}
static ent_type maxId() { return _maxId; }
template <class T>
static T& getComponent(ent_type e) {
return Storage<T>::type::get(e);
}
template <class T>
static void addComponent(ent_type e, const T& t) {
_masks[e.id].set(Component<T>::Bit);
Storage<T>::type::add(e,t);
}
template <class T, class...Ts>
static void addComponents(ent_type e, const T& t, const Ts&... ts) {
addComponent(e, t);
if constexpr (sizeof...(Ts)>0)
addComponents(e, ts...);
}
template <class T>
static void delComponent(ent_type e) {
_masks[e.id].clear(Component<T>::Bit);
Storage<T>::type::del(e);
}
template <class T, class ...Ts>
static void delComponents(ent_type e) {
delComponent<T>(e);
if constexpr (sizeof...(Ts)>0)
delComponents<Ts...>(e);
}
private:
static inline ent_type _maxId{-1};
static inline Bag<Mask, Params.InitialEntities> _masks;
static inline Bag<ent_type, Params.IdBagSize> _ids;
};
class Entity
{
public:
Entity(ent_type e) : _ent(e) {}
ent_type entity() const { return _ent; }
static Entity create() { return World::createEntity(); }
void destroy() const { World::destroyEntity(_ent); }
const Mask& mask() const { return World::mask(_ent); }
template <class T> T& get() const { return World::getComponent<T>(_ent); }
template <class T> void add(const T& t) const {
return World::addComponent<T>(_ent, t);
}
template <class T> void del() const {
return World::delComponent<T>(_ent);
}
template <class T, class ...Ts> void addAll(const T& t, const Ts&... ts) const {
World::addComponents(_ent, t, ts...);
}
template <class T, class ...Ts> void delAll() const {
World::delComponents<T,Ts...>(_ent);
}
template <class T> bool has() const { return mask().test(Component<T>::Bit); }
bool test(const Mask& m) const { return mask().test(m); }
private:
ent_type _ent;
};
class MaskBuilder
{
public:
template <class T>
MaskBuilder& set() {
m.set(Component<T>::Bit);
return *this;
}
Mask build() const { return m; }
private:
Mask m;
};
}