next.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___ITERATOR_NEXT_H
  10. #define _LIBCPP___ITERATOR_NEXT_H
  11. #include <__assert>
  12. #include <__config>
  13. #include <__iterator/advance.h>
  14. #include <__iterator/concepts.h>
  15. #include <__iterator/incrementable_traits.h>
  16. #include <__iterator/iterator_traits.h>
  17. #include <__type_traits/enable_if.h>
  18. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  19. # pragma GCC system_header
  20. #endif
  21. _LIBCPP_BEGIN_NAMESPACE_STD
  22. template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> = 0>
  23. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17
  24. _InputIter
  25. next(_InputIter __x, typename iterator_traits<_InputIter>::difference_type __n = 1) {
  26. _LIBCPP_ASSERT_UNCATEGORIZED(__n >= 0 || __has_bidirectional_iterator_category<_InputIter>::value,
  27. "Attempt to next(it, n) with negative n on a non-bidirectional iterator");
  28. _VSTD::advance(__x, __n);
  29. return __x;
  30. }
  31. #if _LIBCPP_STD_VER >= 20
  32. // [range.iter.op.next]
  33. namespace ranges {
  34. namespace __next {
  35. struct __fn {
  36. template <input_or_output_iterator _Ip>
  37. _LIBCPP_HIDE_FROM_ABI
  38. constexpr _Ip operator()(_Ip __x) const {
  39. ++__x;
  40. return __x;
  41. }
  42. template <input_or_output_iterator _Ip>
  43. _LIBCPP_HIDE_FROM_ABI
  44. constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n) const {
  45. ranges::advance(__x, __n);
  46. return __x;
  47. }
  48. template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
  49. _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, _Sp ___bound_sentinel) const {
  50. ranges::advance(__x, ___bound_sentinel);
  51. return __x;
  52. }
  53. template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
  54. _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n, _Sp ___bound_sentinel) const {
  55. ranges::advance(__x, __n, ___bound_sentinel);
  56. return __x;
  57. }
  58. };
  59. } // namespace __next
  60. inline namespace __cpo {
  61. inline constexpr auto next = __next::__fn{};
  62. } // namespace __cpo
  63. } // namespace ranges
  64. #endif // _LIBCPP_STD_VER >= 20
  65. _LIBCPP_END_NAMESPACE_STD
  66. #endif // _LIBCPP___ITERATOR_NEXT_H