From e9c51a4a2d79a53125655a0dbd453a50e3e86421 Mon Sep 17 00:00:00 2001 From: Omar Boukli-Hacene Date: Tue, 24 Dec 2024 22:10:18 +0200 Subject: [PATCH] refactor: Improve exception guarantee --- include/forfun/top_k_frequent_elements.hpp | 34 +++++++++++----------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/include/forfun/top_k_frequent_elements.hpp b/include/forfun/top_k_frequent_elements.hpp index 2ee579d..9cccb03 100644 --- a/include/forfun/top_k_frequent_elements.hpp +++ b/include/forfun/top_k_frequent_elements.hpp @@ -32,7 +32,7 @@ namespace bucket_sort_based { template Sentinel> requires std::integral> [[nodiscard]] auto -top_frequent(Iter const first, Sentinel const end, std::size_t k) noexcept +top_frequent(Iter const first, Sentinel const end, std::size_t k) -> std::vector> { using ValType = std::iter_value_t; @@ -42,14 +42,14 @@ top_frequent(Iter const first, Sentinel const end, std::size_t k) noexcept return {}; } - std::sort(first, end); - std::size_t const size{static_cast(end - first)}; std::vector> counts(size); k = std::min(k, size); + std::sort(first, end); + ValType current{*first}; std::size_t current_count{0U}; std::size_t counts_size{1U}; @@ -102,7 +102,7 @@ namespace bucket_sort_based_functional { template Sentinel> requires std::integral> [[nodiscard]] auto -top_frequent(Iter const first, Sentinel const end, std::size_t k) noexcept +top_frequent(Iter const first, Sentinel const end, std::size_t k) -> std::vector> { using ValType = std::iter_value_t; @@ -112,12 +112,12 @@ top_frequent(Iter const first, Sentinel const end, std::size_t k) noexcept return {}; } - std::sort(first, end); - std::vector> counts( static_cast(end - first) ); + std::sort(first, end); + ValType current{*first}; std::size_t current_count{0U}; std::size_t counts_size{1U}; @@ -156,8 +156,7 @@ namespace priority_queue_based { /// Invalid input may result in undefined behavior. template Sentinel> requires std::integral> -[[nodiscard]] auto -top_frequent(Iter iter, Sentinel const end, std::size_t k) noexcept +[[nodiscard]] auto top_frequent(Iter iter, Sentinel const end, std::size_t k) -> std::vector> { using ValType = std::iter_value_t; @@ -175,8 +174,9 @@ top_frequent(Iter iter, Sentinel const end, std::size_t k) noexcept ++counts.try_emplace(*iter, std::size_t{0U}).first->second; } - auto const comparator - = [](auto const& a, auto const& b) { return a.second < b.second; }; + auto const comparator = [](auto const& a, auto const& b) noexcept { + return a.second < b.second; + }; using T = std::pair; std::priority_queue, decltype(comparator)> intermediate( comparator @@ -205,8 +205,7 @@ namespace priority_queue_based_functional { /// Invalid input may result in undefined behavior. template Sentinel> requires std::integral> -[[nodiscard]] auto -top_frequent(Iter iter, Sentinel const end, std::size_t k) noexcept +[[nodiscard]] auto top_frequent(Iter iter, Sentinel const end, std::size_t k) -> std::vector> { using ValType = std::iter_value_t; @@ -252,8 +251,7 @@ namespace unordered_map_based { /// Assume input is valid and guarantees having a unique solution. /// Invalid input may result in undefined behavior. template Sentinel> -[[nodiscard]] auto -top_frequent(Iter iter, Sentinel const end, std::size_t k) noexcept +[[nodiscard]] auto top_frequent(Iter iter, Sentinel const end, std::size_t k) -> std::vector> { using ValType = std::iter_value_t; @@ -279,7 +277,9 @@ top_frequent(Iter iter, Sentinel const end, std::size_t k) noexcept std::sort( intermediate.begin(), intermediate.end(), - [](auto const& a, auto const& b) { return a.second > b.second; } + [](auto const& a, auto const& b) noexcept { + return a.second > b.second; + } ); std::vector result; @@ -289,7 +289,7 @@ top_frequent(Iter iter, Sentinel const end, std::size_t k) noexcept intermediate.cbegin() + static_cast(k), std::back_inserter(result), - [](auto const& bucket) { return bucket.first; } + [](auto const& bucket) noexcept { return bucket.first; } ); return result; @@ -303,7 +303,7 @@ namespace unordered_map_based_functional { /// Invalid input may result in undefined behavior. template Sentinel> [[nodiscard]] auto -top_frequent(Iter iter, Sentinel const end, std::size_t const k) noexcept +top_frequent(Iter iter, Sentinel const end, std::size_t const k) -> std::vector> { using ValType = std::iter_value_t;