Skip to content

Commit

Permalink
datastructure: Make iteration order a constexpr template parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
heinezen committed Nov 25, 2024
1 parent 6571e0d commit 5a0c3ec
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions libopenage/datastructure/pairing_heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ class PairingHeap final {
*/
void clear() {
auto delete_node = [](element_t node) { delete node; };
this->iter_all(delete_node, true);
this->iter_all<true>(delete_node);
this->root_node = nullptr;
this->node_count = 0;
#if OPENAGE_PAIRINGHEAP_DEBUG
Expand Down Expand Up @@ -579,30 +579,44 @@ class PairingHeap final {
}
#endif

void iter_all(const std::function<void(const element_t &)> &func, bool reverse = true) const {
this->walk_tree(this->root_node, func, reverse);
/**
* Apply the given function to all nodes in the tree.
*
* @tparam reverse If true, the function is applied to the nodes in reverse order.
* @param func Function to apply to each node.
*/
template <bool reverse = false>
void iter_all(const std::function<void(const element_t &)> &func) const {
this->walk_tree<reverse>(this->root_node, func);
}

private:
void walk_tree(const element_t &root,
const std::function<void(const element_t &)> &func,
bool reverse = false) const {
if (!reverse) {
func(root);
/**
* Apply the given function to all nodes in the tree.
*
* @tparam reverse If true, the function is applied to the nodes in reverse order.
* @param start Starting node.
* @param func Function to apply to each node.
*/
template <bool reverse = false>
void walk_tree(const element_t &start,
const std::function<void(const element_t &)> &func) const {
if constexpr (not reverse) {
func(start);
}

if (root) {
auto node = root->first_child;
if (start) {
auto node = start->first_child;
while (true) {
if (not node) {
break;
}

this->walk_tree(node, func, reverse);
this->walk_tree<reverse>(node, func);
node = node->next_sibling;
}
if (reverse) {
func(root);
if constexpr (reverse) {
func(start);
}
}
}
Expand Down

0 comments on commit 5a0c3ec

Please sign in to comment.