unwrap_iter.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 <__memory/pointer_traits.h>
  12. #include <iterator>
  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. #if _LIBCPP_DEBUG_LEVEL < 2
  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_DEBUG_LEVEL < 2
  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>
  56. _LIBCPP_HIDE_FROM_ABI
  57. _OrigIter __rewrap_iter(_OrigIter, _OrigIter __result)
  58. {
  59. return __result;
  60. }
  61. template<class _OrigIter, class _UnwrappedIter>
  62. _LIBCPP_HIDE_FROM_ABI
  63. _OrigIter __rewrap_iter(_OrigIter __first, _UnwrappedIter __result)
  64. {
  65. // Precondition: __result is reachable from __first
  66. // Precondition: _OrigIter is a contiguous iterator
  67. return __first + (__result - _VSTD::__unwrap_iter(__first));
  68. }
  69. _LIBCPP_END_NAMESPACE_STD
  70. #endif // _LIBCPP___ALGORITHM_UNWRAP_ITER_H