gnucash stable: Multiple changes pushed
Christopher Lam
clam at code.gnucash.org
Thu May 2 10:22:14 EDT 2024
Updated via https://github.com/Gnucash/gnucash/commit/4c29f69d (commit)
via https://github.com/Gnucash/gnucash/commit/87dbbf25 (commit)
from https://github.com/Gnucash/gnucash/commit/2c6f1509 (commit)
commit 4c29f69d673036270ff9ab5da5642bcb9694b280
Author: Christopher Lam <christopher.lck at gmail.com>
Date: Sun Apr 28 23:12:56 2024 +0800
[engine.i] even faster gnc_get_match_commodity_splits
gnc_get_match_commodity_splits needs to scan the account splitlists to
find suitable splits upto end_date. Because splitlist is date-sorted,
use gnc_account_foreach_split_until_date to avoid scanning later
splits.
diff --git a/bindings/engine.i b/bindings/engine.i
index 0546363c68..20158bf52f 100644
--- a/bindings/engine.i
+++ b/bindings/engine.i
@@ -129,19 +129,27 @@ static const GncGUID * gncBudgetGetGUID(GncBudget *x)
SplitsVec gnc_get_match_commodity_splits (AccountVec accounts, bool use_end_date,
time64 end_date, gnc_commodity *comm, bool sort)
{
- auto match = [use_end_date, end_date, comm](const Split* s) -> bool
+ SplitsVec rv;
+
+ auto maybe_accumulate = [&rv, comm](auto s)
{
- if (xaccSplitGetReconcile (s) == VREC) return false;
- auto trans{xaccSplitGetParent (s)};
- if (use_end_date && xaccTransGetDate(trans) > end_date) return false;
- auto txn_comm{xaccTransGetCurrency (trans)};
+ auto txn_comm{xaccTransGetCurrency (xaccSplitGetParent (s))};
auto acc_comm{xaccAccountGetCommodity (xaccSplitGetAccount (s))};
- return (txn_comm != acc_comm) && (!comm || comm == txn_comm || comm == acc_comm);
+ if ((xaccSplitGetReconcile (s) != VREC) &&
+ (txn_comm != acc_comm) &&
+ (!comm || comm == txn_comm || comm == acc_comm))
+ rv.push_back (s);
};
- std::vector<Split*> rv;
- auto maybe_accumulate_split = [&rv, match](auto s){ if (match(s)) rv.push_back (s); };
- for (const auto acc : accounts)
- gnc_account_foreach_split (acc, maybe_accumulate_split, true);
+
+ std::function<void(Account*)> scan_account;
+ if (use_end_date)
+ scan_account = [end_date, maybe_accumulate](auto acc)
+ { gnc_account_foreach_split_until_date (acc, end_date, maybe_accumulate); };
+ else
+ scan_account = [maybe_accumulate](auto acc)
+ { gnc_account_foreach_split (acc, maybe_accumulate, false); };
+
+ std::for_each (accounts.begin(), accounts.end(), scan_account);
if (sort)
std::sort (rv.begin(), rv.end(), [](auto a, auto b){ return xaccSplitOrder (a, b) < 0; });
return rv;
commit 87dbbf25f8a7c02d4f15c608ca8be2e1e37b94b5
Author: Christopher Lam <christopher.lck at gmail.com>
Date: Wed Apr 24 13:44:54 2024 +0800
[account.cpp] add gnc_account_foreach_until_date
- uses binary search to find first split after date
- for_each from earliest split to (but excluding) the above first split
diff --git a/libgnucash/engine/Account.cpp b/libgnucash/engine/Account.cpp
index 589593b799..2b45128148 100644
--- a/libgnucash/engine/Account.cpp
+++ b/libgnucash/engine/Account.cpp
@@ -1151,6 +1151,22 @@ gnc_account_foreach_split (const Account *acc, std::function<void(Split*)> func,
std::for_each(splits.begin(), splits.end(), func);
}
+void
+gnc_account_foreach_split_until_date (const Account *acc, time64 end_date,
+ std::function<void(Split*)> f)
+{
+ if (!GNC_IS_ACCOUNT (acc))
+ return;
+
+ auto after_date = [](time64 end_date, auto s) -> bool
+ { return (xaccTransGetDate (xaccSplitGetParent (s)) > end_date); };
+
+ auto splits{GET_PRIVATE(acc)->splits};
+ auto after_date_iter = std::upper_bound (splits.begin(), splits.end(), end_date, after_date);
+ std::for_each (splits.begin(), after_date_iter, f);
+}
+
+
Split*
gnc_account_find_split (const Account *acc, std::function<bool(const Split*)> predicate,
bool reverse)
diff --git a/libgnucash/engine/Account.hpp b/libgnucash/engine/Account.hpp
index b246bf6885..332e45ff7b 100644
--- a/libgnucash/engine/Account.hpp
+++ b/libgnucash/engine/Account.hpp
@@ -43,6 +43,9 @@ const SplitsVec xaccAccountGetSplits (const Account*);
void gnc_account_foreach_split (const Account*, std::function<void(Split*)>, bool);
+void gnc_account_foreach_split_until_date (const Account *acc, time64 end_date,
+ std::function<void(Split*)> f);
+
/** scans account split list (in forward or reverse order) until
* predicate split->bool returns true. Maybe return the split.
*
Summary of changes:
bindings/engine.i | 28 ++++++++++++++++++----------
libgnucash/engine/Account.cpp | 16 ++++++++++++++++
libgnucash/engine/Account.hpp | 3 +++
3 files changed, 37 insertions(+), 10 deletions(-)
More information about the gnucash-changes
mailing list