Skip to content

Commit

Permalink
refactor: Add likely and unlikely attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
oboukli committed Feb 1, 2025
1 parent 57fc2f1 commit 31722e1
Show file tree
Hide file tree
Showing 23 changed files with 45 additions and 44 deletions.
6 changes: 3 additions & 3 deletions include/forfun/best_time_to_buy_and_sell_stock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ calc_max_profit(Iter iter, Sentinel const last) noexcept
{
using TypeValue = std::iter_value_t<Iter>;

if (iter == last)
if (iter == last) [[unlikely]]
{
return TypeValue{0};
}
Expand Down Expand Up @@ -59,7 +59,7 @@ calc_max_profit(Iter iter, Sentinel const last) noexcept
{
using TypeValue = std::iter_value_t<Iter>;

if (iter == last)
if (iter == last) [[unlikely]]
{
return TypeValue{0};
}
Expand Down Expand Up @@ -96,7 +96,7 @@ calc_max_profit(Iter iter, Sentinel const last) noexcept
{
using TypeValue = std::iter_value_t<Iter>;

if (iter == last)
if (iter == last) [[unlikely]]
{
return TypeValue{0};
}
Expand Down
6 changes: 3 additions & 3 deletions include/forfun/evaluate_reverse_polish_notation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ template <std::contiguous_iterator Iter, std::sentinel_for<Iter> Sentinel>
[[nodiscard]] auto eval_expression(Iter iter, Sentinel const end)
-> std::pair<int, std::errc>
{
if (iter == end)
if (iter == end) [[unlikely]]
{
return {0, std::errc{}};
}
Expand Down Expand Up @@ -76,7 +76,7 @@ template <std::contiguous_iterator Iter, std::sentinel_for<Iter> 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;
Expand Down Expand Up @@ -109,7 +109,7 @@ template <std::contiguous_iterator Iter, std::sentinel_for<Iter> Sentinel>
[[nodiscard]] auto eval_expression(Iter iter, Sentinel const end)
-> std::pair<int, std::errc>
{
if (iter == end)
if (iter == end) [[unlikely]]
{
return {0, std::errc{}};
}
Expand Down
2 changes: 1 addition & 1 deletion include/forfun/factorial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ template <common::concepts::addition_unpromoted T>
{
assert(n >= T{0});

if (n <= T{1})
if (n <= T{1}) [[unlikely]]
{
return T{1};
}
Expand Down
4 changes: 2 additions & 2 deletions include/forfun/fibonacci.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ namespace recursive {
template <common::concepts::addition_unpromoted T>
[[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};
}
Expand Down
2 changes: 1 addition & 1 deletion include/forfun/fizzbuzz.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion include/forfun/gcd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions include/forfun/last_stone_weight.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ last_stone_weight(Iter const first, Sentinel last) noexcept
{
using ValueType = std::iter_value_t<Iter>;

if (first == last)
if (first == last) [[unlikely]]
{
return ValueType{0};
}
Expand Down Expand Up @@ -136,7 +136,7 @@ template <std::contiguous_iterator Iter, std::sentinel_for<Iter> Sentinel>
last_stone_weight(Iter const first, Sentinel last) noexcept
-> std::iter_value_t<Iter>
{
if (first == last)
if (first == last) [[unlikely]]
{
return 0;
}
Expand Down
8 changes: 4 additions & 4 deletions include/forfun/maximum_subarray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ max_sum(Iter const first, Sentinel const end) noexcept
{
using T = PromotedValueType<std::iter_value_t<Iter>>;

if (first == end)
if (first == end) [[unlikely]]
{
return T{0};
}
Expand Down Expand Up @@ -67,7 +67,7 @@ template <std::forward_iterator Iter, std::sentinel_for<Iter> Sentinel>
PromotedValueType<std::iter_value_t<Iter>> sum
) noexcept -> PromotedValueType<std::iter_value_t<Iter>>
{
if (iter == end)
if (iter == end) [[unlikely]]
{
return sum;
}
Expand All @@ -87,7 +87,7 @@ template <std::forward_iterator Iter, std::sentinel_for<Iter> Sentinel>
{
using T = PromotedValueType<std::iter_value_t<Iter>>;

if (iter == end)
if (iter == end) [[unlikely]]
{
return T{0};
}
Expand All @@ -106,7 +106,7 @@ template <std::forward_iterator Iter, std::sentinel_for<Iter> Sentinel>
{
using T = PromotedValueType<std::iter_value_t<Iter>>;

if (iter == last)
if (iter == last) [[unlikely]]
{
return T{0};
}
Expand Down
6 changes: 3 additions & 3 deletions include/forfun/move_zeroes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions include/forfun/primality.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ template <std::unsigned_integral UInteger>
is_prime(UInteger const n) noexcept(noexcept(std::sqrt(std::declval<UInteger>())
)) -> bool
{
if (n < UInteger{2U})
if (n < UInteger{2U}) [[unlikely]]
{
return false;
}

if (n == UInteger{2U})
if (n == UInteger{2U}) [[unlikely]]
{
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion include/forfun/product_except_self.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ constexpr auto product_except_self(
using ValType = std::iter_value_t<OutItr>;
using DiffType = std::iter_difference_t<InItr>;

if (first == last)
if (first == last) [[unlikely]]
{
return;
}
Expand Down
4 changes: 2 additions & 2 deletions include/forfun/sorting/bubble_sort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ template <std::contiguous_iterator Iter, std::sized_sentinel_for<Iter> Sentinel>
requires std::sortable<Iter>
constexpr auto bubble_sort(Iter const begin, Sentinel end) noexcept -> void
{
if (begin == end)
if (begin == end) [[unlikely]]
{
return;
}
Expand Down Expand Up @@ -58,7 +58,7 @@ constexpr auto bubble_sort(Iter const begin, Sentinel end) noexcept -> void
{
using DiffType = std::iter_difference_t<Iter>;

if (begin == end)
if (begin == end) [[unlikely]]
{
return;
}
Expand Down
2 changes: 1 addition & 1 deletion include/forfun/sorting/insertion_sort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ constexpr auto insertion_sort(Iter const begin, Sentinel const end) noexcept
{
using DiffType = std::iter_difference_t<Iter>;

if (begin != end)
if (begin != end) [[likely]]
{
for (Iter it_i{begin + DiffType{1}}; it_i != end; ++it_i)
{
Expand Down
2 changes: 1 addition & 1 deletion include/forfun/sub_array_sums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions include/forfun/top_k_frequent_elements.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ top_frequent(Iter const first, Sentinel const end, std::size_t k)
{
using ValType = std::iter_value_t<Iter>;

if (first == end)
if (first == end) [[unlikely]]
{
return {};
}
Expand Down Expand Up @@ -107,7 +107,7 @@ top_frequent(Iter const first, Sentinel const end, std::size_t k)
{
using ValType = std::iter_value_t<Iter>;

if (first == end)
if (first == end) [[unlikely]]
{
return {};
}
Expand Down
2 changes: 1 addition & 1 deletion include/forfun/tower_of_hanoi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion include/forfun/trie.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ auto insert(T& root, StringViewT<T> const& word) -> void
using LenT = StringViewT<T>::size_type;

auto const word_len{word.length()};
if (word_len == LenT{0U})
if (word_len == LenT{0U}) [[unlikely]]
{
return;
}
Expand Down
6 changes: 3 additions & 3 deletions src/forfun/container/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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{});
Expand All @@ -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_);
Expand Down
8 changes: 4 additions & 4 deletions src/forfun/graph/balanced_binary_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace {
auto is_unbalanced_internal(binary_tree_node const* const head) noexcept
-> std::pair<std::size_t, bool>
{
if (head == nullptr)
if (head == nullptr) [[unlikely]]
{
return {std::size_t{0U}, false};
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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};
}
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/forfun/graph/subsets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ auto do_explode_subsets(
Container& subsets
) -> void
{
if (iter == last)
if (iter == last) [[unlikely]]
{
subsets.emplace_back(subset);
return;
Expand Down
7 changes: 4 additions & 3 deletions src/forfun/set_matrix_zeroes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ auto set_zeroes(std::vector<std::vector<int>>& matrix) -> void
{
using DiffC = std::vector<std::vector<int>>::value_type::difference_type;

if ((matrix.begin() == matrix.end()) || matrix.front().empty())
if ((matrix.begin() == matrix.end()) || matrix.front().empty()) [[unlikely]]
{
return;
}
Expand Down Expand Up @@ -98,6 +98,7 @@ auto set_zeroes(std::vector<std::vector<int>>& matrix) noexcept -> void
using DiffC = std::vector<std::vector<int>>::value_type::difference_type;

if ((matrix.begin() == matrix.end()) || matrix.begin()->empty())
[[unlikely]]
{
return;
}
Expand Down Expand Up @@ -175,14 +176,14 @@ auto set_zeroes(std::vector<std::vector<int>>& 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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/forfun/sonar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/forfun/valid_anagram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 31722e1

Please sign in to comment.