Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:26

0001 /*
0002   Copyright (c) Alexander Zaitsev <zamazan4ik@gmail.com>, 2016
0003 
0004   Distributed under the Boost Software License, Version 1.0. (See
0005   accompanying file LICENSE_1_0.txt or copy at
0006   http://www.boost.org/LICENSE_1_0.txt)
0007 
0008   See http://www.boost.org/ for latest version.
0009 */
0010 
0011 /// \file  is_palindrome.hpp
0012 /// \brief Checks the input sequence on palindrome.
0013 /// \author Alexander Zaitsev
0014 
0015 #ifndef BOOST_ALGORITHM_IS_PALINDROME_HPP
0016 #define BOOST_ALGORITHM_IS_PALINDROME_HPP
0017 
0018 #include <iterator>
0019 #include <functional>
0020 #include <cstring>
0021 
0022 #include <boost/config.hpp>
0023 #include <boost/range/begin.hpp>
0024 #include <boost/range/end.hpp>
0025 
0026 namespace boost {  namespace algorithm {
0027 
0028 /// \fn is_palindrome ( BidirectionalIterator begin, BidirectionalIterator end, Predicate p )
0029 /// \return true if the entire sequence is palindrome
0030 ///
0031 /// \param begin    The start of the input sequence
0032 /// \param end      One past the end of the input sequence
0033 /// \param p        A predicate used to compare the values.
0034 ///
0035 /// \note This function will return true for empty sequences and for palindromes.
0036 ///     For other sequences function will return false.
0037 ///     Complexity: O(N).
0038 template <typename BidirectionalIterator, typename Predicate>
0039 bool is_palindrome(BidirectionalIterator begin, BidirectionalIterator end, Predicate p)
0040 {
0041     if(begin == end)
0042     {
0043         return true;
0044     }
0045 
0046     --end;
0047     while(begin != end)
0048     {
0049         if(!p(*begin, *end))
0050         {
0051             return false;
0052         }
0053         ++begin;
0054         if(begin == end)
0055         {
0056             break;
0057         }
0058         --end;
0059     }
0060     return true;
0061 }
0062 
0063 /// \fn is_palindrome ( BidirectionalIterator begin, BidirectionalIterator end )
0064 /// \return true if the entire sequence is palindrome
0065 ///
0066 /// \param begin    The start of the input sequence
0067 /// \param end      One past the end of the input sequence
0068 ///
0069 /// \note This function will return true for empty sequences and for palindromes.
0070 ///     For other sequences function will return false.
0071 ///     Complexity: O(N).
0072 template <typename BidirectionalIterator>
0073 bool is_palindrome(BidirectionalIterator begin, BidirectionalIterator end)
0074 {
0075     return is_palindrome(begin, end,
0076                          std::equal_to<typename std::iterator_traits<BidirectionalIterator>::value_type> ());
0077 }
0078 
0079 /// \fn is_palindrome ( const R& range )
0080 /// \return true if the entire sequence is palindrome
0081 ///
0082 /// \param range The range to be tested.
0083 ///
0084 /// \note This function will return true for empty sequences and for palindromes.
0085 ///     For other sequences function will return false.
0086 ///     Complexity: O(N).
0087 template <typename R>
0088 bool is_palindrome(const R& range)
0089 {
0090     return is_palindrome(boost::begin(range), boost::end(range));
0091 }
0092 
0093 /// \fn is_palindrome ( const R& range, Predicate p )
0094 /// \return true if the entire sequence is palindrome
0095 ///
0096 /// \param range The range to be tested.
0097 /// \param p     A predicate used to compare the values.
0098 ///
0099 /// \note This function will return true for empty sequences and for palindromes.
0100 ///     For other sequences function will return false.
0101 ///     Complexity: O(N).
0102 template <typename R, typename Predicate>
0103 bool is_palindrome(const R& range, Predicate p)
0104 {
0105     return is_palindrome(boost::begin(range), boost::end(range), p);
0106 }
0107 
0108 /// \fn is_palindrome ( const char* str )
0109 /// \return true if the entire sequence is palindrome
0110 ///
0111 /// \param str C-string to be tested.
0112 ///
0113 /// \note This function will return true for empty sequences and for palindromes.
0114 ///     For other sequences function will return false.
0115 ///     Complexity: O(N).
0116 inline bool is_palindrome(const char* str)
0117 {
0118     if(!str)
0119         return true;
0120     return is_palindrome(str, str + strlen(str));
0121 }
0122 
0123 /// \fn is_palindrome ( const char* str, Predicate p )
0124 /// \return true if the entire sequence is palindrome
0125 ///
0126 /// \param str C-string to be tested.
0127 /// \param p   A predicate used to compare the values.
0128 ///
0129 /// \note This function will return true for empty sequences and for palindromes.
0130 ///     For other sequences function will return false.
0131 ///     Complexity: O(N).
0132 template<typename Predicate>
0133 bool is_palindrome(const char* str, Predicate p)
0134 {
0135     if(!str)
0136         return true;
0137     return is_palindrome(str, str + strlen(str), p);
0138 }
0139 }}
0140 
0141 #endif // BOOST_ALGORITHM_IS_PALINDROME_HPP