From fae7ea02cd078ea08c90bcff57979079d403e22c Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Sat, 2 Apr 2022 10:26:34 +0800 Subject: [PATCH 1/4] [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. --- bindings/engine-common.i | 4 ++++ libgnucash/engine/Account.cpp | 16 ++++++++++++++++ libgnucash/engine/Account.h | 1 + 3 files changed, 21 insertions(+) diff --git a/bindings/engine-common.i b/bindings/engine-common.i index b4d861dd67..d85e7ef8d1 100644 --- a/bindings/engine-common.i +++ b/bindings/engine-common.i @@ -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 %include diff --git a/libgnucash/engine/Account.cpp b/libgnucash/engine/Account.cpp index 92f4d2bf5b..4140ca1f6f 100644 --- a/libgnucash/engine/Account.cpp +++ b/libgnucash/engine/Account.cpp @@ -49,6 +49,7 @@ extern "C" { #include #include +#include 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; +static void maybe_add_descendants (Account* acc, gpointer arg) +{ + if (static_cast (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. */ diff --git a/libgnucash/engine/Account.h b/libgnucash/engine/Account.h index 19093036c1..868c1b9557 100644 --- a/libgnucash/engine/Account.h +++ b/libgnucash/engine/Account.h @@ -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 @{ From 7cf3a5d93d3dea2e7dc36914024acbaa61bc0a8d Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Sat, 2 Apr 2022 10:26:39 +0800 Subject: [PATCH 2/4] [report-utilities.scm] deprecate gnc:accounts-and-all-descendants --- gnucash/report/report-utilities.scm | 7 ++++--- gnucash/report/test/test-report-utilities.scm | 8 +++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/gnucash/report/report-utilities.scm b/gnucash/report/report-utilities.scm index 126e2aaaed..1d29011580 100644 --- a/gnucash/report/report-utilities.scm +++ b/gnucash/report/report-utilities.scm @@ -234,9 +234,10 @@ ;; Return accountslist *and* their descendant accounts (define (gnc:accounts-and-all-descendants accountslist) - (sort-and-delete-duplicates - (apply append accountslist (map gnc-account-get-descendants accountslist)) - gnc:account-path-less-p equal?)) + (issue-deprecation-warning "gnc:accounts-and-all-descendants is \ +now deprecated, use gnc-accounts-and-all-descendants instead. sort \ +with gnc:account-full-name Date: Thu, 7 Apr 2022 09:49:24 +0800 Subject: [PATCH 3/4] [html-acct-table] deprecate gnc:account-path-less-p --- gnucash/report/html-acct-table.scm | 1 + 1 file changed, 1 insertion(+) diff --git a/gnucash/report/html-acct-table.scm b/gnucash/report/html-acct-table.scm index 6a04c2fcbc..d77ce79da2 100644 --- a/gnucash/report/html-acct-table.scm +++ b/gnucash/report/html-acct-table.scm @@ -555,6 +555,7 @@ (gnc:string-locale Date: Fri, 15 Apr 2022 11:15:06 +0800 Subject: [PATCH 4/4] [reports] use new function names gnc:accounts-and-all-descendants-sorted gnc-accounts-and-all-descendants --- gnucash/report/html-acct-table.scm | 2 +- gnucash/report/reports/example/average-balance.scm | 2 +- gnucash/report/reports/example/daily-reports.scm | 4 ++-- gnucash/report/reports/standard/account-summary.scm | 2 +- gnucash/report/reports/standard/budget.scm | 2 +- gnucash/report/reports/standard/cash-flow.scm | 2 +- gnucash/report/reports/standard/category-barchart.scm | 4 ++-- gnucash/report/reports/standard/net-charts.scm | 2 +- gnucash/report/reports/standard/portfolio.scm | 2 +- gnucash/report/reports/standard/test/test-income-gst.scm | 4 ++-- 10 files changed, 13 insertions(+), 13 deletions(-) diff --git a/gnucash/report/html-acct-table.scm b/gnucash/report/html-acct-table.scm index d77ce79da2..9d8952fa38 100644 --- a/gnucash/report/html-acct-table.scm +++ b/gnucash/report/html-acct-table.scm @@ -697,7 +697,7 @@ (for-each (lambda (acct) (this-collector 'merge (get-balance acct-balances acct) #f)) - (gnc:accounts-and-all-descendants (list account))) + (cons account (gnc-account-get-descendants-sorted account))) this-collector)) (let lp ((accounts (if less-p (sort accts less-p) accts)) diff --git a/gnucash/report/reports/example/average-balance.scm b/gnucash/report/reports/example/average-balance.scm index 66a285a49e..d0f937b7d9 100644 --- a/gnucash/report/reports/example/average-balance.scm +++ b/gnucash/report/reports/example/average-balance.scm @@ -296,7 +296,7 @@ (accounts (get-option gnc:pagename-accounts (N_ "Accounts"))) (dosubs? (get-option gnc:pagename-accounts optname-subacct)) (accounts (if dosubs? - (gnc:accounts-and-all-descendants accounts) + (gnc-accounts-and-all-descendants accounts) accounts)) (plot-type (get-option gnc:pagename-display (N_ "Plot Type"))) (show-plot? (get-option gnc:pagename-display (N_ "Show plot"))) diff --git a/gnucash/report/reports/example/daily-reports.scm b/gnucash/report/reports/example/daily-reports.scm index 2dc965866b..239aa39cc7 100644 --- a/gnucash/report/reports/example/daily-reports.scm +++ b/gnucash/report/reports/example/daily-reports.scm @@ -186,7 +186,7 @@ ;; needed so as to amortize the cpu time properly. (gnc:report-percent-done 1) (set! commodity-list (gnc:accounts-get-commodities - (gnc:accounts-and-all-descendants accounts) + (gnc-accounts-and-all-descendants accounts) report-currency)) (gnc:report-percent-done 5) (set! exchange-fn (gnc:case-exchange-time-fn @@ -208,7 +208,7 @@ (gnc:report-percent-done 25) (if dosubs? (set! accounts - (gnc:accounts-and-all-descendants accounts))) + (gnc-accounts-and-all-descendants accounts))) (gnc:report-percent-done 30) (xaccQueryAddAccountMatch diff --git a/gnucash/report/reports/standard/account-summary.scm b/gnucash/report/reports/standard/account-summary.scm index 96b8c273f7..cd4259e0ae 100644 --- a/gnucash/report/reports/standard/account-summary.scm +++ b/gnucash/report/reports/standard/account-summary.scm @@ -444,7 +444,7 @@ (gnc:html-document-add-object! doc (gnc:html-make-rates-table report-commodity price-fn - (gnc:accounts-and-all-descendants accounts)))))) + (gnc-accounts-and-all-descendants accounts)))))) (gnc:report-finished) doc)) diff --git a/gnucash/report/reports/standard/budget.scm b/gnucash/report/reports/standard/budget.scm index a960d50c72..76ba6ee053 100644 --- a/gnucash/report/reports/standard/budget.scm +++ b/gnucash/report/reports/standard/budget.scm @@ -686,7 +686,7 @@ optname-period-collapse-after))) (doc (gnc:make-html-document)) (accounts (if show-subaccts? - (gnc:accounts-and-all-descendants accounts) + (gnc-accounts-and-all-descendants accounts) accounts))) ;; end of defines diff --git a/gnucash/report/reports/standard/cash-flow.scm b/gnucash/report/reports/standard/cash-flow.scm index 1dd900e6fc..5a08b5ac09 100644 --- a/gnucash/report/reports/standard/cash-flow.scm +++ b/gnucash/report/reports/standard/cash-flow.scm @@ -156,7 +156,7 @@ ;;add subaccounts if requested (accounts (if show-subaccts? - (gnc:accounts-and-all-descendants accounts) + (gnc-accounts-and-all-descendants accounts) accounts)) (accounts (sort accounts gnc:account-full-nameelt (gnc:make-gnc-monetary comm 0))))) ;; all selected accounts (of report-specific type), *and* ;; their descendants (of any type) need to be scanned. - (gnc:accounts-and-all-descendants accounts))) + (gnc-accounts-and-all-descendants accounts))) ;; Creates the to be used in the function ;; below. diff --git a/gnucash/report/reports/standard/net-charts.scm b/gnucash/report/reports/standard/net-charts.scm index 6d9d0f7d5e..1f58450f7b 100644 --- a/gnucash/report/reports/standard/net-charts.scm +++ b/gnucash/report/reports/standard/net-charts.scm @@ -299,7 +299,7 @@ (gnc:report-percent-done 1) (set! commodity-list (gnc:accounts-get-commodities - (gnc:accounts-and-all-descendants accounts) + (gnc-accounts-and-all-descendants accounts) report-currency)) (gnc:report-percent-done 10) (set! exchange-fn (gnc:case-exchange-time-fn diff --git a/gnucash/report/reports/standard/portfolio.scm b/gnucash/report/reports/standard/portfolio.scm index 45272d12ca..020e867ded 100644 --- a/gnucash/report/reports/standard/portfolio.scm +++ b/gnucash/report/reports/standard/portfolio.scm @@ -195,7 +195,7 @@ ;(gnc:debug "accounts" accounts) (if (not (null? accounts)) (let* ((commodity-list (gnc:accounts-get-commodities - (gnc:accounts-and-all-descendants accounts) + (gnc-accounts-and-all-descendants accounts) currency)) (pricedb (gnc-pricedb-get-db (gnc-get-current-book))) (exchange-fn (gnc:case-exchange-fn price-source currency to-date)) diff --git a/gnucash/report/reports/standard/test/test-income-gst.scm b/gnucash/report/reports/standard/test/test-income-gst.scm index a78bb4c649..8c069d8b9a 100644 --- a/gnucash/report/reports/standard/test/test-income-gst.scm +++ b/gnucash/report/reports/standard/test/test-income-gst.scm @@ -232,10 +232,10 @@ (define (default-testing-options) (let ((options (gnc:make-report-options rpt-uuid))) (set-option! options "Accounts" "Sales" - (gnc:accounts-and-all-descendants + (gnc-accounts-and-all-descendants (list (get-acct "Income")))) (set-option! options "Accounts" "Purchases" - (gnc:accounts-and-all-descendants + (gnc-accounts-and-all-descendants (list (get-acct "Expenses")))) (set-option! options "Accounts" "Tax Accounts" (list (get-acct "Purchases VAT")