Yggdrasill  0.2
wbtree.hpp
1 #ifndef YGG_WBTREE_HPP
2 #define YGG_WBTREE_HPP
3 
4 #include "bst.hpp"
5 #include "debug.hpp"
6 #include "options.hpp"
7 #include "size_holder.hpp"
8 #include "tree_iterator.hpp"
9 
10 #include <cassert>
11 #include <cstddef>
12 #include <set>
13 #include <type_traits>
14 
15 // Only for debugging purposes
16 #include <fstream>
17 #include <vector>
18 
19 namespace ygg {
35 template <class Node, class Options = DefaultOptions, class Tag = int>
36 class WBTreeNodeBase : public bst::BSTNodeBase<Node, Tag> {
37 public:
38  // TODO namespacing!
39  void swap_parent_with(Node * other) noexcept;
40  size_t _wbt_size;
41 };
42 
52 public:
53  // TODO document
54  template <class Node, class Tree>
55  static void
56  leaf_inserted(Node & node, Tree & t) noexcept
57  {
58  (void)node;
59  (void)t;
60  }
61 
62  template <class Node, class Tree>
63  static void
64  rotated_left(Node & node, Tree & t) noexcept
65  {
66  (void)node;
67  (void)t;
68  }
69 
70  template <class Node, class Tree>
71  static void
72  rotated_right(Node & node, Tree & t) noexcept
73  {
74  (void)node;
75  (void)t;
76  }
77 
78  template <class Node, class Tree>
79  static void
80  delete_leaf(Node & node, Tree & t) noexcept
81  {
82  (void)node;
83  (void)t;
84  }
85 
86  template <class Node, class Tree>
87  static void
88  splice_out_left_knee(Node & node, Tree & t) noexcept
89  {
90  (void)node;
91  (void)t;
92  }
93 
94  template <class Node, class Tree>
95  static void
96  splice_out_right_knee(Node & node, Tree & t) noexcept
97  {
98  (void)node;
99  (void)t;
100  }
101 
102  template <class Node, class Tree>
103  static void
104  deleted_below(Node & node, Tree & t) noexcept
105  {
106  (void)node;
107  (void)t;
108  }
109 
110  template <class Node, class Tree>
111  static void
112  swapped(Node & old_ancestor, Node & old_descendant, Tree & t) noexcept
113  {
114  (void)old_ancestor;
115  (void)old_descendant;
116  (void)t;
117  }
118 };
119 
140 template <class Node, class NodeTraits, class Options = DefaultOptions,
141  class Tag = int, class Compare = ygg::utilities::flexible_less>
142 class WBTree : public bst::BinarySearchTree<Node, Options, Tag, Compare> {
143 public:
145  // Node Base
148  static_assert(std::is_base_of<NB, Node>::value,
149  "Node class not properly derived from WBTreeNodeBase");
150 
154  WBTree() noexcept;
155 
165  WBTree(MyClass && other) noexcept;
166 
167  /*
168  * Pull in classes from base tree
169  */
170  template <bool reverse>
171  using iterator = typename TB::template iterator<reverse>;
172  template <bool reverse>
173  using const_iterator = typename TB::template const_iterator<reverse>;
174 
189  void insert(Node & node) CMP_NOEXCEPT(node);
190 
191  // TODO document
192  void insert_left_leaning(Node & node) CMP_NOEXCEPT(node);
193  void insert_right_leaning(Node & node) CMP_NOEXCEPT(node);
194 
216  template <class Comparable>
217  utilities::select_type_t<size_t, Node *, Options::stl_erase>
218  erase(const Comparable & c) CMP_NOEXCEPT(c);
219 
234  template <bool reverse>
235  utilities::select_type_t<const iterator<reverse>, Node *, Options::stl_erase>
236  erase(const iterator<reverse> & it) CMP_NOEXCEPT(*it);
237 
238  // TODO document
239  template <class Comparable>
240  Node * erase_optimistic(const Comparable & c) CMP_NOEXCEPT(c);
241 
249  void remove(Node & node) CMP_NOEXCEPT(node);
250 
251  // Mainly debugging methods
253  bool verify_integrity() const;
254 
255  /* Debugging methods */
256  // TODO only here for compatibility with the Zip Tree
257  void dbg_verify() const;
258  size_t dbg_count_violations(std::vector<size_t> * depths = nullptr,
259  std::vector<size_t> * amounts = nullptr) const;
260  void dbg_assert_balance_at(Node * n) const;
262 
263 protected:
264  template <bool fix_upward>
265  void remove_onepass(Node & node) CMP_NOEXCEPT(node);
266  void remove_to_leaf(Node & node) CMP_NOEXCEPT(node);
267  void fixup_after_delete(Node * parent, bool deleted_left)
268  CMP_NOEXCEPT(*parent);
269 
270  template <bool call_fixup>
271  bool remove_swap_and_remove_left(Node * node, Node * replacement)
272  CMP_NOEXCEPT(*node);
273  template <bool call_fixup>
274  bool remove_swap_and_remove_right(Node * node, Node * replacement)
275  CMP_NOEXCEPT(*node);
276  template <bool call_fixup>
277  void remove_leaf(Node * node) CMP_NOEXCEPT(*node);
278 
279  template <bool on_equality_prefer_left>
280  void insert_leaf_base_twopass(Node & node, Node * start) CMP_NOEXCEPT(node);
281  void fixup_after_insert_twopass(Node * node) CMP_NOEXCEPT(*node);
282 
283  template <bool on_equality_prefer_left>
284  void insert_leaf_onepass(Node & node) CMP_NOEXCEPT(node);
285 
286  void rotate_left(Node * parent) noexcept;
287  void rotate_right(Node * parent) noexcept;
288 
289  void swap_nodes(Node * n1, Node * n2) noexcept;
290  void replace_node(Node * to_be_replaced, Node * replace_with) noexcept;
291  void swap_unrelated_nodes(Node * n1, Node * n2) noexcept;
292  void swap_neighbors(Node * parent, Node * child) noexcept;
293 
294  void verify_sizes() const;
295 };
296 
297 } // namespace ygg
298 
299 #ifndef YGG_WBTREE_CPP
300 #include "wbtree.cpp"
301 #endif
302 
303 #endif // YGG_WBTREE_HPP
Definition: bst.hpp:123
Definition: bst.hpp:78
Helper base class for the NodeTraits you need to implement for the weight balanced tree...
Definition: wbtree.hpp:51
Base class (template) to supply your node class with metainformation.
Definition: wbtree.hpp:36
The Weight Balanced Tree.
Definition: wbtree.hpp:142
Definition: rbtree.hpp:18