Platform.Data.Doublets C++
Loading...
Searching...
No Matches
LinksSizeBalancedTreeMethodsBase.h
Go to the documentation of this file.
2{
3 template <typename Self, typename TLinksOptions>
5 : public Platform::Collections::Methods::Trees::SizeBalancedTreeMethods<Self,
6 typename TLinksOptions::LinkAddressType>
7 {
8 using LinksOptionsType = TLinksOptions;
9 using LinkAddressType = typename LinksOptionsType::LinkAddressType;
10 using LinkType = typename LinksOptionsType::LinkType;
11 using ReadHandlerType = typename LinksOptionsType::ReadHandlerType;
12 static constexpr LinksConstants<LinkAddressType> Constants = LinksOptionsType::Constants;
13
14 public:
15 using methods = Platform::Collections::Methods::Trees::SizeBalancedTreeMethods<Self, LinkAddressType>;
16
17 public:
18 std::byte* const Links;
19
20 public:
21 std::byte* const Header;
22
23 public:
24 LinksSizeBalancedTreeMethodsBase(std::byte* storage, std::byte* header) : Links(storage), Header(header) {}
25
26 public:
28 {
29 return *reinterpret_cast<LinksHeader<LinkAddressType>*>(Header);
30 }
31
32 public:
34 {
35 return reinterpret_cast<RawLink<LinkAddressType>*>(Links)[linkAddress];
36 }
37
38 public:
40 {
41 auto& link = GetLinkReference(linkIndex);
42 return LinkType{linkIndex, link.Source, link.Target};
43 }
44
45 public:
47 {
48 auto& firstLink = this->GetLinkReference(first);
49 auto& secondLink = this->GetLinkReference(second);
50 return this->object().FirstIsToTheLeftOfSecond(firstLink.Source, firstLink.Target, secondLink.Source,
51 secondLink.Target);
52 }
53
54 public:
56 {
57 auto& firstLink = this->GetLinkReference(first);
58 auto& secondLink = this->GetLinkReference(second);
59 return this->object().FirstIsToTheRightOfSecond(firstLink.Source, firstLink.Target, secondLink.Source,
60 secondLink.Target);
61 }
62
63 auto operator[](std::size_t index)
64 {
65 auto root = this->object().GetTreeRoot();
66 if (index >= this->object().GetSize(root))
67 {
68 return 0;
69 }
70 while (root != 0)
71 {
72 auto left = GetLeftOrDefault(root);
73 auto leftSize = GetSizeOrZero(left);
74 if (index < leftSize)
75 {
76 root = left;
77 continue;
78 }
79 if (index == leftSize)
80 {
81 return root;
82 }
83 root = GetRightOrDefault(root);
84 index = index - (leftSize + 1);
85 }
86 return 0;
87 }
88
89 public:
91 {
92 auto root = this->object().GetTreeRoot();
93 while (root != 0)
94 {
95 auto& rootLink = this->GetLinkReference(root);
96 auto rootSource = rootLink.Source;
97 auto rootTarget = rootLink.Target;
98 if (this->object().FirstIsToTheLeftOfSecond(source, target, rootSource, rootTarget))
99 {
100 root = this->GetLeftOrDefault(root);
101 }
102 else if (this->object().FirstIsToTheRightOfSecond(source, target, rootSource, rootTarget))
103 {
104 root = this->GetRightOrDefault(root);
105 }
106 else
107 {
108 return root;
109 }
110 }
111 return 0;
112 }
113
114 public:
116 {
117 auto root = this->object().GetTreeRoot();
118 auto total = this->object().GetSize(root);
119 auto totalRightIgnore = 0;
120 while (root != 0)
121 {
122 auto base = this->object().GetBasePartValue(root);
123 if (base <= linkAddress)
124 {
125 root = this->GetRightOrDefault(root);
126 }
127 else
128 {
129 totalRightIgnore = totalRightIgnore + (this->GetRightSize(root) + 1);
130 root = this->GetLeftOrDefault(root);
131 }
132 }
133 root = this->object().GetTreeRoot();
134 auto totalLeftIgnore = 0;
135 while (root != 0)
136 {
137 auto base = this->object().GetBasePartValue(root);
138 if (base >= linkAddress)
139 {
140 root = this->GetLeftOrDefault(root);
141 }
142 else
143 {
144 totalLeftIgnore = totalLeftIgnore + (this->GetLeftSize(root) + 1);
145 root = this->GetRightOrDefault(root);
146 }
147 }
148 return total - totalRightIgnore - totalLeftIgnore;
149 }
150
151 public:
153 {
154 return this->EachUsageCore(base, this->object().GetTreeRoot(), handler);
155 }
156
157 private:
158 LinkAddressType EachUsageCore(LinkAddressType base, LinkAddressType linkAddress, const ReadHandlerType& handler)
159 {
160 auto $continue = Constants.Continue;
161 if (linkAddress == 0)
162 {
163 return $continue;
164 }
165 auto linkBasePart = this->object().GetBasePartValue(linkAddress);
166 auto $break = Constants.Break;
167 if (linkBasePart > (base))
168 {
169 if ((this->EachUsageCore(base, this->GetLeftOrDefault(linkAddress), handler) == $break))
170 {
171 return $break;
172 }
173 }
174 else if (linkBasePart < base)
175 {
176 if ((this->EachUsageCore(base, this->GetRightOrDefault(linkAddress), handler) == $break))
177 {
178 return $break;
179 }
180 }
181 else
182 {
183 auto link = this->GetLinkValues(linkAddress);
184 if (handler(link) == ($break))
185 {
186 return $break;
187 }
188 if ((this->EachUsageCore(base, this->GetLeftOrDefault(linkAddress), handler) == $break))
189 {
190 return $break;
191 }
192 if ((this->EachUsageCore(base, this->GetRightOrDefault(linkAddress), handler) == $break))
193 {
194 return $break;
195 }
196 }
197 return $continue;
198 }
199 };
200} // namespace Platform::Data::Doublets::Memory::United::Generic
Definition LinksAvlBalancedTreeMethodsBase.h:2
auto operator[](std::size_t index)
Definition LinksSizeBalancedTreeMethodsBase.h:63
bool FirstIsToTheRightOfSecond(LinkAddressType first, LinkAddressType second)
Definition LinksSizeBalancedTreeMethodsBase.h:55
bool FirstIsToTheLeftOfSecond(LinkAddressType first, LinkAddressType second)
Definition LinksSizeBalancedTreeMethodsBase.h:46
LinkType GetLinkValues(LinkAddressType linkIndex)
Definition LinksSizeBalancedTreeMethodsBase.h:39
typename LinksOptionsType::LinkAddressType LinkAddressType
Definition LinksSizeBalancedTreeMethodsBase.h:9
Platform::Collections::Methods::Trees::SizeBalancedTreeMethods< Self, LinkAddressType > methods
Definition LinksSizeBalancedTreeMethodsBase.h:15
LinkAddressType EachUsage(LinkAddressType base, const ReadHandlerType &handler)
Definition LinksSizeBalancedTreeMethodsBase.h:152
TLinksOptions LinksOptionsType
Definition LinksSizeBalancedTreeMethodsBase.h:8
LinksSizeBalancedTreeMethodsBase(std::byte *storage, std::byte *header)
Definition LinksSizeBalancedTreeMethodsBase.h:24
typename LinksOptionsType::ReadHandlerType ReadHandlerType
Definition LinksSizeBalancedTreeMethodsBase.h:11
LinkAddressType CountUsages(LinkAddressType linkAddress)
Definition LinksSizeBalancedTreeMethodsBase.h:115
static constexpr LinksConstants< LinkAddressType > Constants
Definition LinksSizeBalancedTreeMethodsBase.h:12
auto & GetLinkReference(LinkAddressType linkAddress)
Definition LinksSizeBalancedTreeMethodsBase.h:33
std::byte *const Links
Definition LinksSizeBalancedTreeMethodsBase.h:18
typename LinksOptionsType::LinkType LinkType
Definition LinksSizeBalancedTreeMethodsBase.h:10
LinkAddressType Search(LinkAddressType source, LinkAddressType target)
Definition LinksSizeBalancedTreeMethodsBase.h:90
auto & GetHeaderReference()
Definition LinksSizeBalancedTreeMethodsBase.h:27
std::byte *const Header
Definition LinksSizeBalancedTreeMethodsBase.h:21