Skip to content

Commit 4ddc6d1

Browse files
committed
Make splay_tree::move_to easier to understand
1 parent 58b5fc2 commit 4ddc6d1

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

include/cpp-sort/detail/splaysort.h

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -169,30 +169,24 @@ namespace cppsort::detail
169169
auto move_to(OutputIterator out)
170170
-> void
171171
{
172-
binary_tree_node_base* curr = root();
173-
while (curr->left_child != &sentinel_node_) {
174-
curr = curr->left_child;
175-
}
176-
*out = std::move(static_cast<node_type*>(curr)->value);
177-
++out;
172+
binary_tree_node_base* current = root();
178173

179-
while (true) {
180-
if (curr->right_child != &sentinel_node_) {
181-
auto prev = std::exchange(curr, curr->right_child);
182-
curr->parent = prev->parent;
183-
prev->right_child = &sentinel_node_;
184-
while (curr->left_child != &sentinel_node_) {
185-
curr = curr->left_child;
186-
}
187-
} else if (curr->parent != &sentinel_node_) {
188-
curr = curr->parent;
189-
} else {
190-
return;
174+
from_parent:
175+
while (current->left_child != &sentinel_node_) {
176+
current = current->left_child;
191177
}
192-
193-
*out = std::move(static_cast<node_type*>(curr)->value);
178+
from_child:
179+
*out = std::move(static_cast<node_type*>(current)->value);
194180
++out;
195-
}
181+
if (current->right_child != &sentinel_node_) {
182+
auto previous = std::exchange(current, current->right_child);
183+
current->parent = previous->parent;
184+
goto from_parent;
185+
}
186+
current = current->parent;
187+
if (current != &sentinel_node_) {
188+
goto from_child;
189+
}
196190
}
197191

198192
private:

0 commit comments

Comments
 (0)