From 77a4ec707f010d9e8e73ba4a471ff7a67f860b76 Mon Sep 17 00:00:00 2001 From: Pascal Brunot Date: Fri, 28 Jun 2024 23:23:17 +0200 Subject: [PATCH] Tasks: simplify sorting --- src/Tasks.cpp | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/Tasks.cpp b/src/Tasks.cpp index 10937fba..6d083f26 100644 --- a/src/Tasks.cpp +++ b/src/Tasks.cpp @@ -80,24 +80,17 @@ namespace fabomatic::Tasks auto Scheduler::execute() const -> void { - // Tasks shall be run in order of expiration (the most expired task shall run first) - std::vector iters; - for (auto it = tasks.begin(); it != tasks.end(); ++it) - { - iters.push_back(it); // Vector of iterators - } + // Need a copy to mutate the order + std::vector mutableTasks(tasks.begin(), tasks.end()); - // Sort the iterators array as we cannot sort directly reference_wrappers - std::sort(iters.begin(), iters.end(), - [](const auto &it1, const auto &it2) - { - return (it1->get().getNextRun() < it2->get().getNextRun()); - }); + // Tasks shall be run in order of expiration (the most expired task shall run first) + std::sort(mutableTasks.begin(), mutableTasks.end(), [](const Task &a, const Task &b) + { return a.getNextRun() < b.getNextRun(); }); - // Now iterate over the sorted iterators to run the tasks - for (const auto &it : iters) + // Now iterate over the sorted tasks to run the tasks + for (const auto &it : mutableTasks) { - it->get().run(); + it.get().run(); } if (conf::debug::ENABLE_TASK_LOGS && millis() % 1024 == 0)