diff options
author | John Wiegley <johnw@newartisans.com> | 2019-01-30 11:09:08 -0800 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2019-01-30 11:09:08 -0800 |
commit | 557ab32d30bcca3622e06a6809a541a015e84aa3 (patch) | |
tree | 0ce7f56328fc86b6705966a52e89f75c3d40cbc1 /src | |
parent | 2c61d41deaab0343f29b424dbcd1a751928b3056 (diff) | |
download | fork-ledger-557ab32d30bcca3622e06a6809a541a015e84aa3.tar.gz fork-ledger-557ab32d30bcca3622e06a6809a541a015e84aa3.tar.bz2 fork-ledger-557ab32d30bcca3622e06a6809a541a015e84aa3.zip |
Expose a new utility function for balances: sorted_amounts
Diffstat (limited to 'src')
-rw-r--r-- | src/balance.cc | 18 | ||||
-rw-r--r-- | src/balance.h | 7 |
2 files changed, 16 insertions, 9 deletions
diff --git a/src/balance.cc b/src/balance.cc index fa1bc20c..478ceb45 100644 --- a/src/balance.cc +++ b/src/balance.cc @@ -240,6 +240,14 @@ balance_t::strip_annotations(const keep_details_t& what_to_keep) const return temp; } +void balance_t::sorted_amounts(amounts_array& sorted) const +{ + foreach (const amounts_map::value_type& pair, amounts) + sorted.push_back(&pair.second); + std::stable_sort(sorted.begin(), sorted.end(), + commodity_t::compare_by_commodity()); +} + void balance_t::map_sorted_amounts(function<void(const amount_t&)> fn) const { if (! amounts.empty()) { @@ -249,16 +257,8 @@ void balance_t::map_sorted_amounts(function<void(const amount_t&)> fn) const fn(amount); } else { - typedef std::vector<const amount_t *> amounts_array; amounts_array sorted; - - foreach (const amounts_map::value_type& pair, amounts) - if (pair.second) - sorted.push_back(&pair.second); - - std::stable_sort(sorted.begin(), sorted.end(), - commodity_t::compare_by_commodity()); - + sorted_amounts(sorted); foreach (const amount_t * amount, sorted) fn(*amount); } diff --git a/src/balance.h b/src/balance.h index 8e773fc5..b9c9c2c8 100644 --- a/src/balance.h +++ b/src/balance.h @@ -81,6 +81,7 @@ class balance_t { public: typedef std::map<commodity_t *, amount_t> amounts_map; + typedef std::vector<const amount_t *> amounts_array; amounts_map amounts; @@ -529,6 +530,12 @@ public: balance_t strip_annotations(const keep_details_t& what_to_keep) const; /** + * Given a balance, insert a commodity-wise sort of the amounts into the + * given amounts_array. + */ + void sorted_amounts(amounts_array& sorted) const; + + /** * Iteration primitives. `map_sorted_amounts' allows one to visit * each amount in balance in the proper order for displaying to the * user. Mostly used by `print' and other routinse where the sort |