You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

generate.hpp 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2014-present
  5. //
  6. // Use, modification and distribution is subject to the
  7. // Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // Project home: https://github.com/ericniebler/range-v3
  12. //
  13. #ifndef RANGES_V3_ALGORITHM_GENERATE_HPP
  14. #define RANGES_V3_ALGORITHM_GENERATE_HPP
  15. #include <utility>
  16. #include <range/v3/range_fwd.hpp>
  17. #include <range/v3/algorithm/result_types.hpp>
  18. #include <range/v3/functional/invoke.hpp>
  19. #include <range/v3/functional/reference_wrapper.hpp>
  20. #include <range/v3/iterator/concepts.hpp>
  21. #include <range/v3/range/access.hpp>
  22. #include <range/v3/range/concepts.hpp>
  23. #include <range/v3/range/dangling.hpp>
  24. #include <range/v3/range/traits.hpp>
  25. #include <range/v3/utility/static_const.hpp>
  26. namespace ranges
  27. {
  28. /// \addtogroup group-algorithms
  29. /// @{
  30. template<typename O, typename F>
  31. using generate_result = detail::out_fun_result<O, F>;
  32. RANGES_BEGIN_NIEBLOID(generate)
  33. /// \brief function template \c generate_n
  34. template<typename O, typename S, typename F>
  35. auto RANGES_FUN_NIEBLOID(generate)(O first, S last, F fun) //
  36. ->CPP_ret(generate_result<O, F>)( //
  37. requires invocable<F &> && output_iterator<O, invoke_result_t<F &>> &&
  38. sentinel_for<S, O>)
  39. {
  40. for(; first != last; ++first)
  41. *first = invoke(fun);
  42. return {detail::move(first), detail::move(fun)};
  43. }
  44. /// \overload
  45. template<typename Rng, typename F>
  46. auto RANGES_FUN_NIEBLOID(generate)(Rng && rng, F fun)
  47. ->CPP_ret(generate_result<safe_iterator_t<Rng>, F>)( //
  48. requires invocable<F &> && output_range<Rng, invoke_result_t<F &>>)
  49. {
  50. return {(*this)(begin(rng), end(rng), ref(fun)).out, detail::move(fun)};
  51. }
  52. RANGES_END_NIEBLOID(generate)
  53. namespace cpp20
  54. {
  55. using ranges::generate;
  56. using ranges::generate_result;
  57. } // namespace cpp20
  58. /// @}
  59. } // namespace ranges
  60. #endif // include guard