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___ALGORITHM_UNWRAP_RANGE_H
10 #define _LIBCPP___ALGORITHM_UNWRAP_RANGE_H
12 #include <__algorithm/unwrap_iter.h>
13 #include <__concepts/constructible.h>
15 #include <__iterator/concepts.h>
16 #include <__iterator/next.h>
17 #include <__utility/declval.h>
18 #include <__utility/move.h>
19 #include <__utility/pair.h>
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 # pragma GCC system_header
25 _LIBCPP_BEGIN_NAMESPACE_STD
27 // __unwrap_range and __rewrap_range are used to unwrap ranges which may have different iterator and sentinel types.
28 // __unwrap_iter and __rewrap_iter don't work for this, because they assume that the iterator and sentinel have
29 // the same type. __unwrap_range tries to get two iterators and then forward to __unwrap_iter.
31 #if _LIBCPP_STD_VER >= 20
32 template <class _Iter
, class _Sent
>
33 struct __unwrap_range_impl
{
34 _LIBCPP_HIDE_FROM_ABI
static constexpr auto __unwrap(_Iter __first
, _Sent __sent
)
35 requires random_access_iterator
<_Iter
> && sized_sentinel_for
<_Sent
, _Iter
>
37 auto __last
= ranges::next(__first
, __sent
);
38 return pair
{std::__unwrap_iter(std::move(__first
)), std::__unwrap_iter(std::move(__last
))};
41 _LIBCPP_HIDE_FROM_ABI
static constexpr auto __unwrap(_Iter __first
, _Sent __last
) {
42 return pair
{std::move(__first
), std::move(__last
)};
45 _LIBCPP_HIDE_FROM_ABI
static constexpr auto
46 __rewrap(_Iter __orig_iter
, decltype(std::__unwrap_iter(std::move(__orig_iter
))) __iter
)
47 requires random_access_iterator
<_Iter
> && sized_sentinel_for
<_Sent
, _Iter
>
49 return std::__rewrap_iter(std::move(__orig_iter
), std::move(__iter
));
52 _LIBCPP_HIDE_FROM_ABI
static constexpr auto __rewrap(const _Iter
&, _Iter __iter
)
53 requires(!(random_access_iterator
<_Iter
> && sized_sentinel_for
<_Sent
, _Iter
>))
59 template <class _Iter
>
60 struct __unwrap_range_impl
<_Iter
, _Iter
> {
61 _LIBCPP_HIDE_FROM_ABI
static constexpr auto __unwrap(_Iter __first
, _Iter __last
) {
62 return pair
{std::__unwrap_iter(std::move(__first
)), std::__unwrap_iter(std::move(__last
))};
65 _LIBCPP_HIDE_FROM_ABI
static constexpr auto
66 __rewrap(_Iter __orig_iter
, decltype(std::__unwrap_iter(__orig_iter
)) __iter
) {
67 return std::__rewrap_iter(std::move(__orig_iter
), std::move(__iter
));
71 template <class _Iter
, class _Sent
>
72 _LIBCPP_HIDE_FROM_ABI
constexpr auto __unwrap_range(_Iter __first
, _Sent __last
) {
73 return __unwrap_range_impl
<_Iter
, _Sent
>::__unwrap(std::move(__first
), std::move(__last
));
76 template < class _Sent
, class _Iter
, class _Unwrapped
>
77 _LIBCPP_HIDE_FROM_ABI
constexpr _Iter
__rewrap_range(_Iter __orig_iter
, _Unwrapped __iter
) {
78 return __unwrap_range_impl
<_Iter
, _Sent
>::__rewrap(std::move(__orig_iter
), std::move(__iter
));
80 #else // _LIBCPP_STD_VER >= 20
81 template <class _Iter
, class _Unwrapped
= decltype(std::__unwrap_iter(std::declval
<_Iter
>()))>
82 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR pair
<_Unwrapped
, _Unwrapped
> __unwrap_range(_Iter __first
, _Iter __last
) {
83 return std::make_pair(std::__unwrap_iter(std::move(__first
)), std::__unwrap_iter(std::move(__last
)));
86 template <class _Iter
, class _Unwrapped
>
87 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter
__rewrap_range(_Iter __orig_iter
, _Unwrapped __iter
) {
88 return std::__rewrap_iter(std::move(__orig_iter
), std::move(__iter
));
90 #endif // _LIBCPP_STD_VER >= 20
92 _LIBCPP_END_NAMESPACE_STD
94 #endif // _LIBCPP___ALGORITHM_UNWRAP_RANGE_H