From 43de941f2f0bef5a883d6bc4f66902fe9d4b23d2 Mon Sep 17 00:00:00 2001 From: Omar Boukli-Hacene Date: Tue, 5 Sep 2023 13:21:47 +0200 Subject: [PATCH] Add palindromic number non-STL implementation --- CMakeLists.txt | 1 + benchmark/palindromic_number_benchmark.cpp | 42 ++++++++++++++++++++++ include/forfun/palindromic_number.hpp | 23 ++++++++++++ src/palindromic_number.cpp | 8 +++++ test/palindromic_number_test.cpp | 3 +- 5 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 benchmark/palindromic_number_benchmark.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0db0352..1213412 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/benchmark/palindromic_number_benchmark.cpp b/benchmark/palindromic_number_benchmark.cpp new file mode 100644 index 0000000..053993f --- /dev/null +++ b/benchmark/palindromic_number_benchmark.cpp @@ -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 + +#include + +#include + +#include + +#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::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); + }) + + ; +} diff --git a/include/forfun/palindromic_number.hpp b/include/forfun/palindromic_number.hpp index daa48be..5b80bfc 100644 --- a/include/forfun/palindromic_number.hpp +++ b/include/forfun/palindromic_number.hpp @@ -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_ diff --git a/src/palindromic_number.cpp b/src/palindromic_number.cpp index 037b838..8cc91f4 100644 --- a/src/palindromic_number.cpp +++ b/src/palindromic_number.cpp @@ -8,6 +8,10 @@ #include +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}; @@ -18,3 +22,7 @@ return n == nn; } + +} // namespace stl + +} // namespace forfun::palindromic_number diff --git a/test/palindromic_number_test.cpp b/test/palindromic_number_test.cpp index 113eadf..6c54130 100644 --- a/test/palindromic_number_test.cpp +++ b/test/palindromic_number_test.cpp @@ -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({