選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

107 行
3.7KB

  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2014-present
  5. // Copyright Casey Carter 2016
  6. //
  7. // Use, modification and distribution is subject to the
  8. // Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. //
  12. // Project home: https://github.com/ericniebler/range-v3
  13. //
  14. //===-------------------------- algorithm ---------------------------------===//
  15. //
  16. // The LLVM Compiler Infrastructure
  17. //
  18. // This file is dual licensed under the MIT and the University of Illinois Open
  19. // Source Licenses. See LICENSE.TXT for details.
  20. //
  21. //===----------------------------------------------------------------------===//
  22. #ifndef RANGES_V3_ALGORITHM_PARTITION_POINT_HPP
  23. #define RANGES_V3_ALGORITHM_PARTITION_POINT_HPP
  24. #include <meta/meta.hpp>
  25. #include <range/v3/range_fwd.hpp>
  26. #include <range/v3/algorithm/aux_/partition_point_n.hpp>
  27. #include <range/v3/functional/identity.hpp>
  28. #include <range/v3/functional/invoke.hpp>
  29. #include <range/v3/iterator/concepts.hpp>
  30. #include <range/v3/iterator/operations.hpp>
  31. #include <range/v3/iterator/traits.hpp>
  32. #include <range/v3/range/access.hpp>
  33. #include <range/v3/range/concepts.hpp>
  34. #include <range/v3/range/dangling.hpp>
  35. #include <range/v3/range/traits.hpp>
  36. #include <range/v3/utility/static_const.hpp>
  37. namespace ranges
  38. {
  39. /// \addtogroup group-algorithms
  40. /// @{
  41. RANGES_BEGIN_NIEBLOID(partition_point)
  42. /// \brief function template \c partition_point
  43. template<typename I, typename S, typename C, typename P = identity>
  44. auto RANGES_FUN_NIEBLOID(partition_point)(
  45. I first, S last, C pred, P proj = P{}) //
  46. ->CPP_ret(I)( //
  47. requires forward_iterator<I> && sentinel_for<S, I> &&
  48. indirect_unary_predicate<C, projected<I, P>>)
  49. {
  50. if(RANGES_CONSTEXPR_IF(sized_sentinel_for<S, I>))
  51. {
  52. auto len = distance(first, std::move(last));
  53. return aux::partition_point_n(
  54. std::move(first), len, std::move(pred), std::move(proj));
  55. }
  56. // Probe exponentially for either last-of-range or an iterator
  57. // that is past the partition point (i.e., does not satisfy pred).
  58. auto len = iter_difference_t<I>{1};
  59. while(true)
  60. {
  61. auto mid = first;
  62. auto d = advance(mid, len, last);
  63. if(mid == last || !invoke(pred, invoke(proj, *mid)))
  64. {
  65. len -= d;
  66. return aux::partition_point_n(
  67. std::move(first), len, std::ref(pred), std::ref(proj));
  68. }
  69. first = std::move(mid);
  70. len *= 2;
  71. }
  72. }
  73. /// \overload
  74. template<typename Rng, typename C, typename P = identity>
  75. auto RANGES_FUN_NIEBLOID(partition_point)(Rng && rng, C pred, P proj = P{}) //
  76. ->CPP_ret(safe_iterator_t<Rng>)( //
  77. requires forward_range<Rng> &&
  78. indirect_unary_predicate<C, projected<iterator_t<Rng>, P>>)
  79. {
  80. if(RANGES_CONSTEXPR_IF(sized_range<Rng>))
  81. {
  82. auto len = distance(rng);
  83. return aux::partition_point_n(
  84. begin(rng), len, std::move(pred), std::move(proj));
  85. }
  86. return (*this)(begin(rng), end(rng), std::move(pred), std::move(proj));
  87. }
  88. RANGES_END_NIEBLOID(partition_point)
  89. namespace cpp20
  90. {
  91. using ranges::partition_point;
  92. }
  93. /// @}
  94. } // namespace ranges
  95. #endif // include guard