LibSWOC++ 1.5.14
Solid Wall of C++
Loading...
Searching...
No Matches
RBTree.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright Apache Software Foundation 2019
7
8#pragma once
9
10#include "swoc/swoc_version.h"
11#include "swoc/IntrusiveDList.h"
12
13#include <string>
14
15namespace swoc { inline namespace SWOC_VERSION_NS { namespace detail {
26struct RBNode {
27 using self_type = RBNode;
28
30 enum class Color {
31 RED,
32 BLACK,
33 };
34
36 enum class Direction {
37 NONE,
38 LEFT,
39 RIGHT,
40 };
41
43 RBNode() = default;
44
46 virtual ~RBNode() {}
47
53 self_type *child_at(Direction d) const;
54
60 Direction direction_of(self_type *const &n) const;
61
63 Color color() const;
64
66 self_type *left_most_descendant() const;
67
72 Direction flip(Direction d);
73
77 int validate();
78
90 self_type *rotate(Direction dir);
91
101 self_type *set_child(self_type *child, Direction dir);
102
107 self_type *remove();
108
116 void clear_child(Direction dir);
117
120
131 virtual void
133
138 virtual bool structure_validate();
140
147 void replace_with(self_type *n);
148
150
155 self_type *rebalance_after_insert();
156
169 self_type *rebalance_after_remove(Color c, Direction dir);
170
178 self_type *ripple_structure_fixup();
179
191 static self_type * buildTree(self_type*& head, int n);
192
194 static self_type * buildTree(self_type*& head, int n, bool isBlack);
195
203 static void printTree(self_type* root, std::string indent = "", bool last = true);
204
205 Color _color{Color::RED};
206 self_type *_parent{nullptr};
207 self_type *_left{nullptr};
208 self_type *_right{nullptr};
209 self_type *_next{nullptr};
210 self_type *_prev{nullptr};
211
213 struct Linkage {
215 static self_type *&
217 return swoc::ptr_ref_cast<self_type>(t->_next);
218 }
219
220 static self_type *&
222 return swoc::ptr_ref_cast<self_type>(t->_prev);
223 }
224 };
225};
226
227// --- Implementation ---
228
229inline auto
231 return (n == _left) ? Direction::LEFT : (n == _right) ? Direction::RIGHT : Direction::NONE;
232}
233
234inline RBNode::Color
236 return _color;
237}
238
241 return Direction::LEFT == d ? Direction::RIGHT : Direction::RIGHT == d ? Direction::LEFT : Direction::NONE;
242}
243
244inline void
246 if (Direction::LEFT == dir)
247 _left = nullptr;
248 else if (Direction::RIGHT == dir)
249 _right = nullptr;
250}
251
252inline bool
254 return true;
255}
256
257}}} // namespace swoc::SWOC_VERSION_NS::detail
For template deduction guides.
Definition ArenaWriter.cc:9
Support for IntrusiveDList.
Definition RBTree.h:213
static self_type *& next_ptr(self_type *t)
Definition RBTree.h:216
static self_type *& prev_ptr(self_type *t)
Definition RBTree.h:221
void clear_child(Direction dir)
Definition RBTree.h:245
virtual void structure_fixup()
Definition RBTree.h:132
RBNode()=default
Default constructor.
Direction direction_of(self_type *const &n) const
Definition RBTree.h:230
self_type * _prev
Previous node.
Definition RBTree.h:210
Color _color
node color
Definition RBTree.h:205
Direction
Directional constants.
Definition RBTree.h:36
RBNode self_type
self reference type
Definition RBTree.h:27
Direction flip(Direction d)
Definition RBTree.h:240
Color
Node colors.
Definition RBTree.h:30
Color color() const
Definition RBTree.h:235
virtual bool structure_validate()
Definition RBTree.h:253
self_type * _right
right child
Definition RBTree.h:208
self_type * _parent
parent node (needed for rotations)
Definition RBTree.h:206
virtual ~RBNode()
Force virtual destructor.
Definition RBTree.h:46
self_type * _left
left child
Definition RBTree.h:207
self_type * _next
Next node.
Definition RBTree.h:209