From 73575f8d88dfc95198ae4a5bd31a2f7f0c74251b Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Fri, 23 Apr 2021 02:16:05 -0700 Subject: [PATCH] convert manual loops to std::count_if Simpler and generates less assembly. Signed-off-by: Rosen Penev --- src/commands/CmdCount.cpp | 6 +----- src/commands/CmdStats.cpp | 10 ++-------- src/feedback.cpp | 6 +----- 3 files changed, 4 insertions(+), 18 deletions(-) diff --git a/src/commands/CmdCount.cpp b/src/commands/CmdCount.cpp index fbdb1d090..850a4f533 100644 --- a/src/commands/CmdCount.cpp +++ b/src/commands/CmdCount.cpp @@ -57,11 +57,7 @@ int CmdCount::execute (std::string& output) filter.subset (filtered); // Find number of matching tasks. Skip recurring parent tasks. - int count = 0; - for (const auto& task : filtered) - if (task.getStatus () != Task::recurring) - ++count; - + int count = std::count_if(filtered.begin(), filtered.end(), [](const auto& task){ return task.getStatus () != Task::recurring; }); output = format (count) + '\n'; return 0; } diff --git a/src/commands/CmdStats.cpp b/src/commands/CmdStats.cpp index 520b141ce..64d04f1f7 100644 --- a/src/commands/CmdStats.cpp +++ b/src/commands/CmdStats.cpp @@ -70,17 +70,11 @@ int CmdStats::execute (std::string& output) // Count the undo transactions. std::vector undoTxns = Context::getContext ().tdb2.undo.get_lines (); - int undoCount = 0; - for (auto& tx : undoTxns) - if (tx == "---") - ++undoCount; + int undoCount = std::count(undoTxns.begin(), undoTxns.end(), "---"); // Count the backlog transactions. std::vector backlogTxns = Context::getContext ().tdb2.backlog.get_lines (); - int backlogCount = 0; - for (auto& tx : backlogTxns) - if (tx[0] == '{') - ++backlogCount; + int backlogCount = std::count_if(backlogTxns.begin(), backlogTxns.end(), [](const auto& tx){ return tx.front() == '{'; }); // Get all the tasks. Filter filter; diff --git a/src/feedback.cpp b/src/feedback.cpp index 2ed56e3ce..7cb567ddf 100644 --- a/src/feedback.cpp +++ b/src/feedback.cpp @@ -406,12 +406,8 @@ void feedback_backlog () if (Context::getContext ().config.get ("taskd.server") != "" && Context::getContext ().verbose ("sync")) { - int count = 0; std::vector lines = Context::getContext ().tdb2.backlog.get_lines (); - for (auto& line : lines) - if ((line)[0] == '{') - ++count; - + int count = std::count_if(lines.begin(), lines.end(), [](const auto& line){ return line.front() == '{'; }); if (count) Context::getContext ().footnote (format (count > 1 ? "There are {1} local changes. Sync required." : "There is {1} local change. Sync required.", count));