From 67abbc833fa16c28da1c12f4ed7f0d0ae3bb43da Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 2 Aug 2025 12:49:03 -1000 Subject: [PATCH] flex --- esphome/core/application.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/esphome/core/application.cpp b/esphome/core/application.cpp index e5103802db..f7f9dce2dd 100644 --- a/esphome/core/application.cpp +++ b/esphome/core/application.cpp @@ -39,36 +39,36 @@ static const char *const TAG = "app"; // by avoiding template instantiations (std::rotate, std::stable_sort, lambdas) // IMPORTANT: This sort is stable (preserves relative order of equal elements), // which is necessary to maintain user-defined component order for same priority -static void insertion_sort_by_setup_priority(Component **components, size_t size) { - for (size_t i = 1; i < size; i++) { - Component *key = components[i]; +template static void insertion_sort_by_setup_priority(Iterator first, Iterator last) { + for (auto it = first + 1; it != last; ++it) { + auto key = *it; float key_priority = key->get_actual_setup_priority(); - int32_t j = i - 1; + auto j = it - 1; // Using '<' (not '<=') ensures stability - equal priority components keep their order - while (j >= 0 && components[j]->get_actual_setup_priority() < key_priority) { - components[j + 1] = components[j]; + while (j >= first && (*j)->get_actual_setup_priority() < key_priority) { + *(j + 1) = *j; j--; } - components[j + 1] = key; + *(j + 1) = key; } } // Helper function for insertion sort of components by loop priority // IMPORTANT: This sort is stable (preserves relative order of equal elements), // which is required when components are re-sorted during setup() if they block -static void insertion_sort_by_loop_priority(Component **components, size_t size) { - for (size_t i = 1; i < size; i++) { - Component *key = components[i]; +template static void insertion_sort_by_loop_priority(Iterator first, Iterator last) { + for (auto it = first + 1; it != last; ++it) { + auto key = *it; float key_priority = key->get_loop_priority(); - int32_t j = i - 1; + auto j = it - 1; // Using '<' (not '<=') ensures stability - equal priority components keep their order - while (j >= 0 && components[j]->get_loop_priority() < key_priority) { - components[j + 1] = components[j]; + while (j >= first && (*j)->get_loop_priority() < key_priority) { + *(j + 1) = *j; j--; } - components[j + 1] = key; + *(j + 1) = key; } } @@ -91,7 +91,7 @@ void Application::setup() { ESP_LOGV(TAG, "Sorting components by setup priority"); // Sort by setup priority using our helper function - insertion_sort_by_setup_priority(this->components_.data(), this->components_.size()); + insertion_sort_by_setup_priority(this->components_.begin(), this->components_.end()); // Initialize looping_components_ early so enable_pending_loops_() works during setup this->calculate_looping_components_(); @@ -108,7 +108,7 @@ void Application::setup() { continue; // Sort components 0 through i by loop priority - insertion_sort_by_loop_priority(this->components_.data(), i + 1); + insertion_sort_by_loop_priority(this->components_.begin(), this->components_.begin() + i + 1); do { uint8_t new_app_state = STATUS_LED_WARNING;