| 
									
										
										
										
											2016-07-06 11:42:18 +03:00
										 |  |  | /*
 | 
					
						
							|  |  |  |   Copyright (c) Alexander Zaitsev <zamazan4ik@gmail.com>, 2016 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Distributed under the Boost Software License, Version 1.0. (See | 
					
						
							|  |  |  |   accompanying file LICENSE_1_0.txt or copy at | 
					
						
							|  |  |  |   http://www.boost.org/LICENSE_1_0.txt)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   See http://www.boost.org/ for latest version.
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /// \file  is_palindrome.hpp
 | 
					
						
							|  |  |  | /// \brief Checks the input sequence on palindrome.
 | 
					
						
							|  |  |  | /// \author Alexander Zaitsev
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-10 22:52:55 +03:00
										 |  |  | #ifndef BOOST_ALGORITHM_IS_PALINDROME_HPP
 | 
					
						
							|  |  |  | #define BOOST_ALGORITHM_IS_PALINDROME_HPP
 | 
					
						
							| 
									
										
										
										
											2016-07-06 11:42:18 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <iterator>
 | 
					
						
							| 
									
										
										
										
											2016-07-11 18:26:18 +03:00
										 |  |  | #include <functional>
 | 
					
						
							| 
									
										
										
										
											2016-08-16 05:14:56 +03:00
										 |  |  | #include <cstring>
 | 
					
						
							| 
									
										
										
										
											2016-07-06 11:42:18 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <boost/range/begin.hpp>
 | 
					
						
							|  |  |  | #include <boost/range/end.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace boost {  namespace algorithm { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /// \fn is_palindrome ( BidirectionalIterator begin, BidirectionalIterator end, Predicate p )
 | 
					
						
							|  |  |  | /// \return true if the entire sequence is palindrome
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// \param begin    The start of the input sequence
 | 
					
						
							|  |  |  | /// \param end		One past the end of the input sequence
 | 
					
						
							|  |  |  | /// \param p        A predicate used to compare the values.
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// \note This function will return true for empty sequences and for palindromes.
 | 
					
						
							|  |  |  | ///     For other sequences function will return false.
 | 
					
						
							|  |  |  | ///     Complexity: O(N).
 | 
					
						
							|  |  |  | template <typename BidirectionalIterator, typename Predicate> | 
					
						
							| 
									
										
										
										
											2016-10-12 22:22:30 +03:00
										 |  |  | bool is_palindrome(BidirectionalIterator begin, BidirectionalIterator end, Predicate p) | 
					
						
							| 
									
										
										
										
											2016-07-06 11:42:18 +03:00
										 |  |  | { | 
					
						
							|  |  |  |     if(begin == end) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     --end; | 
					
						
							|  |  |  |     while(begin != end) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if(!p(*begin, *end)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         ++begin; | 
					
						
							|  |  |  |         if(begin == end) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         --end; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /// \fn is_palindrome ( BidirectionalIterator begin, BidirectionalIterator end )
 | 
					
						
							|  |  |  | /// \return true if the entire sequence is palindrome
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// \param begin    The start of the input sequence
 | 
					
						
							| 
									
										
										
										
											2016-10-12 22:22:30 +03:00
										 |  |  | /// \param end	    One past the end of the input sequence
 | 
					
						
							| 
									
										
										
										
											2016-07-06 11:42:18 +03:00
										 |  |  | ///
 | 
					
						
							|  |  |  | /// \note This function will return true for empty sequences and for palindromes.
 | 
					
						
							|  |  |  | ///     For other sequences function will return false.
 | 
					
						
							|  |  |  | ///     Complexity: O(N).
 | 
					
						
							|  |  |  | template <typename BidirectionalIterator> | 
					
						
							|  |  |  | bool is_palindrome(BidirectionalIterator begin, BidirectionalIterator end) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-10-12 22:22:30 +03:00
										 |  |  |     return is_palindrome(begin, end, | 
					
						
							|  |  |  |                          std::equal_to<typename std::iterator_traits<BidirectionalIterator>::value_type> ()); | 
					
						
							| 
									
										
										
										
											2016-07-06 11:42:18 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /// \fn is_palindrome ( const R& range )
 | 
					
						
							|  |  |  | /// \return true if the entire sequence is palindrome
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// \param range The range to be tested.
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// \note This function will return true for empty sequences and for palindromes.
 | 
					
						
							|  |  |  | ///     For other sequences function will return false.
 | 
					
						
							|  |  |  | ///     Complexity: O(N).
 | 
					
						
							| 
									
										
										
										
											2016-08-16 16:18:21 +03:00
										 |  |  | template <typename R> | 
					
						
							| 
									
										
										
										
											2016-07-06 11:42:18 +03:00
										 |  |  | bool is_palindrome(const R& range) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return is_palindrome(boost::begin(range), boost::end(range)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /// \fn is_palindrome ( const R& range, Predicate p )
 | 
					
						
							|  |  |  | /// \return true if the entire sequence is palindrome
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// \param range The range to be tested.
 | 
					
						
							|  |  |  | /// \param p     A predicate used to compare the values.
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// \note This function will return true for empty sequences and for palindromes.
 | 
					
						
							|  |  |  | ///     For other sequences function will return false.
 | 
					
						
							|  |  |  | ///     Complexity: O(N).
 | 
					
						
							| 
									
										
										
										
											2016-08-16 16:18:21 +03:00
										 |  |  | template <typename R, typename Predicate> | 
					
						
							| 
									
										
										
										
											2016-07-06 11:42:18 +03:00
										 |  |  | bool is_palindrome(const R& range, Predicate p) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return is_palindrome(boost::begin(range), boost::end(range), p); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-16 05:14:56 +03:00
										 |  |  | /// \fn is_palindrome ( const char* str )
 | 
					
						
							|  |  |  | /// \return true if the entire sequence is palindrome
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// \param str C-string to be tested.
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// \note This function will return true for empty sequences and for palindromes.
 | 
					
						
							|  |  |  | ///     For other sequences function will return false.
 | 
					
						
							|  |  |  | ///     Complexity: O(N).
 | 
					
						
							| 
									
										
										
										
											2016-08-15 04:09:48 +03:00
										 |  |  | bool is_palindrome(const char* str) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-08-17 13:49:16 -07:00
										 |  |  |     if(!str) | 
					
						
							|  |  |  | 	    return true; | 
					
						
							| 
									
										
										
										
											2016-08-16 05:14:56 +03:00
										 |  |  |     return is_palindrome(str, str + strlen(str)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /// \fn is_palindrome ( const char* str, Predicate p )
 | 
					
						
							|  |  |  | /// \return true if the entire sequence is palindrome
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// \param str C-string to be tested.
 | 
					
						
							|  |  |  | /// \param p   A predicate used to compare the values.
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// \note This function will return true for empty sequences and for palindromes.
 | 
					
						
							|  |  |  | ///     For other sequences function will return false.
 | 
					
						
							|  |  |  | ///     Complexity: O(N).
 | 
					
						
							|  |  |  | template<typename Predicate> | 
					
						
							|  |  |  | bool is_palindrome(const char* str, Predicate p) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-08-17 13:49:16 -07:00
										 |  |  |     if(!str) | 
					
						
							|  |  |  | 	    return true; | 
					
						
							| 
									
										
										
										
											2016-08-16 05:14:56 +03:00
										 |  |  |     return is_palindrome(str, str + strlen(str), p); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-07-06 11:42:18 +03:00
										 |  |  | }} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-10 22:52:55 +03:00
										 |  |  | #endif // BOOST_ALGORITHM_IS_PALINDROME_HPP
 |