1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___CXX03___ALGORITHM_UNWRAP_RANGE_H
10 #define _LIBCPP___CXX03___ALGORITHM_UNWRAP_RANGE_H
12 #include <__cxx03/__algorithm/unwrap_iter.h>
13 #include <__cxx03/__concepts/constructible.h>
14 #include <__cxx03/__config>
15 #include <__cxx03/__iterator/concepts.h>
16 #include <__cxx03/__iterator/next.h>
17 #include <__cxx03/__utility/declval.h>
18 #include <__cxx03/__utility/move.h>
19 #include <__cxx03/__utility/pair.h>
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 # pragma GCC system_header
26 #include <__cxx03/__undef_macros>
28 _LIBCPP_BEGIN_NAMESPACE_STD
30 // __unwrap_range and __rewrap_range are used to unwrap ranges which may have different iterator and sentinel types.
31 // __unwrap_iter and __rewrap_iter don't work for this, because they assume that the iterator and sentinel have
32 // the same type. __unwrap_range tries to get two iterators and then forward to __unwrap_iter.
34 #if _LIBCPP_STD_VER >= 20
35 template <class _Iter
, class _Sent
>
36 struct __unwrap_range_impl
{
37 _LIBCPP_HIDE_FROM_ABI
static constexpr auto __unwrap(_Iter __first
, _Sent __sent
)
38 requires random_access_iterator
<_Iter
> && sized_sentinel_for
<_Sent
, _Iter
>
40 auto __last
= ranges::next(__first
, __sent
);
41 return pair
{std::__unwrap_iter(std::move(__first
)), std::__unwrap_iter(std::move(__last
))};
44 _LIBCPP_HIDE_FROM_ABI
static constexpr auto __unwrap(_Iter __first
, _Sent __last
) {
45 return pair
{std::move(__first
), std::move(__last
)};
48 _LIBCPP_HIDE_FROM_ABI
static constexpr auto
49 __rewrap(_Iter __orig_iter
, decltype(std::__unwrap_iter(std::move(__orig_iter
))) __iter
)
50 requires random_access_iterator
<_Iter
> && sized_sentinel_for
<_Sent
, _Iter
>
52 return std::__rewrap_iter(std::move(__orig_iter
), std::move(__iter
));
55 _LIBCPP_HIDE_FROM_ABI
static constexpr auto __rewrap(const _Iter
&, _Iter __iter
)
56 requires(!(random_access_iterator
<_Iter
> && sized_sentinel_for
<_Sent
, _Iter
>))
62 template <class _Iter
>
63 struct __unwrap_range_impl
<_Iter
, _Iter
> {
64 _LIBCPP_HIDE_FROM_ABI
static constexpr auto __unwrap(_Iter __first
, _Iter __last
) {
65 return pair
{std::__unwrap_iter(std::move(__first
)), std::__unwrap_iter(std::move(__last
))};
68 _LIBCPP_HIDE_FROM_ABI
static constexpr auto
69 __rewrap(_Iter __orig_iter
, decltype(std::__unwrap_iter(__orig_iter
)) __iter
) {
70 return std::__rewrap_iter(std::move(__orig_iter
), std::move(__iter
));
74 template <class _Iter
, class _Sent
>
75 _LIBCPP_HIDE_FROM_ABI
constexpr auto __unwrap_range(_Iter __first
, _Sent __last
) {
76 return __unwrap_range_impl
<_Iter
, _Sent
>::__unwrap(std::move(__first
), std::move(__last
));
79 template < class _Sent
, class _Iter
, class _Unwrapped
>
80 _LIBCPP_HIDE_FROM_ABI
constexpr _Iter
__rewrap_range(_Iter __orig_iter
, _Unwrapped __iter
) {
81 return __unwrap_range_impl
<_Iter
, _Sent
>::__rewrap(std::move(__orig_iter
), std::move(__iter
));
83 #else // _LIBCPP_STD_VER >= 20
84 template <class _Iter
, class _Unwrapped
= decltype(std::__unwrap_iter(std::declval
<_Iter
>()))>
85 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR pair
<_Unwrapped
, _Unwrapped
> __unwrap_range(_Iter __first
, _Iter __last
) {
86 return std::make_pair(std::__unwrap_iter(std::move(__first
)), std::__unwrap_iter(std::move(__last
)));
89 template <class _Iter
, class _Unwrapped
>
90 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter
__rewrap_range(_Iter __orig_iter
, _Unwrapped __iter
) {
91 return std::__rewrap_iter(std::move(__orig_iter
), std::move(__iter
));
93 #endif // _LIBCPP_STD_VER >= 20
95 _LIBCPP_END_NAMESPACE_STD
99 #endif // _LIBCPP___CXX03___ALGORITHM_UNWRAP_RANGE_H