]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /* |
2 | Copyright (c) Alexander Zaitsev <zamazan4ik@gmail.com>, 2016 | |
3 | ||
4 | Distributed under the Boost Software License, Version 1.0. (See | |
5 | accompanying file LICENSE_1_0.txt or copy at | |
6 | http://www.boost.org/LICENSE_1_0.txt) | |
7 | ||
8 | See http://www.boost.org/ for latest version. | |
9 | */ | |
10 | ||
11 | /// \file is_palindrome.hpp | |
12 | /// \brief Checks the input sequence on palindrome. | |
13 | /// \author Alexander Zaitsev | |
14 | ||
15 | #ifndef BOOST_ALGORITHM_IS_PALINDROME_HPP | |
16 | #define BOOST_ALGORITHM_IS_PALINDROME_HPP | |
17 | ||
18 | #include <iterator> | |
19 | #include <functional> | |
20 | #include <cstring> | |
21 | ||
22 | #include <boost/range/begin.hpp> | |
23 | #include <boost/range/end.hpp> | |
24 | ||
25 | namespace boost { namespace algorithm { | |
26 | ||
27 | /// \fn is_palindrome ( BidirectionalIterator begin, BidirectionalIterator end, Predicate p ) | |
28 | /// \return true if the entire sequence is palindrome | |
29 | /// | |
30 | /// \param begin The start of the input sequence | |
31 | /// \param end One past the end of the input sequence | |
32 | /// \param p A predicate used to compare the values. | |
33 | /// | |
34 | /// \note This function will return true for empty sequences and for palindromes. | |
35 | /// For other sequences function will return false. | |
36 | /// Complexity: O(N). | |
37 | template <typename BidirectionalIterator, typename Predicate> | |
b32b8144 | 38 | bool is_palindrome(BidirectionalIterator begin, BidirectionalIterator end, Predicate p) |
7c673cae FG |
39 | { |
40 | if(begin == end) | |
41 | { | |
42 | return true; | |
43 | } | |
44 | ||
45 | --end; | |
46 | while(begin != end) | |
47 | { | |
48 | if(!p(*begin, *end)) | |
49 | { | |
50 | return false; | |
51 | } | |
52 | ++begin; | |
53 | if(begin == end) | |
54 | { | |
55 | break; | |
56 | } | |
57 | --end; | |
58 | } | |
59 | return true; | |
60 | } | |
61 | ||
62 | /// \fn is_palindrome ( BidirectionalIterator begin, BidirectionalIterator end ) | |
63 | /// \return true if the entire sequence is palindrome | |
64 | /// | |
65 | /// \param begin The start of the input sequence | |
b32b8144 | 66 | /// \param end One past the end of the input sequence |
7c673cae FG |
67 | /// |
68 | /// \note This function will return true for empty sequences and for palindromes. | |
69 | /// For other sequences function will return false. | |
70 | /// Complexity: O(N). | |
71 | template <typename BidirectionalIterator> | |
72 | bool is_palindrome(BidirectionalIterator begin, BidirectionalIterator end) | |
73 | { | |
b32b8144 FG |
74 | return is_palindrome(begin, end, |
75 | std::equal_to<typename std::iterator_traits<BidirectionalIterator>::value_type> ()); | |
7c673cae FG |
76 | } |
77 | ||
78 | /// \fn is_palindrome ( const R& range ) | |
79 | /// \return true if the entire sequence is palindrome | |
80 | /// | |
81 | /// \param range The range to be tested. | |
82 | /// | |
83 | /// \note This function will return true for empty sequences and for palindromes. | |
84 | /// For other sequences function will return false. | |
85 | /// Complexity: O(N). | |
86 | template <typename R> | |
87 | bool is_palindrome(const R& range) | |
88 | { | |
89 | return is_palindrome(boost::begin(range), boost::end(range)); | |
90 | } | |
91 | ||
92 | /// \fn is_palindrome ( const R& range, Predicate p ) | |
93 | /// \return true if the entire sequence is palindrome | |
94 | /// | |
95 | /// \param range The range to be tested. | |
96 | /// \param p A predicate used to compare the values. | |
97 | /// | |
98 | /// \note This function will return true for empty sequences and for palindromes. | |
99 | /// For other sequences function will return false. | |
100 | /// Complexity: O(N). | |
101 | template <typename R, typename Predicate> | |
102 | bool is_palindrome(const R& range, Predicate p) | |
103 | { | |
104 | return is_palindrome(boost::begin(range), boost::end(range), p); | |
105 | } | |
106 | ||
7c673cae FG |
107 | /// \fn is_palindrome ( const char* str ) |
108 | /// \return true if the entire sequence is palindrome | |
109 | /// | |
110 | /// \param str C-string to be tested. | |
111 | /// | |
112 | /// \note This function will return true for empty sequences and for palindromes. | |
113 | /// For other sequences function will return false. | |
114 | /// Complexity: O(N). | |
115 | bool is_palindrome(const char* str) | |
116 | { | |
117 | if(!str) | |
118 | return true; | |
119 | return is_palindrome(str, str + strlen(str)); | |
120 | } | |
121 | ||
7c673cae FG |
122 | /// \fn is_palindrome ( const char* str, Predicate p ) |
123 | /// \return true if the entire sequence is palindrome | |
124 | /// | |
125 | /// \param str C-string to be tested. | |
126 | /// \param p A predicate used to compare the values. | |
127 | /// | |
128 | /// \note This function will return true for empty sequences and for palindromes. | |
129 | /// For other sequences function will return false. | |
130 | /// Complexity: O(N). | |
131 | template<typename Predicate> | |
132 | bool is_palindrome(const char* str, Predicate p) | |
133 | { | |
134 | if(!str) | |
135 | return true; | |
136 | return is_palindrome(str, str + strlen(str), p); | |
137 | } | |
7c673cae FG |
138 | }} |
139 | ||
140 | #endif // BOOST_ALGORITHM_IS_PALINDROME_HPP |