70 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef _LIBCPP___ALGORITHM_RANGES_GENERATE_H10#define _LIBCPP___ALGORITHM_RANGES_GENERATE_H11 12#include <__concepts/constructible.h>13#include <__concepts/invocable.h>14#include <__config>15#include <__iterator/concepts.h>16#include <__iterator/iterator_traits.h>17#include <__ranges/access.h>18#include <__ranges/concepts.h>19#include <__ranges/dangling.h>20#include <__type_traits/invoke.h>21#include <__utility/move.h>22 23#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)24# pragma GCC system_header25#endif26 27_LIBCPP_PUSH_MACROS28#include <__undef_macros>29 30#if _LIBCPP_STD_VER >= 2031 32_LIBCPP_BEGIN_NAMESPACE_STD33 34namespace ranges {35struct __generate {36 template <class _OutIter, class _Sent, class _Func>37 _LIBCPP_HIDE_FROM_ABI constexpr static _OutIter __generate_fn_impl(_OutIter __first, _Sent __last, _Func& __gen) {38 for (; __first != __last; ++__first) {39 *__first = __gen();40 }41 42 return __first;43 }44 45 template <input_or_output_iterator _OutIter, sentinel_for<_OutIter> _Sent, copy_constructible _Func>46 requires invocable<_Func&> && indirectly_writable<_OutIter, invoke_result_t<_Func&>>47 _LIBCPP_HIDE_FROM_ABI constexpr _OutIter operator()(_OutIter __first, _Sent __last, _Func __gen) const {48 return __generate_fn_impl(std::move(__first), std::move(__last), __gen);49 }50 51 template <class _Range, copy_constructible _Func>52 requires invocable<_Func&> && output_range<_Range, invoke_result_t<_Func&>>53 _LIBCPP_HIDE_FROM_ABI constexpr borrowed_iterator_t<_Range> operator()(_Range&& __range, _Func __gen) const {54 return __generate_fn_impl(ranges::begin(__range), ranges::end(__range), __gen);55 }56};57 58inline namespace __cpo {59inline constexpr auto generate = __generate{};60} // namespace __cpo61} // namespace ranges62 63_LIBCPP_END_NAMESPACE_STD64 65#endif // _LIBCPP_STD_VER >= 2066 67_LIBCPP_POP_MACROS68 69#endif // _LIBCPP___ALGORITHM_RANGES_GENERATE_H70