Skip to content

Commit 829cbf4

Browse files
committed
Move from the stored function in generate_n if we are generating only one value.
1 parent d8e2817 commit 829cbf4

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

source/containers/algorithms/generate.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@ import containers.array;
1414
import containers.common_iterator_functions;
1515
import containers.index_type;
1616
import containers.offset_type;
17+
import containers.reference_wrapper;
1718
import containers.stored_function;
1819

1920
import bounded;
2021
import numeric_traits;
2122
import std_module;
2223

23-
// TODO: Implement `generate`, then `generate_n(f, n)` is `take(generate(f), n)`
24-
2524
using namespace bounded::literal;
2625

2726
namespace containers {
@@ -33,6 +32,15 @@ concept construct_subtractable = requires(LHS const lhs, RHS const rhs) {
3332

3433
struct generate_sentinel {};
3534

35+
template<typename Offset>
36+
constexpr auto get_generator(auto && generator) -> auto && {
37+
if constexpr (numeric_traits::max_value<Offset> <= 1_bi) {
38+
return std::move(containers::unwrap(generator));
39+
} else {
40+
return containers::unwrap(generator);
41+
}
42+
}
43+
3644
template<typename Offset, typename Function>
3745
struct generate_n_iterator {
3846
using iterator_category = std::input_iterator_tag;
@@ -44,8 +52,8 @@ struct generate_n_iterator {
4452
{
4553
}
4654

47-
constexpr decltype(auto) operator*() const {
48-
return std::invoke(m_generator);
55+
constexpr auto operator*() const -> decltype(auto) {
56+
return std::invoke(::containers::get_generator<Offset>(m_generator));
4957
}
5058
OPERATORS_ARROW_DEFINITIONS
5159

0 commit comments

Comments
 (0)