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_GENERATE_N_H
10#define _LIBCPP___ALGORITHM_GENERATE_N_H
11
12#include <__algorithm/for_each_n.h>
13#include <__config>
14#include <__functional/identity.h>
15#include <__utility/convert_to_integral.h>
16#include <__utility/forward.h>
17#include <__utility/move.h>
18
19#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20# pragma GCC system_header
21#endif
22
23_LIBCPP_PUSH_MACROS
24#include <__undef_macros>
25
26_LIBCPP_BEGIN_NAMESPACE_STD
27
28template <class _OutputIterator, class _Size, class _Generator>
29inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
30__generate_n(_OutputIterator __first, _Size __n, _Generator& __gen) {
31 if (__n <= 0)
32 return __first;
33
34 using __iter_ref = decltype(*__first);
35 __identity __proj;
36 return std::__for_each_n(
37 std::move(__first), __n, [&](__iter_ref __element) { std::forward<__iter_ref>(__element) = __gen(); }, __proj);
38}
39
40template <class _OutputIterator, class _Size, class _Generator>
41inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
42generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen) {
43 return std::__generate_n(std::move(__first), std::__convert_to_integral(__orig_n), __gen);
44}
45
46_LIBCPP_END_NAMESPACE_STD
47
48_LIBCPP_POP_MACROS
49
50#endif // _LIBCPP___ALGORITHM_GENERATE_N_H
51