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_n.hpp 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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_N_HPP
  14. #define RANGES_V3_ALGORITHM_GENERATE_N_HPP
  15. #include <tuple>
  16. #include <utility>
  17. #include <range/v3/range_fwd.hpp>
  18. #include <range/v3/algorithm/result_types.hpp>
  19. #include <range/v3/functional/invoke.hpp>
  20. #include <range/v3/iterator/concepts.hpp>
  21. #include <range/v3/iterator/operations.hpp>
  22. #include <range/v3/range/access.hpp>
  23. #include <range/v3/range/concepts.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_n_result = detail::out_fun_result<O, F>;
  32. RANGES_BEGIN_NIEBLOID(generate_n)
  33. /// \brief function template \c generate_n
  34. template<typename O, typename F>
  35. auto RANGES_FUN_NIEBLOID(generate_n)(O first, iter_difference_t<O> n, F fun)
  36. ->CPP_ret(generate_n_result<O, F>)( //
  37. requires invocable<F &> && output_iterator<O, invoke_result_t<F &>>)
  38. {
  39. RANGES_EXPECT(n >= 0);
  40. auto norig = n;
  41. auto b = uncounted(first);
  42. for(; 0 != n; ++b, --n)
  43. *b = invoke(fun);
  44. return {recounted(first, b, norig), detail::move(fun)};
  45. }
  46. RANGES_END_NIEBLOID(generate_n)
  47. namespace cpp20
  48. {
  49. using ranges::generate_n;
  50. using ranges::generate_n_result;
  51. } // namespace cpp20
  52. // @}
  53. } // namespace ranges
  54. #endif // include guard