[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
This commit is contained in:
Christopher Lam 2024-04-24 13:44:54 +08:00
parent 2c6f15090a
commit 87dbbf25f8
2 changed files with 19 additions and 0 deletions

View File

@ -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)

View File

@ -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.
*