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
9#ifndef _LIBCPP___ALGORITHM_UNWRAP_RANGE_H
10#define _LIBCPP___ALGORITHM_UNWRAP_RANGE_H
11
12#include <__algorithm/unwrap_iter.h>
13#include <__config>
14#include <__iterator/concepts.h>
15#include <__type_traits/is_same.h>
16#include <__utility/declval.h>
17#include <__utility/move.h>
18#include <__utility/pair.h>
19
20#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21# pragma GCC system_header
22#endif
23
24_LIBCPP_PUSH_MACROS
25#include <__undef_macros>
26
27_LIBCPP_BEGIN_NAMESPACE_STD
28
29// __unwrap_range and __rewrap_range are used to unwrap ranges which may have different iterator and sentinel types.
30// __unwrap_iter and __rewrap_iter don't work for this, because they assume that the iterator and sentinel have
31// the same type. __unwrap_range tries to get two iterators and then forward to __unwrap_iter.
32
33#if _LIBCPP_STD_VER >= 20
34template <class _Iter, class _Sent>
35_LIBCPP_HIDE_FROM_ABI constexpr auto __unwrap_range(_Iter __first, _Sent __last) {
36 if constexpr (is_same_v<_Iter, _Sent>)
37 return pair{std::__unwrap_iter(std::move(__first)), std::__unwrap_iter(std::move(__last))};
38 else if constexpr (random_access_iterator<_Iter> && sized_sentinel_for<_Sent, _Iter>) {
39 auto __iter_last = __first + (__last - __first);
40 return pair{std::__unwrap_iter(std::move(__first)), std::__unwrap_iter(std::move(__iter_last))};
41 } else
42 return pair{std::move(__first), std::move(__last)};
43}
44
45template < class _Sent, class _Iter, class _Unwrapped>
46_LIBCPP_HIDE_FROM_ABI constexpr _Iter __rewrap_range(_Iter __orig_iter, _Unwrapped __iter) {
47 if constexpr (is_same_v<_Iter, _Sent>)
48 return std::__rewrap_iter(std::move(__orig_iter), std::move(__iter));
49 else if constexpr (random_access_iterator<_Iter> && sized_sentinel_for<_Sent, _Iter>)
50 return std::__rewrap_iter(std::move(__orig_iter), std::move(__iter));
51 else
52 return __iter;
53}
54#else // _LIBCPP_STD_VER >= 20
55template <class _Iter, class _Unwrapped = decltype(std::__unwrap_iter(std::declval<_Iter>()))>
56_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR pair<_Unwrapped, _Unwrapped> __unwrap_range(_Iter __first, _Iter __last) {
57 return std::make_pair(std::__unwrap_iter(std::move(__first)), std::__unwrap_iter(std::move(__last)));
58}
59
60template <class _Iter, class _Unwrapped>
61_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter __rewrap_range(_Iter __orig_iter, _Unwrapped __iter) {
62 return std::__rewrap_iter(std::move(__orig_iter), std::move(__iter));
63}
64#endif // _LIBCPP_STD_VER >= 20
65
66_LIBCPP_END_NAMESPACE_STD
67
68_LIBCPP_POP_MACROS
69
70#endif // _LIBCPP___ALGORITHM_UNWRAP_RANGE_H
71