3 template <
typename TStorage>
4 static void RunRandomCreations(TStorage& storage,
typename TStorage::LinkAddressType amountOfCreations)
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)
12 Range<typename TStorage::LinkAddressType> linksAddressRange{0, Count(storage)};
13 auto source = Random::NextUInt64(randomGenerator64, linksAddressRange);
14 auto target = Random::NextUInt64(randomGenerator64, linksAddressRange);
19 template <
typename TStorage>
20 static void RunRandomSearches(
const TStorage& storage,
typename TStorage::LinkAddressType amountOfSearches)
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)
28 auto linksAddressRange = Range<typename TStorage::LinkAddressType>(0, Count(storage));
29 auto source = Random::NextUInt64(randomGenerator64, linksAddressRange);
30 auto target = Random::NextUInt64(randomGenerator64, linksAddressRange);
35 template <
typename TStorage>
36 static void RunRandomDeletions(TStorage& storage,
typename TStorage::LinkAddressType amountOfDeletions)
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)
46 linksCount = Count(storage);
47 if (linksCount <= min)
51 auto linksAddressRange = Range<typename TStorage::LinkAddressType>(min, linksCount);
52 auto linkAddress = Random::NextUInt64(randomGenerator64, linksAddressRange);
53 Delete(storage, linkAddress);
57 template <
typename TStorage>
59 typename TStorage::LinkAddressType maximumOperationsPerCycle)
61 using namespace Platform::Random;
62 using namespace Platform::Ranges;
63 using namespace Platform::Interfaces;
64 for (
auto N = 1; N < maximumOperationsPerCycle; N++)
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++)
71 auto linksCount = Count(storage);
72 auto createPoint = Random::NextBoolean(randomGen64);
73 if (linksCount >= 2 && createPoint)
75 Ranges::Range linksAddressRange{1, linksCount};
76 auto source = Random::NextUInt64(randomGen64, linksAddressRange);
77 auto target = Random::NextUInt64(randomGen64, linksAddressRange);
78 auto resultLink =
GetOrCreate(storage, source, target);
79 if (resultLink > linksCount)
90 auto count = Count(storage);
91 for (
auto i = 0; i < count; i++)
95 DIRECT_METHOD_CALL(TStorage, storage,
Update, link, 0, 0);
96 Delete(storage, link);
103 template <
typename TStorage>
104 static typename TStorage::LinkAddressType Delete(TStorage& storage,
typename TStorage::LinkAddressType linkToDelete,
105 const typename TStorage::WriteHandlerType& handler)
107 if (Exists(storage, linkToDelete))
109 EnforceResetValues(storage, linkToDelete, handler);
112 return DIRECT_METHOD_CALL(TStorage, storage, Delete,
typename TStorage::LinkType{linkToDelete}, handler);
115 template <
typename TStorage>
116 static typename TStorage::LinkAddressType First(
const TStorage& storage)
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);
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) {
129 Ensures(constants.Null != firstLink);
133 template <
typename TStorage>
134 static typename TStorage::HandlerParameterType SingleOrDefault(
const TStorage& storage,
135 const typename TStorage::LinkType& query)
137 typename TStorage::LinkType result{};
139 constexpr auto constants = std::remove_cvref_t<
decltype(storage)>::Constants;
140 auto linkHandler{[&result, &count, &constants](
const typename TStorage::LinkType& link) {
145 return constants.Continue;
150 return constants.Break;
153 DIRECT_METHOD_CALL(TStorage, storage, Each, query, linkHandler);
157 template <
typename TStorage>
158 static bool CheckPathExistence(
const TStorage& storage,
159 std::convertible_to<typename TStorage::LinkAddressType>
auto... pathPack)
161 typename TStorage::HandlerParameterType path{
static_cast<typename TStorage::LinkdAddressType
>(pathPack)...};
162 auto current = path[0];
163 if (!Exists(storage, current))
167 constexpr auto constants = std::remove_cvref_t<
decltype(storage)>::Constants;
168 for (
typename TStorage::LinkAddressType i{1}; i < std::ranges::size(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)
178 if ((source != next) && (target != next))
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)
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)
197 currentLink = GetLink(storage, currentLink)[path[i]];
202 template <
typename TStorage>
203 static auto GetIndex(
const TStorage& storage,
const typename TStorage::LinkType& link)
205 return link[storage.Constants.IndexPart];
208 template <
typename TStorage>
209 static typename TStorage::LinkAddressType GetSource(
const TStorage& storage,
210 typename TStorage::LinkAddressType linkAddress)
212 return GetLink(storage, linkAddress)[storage.Constants.SourcePart];
215 template <
typename TStorage>
216 static typename TStorage::LinkAddressType GetSource(
const TStorage& storage,
217 const typename TStorage::LinkType& link)
219 return link[storage.Constants.SourcePart];
222 template <
typename TStorage>
223 static typename TStorage::LinkAddressType GetTarget(
const TStorage& storage,
224 typename TStorage::LinkAddressType linkAddress)
226 return GetLink(storage, linkAddress)[storage.Constants.TargetPart];
229 template <
typename TStorage>
230 static typename TStorage::LinkAddressType GetTarget(
const TStorage& storage,
231 const typename TStorage::LinkType& link)
233 return link[storage.Constants.TargetPart];
236 template <
typename TStorage>
237 static std::vector<typename TStorage::LinkType> All(
const TStorage& storage,
238 const typename TStorage::LinkType& restriction)
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);
251 template <
typename TStorage>
252 static auto All(
const TStorage& storage)
254 return All(storage,
typename TStorage::LinkType{});
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)
262 typename TStorage::LinkType restriction{
static_cast<typename TStorage::LinkAddressType
>(restrictionPack)...};
263 return All(storage, restriction);
266 template <
typename TStorage>
267 static typename TStorage::LinkAddressType AllIndices(TStorage& storage,
268 const typename TStorage::LinkType& restriction)
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);
281 template <
typename TStorage>
282 static void EnsureLinkExists(TStorage& storage,
const typename TStorage::LinkType& restriction)
284 for (
auto i{0}; i < restriction.Count(); ++i)
286 if (!storage.Exists(restriction[i]))
288 throw ArgumentLinkDoesNotExistsException<typename TStorage::LinkAddressType>(
290 std::string(
"sequence[").append(Platform::Converters::To<std::string>(i)).append(1,
']'));
295 template <
typename TStorage>
296 static bool Exists(TStorage& storage,
typename TStorage::LinkAddressType source,
297 typename TStorage::LinkAddressType target)
299 return DIRECT_METHOD_CALL(TStorage, storage, Count,
300 typename TStorage::LinkType{storage.Constants.Any, source, target}) > 0;
303 template <
typename TStorage>
304 static void EnsureInnerReferenceExists(TStorage& storage,
typename TStorage::LinkAddressType reference,
305 std::string argumentName)
307 using namespace Platform::Exceptions;
308 if (storage.Constants.IsInternalReference(reference) && !storage.Exists(reference))
310 throw ArgumentLinkDoesNotExistsException<typename TStorage::LinkAddressType>(reference, argumentName);
314 template <
typename TStorage>
315 static void EnsureInnerReferenceExists(TStorage& storage,
const typename TStorage::LinkType& restriction,
316 std::string argumentName)
318 using namespace Platform::Exceptions;
319 for (std::int32_t i = 0; i < restriction.Count(); ++i)
321 storage.EnsureInnerReferenceExists(restriction[i], argumentName);
325 template <
typename TStorage>
326 static void EnsureLinkIsAnyOrExists(TStorage& storage,
const typename TStorage::LinkType& restriction)
328 using namespace Platform::Exceptions;
329 auto any = storage.Constants.Any;
330 for (
auto i{0}; i < restriction.Count(); ++i)
332 if ((any != restriction[i]) && !storage.Exists(restriction[i]))
334 throw ArgumentLinkDoesNotExistsException<typename TStorage::LinkAddressType>(
336 std::string(
"sequence[").append(Platform::Converters::To<std::string>(i)).append(1,
']'));
341 template <
typename TStorage>
342 static void EnsureLinkIsAnyOrExists(TStorage& storage,
typename TStorage::LinkAddressType linkAddress,
343 std::string argumentName)
345 using namespace Platform::Exceptions;
346 if ((storage.Constants.Any != linkAddress) && !storage.Exists(linkAddress))
348 throw ArgumentLinkDoesNotExistsException<typename TStorage::LinkAddressType>(linkAddress, argumentName);
352 template <
typename TStorage>
353 static void EnsureLinkIsItselfOrExists(TStorage& storage,
typename TStorage::LinkAddressType linkAddress,
354 std::string argumentName)
356 using namespace Platform::Exceptions;
357 if ((storage.Constants.Itself != linkAddress) && !storage.Exists(linkAddress))
359 throw ArgumentLinkDoesNotExistsException<typename TStorage::LinkAddressType>(linkAddress, argumentName);
363 template <
typename TStorage>
364 static void EnsureDoesNotExists(TStorage& storage,
typename TStorage::LinkAddressType source,
365 typename TStorage::LinkAddressType target)
367 using namespace Platform::Exceptions;
368 if (storage.Exists(source, target))
370 throw Platform::Exceptions::LinkWithSameValueAlreadyExistsException();
374 template <
typename TStorage>
375 static void EnsureNoUsages(TStorage& storage,
typename TStorage::LinkAddressType linkAddress)
377 using namespace Platform::Exceptions;
378 if (storage.HasUsages(linkAddress))
380 throw ArgumentLinkHasDependenciesException<typename TStorage::LinkAddressType>(linkAddress);
384 template <
typename TStorage>
385 static void EnsureCreated(TStorage& storage, std::function<
typename TStorage::LinkAddressType()> creator,
386 const typename TStorage::LinkType& addresses)
388 using Address =
typename TStorage::LinkAddressType;
389 std::unordered_set<Address> remaining;
390 for (
auto address : addresses)
392 if (address != storage.Constants.Null && address <= storage.Constants.InternalReferencesRange.Maximum &&
393 !Platform::Data::Exists(storage, address))
395 remaining.insert(address);
398 const auto requested = remaining;
399 std::unordered_set<Address> created;
402 while (!remaining.empty())
404 auto address = creator();
405 if (address == storage.Constants.Null || !created.insert(address).second)
407 throw std::runtime_error(
"The creator did not allocate a new link");
409 remaining.erase(address);
411 for (
auto address : created)
413 if (!requested.contains(address))
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);
423 template <
typename TStorage>
424 static void EnsureCreated(TStorage& storage,
const typename TStorage::LinkType& addresses)
426 EnsureCreated(storage, std::function<
typename TStorage::LinkAddressType()>{[&storage] {
427 return Platform::Data::Create(storage);
432 template <
typename TStorage>
433 static void EnsurePointsCreated(TStorage& storage,
const typename TStorage::LinkType& addresses)
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});
443 template <
typename TStorage>
444 static typename TStorage::LinkAddressType CountUsages(TStorage& storage,
445 typename TStorage::LinkAddressType linkAddress)
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))
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))
461 return usagesAsSource + usagesAsTarget;
464 template <
typename TStorage>
465 static bool HasUsages(TStorage& storage,
typename TStorage::LinkAddressType linkAddress)
467 return CountUsages(storage, linkAddress) > 0;
470 template <
typename TStorage>
471 typename TStorage::LinkAddressType
SearchOrDefault(TStorage& storage,
typename TStorage::LinkAddressType source,
472 typename TStorage::LinkAddressType target)
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];
482 return searchedLinkAddress;
484 template <
typename TStorage>
487 auto point = Platform::Data::Create(storage);
488 return Update(storage, point, point, point);
491 template <
typename TStorage>
492 static typename TStorage::LinkAddressType
CreatePoint(TStorage& storage,
493 const typename TStorage::WriteHandlerType& handler)
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);
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));
510 template <
typename TStorage>
511 typename TStorage::LinkAddressType
CreateAndUpdate(TStorage& storage,
typename TStorage::LinkAddressType source,
512 typename TStorage::LinkAddressType target)
514 auto $continue{storage.Constants.Continue};
515 typename TStorage::LinkAddressType linkAddress{};
518 storage, source, target,
519 [&linkAddress, $continue](
const typename TStorage::LinkType&,
const typename TStorage::LinkType& after) {
520 linkAddress = after[0];
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)
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};
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);
542 return handlerState.Apply(
Update(storage, createdLinkAddress, source, target, handlerState.Handler));
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)
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];
558 return updatedLinkAddress;
561 template <
typename TStorage>
562 static typename TStorage::LinkAddressType
Update(TStorage& storage,
const typename TStorage::LinkType& restriction,
563 const typename TStorage::WriteHandlerType& handler)
565 const auto length{std::ranges::size(restriction)};
568 return MergeAndDelete(storage, restriction[0], restriction[1], handler);
570 else if (length == 4)
572 return UpdateOrCreateOrGet(storage, restriction[0], restriction[1], restriction[2], restriction[3],
577 return Update(storage, restriction[0], restriction[1], restriction[2], handler);
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)
587 return DIRECT_METHOD_CALL(TStorage, storage,
Update,
typename TStorage::LinkType{linkAddress},
588 typename TStorage::LinkType{linkAddress, newSource, newTarget}, handler);
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)
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];
615 return updatedLinkAddress;
618 template <
typename TStorage>
619 static typename TStorage::LinkAddressType
Update(TStorage& storage,
const typename TStorage::LinkType& restriction)
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;
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)
636 typename TStorage::HandlerParameterType restriction{
637 static_cast<typename TStorage::HandlerParameterType
>(restrictionPack)...};
638 return Update(storage, restriction, handler);
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)
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)
652 substitutionIndex = restrictionIndex;
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};
661 template <
typename TStorage>
662 typename TStorage::LinkAddressType
GetOrCreate(TStorage& storage,
typename TStorage::LinkAddressType source,
663 typename TStorage::LinkAddressType target)
665 constexpr auto constants = std::remove_cvref_t<
decltype(storage)>::Constants;
667 if (link == constants.Null)
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)
683 if (storage.Constants.Null == link)
687 if ((source == newSource) && (target == newTarget))
691 return Update(storage, link, newSource, newTarget, handler);
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)
701 auto $continue = storage.Constants.Continue;
702 typename TStorage::LinkAddressType resultLink;
704 storage, source, target, newSource, newTarget,
705 [&resultLink, $continue](
const typename TStorage::LinkType&,
const typename TStorage::LinkType& after) {
706 resultLink = after[0];
712 template <
typename TStorage>
713 typename TStorage::LinkAddressType
DeleteIfExists(TStorage& storage,
typename TStorage::LinkAddressType source,
714 typename TStorage::LinkAddressType target)
716 constexpr auto constants = std::remove_cvref_t<
decltype(storage)>::Constants;
717 auto linkAddress = storage.SearchOrDefault(source, target);
718 if (linkAddress != constants.Null)
720 return Delete(storage, linkAddress);
722 return constants.Null;
725 template <
typename TStorage>
726 static void DeleteMany(TStorage& storage,
const typename TStorage::LinkType& linkAddressesToDelete)
728 for (
auto linkAddress : linkAddressesToDelete)
730 Delete(storage, linkAddress);
734 template <
typename TStorage>
735 static typename TStorage::LinkAddressType DeleteAllUsages(TStorage& storage,
736 typename TStorage::LinkAddressType linkIndex,
737 const typename TStorage::WriteHandlerType& handler)
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);
751 DIRECT_METHOD_CALL(TStorage, storage, Each, usagesAsTargetQuery, [&usages, $continue](
auto&& link) {
752 usages.push_back(link);
755 WriteHandlerState<TStorage> handlerState{$continue, $break, handler};
756 for (
auto usage : usages)
758 auto usageAddress{GetIndex(storage, usage)};
759 if (linkIndex == usageAddress || Exists(storage, usageAddress))
763 auto result = DIRECT_METHOD_CALL(TStorage, storage, Delete, usage, handlerState.Handler);
764 handlerState.Apply(result);
766 return handlerState.Result;
769 template <
typename TStorage>
770 static void DeleteAllUsages(TStorage& storage,
typename TStorage::LinkAddressType linkIndex)
772 DeleteAllUsages(storage, linkIndex,
nullptr);
778 template <
typename TStorage>
779 static void DeleteAll(TStorage& storage)
781 using Address =
typename TStorage::LinkAddressType;
782 auto $continue = storage.Constants.Continue;
784 auto handler = [$continue](
const auto&,
const auto&) {
return $continue; };
787 auto remaining = Count(storage);
788 while (remaining != 0)
791 if (Exists(storage, last))
796 for (
auto address = last; address != 0; --address)
798 if (Exists(storage, address))
800 Delete(storage, address, handler);
805 template <
typename TStorage>
806 static bool AreValuesReset(
const TStorage& storage,
typename TStorage::LinkAddressType linkIndex)
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)
812 if (nullConstant != link[i])
820 template <
typename TStorage>
821 typename TStorage::LinkAddressType
ResetValues(TStorage& storage,
typename TStorage::LinkAddressType linkAddress,
822 const typename TStorage::WriteHandlerType& handler)
824 return Update(storage, linkAddress, 0, 0, handler);
827 template <
typename TStorage>
828 void ResetValues(TStorage& storage,
typename TStorage::LinkAddressType linkAddress)
830 auto $break = storage.Constants.Break;
834 template <
typename TStorage>
835 static typename TStorage::LinkAddressType EnforceResetValues(TStorage& storage,
836 typename TStorage::LinkAddressType linkIndex,
837 const typename TStorage::WriteHandlerType& handler)
839 if (!AreValuesReset(storage, linkIndex))
843 return storage.Constants.Continue;
846 template <
typename TStorage>
847 static typename TStorage::LinkAddressType EnforceResetValues(TStorage& storage,
848 typename TStorage::LinkAddressType linkIndex)
850 return EnforceResetValues(storage, linkIndex,
nullptr);
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)
859 if (newLinkIndex == oldLinkIndex)
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};
867 auto handle = [&handlerState](
const LinkType& before,
const LinkType& after) {
868 return handlerState.Handle(before, after);
871 auto usagesAsSource = All(storage, LinkType{constants.Any, oldLinkIndex, constants.Any});
872 for (
const auto& usageAsSource : usagesAsSource)
874 auto index = GetIndex(storage, usageAsSource);
875 if (oldLinkIndex == index)
879 LinkType restriction{index};
880 LinkType substitution{index, newLinkIndex, GetTarget(storage, usageAsSource)};
881 handlerState.Apply(DIRECT_METHOD_CALL(TStorage, storage,
Update, restriction, substitution, handle));
884 auto usagesAsTarget = All(storage, LinkType{constants.Any, constants.Any, oldLinkIndex});
885 for (
const auto& usageAsTarget : usagesAsTarget)
887 auto index = GetIndex(storage, usageAsTarget);
888 if (oldLinkIndex == index)
892 LinkType restriction{index};
893 LinkType substitution{index, GetSource(storage, usageAsTarget), newLinkIndex};
894 handlerState.Apply(DIRECT_METHOD_CALL(TStorage, storage,
Update, restriction, substitution, handle));
896 return handlerState.Result;
899 template <
typename TStorage>
900 static void MergeUsages(TStorage& storage,
typename TStorage::LinkAddressType oldLinkIndex,
901 typename TStorage::LinkAddressType newLinkIndex)
903 MergeUsages(storage, oldLinkIndex, newLinkIndex,
nullptr);
906 template <
typename TStorage>
907 static typename TStorage::LinkAddressType MergeAndDelete(TStorage& storage,
908 typename TStorage::LinkAddressType oldLinkIndex,
909 typename TStorage::LinkAddressType newLinkIndex)
911 if (newLinkIndex != oldLinkIndex)
913 MergeUsages(storage, oldLinkIndex, newLinkIndex);
914 Delete(storage, oldLinkIndex);
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)
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);
931 if (newLinkIndex != oldLinkIndex)
933 handlerState.Apply(MergeUsages(storage, oldLinkIndex, newLinkIndex, handle));
934 handlerState.Apply(Delete(storage, oldLinkIndex, handle));
936 return handlerState.Result;
939 template <
typename TStorage>
940 static std::string Format(TStorage& storage,
const typename TStorage::LinkType& link)
942 constexpr auto constants = std::remove_cvref_t<
decltype(storage)>::Constants;
945 .append(std::to_string(GetIndex(storage, link)))
948 .append(std::to_string(GetSource(storage, link)))
952 .append(std::to_string(GetTarget(storage, link)))
956 template <
typename TStorage>
957 std::string Format(TStorage& storage,
typename TStorage::LinkAddressType linkAddress)
959 return Format(GetLink(storage, linkAddress));
962 template <
typename TStorage>
963 typename TStorage::LinkAddressType HasUsages(TStorage& storage,
typename TStorage::LinkAddressType linkAddress)
965 return storage.CountUsages(linkAddress) != 0;
968 template <
typename TStorage>
969 static void DeleteByQuery(TStorage& storage,
typename TStorage::LinkType query)
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;
978 for (
auto link : queryResult)
980 Delete(storage, link);