unwrap_range.h 3.8 KB

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