1 | // $Id: Impl.cc 4070 2021-08-09 06:32:59Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2021 Peter Johansson |
---|
5 | |
---|
6 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
7 | |
---|
8 | The yat library is free software; you can redistribute it and/or |
---|
9 | modify it under the terms of the GNU General Public License as |
---|
10 | published by the Free Software Foundation; either version 3 of the |
---|
11 | License, or (at your option) any later version. |
---|
12 | |
---|
13 | The yat library is distributed in the hope that it will be useful, |
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
20 | */ |
---|
21 | |
---|
22 | #include <config.h> |
---|
23 | |
---|
24 | #include "Ranking.h" |
---|
25 | |
---|
26 | #include <cassert> |
---|
27 | #include <cstddef> |
---|
28 | #include <iostream> |
---|
29 | |
---|
30 | namespace theplu { |
---|
31 | namespace yat { |
---|
32 | namespace utility { |
---|
33 | namespace ranking { |
---|
34 | |
---|
35 | Impl::Impl(void) |
---|
36 | { |
---|
37 | reset(); |
---|
38 | } |
---|
39 | |
---|
40 | |
---|
41 | Impl::Impl(Impl&& from) |
---|
42 | { |
---|
43 | reset(); |
---|
44 | if (from.head_.parent_) |
---|
45 | move_data(std::move(from)); |
---|
46 | } |
---|
47 | |
---|
48 | |
---|
49 | Impl& Impl::operator=(Impl&& rhs) |
---|
50 | { |
---|
51 | assert(!head_.parent_); |
---|
52 | move_data(std::move(rhs)); |
---|
53 | assert(validate()); |
---|
54 | return *this; |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | void Impl::move_data(Impl&& from) |
---|
59 | { |
---|
60 | assert(head_.parent_ == nullptr); |
---|
61 | head_.parent_ = from.head_.parent_; |
---|
62 | head_.left_ = from.head_.left_; |
---|
63 | assert(head_.right_ == &head_); |
---|
64 | head_.parent_->right_ = &head_; |
---|
65 | node_count_ = from.node_count_; |
---|
66 | from.reset(); |
---|
67 | assert(validate()); |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | void Impl::relink(const NodeBase* node, NodeBase* child) |
---|
72 | { |
---|
73 | assert(node->parent_); |
---|
74 | assert(node != &head_); |
---|
75 | assert(child != &head_); |
---|
76 | |
---|
77 | // make child the child of node's parent |
---|
78 | if (node->is_left_node()) |
---|
79 | node->parent_->left_ = child; |
---|
80 | else if (node->is_right_node()) |
---|
81 | node->parent_->right_ = child; |
---|
82 | |
---|
83 | // make node's parent the parent of child |
---|
84 | if (child) |
---|
85 | child->parent_ = node->parent_; |
---|
86 | |
---|
87 | // inherit the kids |
---|
88 | if (child) { |
---|
89 | if (node->left_ && node->left_!=child) { |
---|
90 | assert(!child->left_); |
---|
91 | child->left_ = node->left_; |
---|
92 | } |
---|
93 | if (node->right_ && node->right_!=child) { |
---|
94 | assert(!child->right_); |
---|
95 | child->right_ = node->right_; |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | // update pointer to left_most node |
---|
100 | if (head_.left_ == node) { |
---|
101 | head_.left_ = child ? child : node->parent_; |
---|
102 | } |
---|
103 | assert(child==nullptr || child->is_left_node() || child->is_right_node()); |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | void Impl::reset(void) |
---|
108 | { |
---|
109 | // FIXME do we need to destruct parent and rest of tree? |
---|
110 | head_.parent_ = nullptr; |
---|
111 | head_.left_ = &head_; |
---|
112 | head_.right_ = &head_; |
---|
113 | node_count_ = 0; |
---|
114 | assert(validate()); |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | bool Impl::validate(void) const |
---|
119 | { |
---|
120 | if (node_count_ == 0) { |
---|
121 | assert(head_.parent_ == nullptr); |
---|
122 | assert(head_.left_ == &head_); |
---|
123 | assert(head_.right_ == &head_); |
---|
124 | assert(head_.is_head_node()); |
---|
125 | } |
---|
126 | else { |
---|
127 | assert(head_.parent_); |
---|
128 | assert(head_.left_); |
---|
129 | if (head_.parent_->right_ != &head_) { |
---|
130 | return false; |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | if (head_.parent_) { |
---|
135 | if (head_.parent_->left_most() != head_.left_) { |
---|
136 | std::cerr << "head_.left incorrect\n"; |
---|
137 | return false; |
---|
138 | } |
---|
139 | if (!head_.parent_->recursive_validate()) |
---|
140 | return false; |
---|
141 | } |
---|
142 | else if (!head_.validate()) |
---|
143 | return false; |
---|
144 | |
---|
145 | return true; |
---|
146 | } |
---|
147 | |
---|
148 | } |
---|
149 | }}} // of namespace utility, yat, and theplu |
---|