unwrap_iter.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_ITER_H
  9. #define _LIBCPP___ALGORITHM_UNWRAP_ITER_H
  10. #include <__config>
  11. #include <__iterator/iterator_traits.h>
  12. #include <__memory/pointer_traits.h>
  13. #include <type_traits>
  14. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  15. # pragma GCC system_header
  16. #endif
  17. _LIBCPP_BEGIN_NAMESPACE_STD
  18. // The job of __unwrap_iter is to lower contiguous iterators (such as
  19. // vector<T>::iterator) into pointers, to reduce the number of template
  20. // instantiations and to enable pointer-based optimizations e.g. in std::copy.
  21. // For iterators that are not contiguous, it must be a no-op.
  22. // In debug mode, we don't do this.
  23. //
  24. // __unwrap_iter is non-constexpr for user-defined iterators whose
  25. // `to_address` and/or `operator->` is non-constexpr. This is okay; but we
  26. // try to avoid doing __unwrap_iter in constant-evaluated contexts anyway.
  27. //
  28. // Some algorithms (e.g. std::copy, but not std::sort) need to convert an
  29. // "unwrapped" result back into a contiguous iterator. Since contiguous iterators
  30. // are random-access, we can do this portably using iterator arithmetic; this
  31. // is the job of __rewrap_iter.
  32. template <class _Iter, bool = __is_cpp17_contiguous_iterator<_Iter>::value>
  33. struct __unwrap_iter_impl {
  34. static _LIBCPP_CONSTEXPR _Iter
  35. __apply(_Iter __i) _NOEXCEPT {
  36. return __i;
  37. }
  38. };
  39. #ifndef _LIBCPP_ENABLE_DEBUG_MODE
  40. template <class _Iter>
  41. struct __unwrap_iter_impl<_Iter, true> {
  42. static _LIBCPP_CONSTEXPR decltype(_VSTD::__to_address(declval<_Iter>()))
  43. __apply(_Iter __i) _NOEXCEPT {
  44. return _VSTD::__to_address(__i);
  45. }
  46. };
  47. #endif // !_LIBCPP_ENABLE_DEBUG_MODE
  48. template<class _Iter, class _Impl = __unwrap_iter_impl<_Iter> >
  49. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  50. decltype(_Impl::__apply(declval<_Iter>()))
  51. __unwrap_iter(_Iter __i) _NOEXCEPT
  52. {
  53. return _Impl::__apply(__i);
  54. }
  55. template <class _OrigIter, class _UnwrappedIter>
  56. struct __rewrap_iter_impl {
  57. static _LIBCPP_CONSTEXPR _OrigIter __apply(_OrigIter __first, _UnwrappedIter __result) {
  58. // Precondition: __result is reachable from __first
  59. // Precondition: _OrigIter is a contiguous iterator
  60. return __first + (__result - std::__unwrap_iter(__first));
  61. }
  62. };
  63. template <class _OrigIter>
  64. struct __rewrap_iter_impl<_OrigIter, _OrigIter> {
  65. static _LIBCPP_CONSTEXPR _OrigIter __apply(_OrigIter, _OrigIter __result) {
  66. return __result;
  67. }
  68. };
  69. template<class _OrigIter>
  70. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
  71. _OrigIter __rewrap_iter(_OrigIter, _OrigIter __result)
  72. {
  73. return __result;
  74. }
  75. template<class _OrigIter, class _UnwrappedIter, class _Impl = __rewrap_iter_impl<_OrigIter, _UnwrappedIter> >
  76. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
  77. _OrigIter __rewrap_iter(_OrigIter __first, _UnwrappedIter __result)
  78. {
  79. return _Impl::__apply(__first, __result);
  80. }
  81. _LIBCPP_END_NAMESPACE_STD
  82. #endif // _LIBCPP___ALGORITHM_UNWRAP_ITER_H