Platform.Data.Doublets C++
Loading...
Searching...
No Matches
LinksAvlBalancedTreeMethodsBase.h
Go to the documentation of this file.
2{
3 template <typename Self, typename TLinksOptions>
5 : public Platform::Collections::Methods::Trees::SizedAndThreadedAVLBalancedTreeMethods<
6 Self, typename TLinksOptions::LinkAddressType> /*, ILinksTreeMethods<TLinksOptions>*/
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 static constexpr auto $break = Constants.Break;
16
17 public:
18 static constexpr auto $continue = Constants.Continue;
19
20 public:
21 using methods =
22 Platform::Collections::Methods::Trees::SizedAndThreadedAVLBalancedTreeMethods<Self, LinkAddressType>;
23
24 public:
25 std::byte* const Links;
26
27 public:
28 std::byte* const Header;
29
30 public:
31 LinksAvlBalancedTreeMethodsBase(std::byte* storage, std::byte* header) : Links(storage), Header(header) {}
32
33 public:
34 auto operator[](std::size_t index)
35 {
36 auto root = this->object().GetTreeRoot();
37 if (index >= GetSize(root))
38 {
39 return 0;
40 }
41 while (!EqualToZero(root))
42 {
43 auto left = GetLeftOrDefault(root);
44 auto leftSize = GetSizeOrZero(left);
45 if (index < leftSize)
46 {
47 root = left;
48 continue;
49 }
50 if (AreEqual(index, leftSize))
51 {
52 return root;
53 }
54 root = GetRightOrDefault(root);
55 index = index - (leftSize + 1);
56 }
57 return 0;
58 }
59
60 public:
62 {
63 auto root = this->object().GetTreeRoot();
64 while (root != 0)
65 {
66 auto& rootLink = this->GetLinkReference(root);
67 auto rootSource = rootLink.Source;
68 auto rootTarget = rootLink.Target;
69 if (this->object().FirstIsToTheLeftOfSecond(source, target, rootSource, rootTarget))
70 {
71 root = this->GetLeftOrDefault(root);
72 }
73 else if (this->object().FirstIsToTheRightOfSecond(source, target, rootSource, rootTarget))
74 {
75 root = this->GetRightOrDefault(root);
76 }
77 else
78 {
79 return root;
80 }
81 }
82 return 0;
83 }
84
85 public:
87 {
88 auto root = this->object().GetTreeRoot();
89 auto total = this->object().GetSize(root);
90 auto totalRightIgnore = 0;
91 while (root != 0)
92 {
93 auto base = this->object().GetBasePartValue(root);
94 if (base <= link)
95 {
96 root = this->GetRightOrDefault(root);
97 }
98 else
99 {
100 totalRightIgnore = totalRightIgnore + (this->GetRightSize(root) + 1);
101 root = this->GetLeftOrDefault(root);
102 }
103 }
104 root = this->object().GetTreeRoot();
105 auto totalLeftIgnore = 0;
106 while (root != 0)
107 {
108 auto base = this->object().GetBasePartValue(root);
109 if (base >= link)
110 {
111 root = this->GetLeftOrDefault(root);
112 }
113 else
114 {
115 totalLeftIgnore = totalLeftIgnore + (this->GetLeftSize(root) + 1);
116
117 root = this->GetRightOrDefault(root);
118 }
119 }
120 return (total - totalRightIgnore) - totalLeftIgnore;
121 }
122
123 public:
125 {
126 auto root = this->object().GetTreeRoot();
127 if (root == 0)
128 {
129 return $continue;
130 }
131 LinkAddressType first = 0, current = root;
132 while (current != 0)
133 {
134 auto base = this->object().GetBasePartValue(current);
135 if (base >= link)
136 {
137 if ((base) == link)
138 {
139 first = current;
140 }
141 current = this->GetLeftOrDefault(current);
142 }
143 else
144 {
145 current = this->GetRightOrDefault(current);
146 }
147 }
148 if (first != 0)
149 {
150 current = first;
151 while (true)
152 {
153 if (handler(this->GetLinkValues(current)) == $break)
154 {
155 return $break;
156 }
157 current = this->GetNext(current);
158 if (current == 0 || this->object().GetBasePartValue(current) != link)
159 {
160 break;
161 }
162 }
163 }
164 return $continue;
165 }
166
167 public:
168 auto& GetHeaderReference() const
169 {
170 return *reinterpret_cast<LinksHeader<LinkAddressType>*>(Header);
171 }
172
173 public:
175 {
176 return reinterpret_cast<RawLink<LinkAddressType>*>(Links)[linkAddress];
177 }
178
179 public:
181 {
182 auto& link = GetLinkReference(linkIndex);
183 return LinkType{linkIndex, link.Source, link.Target};
184 }
185
186 public:
188 {
189 auto& firstLink = this->GetLinkReference(first);
190 auto& secondLink = this->GetLinkReference(second);
191 return this->object().FirstIsToTheLeftOfSecond(firstLink.Source, firstLink.Target, secondLink.Source,
192 secondLink.Target);
193 }
194
195 public:
197 {
198 auto& firstLink = this->GetLinkReference(first);
199 auto& secondLink = this->GetLinkReference(second);
200 return this->object().FirstIsToTheRightOfSecond(firstLink.Source, firstLink.Target, secondLink.Source,
201 secondLink.Target);
202 }
203
204 public:
206 {
207 return Platform::Numbers::Bit::PartialRead<LinkAddressType>(value, 5, -5);
208 }
209
210 public:
212 {
213 *storedValue = Platform::Numbers::Bit::PartialWrite<LinkAddressType>(*storedValue, size, 5, -5);
214 }
215
216 public:
218 {
219 {
220 return (bool)Platform::Numbers::Bit::PartialRead<LinkAddressType>(value, 4, 1);
221 }
222 }
223
224 public:
225 void SetLeftIsChildValue(LinkAddressType* storedValue, bool value)
226 {
227 {
228 auto previousValue = *storedValue;
229 auto modified =
230 Platform::Numbers::Bit::PartialWrite<LinkAddressType>(previousValue, (LinkAddressType)value, 4, 1);
231 *storedValue = modified;
232 }
233 }
234
235 public:
237 {
238 {
239 return (bool)Platform::Numbers::Bit::PartialRead<LinkAddressType>(value, 3, 1);
240 }
241 }
242
243 public:
244 void SetRightIsChildValue(LinkAddressType* storedValue, bool value)
245 {
246 {
247 auto previousValue = *storedValue;
248 auto modified =
249 Platform::Numbers::Bit::PartialWrite<LinkAddressType>(previousValue, (LinkAddressType)value, 3, 1);
250 *storedValue = modified;
251 }
252 }
253
254 public:
255 bool IsChild(LinkAddressType parent, LinkAddressType possibleChild)
256 {
257 auto parentSize = this->GetSize(parent);
258 auto childSize = this->GetSizeOrZero(possibleChild);
259 return childSize > 0 && (childSize <= parentSize);
260 }
261
262 public:
263 std::int8_t GetBalanceValue(LinkAddressType storedValue)
264 {
265 {
266 auto value = (std::int32_t)Platform::Numbers::Bit::PartialRead<LinkAddressType>(storedValue, 0, 3);
267 value |= 0xF8 * ((value & 4) >> 2);
268 return (std::int8_t)value;
269 }
270 }
271
272 public:
273 void SetBalanceValue(LinkAddressType* storedValue, std::int8_t value)
274 {
275 {
276 auto packagedValue = (LinkAddressType)((((std::uint8_t)value >> 5) & 4) | (value & 3));
277 auto modified =
278 Platform::Numbers::Bit::PartialWrite<LinkAddressType>(*storedValue, packagedValue, 0, 3);
279 *storedValue = modified;
280 }
281 }
282 };
283} // namespace Platform::Data::Doublets::Memory::United::Generic
Definition LinksAvlBalancedTreeMethodsBase.h:2
void SetSizeValue(LinkAddressType *storedValue, LinkAddressType size)
Definition LinksAvlBalancedTreeMethodsBase.h:211
std::byte *const Header
Definition LinksAvlBalancedTreeMethodsBase.h:28
bool IsChild(LinkAddressType parent, LinkAddressType possibleChild)
Definition LinksAvlBalancedTreeMethodsBase.h:255
static constexpr auto $break
Definition LinksAvlBalancedTreeMethodsBase.h:15
typename LinksOptionsType::ReadHandlerType ReadHandlerType
Definition LinksAvlBalancedTreeMethodsBase.h:11
static constexpr LinksConstants< LinkAddressType > Constants
Definition LinksAvlBalancedTreeMethodsBase.h:12
LinkAddressType EachUsage(LinkAddressType link, std::function< LinkAddressType(LinkType)> handler)
Definition LinksAvlBalancedTreeMethodsBase.h:124
typename LinksOptionsType::LinkAddressType LinkAddressType
Definition LinksAvlBalancedTreeMethodsBase.h:9
auto & GetHeaderReference() const
Definition LinksAvlBalancedTreeMethodsBase.h:168
TLinksOptions LinksOptionsType
Definition LinksAvlBalancedTreeMethodsBase.h:8
bool FirstIsToTheRightOfSecond(LinkAddressType first, LinkAddressType second)
Definition LinksAvlBalancedTreeMethodsBase.h:196
LinkAddressType GetSizeValue(LinkAddressType value)
Definition LinksAvlBalancedTreeMethodsBase.h:205
std::byte *const Links
Definition LinksAvlBalancedTreeMethodsBase.h:25
static constexpr auto $continue
Definition LinksAvlBalancedTreeMethodsBase.h:18
void SetBalanceValue(LinkAddressType *storedValue, std::int8_t value)
Definition LinksAvlBalancedTreeMethodsBase.h:273
void SetRightIsChildValue(LinkAddressType *storedValue, bool value)
Definition LinksAvlBalancedTreeMethodsBase.h:244
bool GetLeftIsChildValue(LinkAddressType value)
Definition LinksAvlBalancedTreeMethodsBase.h:217
LinkType GetLinkValues(LinkAddressType linkIndex)
Definition LinksAvlBalancedTreeMethodsBase.h:180
Platform::Collections::Methods::Trees::SizedAndThreadedAVLBalancedTreeMethods< Self, LinkAddressType > methods
Definition LinksAvlBalancedTreeMethodsBase.h:22
void SetLeftIsChildValue(LinkAddressType *storedValue, bool value)
Definition LinksAvlBalancedTreeMethodsBase.h:225
LinkAddressType Search(LinkAddressType source, LinkAddressType target)
Definition LinksAvlBalancedTreeMethodsBase.h:61
LinkAddressType CountUsages(LinkAddressType link)
Definition LinksAvlBalancedTreeMethodsBase.h:86
bool FirstIsToTheLeftOfSecond(LinkAddressType first, LinkAddressType second)
Definition LinksAvlBalancedTreeMethodsBase.h:187
auto operator[](std::size_t index)
Definition LinksAvlBalancedTreeMethodsBase.h:34
std::int8_t GetBalanceValue(LinkAddressType storedValue)
Definition LinksAvlBalancedTreeMethodsBase.h:263
typename LinksOptionsType::LinkType LinkType
Definition LinksAvlBalancedTreeMethodsBase.h:10
LinksAvlBalancedTreeMethodsBase(std::byte *storage, std::byte *header)
Definition LinksAvlBalancedTreeMethodsBase.h:31
auto & GetLinkReference(LinkAddressType linkAddress)
Definition LinksAvlBalancedTreeMethodsBase.h:174
bool GetRightIsChildValue(LinkAddressType value)
Definition LinksAvlBalancedTreeMethodsBase.h:236