[account.cpp] gnc_accounts_and_all_descendants converted from scm

much more efficient than guile algorithm, avoids numerous repeated
GList<->SCM conversions, and traversals of account descendants.
This commit is contained in:
Christopher Lam 2022-04-02 10:26:34 +08:00
parent 07fbcc0e9e
commit fae7ea02cd
3 changed files with 21 additions and 0 deletions

View File

@ -48,10 +48,14 @@ AccountList * gnc_account_get_descendants (const Account *account);
%newobject gnc_account_get_descendants_sorted;
AccountList * gnc_account_get_descendants_sorted (const Account *account);
%newobject gnc_accounts_and_all_descendants;
AccountList * gnc_accounts_and_all_descendants (AccountList *accounts);
%ignore gnc_account_get_children;
%ignore gnc_account_get_children_sorted;
%ignore gnc_account_get_descendants;
%ignore gnc_account_get_descendants_sorted;
%ignore gnc_accounts_and_all_descendants;
%include <Account.h>
%include <Transaction.h>

View File

@ -49,6 +49,7 @@ extern "C" {
#include <numeric>
#include <map>
#include <unordered_set>
static QofLogModule log_module = GNC_MOD_ACCOUNT;
@ -6228,6 +6229,21 @@ gboolean xaccAccountRegister (void)
return qof_object_register (&account_object_def);
}
using AccountSet = std::unordered_set<Account*>;
static void maybe_add_descendants (Account* acc, gpointer arg)
{
if (static_cast <AccountSet*> (arg)->insert (acc).second)
g_list_foreach (GET_PRIVATE(acc)->children, (GFunc) maybe_add_descendants, arg);
};
GList *
gnc_accounts_and_all_descendants (GList *accounts)
{
AccountSet accset;
g_list_foreach (accounts, (GFunc) maybe_add_descendants, &accset);
return std::accumulate (accset.begin(), accset.end(), (GList*) nullptr, g_list_prepend);
}
/* ======================= UNIT TESTING ACCESS =======================
* The following functions are for unit testing use only.
*/

View File

@ -1620,6 +1620,7 @@ void dxaccAccountSetQuoteTZ (Account *account, const char *tz);
const char * dxaccAccountGetQuoteTZ (const Account *account);
/** @} */
GList * gnc_accounts_and_all_descendants (GList *accounts);
/** @name Account parameter names
@{