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