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_RANGES_FOR_EACH_N_H 10 #define _LIBCPP___ALGORITHM_RANGES_FOR_EACH_N_H 11 12 #include <__algorithm/in_fun_result.h> 13 #include <__config> 14 #include <__functional/identity.h> 15 #include <__functional/invoke.h> 16 #include <__iterator/concepts.h> 17 #include <__iterator/incrementable_traits.h> 18 #include <__iterator/iterator_traits.h> 19 #include <__iterator/projected.h> 20 #include <__ranges/concepts.h> 21 #include <__ranges/dangling.h> 22 #include <__utility/move.h> 23 24 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 25 # pragma GCC system_header 26 #endif 27 28 #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 29 30 _LIBCPP_BEGIN_NAMESPACE_STD 31 32 namespace ranges { 33 34 template <class _Iter, class _Func> 35 using for_each_n_result = in_fun_result<_Iter, _Func>; 36 37 namespace __for_each_n { 38 struct __fn { 39 40 template <input_iterator _Iter, 41 class _Proj = identity, 42 indirectly_unary_invocable<projected<_Iter, _Proj>> _Func> 43 _LIBCPP_HIDE_FROM_ABI constexpr 44 for_each_n_result<_Iter, _Func> operator()(_Iter __first, 45 iter_difference_t<_Iter> __count, 46 _Func __func, 47 _Proj __proj = {}) const { 48 while (__count-- > 0) { 49 std::invoke(__func, std::invoke(__proj, *__first)); 50 ++__first; 51 } 52 return {std::move(__first), std::move(__func)}; 53 } 54 55 }; 56 } // namespace __for_each_n 57 58 inline namespace __cpo { 59 inline constexpr auto for_each_n = __for_each_n::__fn{}; 60 } // namespace __cpo 61 } // namespace ranges 62 63 _LIBCPP_END_NAMESPACE_STD 64 65 #endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 66 67 #endif // _LIBCPP___ALGORITHM_RANGES_FOR_EACH_N_H 68