reverse.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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_REVERSE_H
  9. #define _LIBCPP___ALGORITHM_REVERSE_H
  10. #include <__algorithm/iter_swap.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_BEGIN_NAMESPACE_STD
  17. template <class _BidirectionalIterator>
  18. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  19. void
  20. __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
  21. {
  22. while (__first != __last)
  23. {
  24. if (__first == --__last)
  25. break;
  26. _VSTD::iter_swap(__first, __last);
  27. ++__first;
  28. }
  29. }
  30. template <class _RandomAccessIterator>
  31. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  32. void
  33. __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
  34. {
  35. if (__first != __last)
  36. for (; __first < --__last; ++__first)
  37. _VSTD::iter_swap(__first, __last);
  38. }
  39. template <class _BidirectionalIterator>
  40. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  41. void
  42. reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
  43. {
  44. _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
  45. }
  46. _LIBCPP_END_NAMESPACE_STD
  47. #endif // _LIBCPP___ALGORITHM_REVERSE_H