ranges_search.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___ALGORITHM_RANGES_SEARCH_H
  9. #define _LIBCPP___ALGORITHM_RANGES_SEARCH_H
  10. #include <__algorithm/iterator_operations.h>
  11. #include <__algorithm/search.h>
  12. #include <__config>
  13. #include <__functional/identity.h>
  14. #include <__functional/ranges_operations.h>
  15. #include <__iterator/advance.h>
  16. #include <__iterator/concepts.h>
  17. #include <__iterator/distance.h>
  18. #include <__iterator/indirectly_comparable.h>
  19. #include <__ranges/access.h>
  20. #include <__ranges/concepts.h>
  21. #include <__ranges/size.h>
  22. #include <__ranges/subrange.h>
  23. #include <__utility/pair.h>
  24. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  25. # pragma GCC system_header
  26. #endif
  27. #if _LIBCPP_STD_VER >= 20
  28. _LIBCPP_BEGIN_NAMESPACE_STD
  29. namespace ranges {
  30. namespace __search {
  31. struct __fn {
  32. template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Pred, class _Proj1, class _Proj2>
  33. _LIBCPP_HIDE_FROM_ABI static constexpr subrange<_Iter1> __ranges_search_impl(
  34. _Iter1 __first1,
  35. _Sent1 __last1,
  36. _Iter2 __first2,
  37. _Sent2 __last2,
  38. _Pred& __pred,
  39. _Proj1& __proj1,
  40. _Proj2& __proj2) {
  41. if constexpr (sized_sentinel_for<_Sent2, _Iter2>) {
  42. auto __size2 = ranges::distance(__first2, __last2);
  43. if (__size2 == 0)
  44. return {__first1, __first1};
  45. if constexpr (sized_sentinel_for<_Sent1, _Iter1>) {
  46. auto __size1 = ranges::distance(__first1, __last1);
  47. if (__size1 < __size2) {
  48. ranges::advance(__first1, __last1);
  49. return {__first1, __first1};
  50. }
  51. if constexpr (random_access_iterator<_Iter1> && random_access_iterator<_Iter2>) {
  52. auto __ret = std::__search_random_access_impl<_RangeAlgPolicy>(
  53. __first1, __last1, __first2, __last2, __pred, __proj1, __proj2, __size1, __size2);
  54. return {__ret.first, __ret.second};
  55. }
  56. }
  57. }
  58. auto __ret =
  59. std::__search_forward_impl<_RangeAlgPolicy>(__first1, __last1, __first2, __last2, __pred, __proj1, __proj2);
  60. return {__ret.first, __ret.second};
  61. }
  62. template <forward_iterator _Iter1,
  63. sentinel_for<_Iter1> _Sent1,
  64. forward_iterator _Iter2,
  65. sentinel_for<_Iter2> _Sent2,
  66. class _Pred = ranges::equal_to,
  67. class _Proj1 = identity,
  68. class _Proj2 = identity>
  69. requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
  70. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr subrange<_Iter1> operator()(
  71. _Iter1 __first1,
  72. _Sent1 __last1,
  73. _Iter2 __first2,
  74. _Sent2 __last2,
  75. _Pred __pred = {},
  76. _Proj1 __proj1 = {},
  77. _Proj2 __proj2 = {}) const {
  78. return __ranges_search_impl(__first1, __last1, __first2, __last2, __pred, __proj1, __proj2);
  79. }
  80. template <forward_range _Range1,
  81. forward_range _Range2,
  82. class _Pred = ranges::equal_to,
  83. class _Proj1 = identity,
  84. class _Proj2 = identity>
  85. requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>, _Pred, _Proj1, _Proj2>
  86. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr borrowed_subrange_t<_Range1> operator()(
  87. _Range1&& __range1, _Range2&& __range2, _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const {
  88. auto __first1 = ranges::begin(__range1);
  89. if constexpr (sized_range<_Range2>) {
  90. auto __size2 = ranges::size(__range2);
  91. if (__size2 == 0)
  92. return {__first1, __first1};
  93. if constexpr (sized_range<_Range1>) {
  94. auto __size1 = ranges::size(__range1);
  95. if (__size1 < __size2) {
  96. ranges::advance(__first1, ranges::end(__range1));
  97. return {__first1, __first1};
  98. }
  99. }
  100. }
  101. return __ranges_search_impl(
  102. ranges::begin(__range1),
  103. ranges::end(__range1),
  104. ranges::begin(__range2),
  105. ranges::end(__range2),
  106. __pred,
  107. __proj1,
  108. __proj2);
  109. }
  110. };
  111. } // namespace __search
  112. inline namespace __cpo {
  113. inline constexpr auto search = __search::__fn{};
  114. } // namespace __cpo
  115. } // namespace ranges
  116. _LIBCPP_END_NAMESPACE_STD
  117. #endif // _LIBCPP_STD_VER >= 20
  118. #endif // _LIBCPP___ALGORITHM_RANGES_SEARCH_H