is_palindrome.hpp 4.2 KB

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