Skip to content

Commit

Permalink
Cleanup with ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
wichtounet committed Oct 10, 2023
1 parent 158c5dc commit 9237a37
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
1 change: 1 addition & 0 deletions include/accounts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ std::vector<budget::account> current_accounts(data_cache & cache);

budget::account get_account(size_t id);
budget::account get_account(std::string_view name, year year, month month);
std::string get_account_name(size_t id);

void set_accounts_changed();
void set_accounts_next_id(size_t next_id);
Expand Down
6 changes: 6 additions & 0 deletions include/views.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ namespace ranges = std::ranges;

namespace budget {

std::string get_account_name(size_t id);

// We define views without parameters as an adaptor to avoid having to use ()

namespace detail {
Expand Down Expand Up @@ -222,6 +224,10 @@ inline auto filter_by_account(size_t id) {
return std::views::filter([id] (const auto & expense) { return expense.account == id; });
}

inline auto filter_by_account_name(std::string_view name) {
return std::views::filter([name] (const auto & expense) { return get_account_name(expense.account) == name; });
}

inline auto filter_by_asset(size_t id) {
return std::views::filter([id] (const auto & share) { return share.asset_id == id; });
}
Expand Down
4 changes: 4 additions & 0 deletions src/accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@ budget::account budget::get_account(size_t id){
return accounts[id];
}

std::string budget::get_account_name(size_t id){
return accounts[id].name;
}

budget::account budget::get_account(std::string_view name, budget::year year, budget::month month) {
if (auto range = accounts.data() | active_at_date({year, month, 5}) | filter_by_name(name); range) {
return *std::ranges::begin(range);
Expand Down
14 changes: 4 additions & 10 deletions src/overview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,16 +571,10 @@ void display_values(budget::writer& w, budget::year year, const std::string& tit
for(auto& account : all_accounts(w.cache, year, m)){
budget::money month_total;

for(auto& value : values){
if(relaxed){
if(get_account(value.account).name == account.name && value.date.year() == year && value.date.month() == m){
month_total += value.amount;
}
} else {
if(value.account == account.id && value.date.year() == year && value.date.month() == m){
month_total += value.amount;
}
}
if (relaxed) {
month_total = fold_left_auto(values | filter_by_account_name(account.name) | filter_by_date(year, m) | to_amount);
} else {
month_total = fold_left_auto(values | filter_by_account(account.id) | filter_by_date(year, m) | to_amount);
}

contents[row_mapping[account.name]].push_back(to_string(month_total));
Expand Down

0 comments on commit 9237a37

Please sign in to comment.