From d2db43019c0be28f95233aaa7a5e97f80a1f96f8 Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Tue, 3 Aug 2021 22:02:12 +0800 Subject: [PATCH] [account.cpp] internal function account_foreach_descendant fast and efficient --- libgnucash/engine/Account.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/libgnucash/engine/Account.cpp b/libgnucash/engine/Account.cpp index 8f12f0c64d..85a3ce909a 100644 --- a/libgnucash/engine/Account.cpp +++ b/libgnucash/engine/Account.cpp @@ -2691,6 +2691,36 @@ DxaccAccountSetCurrency (Account * acc, gnc_commodity * currency) /********************************************************************\ \********************************************************************/ +static void +account_foreach_descendant (const Account *acc, AccountCb thunk, + void* user_data, bool sort) +{ + gpointer result = nullptr; + GList *children; + + g_return_if_fail (GNC_IS_ACCOUNT(acc)); + g_return_if_fail (thunk); + + auto priv{GET_PRIVATE(acc)}; + if (sort) + { + children = g_list_copy (priv->children); + children = g_list_sort (children, (GCompareFunc)xaccAccountOrder); + } + else + children = priv->children; + + for (auto node = children; node; node = node->next) + { + auto child = static_cast(node->data); + thunk (child, user_data); + account_foreach_descendant (child, thunk, user_data, sort); + } + + if (sort) + g_list_free (children); +} + void gnc_account_append_child (Account *new_parent, Account *child) {