xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/shift_left.h (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
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_SHIFT_LEFT_H
10 #define _LIBCPP___ALGORITHM_SHIFT_LEFT_H
11 
12 #include <__algorithm/move.h>
13 #include <__config>
14 #include <__iterator/iterator_traits.h>
15 
16 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17 #  pragma GCC system_header
18 #endif
19 
20 _LIBCPP_BEGIN_NAMESPACE_STD
21 
22 #if _LIBCPP_STD_VER >= 20
23 
24 template <class _ForwardIterator>
25 inline _LIBCPP_HIDE_FROM_ABI constexpr _ForwardIterator
26 shift_left(_ForwardIterator __first,
27            _ForwardIterator __last,
28            typename iterator_traits<_ForwardIterator>::difference_type __n) {
29   if (__n == 0) {
30     return __last;
31   }
32 
33   _ForwardIterator __m = __first;
34   if constexpr (__has_random_access_iterator_category<_ForwardIterator>::value) {
35     if (__n >= __last - __first) {
36       return __first;
37     }
38     __m += __n;
39   } else {
40     for (; __n > 0; --__n) {
41       if (__m == __last) {
42         return __first;
43       }
44       ++__m;
45     }
46   }
47   return std::move(__m, __last, __first);
48 }
49 
50 #endif // _LIBCPP_STD_VER >= 20
51 
52 _LIBCPP_END_NAMESPACE_STD
53 
54 #endif // _LIBCPP___ALGORITHM_SHIFT_LEFT_H
55