Skip to content

Commit

Permalink
Add palindromic number non-STL implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
oboukli committed Sep 5, 2023
1 parent 72e7647 commit 43de941
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ add_executable(
"benchmark/first_missing_positive_benchmark.cpp"
"benchmark/lru_cache_benchmark.cpp"
"benchmark/palindrome_benchmark.cpp"
"benchmark/palindromic_number_benchmark.cpp"
)
target_link_libraries(
benchmark
Expand Down
42 changes: 42 additions & 0 deletions benchmark/palindromic_number_benchmark.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) Omar Boukli-Hacene. All rights reserved.
// Distributed under an MIT-style license that can be
// found in the LICENSE file.

// SPDX-License-Identifier: MIT

#include <type_traits>

#include <catch2/catch_test_macros.hpp>

#include <nanobench.h>

#include <nameof.hpp>

#include "forfun/palindromic_number.hpp"

inline constexpr int const p{1234554321};

TEST_CASE(
"palindromic_number benchmarking", "[benchmark][palindromic_number]") {
static_assert(p >= 0 && p <= std::numeric_limits<decltype(p)>::max());

ankerl::nanobench::Bench()

.title("Palindromic number")

.run(
NAMEOF_RAW(forfun::palindromic_number::fast::is_palindrome).c_str(),
[]() {
auto r{forfun::palindromic_number::fast::is_palindrome(p)};
ankerl::nanobench::doNotOptimizeAway(r);
})

.run(
NAMEOF_RAW(forfun::palindromic_number::stl::is_palindrome).c_str(),
[]() {
auto r{forfun::palindromic_number::stl::is_palindrome(p)};
ankerl::nanobench::doNotOptimizeAway(r);
})

;
}
23 changes: 23 additions & 0 deletions include/forfun/palindromic_number.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,29 @@
#ifndef FORFUN_PALINDROMIC_NUMBER_HPP_
#define FORFUN_PALINDROMIC_NUMBER_HPP_

namespace forfun::palindromic_number {

namespace fast {

[[nodiscard]] constexpr bool is_palindrome(int const n) noexcept {
int nn{0};
int d{n};
while (d > 0) {
nn = (nn * 10) + d % 10;
d /= 10;
}

return n == nn;
}

} // namespace fast

namespace stl {

[[nodiscard]] bool is_palindrome(int const n) noexcept;

} // namespace stl

} // namespace forfun::palindromic_number

#endif // FORFUN_PALINDROMIC_NUMBER_HPP_
8 changes: 8 additions & 0 deletions src/palindromic_number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

#include <cstdlib>

namespace forfun::palindromic_number {

namespace stl {

[[nodiscard]] bool is_palindrome(int const n) noexcept {
int nn{0};
std::div_t d{.quot = n, .rem = 0};
Expand All @@ -18,3 +22,7 @@

return n == nn;
}

} // namespace stl

} // namespace forfun::palindromic_number
3 changes: 2 additions & 1 deletion test/palindromic_number_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

TEMPLATE_TEST_CASE_SIG(
"palindromic_number", "", ((auto sut), sut),
(is_palindrome)) {
(forfun::palindromic_number::fast::is_palindrome),
(forfun::palindromic_number::stl::is_palindrome)) {
SECTION("Palindromic numbers") {
GIVEN("a palindromic number") {
auto const [palindromic_number]{GENERATE(table<int>({
Expand Down

0 comments on commit 43de941

Please sign in to comment.