find_backward.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. Copyright (c) T. Zachary Laine 2018.
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. */
  6. #ifndef BOOST_ALGORITHM_FIND_BACKWARD_HPP
  7. #define BOOST_ALGORITHM_FIND_BACKWARD_HPP
  8. #include <utility>
  9. #include <boost/config.hpp>
  10. #include <boost/range/begin.hpp>
  11. #include <boost/range/end.hpp>
  12. namespace boost { namespace algorithm {
  13. template<typename BidiIter, typename T>
  14. BOOST_CXX14_CONSTEXPR
  15. BidiIter find_backward(BidiIter first, BidiIter last, const T & x)
  16. {
  17. BidiIter it = last;
  18. while (it != first) {
  19. if (*--it == x)
  20. return it;
  21. }
  22. return last;
  23. }
  24. template<typename Range, typename T>
  25. BOOST_CXX14_CONSTEXPR
  26. typename boost::range_iterator<Range>::type find_backward(Range & range, const T & x)
  27. {
  28. return ::boost::algorithm::find_backward(boost::begin(range), boost::end(range), x);
  29. }
  30. template<typename BidiIter, typename T>
  31. BOOST_CXX14_CONSTEXPR
  32. BidiIter find_not_backward(BidiIter first, BidiIter last, const T & x)
  33. {
  34. BidiIter it = last;
  35. while (it != first) {
  36. if (*--it != x)
  37. return it;
  38. }
  39. return last;
  40. }
  41. template<typename Range, typename T>
  42. BOOST_CXX14_CONSTEXPR
  43. typename boost::range_iterator<Range>::type find_not_backward(Range & range, const T & x)
  44. {
  45. return ::boost::algorithm::find_not_backward(boost::begin(range), boost::end(range), x);
  46. }
  47. template<typename BidiIter, typename Pred>
  48. BOOST_CXX14_CONSTEXPR
  49. BidiIter find_if_backward(BidiIter first, BidiIter last, Pred p)
  50. {
  51. BidiIter it = last;
  52. while (it != first) {
  53. if (p(*--it))
  54. return it;
  55. }
  56. return last;
  57. }
  58. template<typename Range, typename Pred>
  59. BOOST_CXX14_CONSTEXPR
  60. typename boost::range_iterator<Range>::type find_if_backward(Range & range, Pred p)
  61. {
  62. return ::boost::algorithm::find_if_backward(boost::begin(range), boost::end(range), p);
  63. }
  64. template<typename BidiIter, typename Pred>
  65. BOOST_CXX14_CONSTEXPR
  66. BidiIter find_if_not_backward(BidiIter first, BidiIter last, Pred p)
  67. {
  68. BidiIter it = last;
  69. while (it != first) {
  70. if (!p(*--it))
  71. return it;
  72. }
  73. return last;
  74. }
  75. template<typename Range, typename Pred>
  76. BOOST_CXX14_CONSTEXPR
  77. typename boost::range_iterator<Range>::type find_if_not_backward(Range & range, Pred p)
  78. {
  79. return ::boost::algorithm::find_if_not_backward(boost::begin(range), boost::end(range), p);
  80. }
  81. }} // namespace boost and algorithm
  82. #endif // BOOST_ALGORITHM_FIND_BACKWARD_HPP