Skip to content

Commit

Permalink
[engine.i] even faster gnc_get_match_commodity_splits
Browse files Browse the repository at this point in the history
gnc_get_match_commodity_splits needs to scan the account splitlists to
find suitable splits upto end_date. We can stop scanning at end_date
because the splitlists are sorted by date.
  • Loading branch information
christopherlam committed Apr 23, 2024
1 parent 9c18bc8 commit faab3c6
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions bindings/engine.i
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,20 @@ 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, use_end_date, end_date, comm](auto s) -> bool
{
if (xaccSplitGetReconcile (s) == VREC) return false;
auto trans{xaccSplitGetParent (s)};
if (use_end_date && xaccTransGetDate(trans) > end_date) return false;
if (use_end_date && xaccTransGetDate(trans) > end_date) return true;
auto txn_comm{xaccTransGetCurrency (trans)};
auto acc_comm{xaccAccountGetCommodity (xaccSplitGetAccount (s))};
return (txn_comm != acc_comm) && (!comm || comm == txn_comm || comm == acc_comm);
if ((txn_comm != acc_comm) && (!comm || comm == txn_comm || comm == acc_comm))
rv.push_back (const_cast<Split*>(s));
return false;
};
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);
auto scan_account = [&](auto acc){ gnc_account_find_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;
Expand Down

0 comments on commit faab3c6

Please sign in to comment.