shift_left.h 1.5 KB

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