Skip to content

Commit 4b1b503

Browse files
committed
datastructure: Make iteration order a constexpr template parameter.
1 parent fd7fd01 commit 4b1b503

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

libopenage/datastructure/pairing_heap.h

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ class PairingHeap final {
405405
*/
406406
void clear() {
407407
auto delete_node = [](element_t node) { delete node; };
408-
this->iter_all(delete_node, true);
408+
this->iter_all<true>(delete_node);
409409
this->root_node = nullptr;
410410
this->node_count = 0;
411411
#if OPENAGE_PAIRINGHEAP_DEBUG
@@ -579,30 +579,44 @@ class PairingHeap final {
579579
}
580580
#endif
581581

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

586593
private:
587-
void walk_tree(const element_t &root,
588-
const std::function<void(const element_t &)> &func,
589-
bool reverse = false) const {
590-
if (!reverse) {
591-
func(root);
594+
/**
595+
* Apply the given function to all nodes in the tree.
596+
*
597+
* @tparam reverse If true, the function is applied to the nodes in reverse order.
598+
* @param start Starting node.
599+
* @param func Function to apply to each node.
600+
*/
601+
template <bool reverse = false>
602+
void walk_tree(const element_t &start,
603+
const std::function<void(const element_t &)> &func) const {
604+
if constexpr (not reverse) {
605+
func(start);
592606
}
593607

594-
if (root) {
595-
auto node = root->first_child;
608+
if (start) {
609+
auto node = start->first_child;
596610
while (true) {
597611
if (not node) {
598612
break;
599613
}
600614

601-
this->walk_tree(node, func, reverse);
615+
this->walk_tree<reverse>(node, func);
602616
node = node->next_sibling;
603617
}
604-
if (reverse) {
605-
func(root);
618+
if constexpr (reverse) {
619+
func(start);
606620
}
607621
}
608622
}

0 commit comments

Comments
 (0)