From 9805abbf2b38d64308bac536eea40e544e8f7cfe Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Thu, 19 Feb 2009 16:53:25 -0400 Subject: Allow for sorting of the balance report Sorting is repeated at each level of the hierarchy, unless --flat was specified in which case it applies to the entire applicable accounts list. --- src/iterators.cc | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'src/iterators.cc') diff --git a/src/iterators.cc b/src/iterators.cc index 01f678d0..3a59c7aa 100644 --- a/src/iterators.cc +++ b/src/iterators.cc @@ -179,6 +179,33 @@ void sorted_accounts_iterator::sort_accounts(account_t& account, compare_items(sort_cmp)); } +void sorted_accounts_iterator::push_all(account_t& account) +{ + accounts_deque_t& deque(accounts_list.back()); + + foreach (accounts_map::value_type& pair, account.accounts) { + deque.push_back(pair.second); + push_all(*pair.second); + } +} + +void sorted_accounts_iterator::push_back(account_t& account) +{ + accounts_list.push_back(accounts_deque_t()); + + if (flatten_all) { + push_all(account); + std::stable_sort(accounts_list.back().begin(), + accounts_list.back().end(), + compare_items(sort_cmp)); + } else { + sort_accounts(account, accounts_list.back()); + } + + sorted_accounts_i.push_back(accounts_list.back().begin()); + sorted_accounts_end.push_back(accounts_list.back().end()); +} + account_t * sorted_accounts_iterator::operator()() { while (! sorted_accounts_i.empty() && @@ -195,9 +222,10 @@ account_t * sorted_accounts_iterator::operator()() assert(account); // If this account has children, queue them up to be iterated next. - if (! account->accounts.empty()) + if (! flatten_all && ! account->accounts.empty()) push_back(*account); + // Make sure the sorting value gets recalculated for this account account->xdata().drop_flags(ACCOUNT_EXT_SORT_CALC); return account; } -- cgit v1.2.3