Debian Codegraph @

src/core/lib/avl/avl.h

raw
1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef GRPC_CORE_LIB_AVL_AVL_H
16 #define GRPC_CORE_LIB_AVL_AVL_H
17
18 #include <grpc/support/port_platform.h>
19
20 #include <stdlib.h>
21
22 #include <algorithm> // IWYU pragma: keep
23 #include <memory>
24 #include <utility>
25
26 #include "src/core/lib/gpr/useful.h"
27
28 namespace grpc_core {
29
30 template <class K, class V = void>
31 class AVL {
32 public:
33 AVL() {}
34
35 AVL Add(K key, V value) const {
36 return AVL(AddKey(root_, std::move(key), std::move(value)));
37 }
38 template <typename SomethingLikeK>
39 AVL Remove(const SomethingLikeK& key) const {
40 return AVL(RemoveKey(root_, key));
41 }
42 template <typename SomethingLikeK>
43 const V* Lookup(const SomethingLikeK& key) const {
44 NodePtr n = Get(root_, key);
45 return n ? &n->kv.second : nullptr;
46 }
47
48 const std::pair<K, V>* LookupBelow(const K& key) const {
49 NodePtr n = GetBelow(root_, *key);
50 return n ? &n->kv : nullptr;
51 }
52
53 bool Empty() const { return root_ == nullptr; }
54
55 template <class F>
56 void ForEach(F&& f) const {
57 ForEachImpl(root_.get(), std::forward<F>(f));
58 }
59
60 bool SameIdentity(const AVL& avl) const { return root_ == avl.root_; }
61
62 friend int QsortCompare(const AVL& left, const AVL& right) {
63 if (left.root_.get() == right.root_.get()) return 0;
64 Iterator a(left.root_);
65 Iterator b(right.root_);
66 for (;;) {
67 Node* p = a.current();
68 Node* q = b.current();
69 if (p != q) {
70 if (p == nullptr) return -1;
71 if (q == nullptr) return 1;
72 const int kv = QsortCompare(p->kv, q->kv);
73 if (kv != 0) return kv;
74 } else if (p == nullptr) {
75 return 0;
76 }
77 a.MoveNext();
78 b.MoveNext();
79 }
80 }
81
82 bool operator==(const AVL& other) const {
83 return QsortCompare(*this, other) == 0;
84 }
85
86 bool operator<(const AVL& other) const {
87 return QsortCompare(*this, other) < 0;
88 }
89
90 private:
91 struct Node;
92
93 typedef std::shared_ptr<Node> NodePtr;
94 struct Node : public std::enable_shared_from_this<Node> {
95 Node(K k, V v, NodePtr l, NodePtr r, long h)
96 : kv(std::move(k), std::move(v)),
97 left(std::move(l)),
98 right(std::move(r)),
99 height(h) {}
100 const std::pair<K, V> kv;
101 const NodePtr left;
102 const NodePtr right;
103 const long height;
104 };
105 NodePtr root_;
106
107 class IteratorStack {
108 public:
109 void Push(Node* n) {
110 nodes_[depth_] = n;
111 ++depth_;
112 }
113
114 Node* Pop() {
115 --depth_;
116 return nodes_[depth_];
117 }
118
119 Node* Back() const { return nodes_[depth_ - 1]; }
120
121 bool Empty() const { return depth_ == 0; }
122
123 private:
124 size_t depth_{0};
125 // 32 is the maximum depth we can accept, and corresponds to ~4billion nodes
126 // - which ought to suffice our use cases.
127 Node* nodes_[32];
128 };
129
130 class Iterator {
131 public:
132 explicit Iterator(const NodePtr& root) {
133 auto* n = root.get();
134 while (n != nullptr) {
135 stack_.Push(n);
136 n = n->left.get();
137 }
138 }
139 Node* current() const { return stack_.Empty() ? nullptr : stack_.Back(); }
140 void MoveNext() {
141 auto* n = stack_.Pop();
142 if (n->right != nullptr) {
143 n = n->right.get();
144 while (n != nullptr) {
145 stack_.Push(n);
146 n = n->left.get();
147 }
148 }
149 }
150
151 private:
152 IteratorStack stack_;
153 };
154
155 explicit AVL(NodePtr root) : root_(std::move(root)) {}
156
157 template <class F>
158 static void ForEachImpl(const Node* n, F&& f) {
159 if (n == nullptr) return;
160 ForEachImpl(n->left.get(), std::forward<F>(f));
161 f(const_cast<const K&>(n->kv.first), const_cast<const V&>(n->kv.second));
162 ForEachImpl(n->right.get(), std::forward<F>(f));
163 }
164
165 static long Height(const NodePtr& n) { return n ? n->height : 0; }
166
167 static NodePtr MakeNode(K key, V value, const NodePtr& left,
168 const NodePtr& right) {
169 return std::make_shared<Node>(std::move(key), std::move(value), left, right,
170 1 + std::max(Height(left), Height(right)));
171 }
172
173 template <typename SomethingLikeK>
174 static NodePtr Get(const NodePtr& node, const SomethingLikeK& key) {
175 if (node == nullptr) {
176 return nullptr;
177 }
178
179 if (node->kv.first > key) {
180 return Get(node->left, key);
181 } else if (node->kv.first < key) {
182 return Get(node->right, key);
183 } else {
184 return node;
185 }
186 }
187
188 static NodePtr GetBelow(const NodePtr& node, const K& key) {
189 if (!node) return nullptr;
190 if (node->kv.first > key) {
191 return GetBelow(node->left, key);
192 } else if (node->kv.first < key) {
193 NodePtr n = GetBelow(node->right, key);
194 if (n == nullptr) n = node;
195 return n;
196 } else {
197 return node;
198 }
199 }
200
201 static NodePtr RotateLeft(K key, V value, const NodePtr& left,
202 const NodePtr& right) {
203 return MakeNode(
204 right->kv.first, right->kv.second,
205 MakeNode(std::move(key), std::move(value), left, right->left),
206 right->right);
207 }
208
209 static NodePtr RotateRight(K key, V value, const NodePtr& left,
210 const NodePtr& right) {
211 return MakeNode(
212 left->kv.first, left->kv.second, left->left,
213 MakeNode(std::move(key), std::move(value), left->right, right));
214 }
215
216 static NodePtr RotateLeftRight(K key, V value, const NodePtr& left,
217 const NodePtr& right) {
218 /* rotate_right(..., rotate_left(left), right) */
219 return MakeNode(
220 left->right->kv.first, left->right->kv.second,
221 MakeNode(left->kv.first, left->kv.second, left->left,
222 left->right->left),
223 MakeNode(std::move(key), std::move(value), left->right->right, right));
224 }
225
226 static NodePtr RotateRightLeft(K key, V value, const NodePtr& left,
227 const NodePtr& right) {
228 /* rotate_left(..., left, rotate_right(right)) */
229 return MakeNode(
230 right->left->kv.first, right->left->kv.second,
231 MakeNode(std::move(key), std::move(value), left, right->left->left),
232 MakeNode(right->kv.first, right->kv.second, right->left->right,
233 right->right));
234 }
235
236 static NodePtr Rebalance(K key, V value, const NodePtr& left,
237 const NodePtr& right) {
238 switch (Height(left) - Height(right)) {
239 case 2:
240 if (Height(left->left) - Height(left->right) == -1) {
241 return RotateLeftRight(std::move(key), std::move(value), left, right);
242 } else {
243 return RotateRight(std::move(key), std::move(value), left, right);
244 }
245 case -2:
246 if (Height(right->left) - Height(right->right) == 1) {
247 return RotateRightLeft(std::move(key), std::move(value), left, right);
248 } else {
249 return RotateLeft(std::move(key), std::move(value), left, right);
250 }
251 default:
252 return MakeNode(key, value, left, right);
253 }
254 }
255
256 static NodePtr AddKey(const NodePtr& node, K key, V value) {
257 if (!node) {
258 return MakeNode(std::move(key), std::move(value), nullptr, nullptr);
259 }
260 if (node->kv.first < key) {
261 return Rebalance(node->kv.first, node->kv.second, node->left,
262 AddKey(node->right, std::move(key), std::move(value)));
263 }
264 if (key < node->kv.first) {
265 return Rebalance(node->kv.first, node->kv.second,
266 AddKey(node->left, std::move(key), std::move(value)),
267 node->right);
268 }
269 return MakeNode(std::move(key), std::move(value), node->left, node->right);
270 }
271
272 static NodePtr InOrderHead(NodePtr node) {
273 while (node->left != nullptr) {
274 node = node->left;
275 }
276 return node;
277 }
278
279 static NodePtr InOrderTail(NodePtr node) {
280 while (node->right != nullptr) {
281 node = node->right;
282 }
283 return node;
284 }
285
286 template <typename SomethingLikeK>
287 static NodePtr RemoveKey(const NodePtr& node, const SomethingLikeK& key) {
288 if (node == nullptr) {
289 return nullptr;
290 }
291 if (key < node->kv.first) {
292 return Rebalance(node->kv.first, node->kv.second,
293 RemoveKey(node->left, key), node->right);
294 } else if (node->kv.first < key) {
295 return Rebalance(node->kv.first, node->kv.second, node->left,
296 RemoveKey(node->right, key));
297 } else {
298 if (node->left == nullptr) {
299 return node->right;
300 } else if (node->right == nullptr) {
301 return node->left;
302 } else if (node->left->height < node->right->height) {
303 NodePtr h = InOrderHead(node->right);
304 return Rebalance(h->kv.first, h->kv.second, node->left,
305 RemoveKey(node->right, h->kv.first));
306 } else {
307 NodePtr h = InOrderTail(node->left);
308 return Rebalance(h->kv.first, h->kv.second,
309 RemoveKey(node->left, h->kv.first), node->right);
310 }
311 }
312 abort();
313 }
314 };
315
316 template <class K>
317 class AVL<K, void> {
318 public:
319 AVL() {}
320
321 AVL Add(K key) const { return AVL(AddKey(root_, std::move(key))); }
322 AVL Remove(const K& key) const { return AVL(RemoveKey(root_, key)); }
323 bool Lookup(const K& key) const { return Get(root_, key) != nullptr; }
324 bool Empty() const { return root_ == nullptr; }
325
326 template <class F>
327 void ForEach(F&& f) const {
328 ForEachImpl(root_.get(), std::forward<F>(f));
329 }
330
331 bool SameIdentity(AVL avl) const { return root_ == avl.root_; }
332
333 private:
334 struct Node;
335
336 typedef std::shared_ptr<Node> NodePtr;
337 struct Node : public std::enable_shared_from_this<Node> {
338 Node(K k, NodePtr l, NodePtr r, long h)
339 : key(std::move(k)),
340 left(std::move(l)),
341 right(std::move(r)),
342 height(h) {}
343 const K key;
344 const NodePtr left;
345 const NodePtr right;
346 const long height;
347 };
348 NodePtr root_;
349
350 explicit AVL(NodePtr root) : root_(std::move(root)) {}
351
352 template <class F>
353 static void ForEachImpl(const Node* n, F&& f) {
354 if (n == nullptr) return;
355 ForEachImpl(n->left.get(), std::forward<F>(f));
356 f(const_cast<const K&>(n->key));
357 ForEachImpl(n->right.get(), std::forward<F>(f));
358 }
359
360 static long Height(const NodePtr& n) { return n ? n->height : 0; }
361
362 static NodePtr MakeNode(K key, const NodePtr& left, const NodePtr& right) {
363 return std::make_shared<Node>(std::move(key), left, right,
364 1 + std::max(Height(left), Height(right)));
365 }
366
367 static NodePtr Get(const NodePtr& node, const K& key) {
368 if (node == nullptr) {
369 return nullptr;
370 }
371
372 if (node->key > key) {
373 return Get(node->left, key);
374 } else if (node->key < key) {
375 return Get(node->right, key);
376 } else {
377 return node;
378 }
379 }
380
381 static NodePtr RotateLeft(K key, const NodePtr& left, const NodePtr& right) {
382 return MakeNode(right->key, MakeNode(std::move(key), left, right->left),
383 right->right);
384 }
385
386 static NodePtr RotateRight(K key, const NodePtr& left, const NodePtr& right) {
387 return MakeNode(left->key, left->left,
388 MakeNode(std::move(key), left->right, right));
389 }
390
391 static NodePtr RotateLeftRight(K key, const NodePtr& left,
392 const NodePtr& right) {
393 /* rotate_right(..., rotate_left(left), right) */
394 return MakeNode(left->right->key,
395 MakeNode(left->key, left->left, left->right->left),
396 MakeNode(std::move(key), left->right->right, right));
397 }
398
399 static NodePtr RotateRightLeft(K key, const NodePtr& left,
400 const NodePtr& right) {
401 /* rotate_left(..., left, rotate_right(right)) */
402 return MakeNode(right->left->key,
403 MakeNode(std::move(key), left, right->left->left),
404 MakeNode(right->key, right->left->right, right->right));
405 }
406
407 static NodePtr Rebalance(K key, const NodePtr& left, const NodePtr& right) {
408 switch (Height(left) - Height(right)) {
409 case 2:
410 if (Height(left->left) - Height(left->right) == -1) {
411 return RotateLeftRight(std::move(key), left, right);
412 } else {
413 return RotateRight(std::move(key), left, right);
414 }
415 case -2:
416 if (Height(right->left) - Height(right->right) == 1) {
417 return RotateRightLeft(std::move(key), left, right);
418 } else {
419 return RotateLeft(std::move(key), left, right);
420 }
421 default:
422 return MakeNode(key, left, right);
423 }
424 }
425
426 static NodePtr AddKey(const NodePtr& node, K key) {
427 if (!node) {
428 return MakeNode(std::move(key), nullptr, nullptr);
429 }
430 if (node->key < key) {
431 return Rebalance(node->key, node->left,
432 AddKey(node->right, std::move(key)));
433 }
434 if (key < node->key) {
435 return Rebalance(node->key, AddKey(node->left, std::move(key)),
436 node->right);
437 }
438 return MakeNode(std::move(key), node->left, node->right);
439 }
440
441 static NodePtr InOrderHead(NodePtr node) {
442 while (node->left != nullptr) {
443 node = node->left;
444 }
445 return node;
446 }
447
448 static NodePtr InOrderTail(NodePtr node) {
449 while (node->right != nullptr) {
450 node = node->right;
451 }
452 return node;
453 }
454
455 static NodePtr RemoveKey(const NodePtr& node, const K& key) {
456 if (node == nullptr) {
457 return nullptr;
458 }
459 if (key < node->key) {
460 return Rebalance(node->key, RemoveKey(node->left, key), node->right);
461 } else if (node->key < key) {
462 return Rebalance(node->key, node->left, RemoveKey(node->right, key));
463 } else {
464 if (node->left == nullptr) {
465 return node->right;
466 } else if (node->right == nullptr) {
467 return node->left;
468 } else if (node->left->height < node->right->height) {
469 NodePtr h = InOrderHead(node->right);
470 return Rebalance(h->key, node->left, RemoveKey(node->right, h->key));
471 } else {
472 NodePtr h = InOrderTail(node->left);
473 return Rebalance(h->key, RemoveKey(node->left, h->key), node->right);
474 }
475 }
476 abort();
477 }
478 };
479
480 } // namespace grpc_core
481
482 #endif // GRPC_CORE_LIB_AVL_AVL_H