xref: /llvm-project/libcxx/include/__algorithm/iterator_operations.h (revision 1f04759316204e677b7bc8c049ea259253a2795d)
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_ITERATOR_OPERATIONS_H
10 #define _LIBCPP___ALGORITHM_ITERATOR_OPERATIONS_H
11 
12 #include <__algorithm/iter_swap.h>
13 #include <__config>
14 #include <__iterator/advance.h>
15 #include <__iterator/distance.h>
16 #include <__iterator/iter_move.h>
17 #include <__iterator/iter_swap.h>
18 #include <__iterator/iterator_traits.h>
19 #include <__iterator/next.h>
20 #include <__utility/forward.h>
21 #include <__utility/move.h>
22 #include <type_traits>
23 
24 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25 #  pragma GCC system_header
26 #endif
27 
28 _LIBCPP_BEGIN_NAMESPACE_STD
29 
30 template <class _AlgPolicy> struct _IterOps;
31 
32 #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
33 struct _RangeAlgPolicy {};
34 
35 template <>
36 struct _IterOps<_RangeAlgPolicy> {
37   static constexpr auto advance = ranges::advance;
38   static constexpr auto distance = ranges::distance;
39   static constexpr auto __iter_move = ranges::iter_move;
40   static constexpr auto iter_swap = ranges::iter_swap;
41   static constexpr auto next = ranges::next;
42 };
43 #endif
44 
45 struct _ClassicAlgPolicy {};
46 
47 template <>
48 struct _IterOps<_ClassicAlgPolicy> {
49 
50   // advance
51   template <class _Iter, class _Distance>
52   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
53   static void advance(_Iter& __iter, _Distance __count) {
54     std::advance(__iter, __count);
55   }
56 
57   // distance
58   template <class _Iter>
59   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
60   static typename iterator_traits<_Iter>::difference_type distance(_Iter __first, _Iter __last) {
61     return std::distance(__first, __last);
62   }
63 
64   // iter_move
65   template <class _Iter>
66   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
67   // Declaring the return type is necessary for the C++03 mode (which doesn't support placeholder return types).
68   static typename iterator_traits<__uncvref_t<_Iter> >::value_type&& __iter_move(_Iter&& __i) {
69     return std::move(*std::forward<_Iter>(__i));
70   }
71 
72   // iter_swap
73   template <class _Iter1, class _Iter2>
74   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
75   static void iter_swap(_Iter1&& __a, _Iter2&& __b) {
76     std::iter_swap(std::forward<_Iter1>(__a), std::forward<_Iter2>(__b));
77   }
78 
79   // next
80   template <class _Iterator>
81   _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_AFTER_CXX11
82   _Iterator next(_Iterator, _Iterator __last) {
83     return __last;
84   }
85 };
86 
87 _LIBCPP_END_NAMESPACE_STD
88 
89 #endif // _LIBCPP___ALGORITHM_ITERATOR_OPERATIONS_H
90