File indexing completed on 2026-05-03 08:13:19
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___CXX03___ALGORITHM_RANGES_ENDS_WITH_H
0010 #define _LIBCPP___CXX03___ALGORITHM_RANGES_ENDS_WITH_H
0011
0012 #include <__cxx03/__algorithm/ranges_equal.h>
0013 #include <__cxx03/__algorithm/ranges_starts_with.h>
0014 #include <__cxx03/__config>
0015 #include <__cxx03/__functional/identity.h>
0016 #include <__cxx03/__functional/ranges_operations.h>
0017 #include <__cxx03/__functional/reference_wrapper.h>
0018 #include <__cxx03/__iterator/advance.h>
0019 #include <__cxx03/__iterator/concepts.h>
0020 #include <__cxx03/__iterator/distance.h>
0021 #include <__cxx03/__iterator/indirectly_comparable.h>
0022 #include <__cxx03/__iterator/reverse_iterator.h>
0023 #include <__cxx03/__ranges/access.h>
0024 #include <__cxx03/__ranges/concepts.h>
0025 #include <__cxx03/__utility/move.h>
0026
0027 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0028 # pragma GCC system_header
0029 #endif
0030
0031 _LIBCPP_PUSH_MACROS
0032 #include <__cxx03/__undef_macros>
0033
0034 #if _LIBCPP_STD_VER >= 23
0035
0036 _LIBCPP_BEGIN_NAMESPACE_STD
0037
0038 namespace ranges {
0039 namespace __ends_with {
0040 struct __fn {
0041 template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Pred, class _Proj1, class _Proj2>
0042 _LIBCPP_HIDE_FROM_ABI static constexpr bool __ends_with_fn_impl_bidirectional(
0043 _Iter1 __first1,
0044 _Sent1 __last1,
0045 _Iter2 __first2,
0046 _Sent2 __last2,
0047 _Pred& __pred,
0048 _Proj1& __proj1,
0049 _Proj2& __proj2) {
0050 auto __rbegin1 = std::make_reverse_iterator(__last1);
0051 auto __rend1 = std::make_reverse_iterator(__first1);
0052 auto __rbegin2 = std::make_reverse_iterator(__last2);
0053 auto __rend2 = std::make_reverse_iterator(__first2);
0054 return ranges::starts_with(
0055 __rbegin1, __rend1, __rbegin2, __rend2, std::ref(__pred), std::ref(__proj1), std::ref(__proj2));
0056 }
0057
0058 template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Pred, class _Proj1, class _Proj2>
0059 _LIBCPP_HIDE_FROM_ABI static constexpr bool __ends_with_fn_impl(
0060 _Iter1 __first1,
0061 _Sent1 __last1,
0062 _Iter2 __first2,
0063 _Sent2 __last2,
0064 _Pred& __pred,
0065 _Proj1& __proj1,
0066 _Proj2& __proj2) {
0067 if constexpr (std::bidirectional_iterator<_Sent1> && std::bidirectional_iterator<_Sent2> &&
0068 (!std::random_access_iterator<_Sent1>) && (!std::random_access_iterator<_Sent2>)) {
0069 return __ends_with_fn_impl_bidirectional(__first1, __last1, __first2, __last2, __pred, __proj1, __proj2);
0070
0071 } else {
0072 auto __n1 = ranges::distance(__first1, __last1);
0073 auto __n2 = ranges::distance(__first2, __last2);
0074 if (__n2 == 0)
0075 return true;
0076 if (__n2 > __n1)
0077 return false;
0078
0079 return __ends_with_fn_impl_with_offset(
0080 std::move(__first1),
0081 std::move(__last1),
0082 std::move(__first2),
0083 std::move(__last2),
0084 __pred,
0085 __proj1,
0086 __proj2,
0087 __n1 - __n2);
0088 }
0089 }
0090
0091 template <class _Iter1,
0092 class _Sent1,
0093 class _Iter2,
0094 class _Sent2,
0095 class _Pred,
0096 class _Proj1,
0097 class _Proj2,
0098 class _Offset>
0099 static _LIBCPP_HIDE_FROM_ABI constexpr bool __ends_with_fn_impl_with_offset(
0100 _Iter1 __first1,
0101 _Sent1 __last1,
0102 _Iter2 __first2,
0103 _Sent2 __last2,
0104 _Pred& __pred,
0105 _Proj1& __proj1,
0106 _Proj2& __proj2,
0107 _Offset __offset) {
0108 if constexpr (std::bidirectional_iterator<_Sent1> && std::bidirectional_iterator<_Sent2> &&
0109 !std::random_access_iterator<_Sent1> && !std::random_access_iterator<_Sent2>) {
0110 return __ends_with_fn_impl_bidirectional(
0111 std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2), __pred, __proj1, __proj2);
0112
0113 } else {
0114 ranges::advance(__first1, __offset);
0115 return ranges::equal(
0116 std::move(__first1),
0117 std::move(__last1),
0118 std::move(__first2),
0119 std::move(__last2),
0120 std::ref(__pred),
0121 std::ref(__proj1),
0122 std::ref(__proj2));
0123 }
0124 }
0125
0126 template <input_iterator _Iter1,
0127 sentinel_for<_Iter1> _Sent1,
0128 input_iterator _Iter2,
0129 sentinel_for<_Iter2> _Sent2,
0130 class _Pred = ranges::equal_to,
0131 class _Proj1 = identity,
0132 class _Proj2 = identity>
0133 requires(forward_iterator<_Iter1> || sized_sentinel_for<_Sent1, _Iter1>) &&
0134 (forward_iterator<_Iter2> || sized_sentinel_for<_Sent2, _Iter2>) &&
0135 indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
0136 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(
0137 _Iter1 __first1,
0138 _Sent1 __last1,
0139 _Iter2 __first2,
0140 _Sent2 __last2,
0141 _Pred __pred = {},
0142 _Proj1 __proj1 = {},
0143 _Proj2 __proj2 = {}) const {
0144 return __ends_with_fn_impl(
0145 std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2), __pred, __proj1, __proj2);
0146 }
0147
0148 template <input_range _Range1,
0149 input_range _Range2,
0150 class _Pred = ranges::equal_to,
0151 class _Proj1 = identity,
0152 class _Proj2 = identity>
0153 requires(forward_range<_Range1> || sized_range<_Range1>) && (forward_range<_Range2> || sized_range<_Range2>) &&
0154 indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>, _Pred, _Proj1, _Proj2>
0155 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(
0156 _Range1&& __range1, _Range2&& __range2, _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const {
0157 if constexpr (sized_range<_Range1> && sized_range<_Range2>) {
0158 auto __n1 = ranges::size(__range1);
0159 auto __n2 = ranges::size(__range2);
0160 if (__n2 == 0)
0161 return true;
0162 if (__n2 > __n1)
0163 return false;
0164 auto __offset = __n1 - __n2;
0165
0166 return __ends_with_fn_impl_with_offset(
0167 ranges::begin(__range1),
0168 ranges::end(__range1),
0169 ranges::begin(__range2),
0170 ranges::end(__range2),
0171 __pred,
0172 __proj1,
0173 __proj2,
0174 __offset);
0175
0176 } else {
0177 return __ends_with_fn_impl(
0178 ranges::begin(__range1),
0179 ranges::end(__range1),
0180 ranges::begin(__range2),
0181 ranges::end(__range2),
0182 __pred,
0183 __proj1,
0184 __proj2);
0185 }
0186 }
0187 };
0188 }
0189
0190 inline namespace __cpo {
0191 inline constexpr auto ends_with = __ends_with::__fn{};
0192 }
0193 }
0194
0195 _LIBCPP_END_NAMESPACE_STD
0196
0197 #endif
0198
0199 _LIBCPP_POP_MACROS
0200
0201 #endif