6 #include "size_holder.hpp" 7 #include "tree_iterator.hpp" 12 #include <type_traits> 19 namespace rbtree_internal {
22 enum class Color : unsigned char
28 template <
class Node,
bool compress_color>
29 class ColorParentStorage;
32 class ColorParentStorage<Node, true> {
34 void set_color(Color new_color) noexcept;
35 void make_black() noexcept;
36 void make_red() noexcept;
38 Color get_color() const noexcept;
39 void set_parent(Node * new_parent) noexcept;
40 Node * get_parent() const noexcept;
42 void swap_parent_with(ColorParentStorage<Node, true> & other) noexcept;
43 void swap_color_with(ColorParentStorage<Node, true> & other) noexcept;
45 static constexpr
bool parent_reference = false;
52 class ColorParentStorage<Node, false> {
54 void set_color(Color new_color) noexcept;
55 void make_black() noexcept;
56 void make_red() noexcept;
58 Color get_color() const noexcept;
59 void set_parent(Node * new_parent) noexcept;
60 Node *& get_parent() noexcept;
61 Node * get_parent() const noexcept;
63 void swap_parent_with(ColorParentStorage<Node, false> & other) noexcept;
64 void swap_color_with(ColorParentStorage<Node, false> & other) noexcept;
66 static constexpr
bool parent_reference = true;
69 Node * parent =
nullptr;
91 template <class Node, class Options = DefaultOptions, class Tag =
int>
93 : public bst::BSTNodeBase<
95 rbtree_internal::ColorParentStorage<Node, Options::compress_color>> {
99 void set_color(rbtree_internal::Color new_color) noexcept;
100 void make_black() noexcept;
101 void make_red() noexcept;
102 rbtree_internal::Color get_color()
const noexcept;
104 void swap_parent_with(Node * other) noexcept;
105 void swap_color_with(Node * other) noexcept;
108 using ActiveOptions = Options;
109 friend class rbtree_internal::ColorParentStorage<Node,
110 Options::compress_color>;
123 template <
class Node,
class Tree>
125 leaf_inserted(Node & node, Tree & t) noexcept
131 template <
class Node,
class Tree>
133 rotated_left(Node & node, Tree & t) noexcept
139 template <
class Node,
class Tree>
141 rotated_right(Node & node, Tree & t) noexcept
147 template <
class Node,
class Tree>
149 delete_leaf(Node & node, Tree & t) noexcept
155 template <
class Node,
class Tree>
157 deleted_below(Node & node, Tree & t) noexcept
163 template <
class Node,
class Tree>
165 swapped(Node & old_ancestor, Node & old_descendant, Tree & t) noexcept
168 (void)old_descendant;
192 template <
class Node,
class NodeTraits,
class Options = DefaultOptions,
193 class Tag = int,
class Compare = ygg::utilities::flexible_less>
196 Node, Options, Tag, Compare,
197 rbtree_internal::ColorParentStorage<Node, Options::compress_color>>
205 Node, Options, Tag, Compare,
206 rbtree_internal::ColorParentStorage<Node, Options::compress_color>>;
207 static_assert(std::is_base_of<NB, Node>::value,
208 "Node class not properly derived from RBTreeNodeBase");
228 template <
bool reverse>
229 using iterator =
typename TB::template iterator<reverse>;
230 template <
bool reverse>
231 using const_iterator =
typename TB::template const_iterator<reverse>;
247 void insert(Node & node) CMP_NOEXCEPT(node);
248 void insert(Node & node, Node & hint) CMP_NOEXCEPT(node);
249 void insert(Node & node, iterator<false> hint) CMP_NOEXCEPT(node);
261 void remove(Node & node) CMP_NOEXCEPT(node);
284 template <
class Comparable>
285 utilities::select_type_t<size_t, Node *, Options::stl_erase>
286 erase(
const Comparable & c) CMP_NOEXCEPT(c);
302 template <
bool reverse>
303 utilities::select_type_t<const iterator<reverse>, Node *, Options::stl_erase>
304 erase(
const iterator<reverse> & it) CMP_NOEXCEPT(*it);
308 void dbg_verify()
const;
309 bool verify_integrity()
const;
313 using Path = std::vector<Node *>;
315 void remove_to_leaf(Node & node) CMP_NOEXCEPT(node);
316 void fixup_after_delete(Node * parent,
bool deleted_left) noexcept;
318 void insert_leaf_base(Node & node, Node * start) CMP_NOEXCEPT(node);
320 void fixup_after_insert(Node * node) noexcept;
321 void rotate_left(Node * parent) noexcept;
322 void rotate_right(Node * parent) noexcept;
324 void swap_nodes(Node * n1, Node * n2,
bool swap_colors =
true) noexcept;
325 void replace_node(Node * to_be_replaced, Node * replace_with) noexcept;
326 void swap_unrelated_nodes(Node * n1, Node * n2) noexcept;
327 void swap_neighbors(Node * parent, Node * child) noexcept;
329 void verify_black_root()
const;
330 void verify_black_paths(
const Node * node,
unsigned int * path_length)
const;
331 void verify_red_black(
const Node * node)
const;
336 #ifndef YGG_RBTREE_CPP 337 #include "rbtree.cpp" 340 #endif // YGG_RBTREE_HPP
Helper base class for the NodeTraits you need to implement.
Definition: rbtree.hpp:120
Base class (template) to supply your node class with metainformation.
Definition: rbtree.hpp:92
The Red-Black Tree.
Definition: rbtree.hpp:194
Definition: rbtree.hpp:18