xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/unwrap_iter.h (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
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 <__memory/pointer_traits.h>
14 #include <iterator>
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 #if _LIBCPP_DEBUG_LEVEL < 2
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_DEBUG_LEVEL < 2
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>
67 _LIBCPP_HIDE_FROM_ABI
68 _OrigIter __rewrap_iter(_OrigIter, _OrigIter __result)
69 {
70     return __result;
71 }
72 
73 template<class _OrigIter, class _UnwrappedIter>
74 _LIBCPP_HIDE_FROM_ABI
75 _OrigIter __rewrap_iter(_OrigIter __first, _UnwrappedIter __result)
76 {
77     // Precondition: __result is reachable from __first
78     // Precondition: _OrigIter is a contiguous iterator
79     return __first + (__result - _VSTD::__unwrap_iter(__first));
80 }
81 
82 _LIBCPP_END_NAMESPACE_STD
83 
84 #endif // _LIBCPP___ALGORITHM_UNWRAP_ITER_H
85