1 | #ifndef theplu_yat_utility_ranking_impl |
---|
2 | #define theplu_yat_utility_ranking_impl |
---|
3 | |
---|
4 | // $Id: Impl.h 4075 2021-08-20 06:54:37Z 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 | |
---|
36 | /// \cond IGNORE_DOXYGEN |
---|
37 | |
---|
38 | // namespace for internal classes used in class Ranking |
---|
39 | namespace ranking { |
---|
40 | |
---|
41 | class Impl |
---|
42 | { |
---|
43 | public: |
---|
44 | Impl(void); |
---|
45 | Impl(const Impl& other) = delete; |
---|
46 | Impl(Impl&& other); |
---|
47 | Impl& operator=(const Impl& rhs) = delete; |
---|
48 | Impl& operator=(Impl&& rhs); |
---|
49 | |
---|
50 | // Head is the only node with no parent; it is also the node |
---|
51 | // with no value. All other nodes are NodeValue<T> pointers. Its |
---|
52 | // left child is the root node and it also holds a short cut to |
---|
53 | // the leftmost node to allow constant access and creating of |
---|
54 | // the begin() iterator. |
---|
55 | NodeBase head_; |
---|
56 | |
---|
57 | bool empty(void) const; |
---|
58 | size_t size(void) const; |
---|
59 | |
---|
60 | NodeBase*& root_node(void); |
---|
61 | const NodeBase* root_node(void) const; |
---|
62 | NodeBase*& left_most(void); |
---|
63 | const NodeBase* left_most(void) const; |
---|
64 | |
---|
65 | /* |
---|
66 | Node \a child is a child of \a parent. The child which is required |
---|
67 | to be a nullptr is assigned as \a element. and parent is updated |
---|
68 | as appropriate (height, size, etc) and rebalanced if needed |
---|
69 | including that balance of ancestors is checked. |
---|
70 | */ |
---|
71 | void insert_and_rebalance(std::unique_ptr<NodeBase>&& element, |
---|
72 | NodeBase& parent, NodeBase*& child); |
---|
73 | |
---|
74 | /* |
---|
75 | Examine the balance of node->parent. If the height of its left |
---|
76 | branch and right branch differs more than one, |
---|
77 | rebalance. Otherwise or if recursive is true, call with parent |
---|
78 | and node as arguments. |
---|
79 | */ |
---|
80 | void insert_rebalance(NodeBase* node, NodeBase* child); |
---|
81 | |
---|
82 | // |
---|
83 | void erase_and_rebalance(const NodeBase* node); |
---|
84 | |
---|
85 | // typically only called with assert, YAT_ASSERT or similar |
---|
86 | bool validate(void) const; |
---|
87 | bool validate(const NodeBase*) const; |
---|
88 | // return number of nodes left of node, i.e., number of nodes |
---|
89 | // with value less than node->value. |
---|
90 | size_t ranking(const NodeBase* node) const; |
---|
91 | void reset(void); |
---|
92 | /* |
---|
93 | \brief replace node with child |
---|
94 | |
---|
95 | Function assumes that child can replace node without collision |
---|
96 | or destrying the order in the tree, which means |
---|
97 | if there is a node->left its value is <= child->value |
---|
98 | if there is a node->right its value is <= child->value |
---|
99 | if there is a node->left (ecl child) there is no child->left |
---|
100 | if there is a node->right (excl child) there is no child->right |
---|
101 | */ |
---|
102 | void relink(const NodeBase* node, NodeBase* child); |
---|
103 | |
---|
104 | /* |
---|
105 | y x |
---|
106 | / \ / \ |
---|
107 | x T3 T1 y |
---|
108 | / \ < - - - - - - - / \ |
---|
109 | T1 T2 Left Rotation T2 T3 |
---|
110 | |
---|
111 | \return new root in the subtree, i.e., y. |
---|
112 | */ |
---|
113 | NodeBase* left_rotate(NodeBase* x); |
---|
114 | |
---|
115 | /* |
---|
116 | y x |
---|
117 | / \ / \ |
---|
118 | x T3 T1 y |
---|
119 | / \ - - - - - - - > / \ |
---|
120 | T1 T2 Right Rotation T2 T3 |
---|
121 | |
---|
122 | \return new root in the subtree, i.e., x. |
---|
123 | */ |
---|
124 | NodeBase* right_rotate(NodeBase* y); |
---|
125 | private: |
---|
126 | void move_data(Impl&&); |
---|
127 | void erase_rebalance(NodeBase* node); |
---|
128 | void erase_rebalance(NodeBase* parent, NodeBase* node); |
---|
129 | |
---|
130 | }; |
---|
131 | } // end of namespace ranking |
---|
132 | |
---|
133 | /// \endcond |
---|
134 | |
---|
135 | }}} // of namespace utility, yat, and theplu |
---|
136 | #endif |
---|