diff --git a/include/forfun/best_time_to_buy_and_sell_stock.hpp b/include/forfun/best_time_to_buy_and_sell_stock.hpp index e0e66e3..8e13125 100644 --- a/include/forfun/best_time_to_buy_and_sell_stock.hpp +++ b/include/forfun/best_time_to_buy_and_sell_stock.hpp @@ -24,7 +24,7 @@ calc_max_profit(Iter iter, Sentinel const last) noexcept { using TypeValue = std::iter_value_t; - if (iter == last) + if (iter == last) [[unlikely]] { return TypeValue{0}; } @@ -59,7 +59,7 @@ calc_max_profit(Iter iter, Sentinel const last) noexcept { using TypeValue = std::iter_value_t; - if (iter == last) + if (iter == last) [[unlikely]] { return TypeValue{0}; } @@ -96,7 +96,7 @@ calc_max_profit(Iter iter, Sentinel const last) noexcept { using TypeValue = std::iter_value_t; - if (iter == last) + if (iter == last) [[unlikely]] { return TypeValue{0}; } diff --git a/include/forfun/evaluate_reverse_polish_notation.hpp b/include/forfun/evaluate_reverse_polish_notation.hpp index fba72e2..353ff80 100644 --- a/include/forfun/evaluate_reverse_polish_notation.hpp +++ b/include/forfun/evaluate_reverse_polish_notation.hpp @@ -27,7 +27,7 @@ template Sentinel> [[nodiscard]] auto eval_expression(Iter iter, Sentinel const end) -> std::pair { - if (iter == end) + if (iter == end) [[unlikely]] { return {0, std::errc{}}; } @@ -76,7 +76,7 @@ template Sentinel> accumulator *= operand_2; break; case '/': - if (operand_2 == 0) + if (operand_2 == 0) [[unlikely]] { // NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto) goto invalid_argument; @@ -109,7 +109,7 @@ template Sentinel> [[nodiscard]] auto eval_expression(Iter iter, Sentinel const end) -> std::pair { - if (iter == end) + if (iter == end) [[unlikely]] { return {0, std::errc{}}; } diff --git a/include/forfun/factorial.hpp b/include/forfun/factorial.hpp index 2bed12c..9b4a66c 100644 --- a/include/forfun/factorial.hpp +++ b/include/forfun/factorial.hpp @@ -51,7 +51,7 @@ template { assert(n >= T{0}); - if (n <= T{1}) + if (n <= T{1}) [[unlikely]] { return T{1}; } diff --git a/include/forfun/fibonacci.hpp b/include/forfun/fibonacci.hpp index 8eefe55..cc5a628 100644 --- a/include/forfun/fibonacci.hpp +++ b/include/forfun/fibonacci.hpp @@ -38,9 +38,9 @@ namespace recursive { template [[nodiscard]] constexpr auto fib(T const n) noexcept -> T { - if (n <= T{2}) + if (n <= T{2}) [[unlikely]] { - if (n < T{1}) + if (n < T{1}) [[unlikely]] { return T{0}; } diff --git a/include/forfun/fizzbuzz.hpp b/include/forfun/fizzbuzz.hpp index 01fefd2..ee29ee2 100644 --- a/include/forfun/fizzbuzz.hpp +++ b/include/forfun/fizzbuzz.hpp @@ -68,7 +68,7 @@ fizzbuzz(int start, int const last, BinaryFunc write) noexcept(noexcept(write)) { write(buzz.data(), buzz.size()); } - else if (is_numeric) + else if (is_numeric) [[likely]] { // Create char buffer sufficient for int's maximum number of decimal // digits and one sign. diff --git a/include/forfun/gcd.hpp b/include/forfun/gcd.hpp index fd3b82b..6387fa6 100644 --- a/include/forfun/gcd.hpp +++ b/include/forfun/gcd.hpp @@ -18,7 +18,7 @@ namespace detail { [[nodiscard]] constexpr auto gcd_imp(int const m, int const n) noexcept -> int { - if (n == 0) + if (n == 0) [[unlikely]] { return m; } diff --git a/include/forfun/last_stone_weight.hpp b/include/forfun/last_stone_weight.hpp index 9b05e02..796f6b4 100644 --- a/include/forfun/last_stone_weight.hpp +++ b/include/forfun/last_stone_weight.hpp @@ -30,7 +30,7 @@ last_stone_weight(Iter const first, Sentinel last) noexcept { using ValueType = std::iter_value_t; - if (first == last) + if (first == last) [[unlikely]] { return ValueType{0}; } @@ -136,7 +136,7 @@ template Sentinel> last_stone_weight(Iter const first, Sentinel last) noexcept -> std::iter_value_t { - if (first == last) + if (first == last) [[unlikely]] { return 0; } diff --git a/include/forfun/maximum_subarray.hpp b/include/forfun/maximum_subarray.hpp index d55a19a..3784f30 100644 --- a/include/forfun/maximum_subarray.hpp +++ b/include/forfun/maximum_subarray.hpp @@ -31,7 +31,7 @@ max_sum(Iter const first, Sentinel const end) noexcept { using T = PromotedValueType>; - if (first == end) + if (first == end) [[unlikely]] { return T{0}; } @@ -67,7 +67,7 @@ template Sentinel> PromotedValueType> sum ) noexcept -> PromotedValueType> { - if (iter == end) + if (iter == end) [[unlikely]] { return sum; } @@ -87,7 +87,7 @@ template Sentinel> { using T = PromotedValueType>; - if (iter == end) + if (iter == end) [[unlikely]] { return T{0}; } @@ -106,7 +106,7 @@ template Sentinel> { using T = PromotedValueType>; - if (iter == last) + if (iter == last) [[unlikely]] { return T{0}; } diff --git a/include/forfun/move_zeroes.hpp b/include/forfun/move_zeroes.hpp index fe0b830..843a4b4 100644 --- a/include/forfun/move_zeroes.hpp +++ b/include/forfun/move_zeroes.hpp @@ -27,7 +27,7 @@ constexpr auto move_zeroes(Itr itr, Sentinel const end) noexcept -> void auto itr_j{itr}; for (; itr != end; ++itr) { - if (*itr != ValType{0}) + if (*itr != ValType{0}) [[likely]] { std::iter_swap(itr, itr_j); ++itr_j; @@ -48,7 +48,7 @@ constexpr auto move_zeroes(Itr itr, Sentinel const end) noexcept -> void auto itr_j{itr}; for (; itr != end; ++itr) { - if (*itr != ValType{0}) + if (*itr != ValType{0}) [[likely]] { *itr_j = *itr; ++itr_j; @@ -74,7 +74,7 @@ constexpr auto move_zeroes(Itr itr, Sentinel const end) noexcept -> void auto itr_j{itr}; for (; itr != end; ++itr) { - if (*itr != ValType{0}) + if (*itr != ValType{0}) [[likely]] { *itr_j = *itr; ++itr_j; diff --git a/include/forfun/primality.hpp b/include/forfun/primality.hpp index 4cb9f7f..ee60709 100644 --- a/include/forfun/primality.hpp +++ b/include/forfun/primality.hpp @@ -20,12 +20,12 @@ template is_prime(UInteger const n) noexcept(noexcept(std::sqrt(std::declval()) )) -> bool { - if (n < UInteger{2U}) + if (n < UInteger{2U}) [[unlikely]] { return false; } - if (n == UInteger{2U}) + if (n == UInteger{2U}) [[unlikely]] { return true; } diff --git a/include/forfun/product_except_self.hpp b/include/forfun/product_except_self.hpp index d0fde77..fa92959 100644 --- a/include/forfun/product_except_self.hpp +++ b/include/forfun/product_except_self.hpp @@ -100,7 +100,7 @@ constexpr auto product_except_self( using ValType = std::iter_value_t; using DiffType = std::iter_difference_t; - if (first == last) + if (first == last) [[unlikely]] { return; } diff --git a/include/forfun/sorting/bubble_sort.hpp b/include/forfun/sorting/bubble_sort.hpp index 128f0d5..d506f9f 100644 --- a/include/forfun/sorting/bubble_sort.hpp +++ b/include/forfun/sorting/bubble_sort.hpp @@ -21,7 +21,7 @@ template Sentinel> requires std::sortable constexpr auto bubble_sort(Iter const begin, Sentinel end) noexcept -> void { - if (begin == end) + if (begin == end) [[unlikely]] { return; } @@ -58,7 +58,7 @@ constexpr auto bubble_sort(Iter const begin, Sentinel end) noexcept -> void { using DiffType = std::iter_difference_t; - if (begin == end) + if (begin == end) [[unlikely]] { return; } diff --git a/include/forfun/sorting/insertion_sort.hpp b/include/forfun/sorting/insertion_sort.hpp index 3da476d..52a0965 100644 --- a/include/forfun/sorting/insertion_sort.hpp +++ b/include/forfun/sorting/insertion_sort.hpp @@ -22,7 +22,7 @@ constexpr auto insertion_sort(Iter const begin, Sentinel const end) noexcept { using DiffType = std::iter_difference_t; - if (begin != end) + if (begin != end) [[likely]] { for (Iter it_i{begin + DiffType{1}}; it_i != end; ++it_i) { diff --git a/include/forfun/sub_array_sums.hpp b/include/forfun/sub_array_sums.hpp index 582cbfb..b373c66 100644 --- a/include/forfun/sub_array_sums.hpp +++ b/include/forfun/sub_array_sums.hpp @@ -39,7 +39,7 @@ constexpr auto sum_each( auto const sums_size{sums.size()}; - if (sums_size == SizeType{0U}) + if (sums_size == SizeType{0U}) [[unlikely]] { return; } diff --git a/include/forfun/top_k_frequent_elements.hpp b/include/forfun/top_k_frequent_elements.hpp index 9cccb03..993a0a2 100644 --- a/include/forfun/top_k_frequent_elements.hpp +++ b/include/forfun/top_k_frequent_elements.hpp @@ -37,7 +37,7 @@ top_frequent(Iter const first, Sentinel const end, std::size_t k) { using ValType = std::iter_value_t; - if (first == end) + if (first == end) [[unlikely]] { return {}; } @@ -107,7 +107,7 @@ top_frequent(Iter const first, Sentinel const end, std::size_t k) { using ValType = std::iter_value_t; - if (first == end) + if (first == end) [[unlikely]] { return {}; } diff --git a/include/forfun/tower_of_hanoi.hpp b/include/forfun/tower_of_hanoi.hpp index d363c3c..0b3f114 100644 --- a/include/forfun/tower_of_hanoi.hpp +++ b/include/forfun/tower_of_hanoi.hpp @@ -19,7 +19,7 @@ constexpr auto toh(Rod& src, Rod& des, Rod& aux, Monk monk, std::integral auto num_moves ) noexcept(noexcept(monk(src, des))) -> void { - if (num_moves == decltype(num_moves){0}) + if (num_moves == decltype(num_moves){0}) [[unlikely]] { return; } diff --git a/include/forfun/trie.hpp b/include/forfun/trie.hpp index 83dc501..1ebf9dd 100644 --- a/include/forfun/trie.hpp +++ b/include/forfun/trie.hpp @@ -53,7 +53,7 @@ auto insert(T& root, StringViewT const& word) -> void using LenT = StringViewT::size_type; auto const word_len{word.length()}; - if (word_len == LenT{0U}) + if (word_len == LenT{0U}) [[unlikely]] { return; } diff --git a/src/forfun/container/list.cpp b/src/forfun/container/list.cpp index 1773b11..c8b0e3a 100644 --- a/src/forfun/container/list.cpp +++ b/src/forfun/container/list.cpp @@ -41,7 +41,7 @@ auto list::push_back(list::value_type value) noexcept(false) -> void new internal::list_node(value, tail_, end_) }; - if (head_ == end_) + if (head_ == end_) [[unlikely]] { head_ = n; } @@ -56,7 +56,7 @@ auto list::push_back(list::value_type value) noexcept(false) -> void auto list::pop_back() noexcept -> void { - if (tail_ == end_) + if (tail_ == end_) [[unlikely]] { assert(head_ == end_); assert(size_ == size_type{}); @@ -67,7 +67,7 @@ auto list::pop_back() noexcept -> void assert(size_ > size_type{}); assert(head_ != end_); - if (tail_->previous_ == end_) + if (tail_->previous_ == end_) [[unlikely]] { assert(size_ == size_type{1}); assert(head_ == tail_); diff --git a/src/forfun/graph/balanced_binary_tree.cpp b/src/forfun/graph/balanced_binary_tree.cpp index 67aea6b..4a38dc4 100644 --- a/src/forfun/graph/balanced_binary_tree.cpp +++ b/src/forfun/graph/balanced_binary_tree.cpp @@ -19,7 +19,7 @@ namespace { auto is_unbalanced_internal(binary_tree_node const* const head) noexcept -> std::pair { - if (head == nullptr) + if (head == nullptr) [[unlikely]] { return {std::size_t{0U}, false}; } @@ -68,7 +68,7 @@ namespace { auto measure_depth_internal(binary_tree_node const* const head) noexcept -> int { - if (head == nullptr) + if (head == nullptr) [[unlikely]] { return 0; } @@ -112,7 +112,7 @@ namespace { auto measure_depth_internal(binary_tree_node const* const head) noexcept -> std::size_t { - if (head == nullptr) + if (head == nullptr) [[unlikely]] { return std::size_t{0U}; } @@ -130,7 +130,7 @@ auto measure_depth_internal(binary_tree_node const* const head) noexcept [[nodiscard]] auto is_balanced(binary_tree_node const* const head) noexcept -> bool { - if (head == nullptr) + if (head == nullptr) [[unlikely]] { return true; } diff --git a/src/forfun/graph/subsets.cpp b/src/forfun/graph/subsets.cpp index c3abd5f..6b12a90 100644 --- a/src/forfun/graph/subsets.cpp +++ b/src/forfun/graph/subsets.cpp @@ -24,7 +24,7 @@ auto do_explode_subsets( Container& subsets ) -> void { - if (iter == last) + if (iter == last) [[unlikely]] { subsets.emplace_back(subset); return; diff --git a/src/forfun/set_matrix_zeroes.cpp b/src/forfun/set_matrix_zeroes.cpp index a4183df..79892ba 100644 --- a/src/forfun/set_matrix_zeroes.cpp +++ b/src/forfun/set_matrix_zeroes.cpp @@ -17,7 +17,7 @@ auto set_zeroes(std::vector>& matrix) -> void { using DiffC = std::vector>::value_type::difference_type; - if ((matrix.begin() == matrix.end()) || matrix.front().empty()) + if ((matrix.begin() == matrix.end()) || matrix.front().empty()) [[unlikely]] { return; } @@ -98,6 +98,7 @@ auto set_zeroes(std::vector>& matrix) noexcept -> void using DiffC = std::vector>::value_type::difference_type; if ((matrix.begin() == matrix.end()) || matrix.begin()->empty()) + [[unlikely]] { return; } @@ -175,14 +176,14 @@ auto set_zeroes(std::vector>& matrix) noexcept -> void auto const matrix_r_size{matrix.size()}; - if (matrix_r_size == SizeTypeR{0U}) + if (matrix_r_size == SizeTypeR{0U}) [[unlikely]] { return; } auto const matrix_c_size{matrix.begin()->size()}; - if (matrix_c_size == SizeTypeC{0U}) + if (matrix_c_size == SizeTypeC{0U}) [[unlikely]] { return; } diff --git a/src/forfun/sonar.cpp b/src/forfun/sonar.cpp index cc39288..aa19850 100644 --- a/src/forfun/sonar.cpp +++ b/src/forfun/sonar.cpp @@ -22,7 +22,7 @@ namespace forfun::sonar { int const width{std::abs(area.right - area.left)}; int const height{std::abs(area.bottom - area.top)}; - if ((width + height) == 0) + if ((width + height) == 0) [[unlikely]] { return 1; } diff --git a/src/forfun/valid_anagram.cpp b/src/forfun/valid_anagram.cpp index c0e21a5..05c684c 100644 --- a/src/forfun/valid_anagram.cpp +++ b/src/forfun/valid_anagram.cpp @@ -19,7 +19,7 @@ namespace forfun::valid_anagram::char_only { { using Iter = std::string_view::const_iterator; - if (s.length() != t.length()) + if (s.length() != t.length()) [[unlikely]] { return false; }