ranges_generate.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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_RANGES_GENERATE_H
  9. #define _LIBCPP___ALGORITHM_RANGES_GENERATE_H
  10. #include <__concepts/constructible.h>
  11. #include <__concepts/invocable.h>
  12. #include <__config>
  13. #include <__functional/invoke.h>
  14. #include <__iterator/concepts.h>
  15. #include <__iterator/iterator_traits.h>
  16. #include <__ranges/access.h>
  17. #include <__ranges/concepts.h>
  18. #include <__ranges/dangling.h>
  19. #include <__utility/move.h>
  20. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  21. # pragma GCC system_header
  22. #endif
  23. _LIBCPP_PUSH_MACROS
  24. #include <__undef_macros>
  25. #if _LIBCPP_STD_VER >= 20
  26. _LIBCPP_BEGIN_NAMESPACE_STD
  27. namespace ranges {
  28. namespace __generate {
  29. struct __fn {
  30. template <class _OutIter, class _Sent, class _Func>
  31. _LIBCPP_HIDE_FROM_ABI constexpr static _OutIter __generate_fn_impl(_OutIter __first, _Sent __last, _Func& __gen) {
  32. for (; __first != __last; ++__first) {
  33. *__first = __gen();
  34. }
  35. return __first;
  36. }
  37. template <input_or_output_iterator _OutIter, sentinel_for<_OutIter> _Sent, copy_constructible _Func>
  38. requires invocable<_Func&> && indirectly_writable<_OutIter, invoke_result_t<_Func&>>
  39. _LIBCPP_HIDE_FROM_ABI constexpr _OutIter operator()(_OutIter __first, _Sent __last, _Func __gen) const {
  40. return __generate_fn_impl(std::move(__first), std::move(__last), __gen);
  41. }
  42. template <class _Range, copy_constructible _Func>
  43. requires invocable<_Func&> && output_range<_Range, invoke_result_t<_Func&>>
  44. _LIBCPP_HIDE_FROM_ABI constexpr borrowed_iterator_t<_Range> operator()(_Range&& __range, _Func __gen) const {
  45. return __generate_fn_impl(ranges::begin(__range), ranges::end(__range), __gen);
  46. }
  47. };
  48. } // namespace __generate
  49. inline namespace __cpo {
  50. inline constexpr auto generate = __generate::__fn{};
  51. } // namespace __cpo
  52. } // namespace ranges
  53. _LIBCPP_END_NAMESPACE_STD
  54. #endif // _LIBCPP_STD_VER >= 20
  55. _LIBCPP_POP_MACROS
  56. #endif // _LIBCPP___ALGORITHM_RANGES_GENERATE_H