unwrap_range.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_BEGIN_NAMESPACE_STD
  22. // __unwrap_range and __rewrap_range are used to unwrap ranges which may have different iterator and sentinel types.
  23. // __unwrap_iter and __rewrap_iter don't work for this, because they assume that the iterator and sentinel have
  24. // the same type. __unwrap_range tries to get two iterators and then forward to __unwrap_iter.
  25. #if _LIBCPP_STD_VER >= 20
  26. template <class _Iter, class _Sent>
  27. struct __unwrap_range_impl {
  28. _LIBCPP_HIDE_FROM_ABI static constexpr auto __unwrap(_Iter __first, _Sent __sent)
  29. requires random_access_iterator<_Iter> && sized_sentinel_for<_Sent, _Iter>
  30. {
  31. auto __last = ranges::next(__first, __sent);
  32. return pair{std::__unwrap_iter(std::move(__first)), std::__unwrap_iter(std::move(__last))};
  33. }
  34. _LIBCPP_HIDE_FROM_ABI static constexpr auto __unwrap(_Iter __first, _Sent __last) {
  35. return pair{std::move(__first), std::move(__last)};
  36. }
  37. _LIBCPP_HIDE_FROM_ABI static constexpr auto
  38. __rewrap(_Iter __orig_iter, decltype(std::__unwrap_iter(std::move(__orig_iter))) __iter)
  39. requires random_access_iterator<_Iter> && sized_sentinel_for<_Sent, _Iter>
  40. {
  41. return std::__rewrap_iter(std::move(__orig_iter), std::move(__iter));
  42. }
  43. _LIBCPP_HIDE_FROM_ABI static constexpr auto __rewrap(const _Iter&, _Iter __iter)
  44. requires(!(random_access_iterator<_Iter> && sized_sentinel_for<_Sent, _Iter>))
  45. {
  46. return __iter;
  47. }
  48. };
  49. template <class _Iter>
  50. struct __unwrap_range_impl<_Iter, _Iter> {
  51. _LIBCPP_HIDE_FROM_ABI static constexpr auto __unwrap(_Iter __first, _Iter __last) {
  52. return pair{std::__unwrap_iter(std::move(__first)), std::__unwrap_iter(std::move(__last))};
  53. }
  54. _LIBCPP_HIDE_FROM_ABI static constexpr auto
  55. __rewrap(_Iter __orig_iter, decltype(std::__unwrap_iter(__orig_iter)) __iter) {
  56. return std::__rewrap_iter(std::move(__orig_iter), std::move(__iter));
  57. }
  58. };
  59. template <class _Iter, class _Sent>
  60. _LIBCPP_HIDE_FROM_ABI constexpr auto __unwrap_range(_Iter __first, _Sent __last) {
  61. return __unwrap_range_impl<_Iter, _Sent>::__unwrap(std::move(__first), std::move(__last));
  62. }
  63. template < class _Sent, class _Iter, class _Unwrapped>
  64. _LIBCPP_HIDE_FROM_ABI constexpr _Iter __rewrap_range(_Iter __orig_iter, _Unwrapped __iter) {
  65. return __unwrap_range_impl<_Iter, _Sent>::__rewrap(std::move(__orig_iter), std::move(__iter));
  66. }
  67. #else // _LIBCPP_STD_VER >= 20
  68. template <class _Iter, class _Unwrapped = decltype(std::__unwrap_iter(std::declval<_Iter>()))>
  69. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR pair<_Unwrapped, _Unwrapped> __unwrap_range(_Iter __first, _Iter __last) {
  70. return std::make_pair(std::__unwrap_iter(std::move(__first)), std::__unwrap_iter(std::move(__last)));
  71. }
  72. template <class _Iter, class _Unwrapped>
  73. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter __rewrap_range(_Iter __orig_iter, _Unwrapped __iter) {
  74. return std::__rewrap_iter(std::move(__orig_iter), std::move(__iter));
  75. }
  76. #endif // _LIBCPP_STD_VER >= 20
  77. _LIBCPP_END_NAMESPACE_STD
  78. #endif // _LIBCPP___ALGORITHM_UNWRAP_RANGE_H