-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMeshNetwork.mpp
More file actions
450 lines (389 loc) · 14.2 KB
/
MeshNetwork.mpp
File metadata and controls
450 lines (389 loc) · 14.2 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
export module CppUtils.Container.MeshNetwork;
import std;
import CppUtils.Container.NetworkPtr;
import CppUtils.Container.SafeShared;
import CppUtils.Thread.UniqueLocker;
import CppUtils.Thread.SharedLocker;
import CppUtils.Thread.SharedPtr;
export namespace CppUtils::Container
{
template<class Key, class Value>
class MeshNodePtr final
{
private:
struct MeshNode final
{
using WeakPtr = SafeWeak<NetworkPtr<MeshNode>>;
using VectorLocker = Thread::SharedLocker<std::vector<WeakPtr>>;
using Branches = std::unordered_map<Key, SafeShared<std::vector<WeakPtr>>>;
Thread::SharedLocker<Value> value;
Thread::SharedLocker<Branches> branches;
explicit inline MeshNode(auto&&... args):
value{std::forward<decltype(args)>(args)...}
{}
};
using SharedPtr = SafeShared<NetworkPtr<MeshNode>>;
using WeakPtr = MeshNode::WeakPtr;
using VectorLocker = MeshNode::VectorLocker;
using Branches = MeshNode::Branches;
private:
static auto attach(
const MeshNodePtr& parent,
const Key& key,
const MeshNodePtr& child,
SafeShared<std::vector<WeakPtr>> existingPersistenceGuard = nullptr) -> void
{
auto accessor = Thread::MultipleAccessor{*parent.m_node, *child.m_node};
auto& parentNetworkPtr = std::get<0>(accessor.values);
auto& childNetworkPtr = std::get<1>(accessor.values);
auto persistenceGuard = SafeShared<std::vector<WeakPtr>>{};
{
auto branchesAccessor = parentNetworkPtr.value.branches.uniqueAccess();
auto& entry = branchesAccessor.value()[key];
if (not entry)
entry = existingPersistenceGuard ? existingPersistenceGuard : makeSafeShared<std::vector<WeakPtr>>();
persistenceGuard = entry;
}
{
auto vectorAccessor = persistenceGuard->uniqueAccess();
if (std::ranges::none_of(vectorAccessor.value(), [&](const auto& weak) {
return Thread::SharedPtr::ownerEqual(weak, child.m_node);
}))
vectorAccessor->push_back(child.m_node);
}
parentNetworkPtr.attachChild(childNetworkPtr);
}
static auto detach(const MeshNodePtr& parent, const Key& key, const MeshNodePtr& child) -> void
{
auto accessor = Thread::MultipleAccessor{*parent.m_node, *child.m_node};
auto& parentNetworkPtr = std::get<0>(accessor.values);
auto& childNetworkPtr = std::get<1>(accessor.values);
auto branchesAccessor = parentNetworkPtr.value.branches.uniqueAccess();
if (auto branchIterator = branchesAccessor->find(key); branchIterator != std::cend(branchesAccessor.value()))
{
auto persistenceGuard = branchIterator->second;
auto vectorAccessor = persistenceGuard->uniqueAccess();
auto& vector = vectorAccessor.value();
if (auto it = std::ranges::find_if(vector, [&](auto& weak) { return Thread::SharedPtr::ownerEqual(weak, child.m_node); }); it != std::cend(vector))
{
vector.erase(it);
parentNetworkPtr.detachChild(childNetworkPtr);
}
if (std::empty(vector))
branchesAccessor->erase(branchIterator);
}
}
static auto detach(const MeshNodePtr& parent, const Key& key) -> void
{
auto children = std::vector<MeshNodePtr>{};
{
auto nodeAccessor = parent.m_node->sharedAccess();
auto branchesAccessor = nodeAccessor->value.branches.sharedAccess();
if (auto branchIterator = branchesAccessor->find(key); branchIterator != std::cend(branchesAccessor.value()))
for (auto vectorAccessor = branchIterator->second->sharedAccess(); auto& weak : vectorAccessor.value())
if (auto shared = weak.lock())
children.push_back(MeshNodePtr{shared});
}
for (auto& child : children)
detach(parent, key, child);
}
static auto attach(
const Key& firstKey,
const MeshNodePtr& firstNode,
const Key& secondKey,
const MeshNodePtr& secondNode,
SafeShared<std::vector<WeakPtr>> firstNodePersistenceGuard = nullptr,
SafeShared<std::vector<WeakPtr>> secondNodePersistenceGuard = nullptr) -> void
{
if (firstNode.m_node->sharedAccess()->getDistanceFromRoot() == std::numeric_limits<std::size_t>::max())
{
attach(secondNode, firstKey, firstNode, secondNodePersistenceGuard);
attach(firstNode, secondKey, secondNode, firstNodePersistenceGuard);
}
else
{
attach(firstNode, secondKey, secondNode, firstNodePersistenceGuard);
attach(secondNode, firstKey, firstNode, secondNodePersistenceGuard);
}
}
static auto detach(const Key& firstKey, const MeshNodePtr& firstNode, const Key& secondKey, const MeshNodePtr& secondNode) -> void
{
detach(firstNode, secondKey, secondNode);
detach(secondNode, firstKey, firstNode);
}
public:
template<bool IsConst>
class Branch final
{
public:
using VectorAccessor = std::conditional_t<IsConst,
Thread::ReadOnlyAccessor<std::vector<WeakPtr>>,
Thread::Accessor<std::vector<WeakPtr>, std::shared_mutex>>;
Branch() = default;
Branch(MeshNodePtr parent, Key key, SafeShared<std::vector<WeakPtr>> persistenceGuard, VectorAccessor vectorAccessor):
m_parent{std::move(parent)}, m_key{std::move(key)},
m_persistenceGuard{std::move(persistenceGuard)},
m_vectorAccessor{std::move(vectorAccessor)}
{}
private:
[[nodiscard]] auto getVectorAccessor(this auto&& self) -> auto&
{
if (not self.m_vectorAccessor) [[unlikely]]
{
if constexpr (IsConst)
self.m_vectorAccessor = self.m_persistenceGuard->sharedAccess();
else
self.m_vectorAccessor = self.m_persistenceGuard->uniqueAccess();
}
return self.m_vectorAccessor.value();
}
public:
[[nodiscard]] auto size() const noexcept -> std::size_t
{
if (not m_persistenceGuard) [[unlikely]]
return 0uz;
return std::size(getVectorAccessor().value());
}
[[nodiscard]] auto empty() const noexcept -> bool { return size() == 0uz; }
[[nodiscard]] auto contains(const MeshNodePtr& node) const -> bool
{
if (not m_persistenceGuard) [[unlikely]]
return false;
return std::ranges::any_of(getVectorAccessor().value(), [&](const auto& weak) {
return Thread::SharedPtr::ownerEqual(weak, node.m_node);
});
}
auto clear() -> void
requires (not IsConst)
{
m_vectorAccessor.reset();
MeshNodePtr::detach(m_parent, m_key);
}
[[nodiscard]] auto operator[](std::size_t index) const -> std::expected<MeshNodePtr, std::string_view>
{
using namespace std::literals;
if (not m_persistenceGuard) [[unlikely]]
return std::unexpected{"Branch is no longer valid"sv};
auto& vectorAccessor = getVectorAccessor();
if (index >= std::size(vectorAccessor.value())) [[unlikely]]
return std::unexpected{"Branch index out of bounds"sv};
if (auto sharedNode = vectorAccessor.value()[index].lock()) [[likely]]
return MeshNodePtr{sharedNode};
return std::unexpected{"Failed to lock weak pointer for branch. Node might have been deleted"sv};
}
auto operator>>(const MeshNodePtr& other) -> const MeshNodePtr&
requires (not IsConst)
{
auto persistenceGuard = m_persistenceGuard;
m_vectorAccessor.reset();
MeshNodePtr::attach(m_parent, m_key, other, persistenceGuard);
return other;
}
auto operator>>(Branch<false>&& other) -> Branch<false>&&
requires (not IsConst)
{
auto persistenceGuard = m_persistenceGuard;
m_vectorAccessor.reset();
MeshNodePtr::attach(m_parent, m_key, other.m_parent, persistenceGuard);
return std::move(other);
}
auto operator-(const MeshNodePtr& other) -> void
requires (not IsConst)
{
m_vectorAccessor.reset();
MeshNodePtr::detach(m_parent, m_key, other);
}
template<bool OtherIsConst>
auto operator&(const Branch<OtherIsConst>& other) -> void
requires (not IsConst)
{
auto persistenceGuard = m_persistenceGuard;
auto otherPersistenceGuard = other.m_persistenceGuard;
m_vectorAccessor.reset();
other.m_vectorAccessor.reset();
MeshNodePtr::attach(other.m_key, m_parent, m_key, other.m_parent, otherPersistenceGuard, persistenceGuard);
}
template<bool OtherIsConst>
auto operator/(const Branch<OtherIsConst>& other) -> void
requires (not IsConst)
{
m_vectorAccessor.reset();
other.m_vectorAccessor.reset();
MeshNodePtr::detach(other.m_key, m_parent, m_key, other.m_parent);
}
[[nodiscard]] auto front() const { return (*this)[0]; }
[[nodiscard]] auto back() const { return (*this)[size() - 1]; }
struct Iterator final
{
using iterator_category = std::random_access_iterator_tag;
using value_type = MeshNodePtr;
using difference_type = std::ptrdiff_t;
using pointer = void;
using reference = MeshNodePtr;
const Branch* branch = nullptr;
std::size_t index = 0uz;
[[nodiscard]] auto operator*() const -> MeshNodePtr { return (*branch)[index].value_or(MeshNodePtr{}); }
auto operator++() -> Iterator&
{
++index;
return *this;
}
auto operator++(int) -> Iterator
{
auto oldValue = *this;
++index;
return oldValue;
}
auto operator--() -> Iterator&
{
--index;
return *this;
}
auto operator--(int) -> Iterator
{
auto oldValue = *this;
--index;
return oldValue;
}
auto operator+=(difference_type n) -> Iterator&
{
index = static_cast<std::size_t>(static_cast<difference_type>(index) + n);
return *this;
}
auto operator-=(difference_type n) -> Iterator&
{
index = static_cast<std::size_t>(static_cast<difference_type>(index) - n);
return *this;
}
[[nodiscard]] friend auto operator+(Iterator it, difference_type n) -> Iterator
{
it += n;
return it;
}
[[nodiscard]] friend auto operator+(difference_type n, Iterator it) -> Iterator
{
it += n;
return it;
}
[[nodiscard]] friend auto operator-(Iterator it, difference_type n) -> Iterator
{
it -= n;
return it;
}
[[nodiscard]] friend auto operator-(const Iterator& lhs, const Iterator& rhs) -> difference_type
{
return static_cast<difference_type>(lhs.index) - static_cast<difference_type>(rhs.index);
}
[[nodiscard]] auto operator[](difference_type n) const -> MeshNodePtr { return *(*this + n); }
[[nodiscard]] auto operator<=>(const Iterator& other) const = default;
};
static_assert(std::random_access_iterator<Iterator>);
[[nodiscard]] auto begin() const { return Iterator{this, 0uz}; }
[[nodiscard]] auto end() const { return Iterator{this, size()}; }
private:
MeshNodePtr m_parent;
Key m_key;
SafeShared<std::vector<WeakPtr>> m_persistenceGuard;
mutable std::optional<VectorAccessor> m_vectorAccessor;
friend class MeshNodePtr;
};
using MutableBranch = Branch<false>;
using ConstBranch = Branch<true>;
[[nodiscard]] static auto makeRoot(auto&&... args) -> MeshNodePtr
{
return MeshNodePtr{NetworkPtr<MeshNode>::makeRoot(std::forward<decltype(args)>(args)...)};
}
[[nodiscard]] static auto make(auto&&... args) -> MeshNodePtr
{
return MeshNodePtr{NetworkPtr<MeshNode>::make(std::forward<decltype(args)>(args)...)};
}
MeshNodePtr() = default;
explicit MeshNodePtr(SharedPtr node): m_node{std::move(node)} {}
[[nodiscard]] auto operator==(const MeshNodePtr& other) const noexcept -> bool
{
return Thread::SharedPtr::ownerEqual(m_node, other.m_node);
}
explicit operator bool() const noexcept { return m_node != nullptr; }
[[nodiscard]] auto operator[](this auto&& self, const Key& key)
{
using SelfType = std::remove_reference_t<decltype(self)>;
if (not self.m_node)
throw std::runtime_error{"MeshNodePtr::operator[]: Invalid MeshNodePtr"};
if constexpr (std::is_const_v<SelfType>)
{
auto mapAccessor = self.m_node->sharedAccess()->value.branches.sharedAccess();
auto persistenceGuardPtr = mapAccessor->at(key);
return Branch<true>{self, key, persistenceGuardPtr, persistenceGuardPtr->sharedAccess()};
}
auto persistenceGuardPtr = SafeShared<std::vector<WeakPtr>>{};
{
auto nodeAccessor = self.m_node->uniqueAccess();
auto mapAccessor = nodeAccessor->value.branches.uniqueAccess();
auto& entry = mapAccessor.value()[key];
if (not entry)
entry = makeSafeShared<std::vector<WeakPtr>>();
persistenceGuardPtr = entry;
}
return Branch<false>{self, key, persistenceGuardPtr, persistenceGuardPtr->uniqueAccess()};
}
[[nodiscard]] auto at(const Key& key) const -> std::expected<Branch<true>, std::string_view>
{
using namespace std::literals;
if (not m_node)
return std::unexpected{"MeshNodePtr::at(): Invalid MeshNodePtr"sv};
auto branchesAccessor = m_node->sharedAccess()->value.branches.sharedAccess();
if (auto branchIterator = branchesAccessor->find(key); branchIterator != std::cend(branchesAccessor.value())) [[likely]]
{
auto persistenceGuardPtr = branchIterator->second;
return Branch<true>{*this, key, persistenceGuardPtr, persistenceGuardPtr->sharedAccess()};
}
return std::unexpected{"MeshNodePtr::at(): Key not found"sv};
}
[[nodiscard]] auto contains(const Key& key) const -> bool
{
if (not m_node)
return false;
auto nodeAccessor = m_node->sharedAccess();
return nodeAccessor->value.branches.sharedAccess()->contains(key);
}
[[nodiscard]] auto count(const Key& key) const -> std::size_t
{
if (not m_node)
return 0uz;
auto nodeAccessor = m_node->sharedAccess();
return nodeAccessor->value.branches.sharedAccess()->count(key);
}
[[nodiscard]] auto getDistanceFromRoot() const -> std::size_t
{
if (not m_node)
return std::numeric_limits<std::size_t>::max();
return m_node->sharedAccess()->getDistanceFromRoot();
}
[[nodiscard]] auto operator->(this auto&& self)
{
return std::addressof(std::forward_like<decltype(self)>(*self.m_node));
}
[[nodiscard]] auto operator*(this auto&& self) -> decltype(auto)
{
return std::forward_like<decltype(self)>(*self.m_node);
}
auto setValue(auto&& newValue) -> void
{
auto nodeAccessor = m_node->uniqueAccess();
auto& meshNode = nodeAccessor->value;
meshNode.value.uniqueAccess().value() = std::forward<decltype(newValue)>(newValue);
}
[[nodiscard]] auto getValue() const -> Thread::ReadOnlyAccessor<Value>
{
auto nodeAccessor = m_node->sharedAccess();
return nodeAccessor->value.value.sharedAccess();
}
[[nodiscard]] auto getBranches() const -> Thread::ReadOnlyAccessor<Branches>
{
auto nodeAccessor = m_node->sharedAccess();
return nodeAccessor->value.branches.sharedAccess();
}
private:
SharedPtr m_node = nullptr;
};
}