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/config.hpp>
  17. #include <boost/range/begin.hpp>
  18. #include <boost/range/end.hpp>
  19. namespace boost { namespace algorithm {
  20. /// \fn is_palindrome ( BidirectionalIterator begin, BidirectionalIterator end, Predicate p )
  21. /// \return true if the entire sequence is palindrome
  22. ///
  23. /// \param begin The start of the input sequence
  24. /// \param end One past the end of the input sequence
  25. /// \param p A predicate used to compare the values.
  26. ///
  27. /// \note This function will return true for empty sequences and for palindromes.
  28. /// For other sequences function will return false.
  29. /// Complexity: O(N).
  30. template <typename BidirectionalIterator, typename Predicate>
  31. bool is_palindrome(BidirectionalIterator begin, BidirectionalIterator end, Predicate p)
  32. {
  33. if(begin == end)
  34. {
  35. return true;
  36. }
  37. --end;
  38. while(begin != end)
  39. {
  40. if(!p(*begin, *end))
  41. {
  42. return false;
  43. }
  44. ++begin;
  45. if(begin == end)
  46. {
  47. break;
  48. }
  49. --end;
  50. }
  51. return true;
  52. }
  53. /// \fn is_palindrome ( BidirectionalIterator begin, BidirectionalIterator end )
  54. /// \return true if the entire sequence is palindrome
  55. ///
  56. /// \param begin The start of the input sequence
  57. /// \param end One past the end of the input sequence
  58. ///
  59. /// \note This function will return true for empty sequences and for palindromes.
  60. /// For other sequences function will return false.
  61. /// Complexity: O(N).
  62. template <typename BidirectionalIterator>
  63. bool is_palindrome(BidirectionalIterator begin, BidirectionalIterator end)
  64. {
  65. return is_palindrome(begin, end,
  66. std::equal_to<typename std::iterator_traits<BidirectionalIterator>::value_type> ());
  67. }
  68. /// \fn is_palindrome ( const R& range )
  69. /// \return true if the entire sequence is palindrome
  70. ///
  71. /// \param range The range to be tested.
  72. ///
  73. /// \note This function will return true for empty sequences and for palindromes.
  74. /// For other sequences function will return false.
  75. /// Complexity: O(N).
  76. template <typename R>
  77. bool is_palindrome(const R& range)
  78. {
  79. return is_palindrome(boost::begin(range), boost::end(range));
  80. }
  81. /// \fn is_palindrome ( const R& range, Predicate p )
  82. /// \return true if the entire sequence is palindrome
  83. ///
  84. /// \param range The range to be tested.
  85. /// \param p A predicate used to compare the values.
  86. ///
  87. /// \note This function will return true for empty sequences and for palindromes.
  88. /// For other sequences function will return false.
  89. /// Complexity: O(N).
  90. template <typename R, typename Predicate>
  91. bool is_palindrome(const R& range, Predicate p)
  92. {
  93. return is_palindrome(boost::begin(range), boost::end(range), p);
  94. }
  95. /// \fn is_palindrome ( const char* str )
  96. /// \return true if the entire sequence is palindrome
  97. ///
  98. /// \param str C-string to be tested.
  99. ///
  100. /// \note This function will return true for empty sequences and for palindromes.
  101. /// For other sequences function will return false.
  102. /// Complexity: O(N).
  103. bool is_palindrome(const char* str)
  104. {
  105. if(!str)
  106. return true;
  107. return is_palindrome(str, str + strlen(str));
  108. }
  109. /// \fn is_palindrome ( const char* str, Predicate p )
  110. /// \return true if the entire sequence is palindrome
  111. ///
  112. /// \param str C-string to be tested.
  113. /// \param p A predicate used to compare the values.
  114. ///
  115. /// \note This function will return true for empty sequences and for palindromes.
  116. /// For other sequences function will return false.
  117. /// Complexity: O(N).
  118. template<typename Predicate>
  119. bool is_palindrome(const char* str, Predicate p)
  120. {
  121. if(!str)
  122. return true;
  123. return is_palindrome(str, str + strlen(str), p);
  124. }
  125. }}
  126. #endif // BOOST_ALGORITHM_IS_PALINDROME_HPP