From eb1e34c5a6de4c3b1e7c301e085290f4b13441f2 Mon Sep 17 00:00:00 2001 From: BruebachL <44814898+BruebachL@users.noreply.github.com> Date: Sat, 19 Sep 2026 10:35:04 +0200 Subject: [PATCH] [DeckList] Extract recursive card traversal helpers (#7322) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Lukas BrĂ¼bach --- .../deck_list/deck_list_node_tree.cpp | 65 +++++++++++-------- 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_node_tree.cpp b/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_node_tree.cpp index 575d99340..adbc2166c 100644 --- a/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_node_tree.cpp +++ b/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_node_tree.cpp @@ -46,6 +46,43 @@ QString encodeDeckHash(const QByteArray &digest) return QString::number(number, 32).rightJustified(8, '0'); } +/** + * @brief Collects every card node in @p node's subtree, in tree order. + * + * @return The collected card nodes. + */ +QList collectCardsRecursive(const InnerDecklistNode *node) +{ + QList result; + for (int i = 0; i < node->size(); i++) { + if (auto *card = dynamic_cast(node->at(i))) { + result.append(card); + } else if (auto *inner = dynamic_cast(node->at(i))) { + result.append(collectCardsRecursive(inner)); + } + } + return result; +} + +/** + * @brief Invokes @p func on every card in @p node's subtree. + * + * Cards nested in custom zones are reported with their top-level @p boardZone + * so that callers can classify cards by board (main/side/maybeboard/tokens). + */ +void forEachCardInNode(InnerDecklistNode *boardZone, + InnerDecklistNode *node, + const std::function &func) +{ + for (int i = 0; i < node->size(); i++) { + if (auto *card = dynamic_cast(node->at(i))) { + func(boardZone, card); + } else if (auto *inner = dynamic_cast(node->at(i))) { + forEachCardInNode(boardZone, inner, func); + } + } +} + } // namespace DecklistNodeTree::DecklistNodeTree() : root(new InnerDecklistNode()) @@ -84,19 +121,8 @@ QList DecklistNodeTree::getCardNodes(const QSet result; - std::function collectCards = [&collectCards, - &result](const InnerDecklistNode *node) { - for (int i = 0; i < node->size(); i++) { - if (auto *card = dynamic_cast(node->at(i))) { - result.append(card); - } else if (auto *inner = dynamic_cast(node->at(i))) { - collectCards(inner); - } - } - }; - for (auto *zoneNode : getZoneNodes(restrictToZones)) { - collectCards(zoneNode); + result.append(collectCardsRecursive(zoneNode)); } return result; @@ -198,22 +224,9 @@ bool DecklistNodeTree::deleteNode(AbstractDecklistNode *node, InnerDecklistNode void DecklistNodeTree::forEachCard(const std::function &func) const { - // Cards nested in custom zones are reported with their top-level board zone - // so that callers can classify cards by board (main/side/maybeboard/tokens). - std::function walk = [&func, &walk](InnerDecklistNode *boardZone, - InnerDecklistNode *node) { - for (int i = 0; i < node->size(); i++) { - if (auto *card = dynamic_cast(node->at(i))) { - func(boardZone, card); - } else if (auto *inner = dynamic_cast(node->at(i))) { - walk(boardZone, inner); - } - } - }; - for (int i = 0; i < root->size(); i++) { if (auto *zone = dynamic_cast(root->at(i))) { - walk(zone, zone); + forEachCardInNode(zone, zone, func); } } }