]>
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> | |
38 | bool is_palindrome(BidirectionalIterator begin, BidirectionalIterator end, Predicate p ) | |
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 | |
66 | /// \param end One past the end of the input sequence | |
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 | { | |
74 | if(begin == end) | |
75 | { | |
76 | return true; | |
77 | } | |
78 | ||
79 | --end; | |
80 | while(begin != end) | |
81 | { | |
82 | if(!(*begin == *end)) | |
83 | { | |
84 | return false; | |
85 | } | |
86 | ++begin; | |
87 | if(begin == end) | |
88 | { | |
89 | break; | |
90 | } | |
91 | --end; | |
92 | } | |
93 | return true; | |
94 | } | |
95 | ||
96 | /// \fn is_palindrome ( const R& range ) | |
97 | /// \return true if the entire sequence is palindrome | |
98 | /// | |
99 | /// \param range The range to be tested. | |
100 | /// | |
101 | /// \note This function will return true for empty sequences and for palindromes. | |
102 | /// For other sequences function will return false. | |
103 | /// Complexity: O(N). | |
104 | template <typename R> | |
105 | bool is_palindrome(const R& range) | |
106 | { | |
107 | return is_palindrome(boost::begin(range), boost::end(range)); | |
108 | } | |
109 | ||
110 | /// \fn is_palindrome ( const R& range, Predicate p ) | |
111 | /// \return true if the entire sequence is palindrome | |
112 | /// | |
113 | /// \param range The range to be tested. | |
114 | /// \param p A predicate used to compare the values. | |
115 | /// | |
116 | /// \note This function will return true for empty sequences and for palindromes. | |
117 | /// For other sequences function will return false. | |
118 | /// Complexity: O(N). | |
119 | template <typename R, typename Predicate> | |
120 | bool is_palindrome(const R& range, Predicate p) | |
121 | { | |
122 | return is_palindrome(boost::begin(range), boost::end(range), p); | |
123 | } | |
124 | ||
125 | ||
126 | /// \fn is_palindrome ( const char* str ) | |
127 | /// \return true if the entire sequence is palindrome | |
128 | /// | |
129 | /// \param str C-string to be tested. | |
130 | /// | |
131 | /// \note This function will return true for empty sequences and for palindromes. | |
132 | /// For other sequences function will return false. | |
133 | /// Complexity: O(N). | |
134 | bool is_palindrome(const char* str) | |
135 | { | |
136 | if(!str) | |
137 | return true; | |
138 | return is_palindrome(str, str + strlen(str)); | |
139 | } | |
140 | ||
141 | ||
142 | /// \fn is_palindrome ( const char* str, Predicate p ) | |
143 | /// \return true if the entire sequence is palindrome | |
144 | /// | |
145 | /// \param str C-string to be tested. | |
146 | /// \param p A predicate used to compare the values. | |
147 | /// | |
148 | /// \note This function will return true for empty sequences and for palindromes. | |
149 | /// For other sequences function will return false. | |
150 | /// Complexity: O(N). | |
151 | template<typename Predicate> | |
152 | bool is_palindrome(const char* str, Predicate p) | |
153 | { | |
154 | if(!str) | |
155 | return true; | |
156 | return is_palindrome(str, str + strlen(str), p); | |
157 | } | |
158 | ||
159 | }} | |
160 | ||
161 | #endif // BOOST_ALGORITHM_IS_PALINDROME_HPP |