-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome.hpp
208 lines (163 loc) · 5.23 KB
/
palindrome.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// 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
/// Problem reference:
/// https://en.wikipedia.org/wiki/Palindrome
#ifndef FORFUN_PALINDROME_HPP_
#define FORFUN_PALINDROME_HPP_
#include <algorithm>
#include <cctype>
#include <concepts>
#include <iterator>
#include <string_view>
namespace forfun::palindrome {
namespace offset_based {
template <std::integral CharT>
#if __has_cpp_attribute(clang::no_sanitize)
[[clang::no_sanitize("unsigned-integer-overflow")]]
#endif // __has_cpp_attribute(clang::no_sanitize)
[[nodiscard]]
constexpr auto is_palindrome(
typename std::basic_string_view<CharT>::const_pointer const s,
typename std::basic_string_view<CharT>::size_type const length
) noexcept -> bool
{
using SizeType = std::basic_string_view<CharT>::size_type;
SizeType end{length - 1U};
SizeType const mid{length / 2U};
for (SizeType i{0U}; i != mid; ++i)
{
if (s[i] != s[end--])
{
return false;
}
}
return true;
}
#if __has_cpp_attribute(clang::no_sanitize)
[[clang::no_sanitize("unsigned-integer-overflow")]]
#endif // __has_cpp_attribute(clang::no_sanitize)
[[nodiscard]]
inline auto is_palindrome_ci(std::string_view const s) noexcept -> bool
{
using SizeType = std::string_view::size_type;
SizeType const length{s.length()};
SizeType const end{length - 1U};
SizeType const mid{length / 2U};
for (SizeType i{0U}; i != mid; ++i)
{
if (std::tolower(static_cast<unsigned char>(s[i]))
!= std::tolower(static_cast<unsigned char>(s[end - i])))
{
return false;
}
}
return true;
}
} // namespace offset_based
namespace iterator_based {
template <std::integral CharT>
// clang-format off
requires std::contiguous_iterator<
typename std::basic_string_view<CharT>::const_iterator>
// clang-format on
[[nodiscard]]
constexpr auto is_palindrome(std::basic_string_view<CharT> const s) noexcept
-> bool
{
using ConstIter = std::basic_string_view<CharT>::const_iterator;
using ConstRevIter = std::basic_string_view<CharT>::const_reverse_iterator;
using DiffType = std::basic_string_view<CharT>::difference_type;
using SizeType = std::basic_string_view<CharT>::size_type;
SizeType const length{s.length()};
ConstIter const cbegin{s.cbegin()};
ConstRevIter upper{s.crbegin()};
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
ConstIter const mid{cbegin + static_cast<DiffType>(length / 2U)};
for (ConstIter lower{cbegin}; lower != mid; ++lower)
{
if ((*lower) != (*upper))
{
return false;
}
++upper;
}
return true;
}
[[nodiscard]]
inline auto is_palindrome_ci(std::string_view const s) noexcept -> bool
{
using ConstIter = std::string_view::const_iterator;
using ConstRevIter = std::string_view::const_reverse_iterator;
using DiffType = std::string_view::difference_type;
ConstIter const cbegin{s.cbegin()};
ConstRevIter upper{s.crbegin()};
ConstIter const mid{cbegin + static_cast<DiffType>(s.length() / 2U)};
for (ConstIter lower{cbegin}; lower != mid; ++lower)
{
if (std::tolower(static_cast<unsigned char>(*lower))
!= std::tolower(static_cast<unsigned char>(*upper)))
{
return false;
}
++upper;
}
return true;
}
} // namespace iterator_based
namespace functional {
/// Adapted from original source:
/// https://en.cppreference.com/w/cpp/algorithm/equal
template <std::integral CharT>
[[nodiscard]]
constexpr auto is_palindrome(std::basic_string_view<CharT> const s) noexcept
-> bool
{
using ConstIter = std::basic_string_view<CharT>::const_iterator;
using DiffType = std::basic_string_view<CharT>::difference_type;
ConstIter const cbegin{s.cbegin()};
return std::equal(
cbegin, cbegin + static_cast<DiffType>(s.length() / 2U), s.crbegin()
);
}
namespace bloated {
/// Adapted from original source:
/// https://en.cppreference.com/w/cpp/algorithm/equal
template <std::integral CharT>
[[nodiscard]]
constexpr auto is_palindrome(std::basic_string_view<CharT> const s) noexcept
-> bool
{
using DiffType = std::basic_string_view<CharT>::difference_type;
return std::equal(
s.cbegin(),
std::next(s.cbegin(), static_cast<DiffType>(s.length() / 2U)),
s.crbegin()
);
}
namespace detail {
[[nodiscard]]
inline auto equal_case_insensitive(char const a, char const b) noexcept -> bool
{
return std::tolower(static_cast<unsigned char>(a))
== std::tolower(static_cast<unsigned char>(b));
}
} // namespace detail
[[nodiscard]]
inline auto is_palindrome_ci(std::string_view const s) noexcept -> bool
{
using ConstIter = std::string_view::const_iterator;
using DiffType = std::string_view::difference_type;
ConstIter const cbegin{s.cbegin()};
return std::equal(
cbegin,
std::next(cbegin, static_cast<DiffType>(s.length() / 2U)),
s.crbegin(),
detail::equal_case_insensitive
);
}
} // namespace bloated
} // namespace functional
} // namespace forfun::palindrome
#endif // FORFUN_PALINDROME_HPP_