-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorage.hpp
More file actions
288 lines (247 loc) · 9.41 KB
/
Storage.hpp
File metadata and controls
288 lines (247 loc) · 9.41 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
#pragma once
#include <list>
#include <memory_resource>
#include <optional>
#include <cassert>
#include <type_traits>
#include <variant>
#include <numeric>
#include "TypeMapping.hpp"
namespace Core
{
/*
* Storage is container of same-type objects.
* It has really fast insertion and deletion complexity (O(1) - complexity)
* Also all objects are close to each other in memory, so it's cache friendly
* API partially copied from std::list
*/
template <class T, size_t BufSize = 4096>
class Storage final : private std::pmr::list<T>
{
using container = std::pmr::list<T>;
public:
using container::begin;
using container::end;
using container::rbegin;
using container::rend;
using container::cbegin;
using container::cend;
using container::crbegin;
using container::crend;
using container::size;
using container::clear;
using container::empty;
using container::swap;
using container::operator=;
using container::assign;
using container::iterator;
using container::const_iterator;
using container::reverse_iterator;
using container::const_reverse_iterator;
Storage() = default;
/// copy constructor
Storage(const Storage& other)
: container(other) {}
Storage(Storage&&) = default;
/// ObjectPointer - handler or reference of object in storage.
struct ObjectPointer;
/// construct object in storage
template<typename... Args>
ObjectPointer emplace(Args&& ...args)
{
T val(std::forward<Args>(args)...);
return ObjectPointer(this, container::emplace(end(), std::move(val)));
}
/// delete object from storage
void erase(ObjectPointer&& ptr)
{
container::erase(ptr.it);
ptr = ObjectPointer(this, end());
}
private:
/*
* Storage is built as list of objects. We use list becuase it's really fast insertion and deletion - O(1) - complexity
* But list has one drawback - nodes are scattered in memory. It's cache-unfriendly, so we use std::pmr::list with monotonic buffer
* Actually it can be any memory_resource
*/
std::pmr::monotonic_buffer_resource pool{ BufSize };
// TODO: make memory resource as template argument
};
/*
* Problem: you must have std::list iterator to erase object for O(1)
* ObjectPointer is solution of the problem.
* ObjectPointer is an std::list iterator without move operators (increment, decrement)
* You can think about it as a reference or pointer on object in memory.
*/
template<typename T, size_t BufSize>
struct Storage<T, BufSize>::ObjectPointer final
{
ObjectPointer() = default;
ObjectPointer(const Storage<T, BufSize>* container, container::iterator it)
: cont(container), it(it) {}
constexpr T& operator*()& { return it.operator*(); }
constexpr const T& operator*() const& { return it.operator*(); }
constexpr T* operator->()& { return it.operator->(); }
constexpr const T* operator->() const& { return it.operator->(); }
constexpr bool operator==(ObjectPointer other) const { return it == other.it; }
constexpr operator bool() const noexcept { return cont && it != cont->end(); }
constexpr const Storage<T, BufSize>* GetStorage() const& { return cont; }
private:
friend Storage<T, BufSize>;
container::iterator it; ///< std::list iterator on object in list
const Storage<T, BufSize>* cont = nullptr; ///< pointer on owning storage
};
/*
* HeterogeneousStorage is multi-type container.
* It provides O(1) complexity for insertion, deletion
* Also it's cache-friendly (all objects are dense packed in memory)
*/
template<typename... Ts>
class HeterogeneousStorage final
{
template<typename ObjT>
using StorageBacket = Storage<ObjT>; ///< Typed storage, one of them
template<typename ObjT>
using ObjPtr = typename StorageBacket<ObjT>::ObjectPointer; ///< short alias for object
/// aliases for storage iterators
template<typename ObjT>
using typed_iterator = typename StorageBacket<ObjT>::iterator;
template<typename ObjT>
using typed_const_iterator = typename StorageBacket<ObjT>::const_iterator;
template<typename ObjT>
using typed_reverse_iterator = typename StorageBacket<ObjT>::reverse_iterator;
template<typename ObjT>
using typed_const_reverse_iterator = typename StorageBacket<ObjT>::const_reverse_iterator;
public:
/// default constructor
HeterogeneousStorage() = default;
HeterogeneousStorage(const HeterogeneousStorage&) = default;
HeterogeneousStorage(HeterogeneousStorage&&) = default;
/// Untyped/Generic ObjectPointer
struct GenericObjectPointer;
friend GenericObjectPointer;
using iterator = std::variant<typed_iterator<Ts>...>;
using const_iterator = std::variant<typed_const_iterator<Ts>...>;
using reverse_iterator = std::variant<typed_reverse_iterator<Ts>...>;
using const_reverse_iterator = std::variant<typed_const_reverse_iterator<Ts>...>;
/// construct object of type ObjT in storage
template <typename ObjT, typename... Args>
ObjPtr<ObjT> emplace(Args &&...args)
{
return Get<ObjT>().emplace(std::forward<Args>(args)...);
}
/// remove object from container
template <typename ObjT>
void erase(ObjPtr<ObjT>&& ptr)
{
return Get<ObjT>().erase(std::forward<ObjPtr<ObjT>>(ptr));
}
/// remove object from container
void erase(GenericObjectPointer&& ptr) { erase(ptr); }
/// returns count of objects in container
constexpr size_t size() const noexcept
{
std::array<size_t, sizeof...(Ts)> sizes = { Get<Ts>().size()... };
return std::reduce(sizes.begin(), sizes.end());
}
/// check if no objects in container
constexpr bool empty() const noexcept{ return size() == 0; }
/// ------------------- Begin/End -----------------
template<typename ObjT>
constexpr decltype(auto) begin() noexcept { return Get<ObjT>().begin(); }
template<typename ObjT>
constexpr decltype(auto) end() noexcept { return Get<ObjT>().end(); }
template<typename ObjT>
constexpr decltype(auto) cbegin() const noexcept { return Get<ObjT>().cbegin(); }
template<typename ObjT>
constexpr decltype(auto) cend() const noexcept { return Get<ObjT>().cend(); }
template<typename ObjT>
constexpr decltype(auto) rbegin() noexcept { return Get<ObjT>().rbegin(); }
template<typename ObjT>
constexpr decltype(auto) rend() noexcept { return Get<ObjT>().rend(); }
template<typename ObjT>
constexpr decltype(auto) crbegin() const noexcept { return Get<ObjT>().crbegin(); }
template<typename ObjT>
constexpr decltype(auto) crend() const noexcept { return Get<ObjT>().crend(); }
private:
using TypeIndexer = metaprogramming::type_table<Ts...>;
using VariadicContainer = std::variant<StorageBacket<Ts>...>;
std::array<VariadicContainer, TypeIndexer::size> containers = { StorageBacket<Ts>()... };
private:
/// Get typed StorageBacket
template<typename ObjT>
constexpr StorageBacket<ObjT>& Get()&
{
return const_cast<StorageBacket<ObjT>&>(
const_cast<const HeterogeneousStorage*>(this)->Get<ObjT>()
);
}
/// Get typed StorageBacket
template<typename ObjT>
constexpr const StorageBacket<ObjT>& Get() const&
{
constexpr size_t idx = TypeIndexer::template index_of<ObjT>;
try
{
return std::get<StorageBacket<ObjT>>(containers[idx]);
}
catch (const std::bad_variant_access&)
{
throw std::runtime_error("Type doesn't stored in this storage");
}
}
};
/*
* TypedView - is adapter for HeterogeneousStorage which is provide for(auto ...) syntax
* Example: for(auto && val : TypedAdapter<int, decltype(storage)>(storage)) - iterate over all int values in container
*/
template<typename ObjT, typename HStorageT>
struct TypedView final
{
HStorageT& storage;
TypedView(HStorageT& storage) : storage(storage) {}
constexpr decltype(auto) begin() noexcept { return storage.begin<ObjT>(); }
constexpr decltype(auto) end() noexcept { return storage.end<ObjT>(); }
constexpr decltype(auto) cbegin() const noexcept { return storage.cbegin<ObjT>(); }
constexpr decltype(auto) cend() const noexcept { return storage.cend<ObjT>(); }
constexpr decltype(auto) rbegin() noexcept { return storage.rbegin<ObjT>(); }
constexpr decltype(auto) rend() noexcept { return storage.rend<ObjT>(); }
constexpr decltype(auto) crbegin() const noexcept { return storage.crbegin<ObjT>(); }
constexpr decltype(auto) crend() const noexcept { return storage.rend<ObjT>(); }
};
/*
* GenericObjectPointer is multi-type pointer
*/
template<typename... Ts>
struct HeterogeneousStorage<Ts...>::GenericObjectPointer final
{
GenericObjectPointer() = default;
template<typename ObjPtrT>
GenericObjectPointer(ObjPtrT ptr) : ptr(ptr) {}
/// static cast to ObjectPointer. Throw exception if type is wrong
template<typename ObjPtrT>
constexpr operator ObjPtrT() const
{
try {
return std::get<ObjPtrT>(ptr);
}
catch (const std::bad_variant_access&)
{
throw std::runtime_error("Type doesn't stored in this storage");
}
}
/// equal operator
constexpr bool operator==(const GenericObjectPointer& other) const noexcept
{
return other.ptr == ptr;
}
/// check if pointer if valid (points on storage-places object)
constexpr operator bool() const
{
return ptr.valueless_by_exception &&
std::visit(ptr, [](auto&& ptr) { return ptr.operator bool(); });
}
private:
std::variant<ObjPtr<Ts>...> ptr; ///< pointer
};
}