yat  0.21pre
Impl.h
1 #ifndef theplu_yat_utility_ranking_impl
2 #define theplu_yat_utility_ranking_impl
3 
4 // $Id: Impl.h 4087 2021-08-30 02:00:02Z peter $
5 
6 /*
7  Copyright (C) 2021 Peter Johansson
8 
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10 
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15 
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23 */
24 
25 // This is a private file used by "yat/utility/Ranking.h"
26 
27 #include "NodeBase.h"
28 
29 #include <cstddef>
30 #include <memory>
31 
32 namespace theplu {
33 namespace yat {
34 namespace utility {
35 
37 
38  // namespace for internal classes used in class Ranking
39  namespace ranking {
40 
48  class Impl
49  {
50  public:
51  Impl(void);
52  Impl(const Impl& other) = delete;
53  Impl(Impl&& other);
54  Impl& operator=(const Impl& rhs) = delete;
55  Impl& operator=(Impl&& rhs);
56 
57  // Head is the only node with no parent; it is also the node
58  // with no value. All other nodes are NodeValue<T> pointers. Its
59  // left child is the root node and it also holds a short cut to
60  // the leftmost node to allow constant access and creating of
61  // the begin() iterator.
62  NodeBase head_;
63 
64  bool empty(void) const;
65  size_t size(void) const;
66 
67  NodeBase*& root_node(void);
68  const NodeBase* root_node(void) const;
69  NodeBase*& left_most(void);
70  const NodeBase* left_most(void) const;
71  NodeBase*& right_most(void);
72  const NodeBase* right_most(void) const;
73 
74  /*
75  Node \a child is a child of \a parent. The child which is required
76  to be a nullptr is assigned as \a element. and parent is updated
77  as appropriate (height, size, etc) and rebalanced if needed
78  including that balance of ancestors is checked.
79  */
80  void insert_and_rebalance(std::unique_ptr<NodeBase>&& element,
81  NodeBase& parent, bool left);
82 
83  /*
84  Examine the balance of node->parent. If the height of its left
85  branch and right branch differs more than one,
86  rebalance. Otherwise or if recursive is true, call with parent
87  and node as arguments.
88  */
89  void insert_rebalance(NodeBase* node, NodeBase* child);
90 
91  //
92  void erase_and_rebalance(const NodeBase* node);
93 
94  // typically only called with assert, YAT_ASSERT or similar
95  bool validate(void) const;
96  bool validate(const NodeBase*) const;
97  // return number of nodes left of node, i.e., number of nodes
98  // with value less than node->value.
99  size_t ranking(const NodeBase* node) const;
100  void reset(void);
101  /*
102  \brief replace node with child
103 
104  Function assumes that child can replace node without collision
105  or destrying the order in the tree, which means
106  if there is a node->left its value is <= child->value
107  if there is a node->right its value is <= child->value
108  if there is a node->left (ecl child) there is no child->left
109  if there is a node->right (excl child) there is no child->right
110  */
111  void relink(const NodeBase* node, NodeBase* child);
112 
113  /*
114  y x
115  / \ / \
116  x T3 T1 y
117  / \ < - - - - - - - / \
118  T1 T2 Left Rotation T2 T3
119 
120  \return new root in the subtree, i.e., y.
121  */
122  NodeBase* left_rotate(NodeBase* x);
123 
124  /*
125  y x
126  / \ / \
127  x T3 T1 y
128  / \ - - - - - - - > / \
129  T1 T2 Right Rotation T2 T3
130 
131  \return new root in the subtree, i.e., x.
132  */
133  NodeBase* right_rotate(NodeBase* y);
134  private:
135  NodeBase* right_most_;
136  void move_data(Impl&&);
137  void erase_rebalance(NodeBase* node);
138  void erase_rebalance(NodeBase* parent, NodeBase* node);
139 
140  };
141  } // end of namespace ranking
142 
144 
145 }}} // of namespace utility, yat, and theplu
146 #endif
The Department of Theoretical Physics namespace as we define it.

Generated on Wed Jan 25 2023 03:34:29 for yat by  doxygen 1.8.14