Platform.Data.Doublets C++
Loading...
Searching...
No Matches
ILinksExtensions.h
Go to the documentation of this file.
2{
3 template <typename TStorage>
4 static void RunRandomCreations(TStorage& storage, typename TStorage::LinkAddressType amountOfCreations)
5 {
6 using namespace Platform::Data;
7 using namespace Platform::Random;
8 using namespace Platform::Ranges;
9 auto& randomGenerator64 = Random::RandomHelpers::Default;
10 for (typename TStorage::LinkAddressType i{0}; i < amountOfCreations; ++i)
11 {
12 Range<typename TStorage::LinkAddressType> linksAddressRange{0, Count(storage)};
13 auto source = Random::NextUInt64(randomGenerator64, linksAddressRange);
14 auto target = Random::NextUInt64(randomGenerator64, linksAddressRange);
15 GetOrCreate(storage, source, target);
16 }
17 }
18
19 template <typename TStorage>
20 static void RunRandomSearches(const TStorage& storage, typename TStorage::LinkAddressType amountOfSearches)
21 {
22 using namespace Platform::Data;
23 using namespace Platform::Random;
24 using namespace Platform::Ranges;
25 auto randomGenerator64 = Random::RandomHelpers::Default;
26 for (typename TStorage::LinkAddressType i{0}; i < amountOfSearches; ++i)
27 {
28 auto linksAddressRange = Range<typename TStorage::LinkAddressType>(0, Count(storage));
29 auto source = Random::NextUInt64(randomGenerator64, linksAddressRange);
30 auto target = Random::NextUInt64(randomGenerator64, linksAddressRange);
31 SearchOrDefault(storage, source, target);
32 }
33 }
34
35 template <typename TStorage>
36 static void RunRandomDeletions(TStorage& storage, typename TStorage::LinkAddressType amountOfDeletions)
37 {
38 using namespace Platform::Data;
39 using namespace Platform::Random;
40 using namespace Platform::Ranges;
41 auto& randomGenerator64 = RandomHelpers::Default;
42 auto linksCount = Count(storage);
43 typename TStorage::LinkAddressType min = amountOfDeletions > linksCount ? 0 : linksCount - amountOfDeletions;
44 for (typename TStorage::LinkAddressType i{0}; i < amountOfDeletions; ++i)
45 {
46 linksCount = Count(storage);
47 if (linksCount <= min)
48 {
49 break;
50 }
51 auto linksAddressRange = Range<typename TStorage::LinkAddressType>(min, linksCount);
52 auto linkAddress = Random::NextUInt64(randomGenerator64, linksAddressRange);
53 Delete(storage, linkAddress);
54 }
55 }
56
57 template <typename TStorage>
58 void TestRandomCreationsAndDeletions(TStorage& storage,
59 typename TStorage::LinkAddressType maximumOperationsPerCycle)
60 {
61 using namespace Platform::Random;
62 using namespace Platform::Ranges;
63 using namespace Platform::Interfaces;
64 for (auto N = 1; N < maximumOperationsPerCycle; N++)
65 {
66 auto& randomGen64 = RandomHelpers::Default;
67 typename TStorage::LinkAddressType created = 0;
68 typename TStorage::LinkAddressType deleted = 0;
69 for (auto i = 0; i < N; i++)
70 {
71 auto linksCount = Count(storage);
72 auto createPoint = Random::NextBoolean(randomGen64);
73 if (linksCount >= 2 && createPoint)
74 {
75 Ranges::Range linksAddressRange{1, linksCount};
76 auto source = Random::NextUInt64(randomGen64, linksAddressRange);
77 auto target = Random::NextUInt64(randomGen64, linksAddressRange); //-V3086
78 auto resultLink = GetOrCreate(storage, source, target);
79 if (resultLink > linksCount)
80 {
81 ++created;
82 }
83 }
84 else
85 {
86 Create(storage);
87 ++created;
88 }
89 }
90 auto count = Count(storage);
91 for (auto i = 0; i < count; i++)
92 {
93 auto link = i + 1;
94 {
95 DIRECT_METHOD_CALL(TStorage, storage, Update, link, 0, 0);
96 Delete(storage, link);
97 ++deleted;
98 }
99 }
100 }
101 }
102
103 template <typename TStorage>
104 static typename TStorage::LinkAddressType Delete(TStorage& storage, typename TStorage::LinkAddressType linkToDelete,
105 const typename TStorage::WriteHandlerType& handler)
106 {
107 if (Exists(storage, linkToDelete))
108 {
109 EnforceResetValues(storage, linkToDelete, handler);
110 }
111 // Call the store directly: calling this overload again would recurse forever.
112 return DIRECT_METHOD_CALL(TStorage, storage, Delete, typename TStorage::LinkType{linkToDelete}, handler);
113 }
114
115 template <typename TStorage>
116 static typename TStorage::LinkAddressType First(const TStorage& storage)
117 {
118 constexpr auto constants = std::remove_cvref_t<decltype(storage)>::Constants;
119 auto $break{constants.Break};
120 typename TStorage::LinkAddressType firstLink = 0;
121 Expects(constants.Null != firstLink);
122 DIRECT_METHOD_CALL(
123 TStorage, storage, Each,
124 typename TStorage::LinkType{storage.Constants.Any, storage.Constants.Any, storage.Constants.Any},
125 [&firstLink, $break](const typename TStorage::LinkType& link) {
126 firstLink = link[0];
127 return $break;
128 });
129 Ensures(constants.Null != firstLink);
130 return firstLink;
131 }
132
133 template <typename TStorage>
134 static typename TStorage::HandlerParameterType SingleOrDefault(const TStorage& storage,
135 const typename TStorage::LinkType& query)
136 {
137 typename TStorage::LinkType result{};
138 auto count = 0;
139 constexpr auto constants = std::remove_cvref_t<decltype(storage)>::Constants;
140 auto linkHandler{[&result, &count, &constants](const typename TStorage::LinkType& link) {
141 if (count == 0)
142 {
143 result = link;
144 ++count;
145 return constants.Continue;
146 }
147 else
148 {
149 result = {};
150 return constants.Break;
151 }
152 }};
153 DIRECT_METHOD_CALL(TStorage, storage, Each, query, linkHandler);
154 return result;
155 }
156
157 template <typename TStorage>
158 static bool CheckPathExistence(const TStorage& storage,
159 std::convertible_to<typename TStorage::LinkAddressType> auto... pathPack)
160 {
161 typename TStorage::HandlerParameterType path{static_cast<typename TStorage::LinkdAddressType>(pathPack)...};
162 auto current = path[0];
163 if (!Exists(storage, current))
164 {
165 return false;
166 }
167 constexpr auto constants = std::remove_cvref_t<decltype(storage)>::Constants;
168 for (typename TStorage::LinkAddressType i{1}; i < std::ranges::size(path); ++i)
169 {
170 auto next = path[i];
171 auto values = GetLink(storage, current);
172 auto source = GetSource(storage, values);
173 auto target = GetTarget(storage, values);
174 if (target == source && next == source)
175 {
176 return false;
177 }
178 if ((source != next) && (target != next))
179 {
180 return false;
181 }
182 current = next;
183 }
184 return true;
185 }
186
187 template <typename TStorage>
188 static typename TStorage::LinkAddressType GetByKeys(
189 TStorage& storage, typename TStorage::LinkAddressType root,
190 std::convertible_to<typename TStorage::LinkAddressType> auto... pathPack)
191 {
192 typename TStorage::HandlerParameterType path{static_cast<typename TStorage::LinkdAddressType>(pathPack)...};
193 storage.EnsureLinkExists(root, "root");
194 auto currentLink = root;
195 for (typename TStorage::LinkAddressType i{0}; i < std::ranges::size(path); ++i)
196 {
197 currentLink = GetLink(storage, currentLink)[path[i]];
198 }
199 return currentLink;
200 }
201
202 template <typename TStorage>
203 static auto GetIndex(const TStorage& storage, const typename TStorage::LinkType& link)
204 {
205 return link[storage.Constants.IndexPart];
206 }
207
208 template <typename TStorage>
209 static typename TStorage::LinkAddressType GetSource(const TStorage& storage,
210 typename TStorage::LinkAddressType linkAddress)
211 {
212 return GetLink(storage, linkAddress)[storage.Constants.SourcePart];
213 }
214
215 template <typename TStorage>
216 static typename TStorage::LinkAddressType GetSource(const TStorage& storage,
217 const typename TStorage::LinkType& link)
218 {
219 return link[storage.Constants.SourcePart];
220 }
221
222 template <typename TStorage>
223 static typename TStorage::LinkAddressType GetTarget(const TStorage& storage,
224 typename TStorage::LinkAddressType linkAddress)
225 {
226 return GetLink(storage, linkAddress)[storage.Constants.TargetPart];
227 }
228
229 template <typename TStorage>
230 static typename TStorage::LinkAddressType GetTarget(const TStorage& storage,
231 const typename TStorage::LinkType& link)
232 {
233 return link[storage.Constants.TargetPart];
234 }
235
236 template <typename TStorage>
237 static std::vector<typename TStorage::LinkType> All(const TStorage& storage,
238 const typename TStorage::LinkType& restriction)
239 {
240 using namespace Platform::Collections;
241 auto $continue{storage.Constants.Continue};
242 std::vector<typename TStorage::LinkType> allLinks{};
243 DIRECT_METHOD_CALL(TStorage, storage, Each, restriction,
244 [&allLinks, $continue](const typename TStorage::LinkType& link) {
245 allLinks.push_back(link);
246 return $continue;
247 });
248 return allLinks;
249 }
250
251 template <typename TStorage>
252 static auto All(const TStorage& storage)
253 {
254 return All(storage, typename TStorage::LinkType{});
255 }
256
257 template <typename TStorage, typename... TRestrictions>
258 requires(sizeof...(TRestrictions) > 0 &&
259 (std::convertible_to<TRestrictions, typename TStorage::LinkAddressType> && ...))
260 static auto All(const TStorage& storage, TRestrictions... restrictionPack)
261 {
262 typename TStorage::LinkType restriction{static_cast<typename TStorage::LinkAddressType>(restrictionPack)...};
263 return All(storage, restriction);
264 }
265
266 template <typename TStorage>
267 static typename TStorage::LinkAddressType AllIndices(TStorage& storage,
268 const typename TStorage::LinkType& restriction)
269 {
270 std::vector<typename TStorage::LinkType> allIndices{};
271 auto usages = std::vector<typename TStorage::LinkType>();
272 auto $continue = storage.Constants.Continue;
273 DIRECT_METHOD_CALL(TStorage, storage, Each, restriction,
274 [&allIndices, $continue](const typename TStorage::LinkType& link) {
275 allIndices.push_back(link);
276 return $continue;
277 });
278 return allIndices;
279 }
280
281 template <typename TStorage>
282 static void EnsureLinkExists(TStorage& storage, const typename TStorage::LinkType& restriction)
283 {
284 for (auto i{0}; i < restriction.Count(); ++i)
285 {
286 if (!storage.Exists(restriction[i]))
287 {
288 throw ArgumentLinkDoesNotExistsException<typename TStorage::LinkAddressType>(
289 restriction[i],
290 std::string("sequence[").append(Platform::Converters::To<std::string>(i)).append(1, ']'));
291 }
292 }
293 }
294
295 template <typename TStorage>
296 static bool Exists(TStorage& storage, typename TStorage::LinkAddressType source,
297 typename TStorage::LinkAddressType target)
298 {
299 return DIRECT_METHOD_CALL(TStorage, storage, Count,
300 typename TStorage::LinkType{storage.Constants.Any, source, target}) > 0;
301 }
302
303 template <typename TStorage>
304 static void EnsureInnerReferenceExists(TStorage& storage, typename TStorage::LinkAddressType reference,
305 std::string argumentName)
306 {
307 using namespace Platform::Exceptions;
308 if (storage.Constants.IsInternalReference(reference) && !storage.Exists(reference))
309 {
310 throw ArgumentLinkDoesNotExistsException<typename TStorage::LinkAddressType>(reference, argumentName);
311 }
312 }
313
314 template <typename TStorage>
315 static void EnsureInnerReferenceExists(TStorage& storage, const typename TStorage::LinkType& restriction,
316 std::string argumentName)
317 {
318 using namespace Platform::Exceptions;
319 for (std::int32_t i = 0; i < restriction.Count(); ++i)
320 {
321 storage.EnsureInnerReferenceExists(restriction[i], argumentName);
322 }
323 }
324
325 template <typename TStorage>
326 static void EnsureLinkIsAnyOrExists(TStorage& storage, const typename TStorage::LinkType& restriction)
327 {
328 using namespace Platform::Exceptions;
329 auto any = storage.Constants.Any;
330 for (auto i{0}; i < restriction.Count(); ++i)
331 {
332 if ((any != restriction[i]) && !storage.Exists(restriction[i]))
333 {
334 throw ArgumentLinkDoesNotExistsException<typename TStorage::LinkAddressType>(
335 restriction[i],
336 std::string("sequence[").append(Platform::Converters::To<std::string>(i)).append(1, ']'));
337 }
338 }
339 }
340
341 template <typename TStorage>
342 static void EnsureLinkIsAnyOrExists(TStorage& storage, typename TStorage::LinkAddressType linkAddress,
343 std::string argumentName)
344 {
345 using namespace Platform::Exceptions;
346 if ((storage.Constants.Any != linkAddress) && !storage.Exists(linkAddress))
347 {
348 throw ArgumentLinkDoesNotExistsException<typename TStorage::LinkAddressType>(linkAddress, argumentName);
349 }
350 }
351
352 template <typename TStorage>
353 static void EnsureLinkIsItselfOrExists(TStorage& storage, typename TStorage::LinkAddressType linkAddress,
354 std::string argumentName)
355 {
356 using namespace Platform::Exceptions;
357 if ((storage.Constants.Itself != linkAddress) && !storage.Exists(linkAddress))
358 {
359 throw ArgumentLinkDoesNotExistsException<typename TStorage::LinkAddressType>(linkAddress, argumentName);
360 }
361 }
362
363 template <typename TStorage>
364 static void EnsureDoesNotExists(TStorage& storage, typename TStorage::LinkAddressType source,
365 typename TStorage::LinkAddressType target)
366 {
367 using namespace Platform::Exceptions;
368 if (storage.Exists(source, target))
369 {
370 throw Platform::Exceptions::LinkWithSameValueAlreadyExistsException();
371 }
372 }
373
374 template <typename TStorage>
375 static void EnsureNoUsages(TStorage& storage, typename TStorage::LinkAddressType linkAddress)
376 {
377 using namespace Platform::Exceptions;
378 if (storage.HasUsages(linkAddress))
379 {
380 throw ArgumentLinkHasDependenciesException<typename TStorage::LinkAddressType>(linkAddress);
381 }
382 }
383
384 template <typename TStorage>
385 static void EnsureCreated(TStorage& storage, std::function<typename TStorage::LinkAddressType()> creator,
386 const typename TStorage::LinkType& addresses)
387 {
388 using Address = typename TStorage::LinkAddressType;
389 std::unordered_set<Address> remaining;
390 for (auto address : addresses)
391 {
392 if (address != storage.Constants.Null && address <= storage.Constants.InternalReferencesRange.Maximum &&
393 !Platform::Data::Exists(storage, address))
394 {
395 remaining.insert(address);
396 }
397 }
398 const auto requested = remaining;
399 std::unordered_set<Address> created;
400 // Retain intermediate allocations until every requested address exists:
401 // freeing one early could make the allocator return it repeatedly.
402 while (!remaining.empty())
403 {
404 auto address = creator();
405 if (address == storage.Constants.Null || !created.insert(address).second)
406 {
407 throw std::runtime_error("The creator did not allocate a new link");
408 }
409 remaining.erase(address);
410 }
411 for (auto address : created)
412 {
413 if (!requested.contains(address))
414 {
415 Platform::Data::Update(
416 storage, typename TStorage::LinkType{address},
417 typename TStorage::LinkType{address, storage.Constants.Null, storage.Constants.Null});
418 Platform::Data::Delete(storage, address);
419 }
420 }
421 }
422
423 template <typename TStorage>
424 static void EnsureCreated(TStorage& storage, const typename TStorage::LinkType& addresses)
425 {
426 EnsureCreated(storage, std::function<typename TStorage::LinkAddressType()>{[&storage] {
427 return Platform::Data::Create(storage);
428 }},
429 addresses);
430 }
431
432 template <typename TStorage>
433 static void EnsurePointsCreated(TStorage& storage, const typename TStorage::LinkType& addresses)
434 {
435 EnsureCreated(storage, std::function<typename TStorage::LinkAddressType()>{[&storage] {
436 auto point = Platform::Data::Create(storage);
437 return Platform::Data::Update(storage, typename TStorage::LinkType{point},
438 typename TStorage::LinkType{point, point, point});
439 }},
440 addresses);
441 }
442
443 template <typename TStorage>
444 static typename TStorage::LinkAddressType CountUsages(TStorage& storage,
445 typename TStorage::LinkAddressType linkAddress)
446 {
447 constexpr auto constants = std::remove_cvref_t<decltype(storage)>::Constants;
448 auto values = GetLink(storage, linkAddress);
449 auto usagesAsSource = DIRECT_METHOD_CALL(
450 TStorage, storage, Count, typename TStorage::LinkType{constants.Any, storage, linkAddress, constants.Any});
451 if (linkAddress == GetSource(storage, values))
452 {
453 --usagesAsSource;
454 }
455 auto usagesAsTarget = DIRECT_METHOD_CALL(
456 TStorage, storage, Count, typename TStorage::LinkType{constants.Any, storage, constants.Any, linkAddress});
457 if (linkAddress == GetTarget(storage, values))
458 {
459 --usagesAsTarget;
460 }
461 return usagesAsSource + usagesAsTarget;
462 }
463
464 template <typename TStorage>
465 static bool HasUsages(TStorage& storage, typename TStorage::LinkAddressType linkAddress)
466 {
467 return CountUsages(storage, linkAddress) > 0;
468 }
469
470 template <typename TStorage>
471 typename TStorage::LinkAddressType SearchOrDefault(TStorage& storage, typename TStorage::LinkAddressType source,
472 typename TStorage::LinkAddressType target)
473 {
474 constexpr auto constants = std::remove_cvref_t<decltype(storage)>::Constants;
475 auto _break = constants.Break;
476 typename TStorage::LinkAddressType searchedLinkAddress{};
477 DIRECT_METHOD_CALL(TStorage, storage, Each, typename TStorage::LinkType{storage.Constants.Any, source, target},
478 [&searchedLinkAddress, _break](const typename TStorage::LinkType& link) {
479 searchedLinkAddress = link[0];
480 return _break;
481 });
482 return searchedLinkAddress;
483 }
484 template <typename TStorage>
485 auto CreatePoint(TStorage& storage)
486 {
487 auto point = Platform::Data::Create(storage);
488 return Update(storage, point, point, point);
489 }
490
491 template <typename TStorage>
492 static typename TStorage::LinkAddressType CreatePoint(TStorage& storage,
493 const typename TStorage::WriteHandlerType& handler)
494 {
495 constexpr auto constants = std::remove_cvref_t<decltype(storage)>::Constants;
496 WriteHandlerState<TStorage> handlerState{constants.Continue, constants.Break, handler};
497 typename TStorage::LinkAddressType linkAddress;
498 typename TStorage::LinkAddressType HandlerWrapper = [&linkAddress,
499 &handlerState](const typename TStorage::LinkType& before,
500 const typename TStorage::LinkType& after) {
501 linkAddress = after[0];
502 return handlerState.Handle(before, after);
503 };
504 handlerState.Apply(
505 DIRECT_METHOD_CALL(TStorage, storage, Create, typename TStorage::LinkType{}, HandlerWrapper));
506 return handlerState.Apply(
507 DIRECT_METHOD_CALL(TStorage, storage, Update, linkAddress, linkAddress, linkAddress, HandlerWrapper));
508 }
509
510 template <typename TStorage>
511 typename TStorage::LinkAddressType CreateAndUpdate(TStorage& storage, typename TStorage::LinkAddressType source,
512 typename TStorage::LinkAddressType target)
513 {
514 auto $continue{storage.Constants.Continue};
515 typename TStorage::LinkAddressType linkAddress{};
516 // The handler-taking overload returns the handler state, so return the recorded address instead.
518 storage, source, target,
519 [&linkAddress, $continue](const typename TStorage::LinkType&, const typename TStorage::LinkType& after) {
520 linkAddress = after[0];
521 return $continue;
522 });
523 return linkAddress;
524 }
525
526 template <typename TStorage>
527 static typename TStorage::LinkAddressType CreateAndUpdate(TStorage& storage,
528 typename TStorage::LinkAddressType source,
529 typename TStorage::LinkAddressType target,
530 const typename TStorage::WriteHandlerType& handler)
531 {
532 constexpr auto constants = std::remove_cvref_t<decltype(storage)>::Constants;
533 typename TStorage::LinkAddressType createdLinkAddress;
534 WriteHandlerState<TStorage> handlerState{constants.Continue, constants.Break, handler};
535 handlerState.Apply(
536 DIRECT_METHOD_CALL(TStorage, storage, Create, typename TStorage::LinkType{},
537 [&createdLinkAddress, &handlerState](const typename TStorage::LinkType& before,
538 const typename TStorage::LinkType& after) {
539 createdLinkAddress = after[0];
540 return handlerState.Handle(before, after);
541 }));
542 return handlerState.Apply(Update(storage, createdLinkAddress, source, target, handlerState.Handler));
543 }
544
545 template <typename TStorage>
546 typename TStorage::LinkAddressType Update(TStorage& storage,
547 CArray<typename TStorage::LinkAddressType> auto&& restriction,
548 CArray<typename TStorage::LinkAddressType> auto&& substitution)
549 {
550 auto $continue{storage.Constants.Continue};
551 typename TStorage::LinkAddressType updatedLinkAddress;
552 DIRECT_METHOD_CALL(TStorage, storage, Update, restriction, substitution,
553 [&updatedLinkAddress, $continue](const typename TStorage::LinkType&,
554 const typename TStorage::LinkType& after) {
555 updatedLinkAddress = after[0];
556 return $continue;
557 });
558 return updatedLinkAddress;
559 }
560
561 template <typename TStorage>
562 static typename TStorage::LinkAddressType Update(TStorage& storage, const typename TStorage::LinkType& restriction,
563 const typename TStorage::WriteHandlerType& handler)
564 {
565 const auto length{std::ranges::size(restriction)};
566 if (length == 2)
567 {
568 return MergeAndDelete(storage, restriction[0], restriction[1], handler);
569 }
570 else if (length == 4)
571 {
572 return UpdateOrCreateOrGet(storage, restriction[0], restriction[1], restriction[2], restriction[3],
573 handler);
574 }
575 else
576 {
577 return Update(storage, restriction[0], restriction[1], restriction[2], handler);
578 }
579 }
580
581 template <typename TStorage>
582 static typename TStorage::LinkAddressType Update(TStorage& storage, typename TStorage::LinkAddressType linkAddress,
583 typename TStorage::LinkAddressType newSource,
584 typename TStorage::LinkAddressType newTarget,
585 const typename TStorage::WriteHandlerType& handler)
586 {
587 return DIRECT_METHOD_CALL(TStorage, storage, Update, typename TStorage::LinkType{linkAddress},
588 typename TStorage::LinkType{linkAddress, newSource, newTarget}, handler);
589 // return std::is_abstract<TStorage>::value ? DIRECT_METHOD_CALL(TStorage, storage, Update,typename
590 // TStorage::LinkType{linkAddress}, typename TStorage::LinkType{linkAddress, newSource, newTarget},
591 // handler) : storage.TStorage::Update(typename TStorage::LinkType{linkAddress}, typename
592 // TStorage::LinkType{linkAddress, newSource, newTarget}, handler); if constexpr
593 // (std::is_abstract<TStorage>::value) {
594 // return DIRECT_METHOD_CALL(TStorage, storage, Update,typename TStorage::LinkType{linkAddress},
595 // typename TStorage::LinkType{linkAddress, newSource, newTarget}, handler);
596 // } else {
597 // return storage.TStorage::Update(typename TStorage::LinkType{linkAddress}, typename
598 // TStorage::LinkType{linkAddress, newSource, newTarget}, handler);
599 // }
600 }
601
602 template <typename TStorage>
603 static typename TStorage::LinkAddressType Update(TStorage& storage, typename TStorage::LinkAddressType linkAddress,
604 typename TStorage::LinkAddressType newSource,
605 typename TStorage::LinkAddressType newTarget)
606 {
607 auto $continue{storage.Constants.Continue};
608 typename TStorage::LinkAddressType updatedLinkAddress;
609 Update(storage, linkAddress, newSource, newTarget,
610 [&updatedLinkAddress, $continue](const typename TStorage::LinkType&,
611 const typename TStorage::LinkType& after) {
612 updatedLinkAddress = after[0];
613 return $continue;
614 });
615 return updatedLinkAddress;
616 }
617
618 template <typename TStorage>
619 static typename TStorage::LinkAddressType Update(TStorage& storage, const typename TStorage::LinkType& restriction)
620 {
621 auto $continue{storage.Constants.Continue};
622 typename TStorage::LinkAddressType updatedLinkAddress;
623 return Update(storage, restriction,
624 [&updatedLinkAddress, $continue](const typename TStorage::LinkType&,
625 const typename TStorage::LinkType& after) {
626 updatedLinkAddress = after;
627 return $continue;
628 });
629 }
630
631 template <typename TStorage>
632 static typename TStorage::LinkAddressType Update(
633 TStorage& storage, const typename TStorage::WriteHandlerType& handler,
634 std::convertible_to<typename TStorage::LinkAddressType> auto... restrictionPack)
635 {
636 typename TStorage::HandlerParameterType restriction{
637 static_cast<typename TStorage::HandlerParameterType>(restrictionPack)...};
638 return Update(storage, restriction, handler);
639 }
640 //
641 template <typename TStorage>
642 static typename TStorage::LinkType ResolveConstantAsSelfReference(const TStorage& storage,
643 typename TStorage::LinkAddressType constant,
644 const typename TStorage::LinkType& restriction,
645 const typename TStorage::LinkType& substitution)
646 {
647 constexpr auto constants = std::remove_cvref_t<decltype(storage)>::Constants;
648 auto restrictionIndex = GetIndex(storage, restriction);
649 auto substitutionIndex = GetIndex(storage, substitution);
650 if (constants.Null == substitutionIndex)
651 {
652 substitutionIndex = restrictionIndex;
653 }
654 auto source = GetSource(storage, substitution);
655 auto target = GetTarget(storage, substitution);
656 source = constant == source ? substitutionIndex : source;
657 target = constant == target ? substitutionIndex : target;
658 return typename TStorage::LinkType{substitutionIndex, source, target};
659 }
660
661 template <typename TStorage>
662 typename TStorage::LinkAddressType GetOrCreate(TStorage& storage, typename TStorage::LinkAddressType source,
663 typename TStorage::LinkAddressType target)
664 {
665 constexpr auto constants = std::remove_cvref_t<decltype(storage)>::Constants;
666 auto link = SearchOrDefault(storage, source, target);
667 if (link == constants.Null)
668 {
669 link = CreateAndUpdate(storage, source, target);
670 }
671 return link;
672 }
673
674 template <typename TStorage>
675 static typename TStorage::LinkAddressType UpdateOrCreateOrGet(TStorage& storage,
676 typename TStorage::LinkAddressType source,
677 typename TStorage::LinkAddressType target,
678 typename TStorage::LinkAddressType newSource,
679 typename TStorage::LinkAddressType newTarget,
680 const typename TStorage::WriteHandlerType& handler)
681 {
682 auto link = SearchOrDefault(storage, source, target);
683 if (storage.Constants.Null == link)
684 {
685 return CreateAndUpdate(storage, newSource, newTarget, handler);
686 }
687 if ((source == newSource) && (target == newTarget))
688 {
689 return link;
690 }
691 return Update(storage, link, newSource, newTarget, handler);
692 }
693
694 template <typename TStorage>
695 static typename TStorage::LinkAddressType UpdateOrCreateOrGet(TStorage& storage,
696 typename TStorage::LinkAddressType source,
697 typename TStorage::LinkAddressType target,
698 typename TStorage::LinkAddressType newSource,
699 typename TStorage::LinkAddressType newTarget)
700 {
701 auto $continue = storage.Constants.Continue;
702 typename TStorage::LinkAddressType resultLink;
703 UpdateOrCreateOrGet(
704 storage, source, target, newSource, newTarget,
705 [&resultLink, $continue](const typename TStorage::LinkType&, const typename TStorage::LinkType& after) {
706 resultLink = after[0];
707 return $continue;
708 });
709 return resultLink;
710 }
711
712 template <typename TStorage>
713 typename TStorage::LinkAddressType DeleteIfExists(TStorage& storage, typename TStorage::LinkAddressType source,
714 typename TStorage::LinkAddressType target)
715 {
716 constexpr auto constants = std::remove_cvref_t<decltype(storage)>::Constants;
717 auto linkAddress = storage.SearchOrDefault(source, target);
718 if (linkAddress != constants.Null)
719 {
720 return Delete(storage, linkAddress);
721 }
722 return constants.Null;
723 }
724
725 template <typename TStorage>
726 static void DeleteMany(TStorage& storage, const typename TStorage::LinkType& linkAddressesToDelete)
727 {
728 for (auto linkAddress : linkAddressesToDelete)
729 {
730 Delete(storage, linkAddress);
731 }
732 }
733
734 template <typename TStorage>
735 static typename TStorage::LinkAddressType DeleteAllUsages(TStorage& storage,
736 typename TStorage::LinkAddressType linkIndex,
737 const typename TStorage::WriteHandlerType& handler)
738 {
739 constexpr auto constants = std::remove_cvref_t<decltype(storage)>::Constants;
740 auto any = constants.Any;
741 auto $continue = storage.Constants.Continue;
742 auto $break = storage.Constants.Break;
743 auto usagesAsSourceQuery = typename TStorage::LinkType{any, linkIndex, any};
744 auto usagesAsTargetQuery = typename TStorage::LinkType{any, any, linkIndex};
745 auto usages = std::vector<typename TStorage::LinkType>();
746 DIRECT_METHOD_CALL(TStorage, storage, Each, usagesAsSourceQuery,
747 [&usages, $continue](const typename TStorage::LinkType& link) {
748 usages.push_back(link);
749 return $continue;
750 });
751 DIRECT_METHOD_CALL(TStorage, storage, Each, usagesAsTargetQuery, [&usages, $continue](auto&& link) {
752 usages.push_back(link);
753 return $continue;
754 });
755 WriteHandlerState<TStorage> handlerState{$continue, $break, handler};
756 for (auto usage : usages)
757 {
758 auto usageAddress{GetIndex(storage, usage)};
759 if (linkIndex == usageAddress || Exists(storage, usageAddress))
760 {
761 continue;
762 }
763 auto result = DIRECT_METHOD_CALL(TStorage, storage, Delete, usage, handlerState.Handler);
764 handlerState.Apply(result);
765 }
766 return handlerState.Result;
767 }
768
769 template <typename TStorage>
770 static void DeleteAllUsages(TStorage& storage, typename TStorage::LinkAddressType linkIndex)
771 {
772 DeleteAllUsages(storage, linkIndex, nullptr);
773 }
774
775 // Count is the number of links, not the highest address: deleted links leave freed addresses
776 // behind. Find the highest existing address, then delete existing links from it down. The
777 // handler-taking Delete resets endpoints first, which keeps the endpoint indexes consistent.
778 template <typename TStorage>
779 static void DeleteAll(TStorage& storage)
780 {
781 using Address = typename TStorage::LinkAddressType;
782 auto $continue = storage.Constants.Continue;
783 // Native stores call the handler unchecked, so pass one that only continues.
784 auto handler = [$continue](const auto&, const auto&) { return $continue; };
785 // The scan stops at the last existing link, so it ends on the highest address.
786 Address last{0};
787 auto remaining = Count(storage);
788 while (remaining != 0)
789 {
790 ++last;
791 if (Exists(storage, last))
792 {
793 --remaining;
794 }
795 }
796 for (auto address = last; address != 0; --address)
797 {
798 if (Exists(storage, address))
799 {
800 Delete(storage, address, handler);
801 }
802 }
803 }
804
805 template <typename TStorage>
806 static bool AreValuesReset(const TStorage& storage, typename TStorage::LinkAddressType linkIndex)
807 {
808 auto nullConstant = storage.Constants.Null;
809 auto link = GetLink(storage, linkIndex);
810 for (std::size_t i = 1; i < std::ranges::size(link); ++i)
811 {
812 if (nullConstant != link[i])
813 {
814 return false;
815 }
816 }
817 return true;
818 }
819
820 template <typename TStorage>
821 typename TStorage::LinkAddressType ResetValues(TStorage& storage, typename TStorage::LinkAddressType linkAddress,
822 const typename TStorage::WriteHandlerType& handler)
823 {
824 return Update(storage, linkAddress, 0, 0, handler);
825 }
826
827 template <typename TStorage>
828 void ResetValues(TStorage& storage, typename TStorage::LinkAddressType linkAddress)
829 {
830 auto $break = storage.Constants.Break;
831 ResetValues(storage, linkAddress, nullptr);
832 }
833
834 template <typename TStorage>
835 static typename TStorage::LinkAddressType EnforceResetValues(TStorage& storage,
836 typename TStorage::LinkAddressType linkIndex,
837 const typename TStorage::WriteHandlerType& handler)
838 {
839 if (!AreValuesReset(storage, linkIndex))
840 {
841 return ResetValues(storage, linkIndex, handler);
842 }
843 return storage.Constants.Continue;
844 }
845
846 template <typename TStorage>
847 static typename TStorage::LinkAddressType EnforceResetValues(TStorage& storage,
848 typename TStorage::LinkAddressType linkIndex)
849 {
850 return EnforceResetValues(storage, linkIndex, nullptr);
851 }
852
853 template <typename TStorage>
854 static typename TStorage::LinkAddressType MergeUsages(TStorage& storage,
855 typename TStorage::LinkAddressType oldLinkIndex,
856 typename TStorage::LinkAddressType newLinkIndex,
857 const typename TStorage::WriteHandlerType& handler)
858 {
859 if (newLinkIndex == oldLinkIndex)
860 {
861 return newLinkIndex;
862 }
863 constexpr auto constants = std::remove_cvref_t<decltype(storage)>::Constants;
864 using LinkType = typename TStorage::LinkType;
865 WriteHandlerState<TStorage> handlerState{constants.Continue, constants.Break, handler};
866 // Stores invoke handlers unchecked; Handle skips a null or already broken handler.
867 auto handle = [&handlerState](const LinkType& before, const LinkType& after) {
868 return handlerState.Handle(before, after);
869 };
870 // Name all three parts: a two-value link is (index, source), not (source, target).
871 auto usagesAsSource = All(storage, LinkType{constants.Any, oldLinkIndex, constants.Any});
872 for (const auto& usageAsSource : usagesAsSource)
873 {
874 auto index = GetIndex(storage, usageAsSource);
875 if (oldLinkIndex == index)
876 {
877 continue;
878 }
879 LinkType restriction{index};
880 LinkType substitution{index, newLinkIndex, GetTarget(storage, usageAsSource)};
881 handlerState.Apply(DIRECT_METHOD_CALL(TStorage, storage, Update, restriction, substitution, handle));
882 }
883 // Re-read usages as target: links using the old link on both ends now have the new source.
884 auto usagesAsTarget = All(storage, LinkType{constants.Any, constants.Any, oldLinkIndex});
885 for (const auto& usageAsTarget : usagesAsTarget)
886 {
887 auto index = GetIndex(storage, usageAsTarget);
888 if (oldLinkIndex == index)
889 {
890 continue;
891 }
892 LinkType restriction{index};
893 LinkType substitution{index, GetSource(storage, usageAsTarget), newLinkIndex};
894 handlerState.Apply(DIRECT_METHOD_CALL(TStorage, storage, Update, restriction, substitution, handle));
895 }
896 return handlerState.Result;
897 }
898
899 template <typename TStorage>
900 static void MergeUsages(TStorage& storage, typename TStorage::LinkAddressType oldLinkIndex,
901 typename TStorage::LinkAddressType newLinkIndex)
902 {
903 MergeUsages(storage, oldLinkIndex, newLinkIndex, nullptr);
904 }
905
906 template <typename TStorage>
907 static typename TStorage::LinkAddressType MergeAndDelete(TStorage& storage,
908 typename TStorage::LinkAddressType oldLinkIndex,
909 typename TStorage::LinkAddressType newLinkIndex)
910 {
911 if (newLinkIndex != oldLinkIndex)
912 {
913 MergeUsages(storage, oldLinkIndex, newLinkIndex);
914 Delete(storage, oldLinkIndex);
915 }
916 return newLinkIndex;
917 }
918
919 template <typename TStorage>
920 static typename TStorage::LinkAddressType MergeAndDelete(TStorage& storage,
921 typename TStorage::LinkAddressType oldLinkIndex,
922 typename TStorage::LinkAddressType newLinkIndex,
923 const typename TStorage::WriteHandlerType& handler)
924 {
925 constexpr auto constants = std::remove_cvref_t<decltype(storage)>::Constants;
926 WriteHandlerState<TStorage> handlerState{constants.Continue, constants.Break, handler};
927 auto handle = [&handlerState](const typename TStorage::LinkType& before,
928 const typename TStorage::LinkType& after) {
929 return handlerState.Handle(before, after);
930 };
931 if (newLinkIndex != oldLinkIndex)
932 {
933 handlerState.Apply(MergeUsages(storage, oldLinkIndex, newLinkIndex, handle));
934 handlerState.Apply(Delete(storage, oldLinkIndex, handle));
935 }
936 return handlerState.Result;
937 }
938
939 template <typename TStorage>
940 static std::string Format(TStorage& storage, const typename TStorage::LinkType& link)
941 {
942 constexpr auto constants = std::remove_cvref_t<decltype(storage)>::Constants;
943 return std::string{}
944 .append("(")
945 .append(std::to_string(GetIndex(storage, link)))
946 .append("): ")
947 .append("(")
948 .append(std::to_string(GetSource(storage, link)))
949 .append(")")
950 .append(" -> ")
951 .append("(")
952 .append(std::to_string(GetTarget(storage, link)))
953 .append(")");
954 }
955
956 template <typename TStorage>
957 std::string Format(TStorage& storage, typename TStorage::LinkAddressType linkAddress)
958 {
959 return Format(GetLink(storage, linkAddress));
960 }
961
962 template <typename TStorage>
963 typename TStorage::LinkAddressType HasUsages(TStorage& storage, typename TStorage::LinkAddressType linkAddress)
964 {
965 return storage.CountUsages(linkAddress) != 0;
966 }
967
968 template <typename TStorage>
969 static void DeleteByQuery(TStorage& storage, typename TStorage::LinkType query)
970 {
971 typename TStorage::LinkType queryResult;
972 DIRECT_METHOD_CALL(TStorage, storage, Each, query,
973 [&](typename TStorage::LinkType vec) -> typename TStorage::LinkAddressType {
974 queryResult.push_back(vec[storage.Constants.IndexPart]);
975 return storage.Constants.Continue;
976 });
977
978 for (auto link : queryResult)
979 {
980 Delete(storage, link);
981 }
982 }
983
984} // namespace Platform::Data::Doublets
Definition TargetMatcher.h:2
TStorage::LinkAddressType SearchOrDefault(TStorage &storage, typename TStorage::LinkAddressType source, typename TStorage::LinkAddressType target)
Definition ILinksExtensions.h:471
TStorage::LinkAddressType DeleteIfExists(TStorage &storage, typename TStorage::LinkAddressType source, typename TStorage::LinkAddressType target)
Definition ILinksExtensions.h:713
auto CreatePoint(TStorage &storage)
Definition ILinksExtensions.h:485
TStorage::LinkAddressType ResetValues(TStorage &storage, typename TStorage::LinkAddressType linkAddress, const typename TStorage::WriteHandlerType &handler)
Definition ILinksExtensions.h:821
TStorage::LinkAddressType GetOrCreate(TStorage &storage, typename TStorage::LinkAddressType source, typename TStorage::LinkAddressType target)
Definition ILinksExtensions.h:662
TStorage::LinkAddressType CreateAndUpdate(TStorage &storage, typename TStorage::LinkAddressType source, typename TStorage::LinkAddressType target)
Definition ILinksExtensions.h:511
void TestRandomCreationsAndDeletions(TStorage &storage, typename TStorage::LinkAddressType maximumOperationsPerCycle)
Definition ILinksExtensions.h:58
TStorage::LinkAddressType Update(TStorage &storage, CArray< typename TStorage::LinkAddressType > auto &&restriction, CArray< typename TStorage::LinkAddressType > auto &&substitution)
Definition ILinksExtensions.h:546