From 87dbbf25f8a7c02d4f15c608ca8be2e1e37b94b5 Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Wed, 24 Apr 2024 13:44:54 +0800 Subject: [PATCH] [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 --- libgnucash/engine/Account.cpp | 16 ++++++++++++++++ libgnucash/engine/Account.hpp | 3 +++ 2 files changed, 19 insertions(+) diff --git a/libgnucash/engine/Account.cpp b/libgnucash/engine/Account.cpp index 589593b799a..2b451281485 100644 --- a/libgnucash/engine/Account.cpp +++ b/libgnucash/engine/Account.cpp @@ -1151,6 +1151,22 @@ gnc_account_foreach_split (const Account *acc, std::function func, std::for_each(splits.begin(), splits.end(), func); } +void +gnc_account_foreach_split_until_date (const Account *acc, time64 end_date, + std::function 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 predicate, bool reverse) diff --git a/libgnucash/engine/Account.hpp b/libgnucash/engine/Account.hpp index b246bf68853..332e45ff7b7 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, bool); +void gnc_account_foreach_split_until_date (const Account *acc, time64 end_date, + std::function f); + /** scans account split list (in forward or reverse order) until * predicate split->bool returns true. Maybe return the split. *