4 template <
typename TSelf,
typename TLinkOptions,
typename TMemory,
typename TSourceTreeMethods,
5 typename TTargetTreeMethods,
typename TUnusedLinks,
typename... TBase>
11 using LinkType =
typename LinksOptionsType::LinkType;
14 static constexpr LinksConstants<LinkAddressType>
Constants = LinksOptionsType::Constants;
32 const std::size_t _memoryReservationStep;
34 std::unique_ptr<TTargetTreeMethods> _TargetsTreeMethods;
36 std::unique_ptr<TSourceTreeMethods> _SourcesTreeMethods;
38 std::unique_ptr<TUnusedLinks> _UnusedLinksListMethods;
57 return reinterpret_cast<RawLink<LinkAddressType>*
>(_links)[linkIndex];
62 auto& header = this->GetHeaderReference();
63 return header.AllocatedLinks - header.FreeLinks;
71 :
_memory(
std::move(memory)), _memoryReservationStep(memoryReservationStep)
76 void Init(TMemory& memory, std::size_t memoryReservationStep)
79 if (memory.ReservedCapacity() < memoryReservationStep)
81 memory.ReservedCapacity(memoryReservationStep);
85 auto& header = this->GetHeaderReference();
95 if (std::ranges::size(restriction) == 0)
100 auto index = restriction[
Constants.IndexPart];
101 if (std::ranges::size(restriction) == 1)
109 if (std::ranges::size(restriction) == 2)
112 auto value = restriction[1];
122 Each(restriction, [&](
const auto&) {
128 count = _SourcesTreeMethods->CountUsages(value);
130 _TargetsTreeMethods->EachUsage(value, [&](
const auto& link) {
147 auto& storedLinkValue = GetLinkReference(index);
148 if (storedLinkValue.Source == value || storedLinkValue.Target == value)
155 if (std::ranges::size(restriction) == 3)
158 auto source = restriction[
Constants.SourcePart];
159 auto target = restriction[
Constants.TargetPart];
162 if (source == any && target == any)
166 else if (source == any)
168 return _TargetsTreeMethods->CountUsages(target);
170 else if (target == any)
172 return _SourcesTreeMethods->CountUsages(source);
176 auto link = _SourcesTreeMethods->Search(source, target);
187 if (source == any && target == any)
191 auto& storedLinkValue = GetLinkReference(index);
193 if ((source == any || storedLinkValue.Source == source) &&
194 (target == any || storedLinkValue.Target == target))
201 throw std::logic_error(
"Not supported exception.");
209 if (std::ranges::size(restriction) == 0)
211 for (
auto link =
LinkAddressType{1}; link <= this->GetHeaderReference().AllocatedLinks; ++link)
222 auto index = restriction[
Constants.IndexPart];
223 if (std::ranges::size(restriction) == 1)
227 return Data::Each(*
this, handler);
235 if (std::ranges::size(restriction) == 2)
237 auto value = restriction[1];
242 return Data::Each(*
this, handler);
253 if (
Each(
LinkType{index, value, any}, handler) == $break)
257 return Each(
LinkType{index, any, value}, [&](
const auto& link) {
258 return link[
Constants.SourcePart] == value ? _continue : handler(link);
271 auto& storedLinkValue = GetLinkReference(index);
272 if ((storedLinkValue.Source == value) || (storedLinkValue.Target == value))
279 if (std::ranges::size(restriction) == 3)
281 auto source = restriction[
Constants.SourcePart];
282 auto target = restriction[
Constants.TargetPart];
285 if (source == any && target == any)
287 return Data::Each(*
this, handler);
289 else if (source == any)
291 return _TargetsTreeMethods->EachUsage(target, handler);
293 else if (target == any)
295 return _SourcesTreeMethods->EachUsage(source, handler);
299 auto link = _SourcesTreeMethods->Search(source, target);
309 if (source == any && target == any)
315 auto& storedLinkValue = GetLinkReference(index);
317 if ((source == any || storedLinkValue.Source == source) &&
318 (target == any || storedLinkValue.Target == target))
325 throw std::logic_error(
"Not supported exception.");
338 auto linkIndex = restriction[
Constants.IndexPart];
341 auto& link = GetLinkReference(linkIndex);
342 LinkType before{linkIndex, link.Source, link.Target};
345 auto& header = this->GetHeaderReference();
348 auto& firstAsSource = header.RootAsSource;
351 auto& firstAsTarget = header.RootAsTarget;
354 if (link.Source != null)
356 _SourcesTreeMethods->Detach(firstAsSource, linkIndex);
358 if (link.Target != null)
360 _TargetsTreeMethods->Detach(firstAsTarget, linkIndex);
362 link.Source = substitution[
Constants.SourcePart];
363 link.Target = substitution[
Constants.TargetPart];
364 if (link.Source != null)
366 _SourcesTreeMethods->Attach(firstAsSource, linkIndex);
368 if (link.Target != null)
370 _TargetsTreeMethods->Attach(firstAsTarget, linkIndex);
372 return handler(before,
LinkType{linkIndex, link.Source, link.Target});
379 auto* header = &this->GetHeaderReference();
380 auto freeLink = header->FirstFreeLink;
383 _UnusedLinksListMethods->Detach(freeLink);
387 auto maximumPossibleInnerReference =
Constants.InternalReferencesRange.Maximum;
388 if (header->AllocatedLinks >= maximumPossibleInnerReference)
390 throw Platform::Exceptions::LinksLimitReachedException();
392 if (header->AllocatedLinks >= (header->ReservedLinks - 1))
394 _memory.ReservedCapacity(
_memory.ReservedCapacity() + _memoryReservationStep);
396 header = &this->GetHeaderReference();
399 ++header->AllocatedLinks;
400 freeLink = header->AllocatedLinks;
409 auto& header = this->GetHeaderReference();
410 auto linkAddress = restriction[
Constants.IndexPart];
412 if (linkAddress < header.AllocatedLinks)
414 _UnusedLinksListMethods->AttachAsFirst(linkAddress);
417 else if (linkAddress == header.AllocatedLinks)
419 --header.AllocatedLinks;
423 _UnusedLinksListMethods->Detach(header.AllocatedLinks);
424 --header.AllocatedLinks;
434 auto& link = this->GetLinkReference(linkIndex);
435 return LinkType{linkIndex, link.Source, link.Target};
446 _links =
static_cast<std::byte*
>(memory.Pointer());
448 _SourcesTreeMethods = std::make_unique<TSourceTreeMethods>(_links, _header);
449 _TargetsTreeMethods = std::make_unique<TTargetTreeMethods>(_links, _header);
450 _UnusedLinksListMethods = std::make_unique<TUnusedLinks>(_links, _header);
455 if (IsExternalReference<LinkAddressType, Constants>(linkAddress))
459 return (linkAddress >=
Constants.InternalReferencesRange.Minimum) &&
460 (linkAddress <= this->GetHeaderReference().AllocatedLinks) && !
IsUnusedLink(linkAddress);
465 if (GetHeaderReference().FirstFreeLink != linkIndex)
467 auto& link = GetLinkReference(linkIndex);
Definition RawLinkDataPart.h:38