From 4b1b503791fd11627e98026f0d9bf086d4dc472a Mon Sep 17 00:00:00 2001 From: heinezen Date: Mon, 25 Nov 2024 19:00:18 +0100 Subject: [PATCH] datastructure: Make iteration order a constexpr template parameter. --- libopenage/datastructure/pairing_heap.h | 40 +++++++++++++++++-------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/libopenage/datastructure/pairing_heap.h b/libopenage/datastructure/pairing_heap.h index 8a571d1ea2..dd348d8c55 100644 --- a/libopenage/datastructure/pairing_heap.h +++ b/libopenage/datastructure/pairing_heap.h @@ -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(delete_node); this->root_node = nullptr; this->node_count = 0; #if OPENAGE_PAIRINGHEAP_DEBUG @@ -579,30 +579,44 @@ class PairingHeap final { } #endif - void iter_all(const std::function &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 + void iter_all(const std::function &func) const { + this->walk_tree(this->root_node, func); } private: - void walk_tree(const element_t &root, - const std::function &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 + void walk_tree(const element_t &start, + const std::function &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(node, func); node = node->next_sibling; } - if (reverse) { - func(root); + if constexpr (reverse) { + func(start); } } }