Skip to content

Commit

Permalink
[account.cpp] add gnc_account_foreach_until_date
Browse files Browse the repository at this point in the history
- uses binary search to find first split after date
- for_each from earliest split to (but excluding) the above first split
  • Loading branch information
christopherlam committed May 2, 2024
1 parent 2c6f150 commit 87dbbf2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
16 changes: 16 additions & 0 deletions libgnucash/engine/Account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};

This comment has been minimized.

Copy link
@christopherlam

christopherlam Jun 17, 2024

Author Contributor

@jralls is this creating a vector copy? Should it be the following?

auto& splits{GET_PRIVATE(acc)->splits};

This comment has been minimized.

Copy link
@jralls

jralls Jun 18, 2024

Member

Yup.

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)
Expand Down
3 changes: 3 additions & 0 deletions libgnucash/engine/Account.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 87dbbf2

Please sign in to comment.