This is a follow-up question for std::array and std::vector Type Arbitrary Nested Iterable Generator Functions Implementation in C++ and the previous questions about recursive functions, including A Summation Function For Arbitrary Nested Vector Implementation In C++, A recursive_count_if Function For Various Type Arbitrary Nested Iterable Implementation in C++, A recursive_count_if Function with Specified value_type for Various Type Arbitrary Nested Iterable Implementation in C++, A recursive_count_if Function with Automatic Type Deducing from Lambda for Various Type Arbitrary Nested Iterable Implementation in C++ and A recursive_count_if Function with Unwrap Level for Various Type Arbitrary Nested Iterable Implementation in C++. Besides the std::array
and std::vector
cases, I am trying to implement other functions n_dim_deque_generator
and n_dim_list_generator
which purpose are similar as n_dim_array_generator
and n_dim_vector_generator
. In other words, n_dim_deque_generator
function can construct nested std::deque
s with the given parameters and n_dim_list_generator
function can construct nested std::list
s with the given parameters.
The usage description
As similar to the usage of n_dim_vector_generator
, there are four key parts in the usage n_dim_deque_generator<2, int>(1, 3)
. The first number 2
represents the nested layers, the second parameter int
represents the type of base elements, the third parameter 1
represents the input element which would be filled in, and the fourth parameter 3
represents the element count in each layer.
The experimental implementation
The experimental implementation of n_dim_deque_generator
function and n_dim_list_generator
function:
template<std::size_t dim, class T>
auto n_dim_deque_generator(T input, std::size_t times)
{
if constexpr (dim == 0)
{
return input;
}
else
{
std::deque<decltype(n_dim_deque_generator<dim - 1>(input, times))> output;
output.resize(times);
std::fill(std::begin(output), std::end(output), n_dim_deque_generator<dim - 1>(input, times));
return output;
}
}
template<std::size_t dim, class T>
auto n_dim_list_generator(T input, std::size_t times)
{
if constexpr (dim == 0)
{
return input;
}
else
{
std::list<decltype(n_dim_list_generator<dim - 1>(input, times))> output;
output.resize(times);
std::fill(std::begin(output), std::end(output), n_dim_list_generator<dim - 1>(input, times));
return output;
}
}
Test cases
The test cases of n_dim_deque_generator
function and n_dim_list_generator
function are as below. The test cases of n_dim_vector_generator
function and n_dim_array_generator
function are also listed, for comparison purposes.
constexpr std::size_t dims_setting = 8;
int input_element = 3;
constexpr std::size_t element_count = 2;
// n_dim_vector_generator function usage with recursive_reduce function and recursive_count_if function
auto test_vector1 = n_dim_vector_generator<dims_setting, int>(input_element, element_count);
std::cout << "recursive_reduce output: " << recursive_reduce(test_vector1, 0) << std::endl;
std::cout << "recursive_count_if output: " << recursive_count_if<dims_setting>(test_vector1, [](auto& i) {return i == 3; }) << std::endl;
// n_dim_array_generator function usage with recursive_reduce function and recursive_count_if function
auto test_array1 = n_dim_array_generator<dims_setting, element_count, int>(input_element);
std::cout << "recursive_reduce output: " << recursive_reduce(test_array1, 0) << std::endl;
std::cout << "recursive_count_if output: " << recursive_count_if<dims_setting>(test_array1, [](auto& i) {return i == 3; }) << std::endl;
// n_dim_deque_generator function usage with recursive_reduce function and recursive_count_if function
auto test_deque1 = n_dim_deque_generator<dims_setting, int>(input_element, element_count);
std::cout << "recursive_reduce output: " << recursive_reduce(test_deque1, 0) << std::endl;
std::cout << "recursive_count_if output: " << recursive_count_if<dims_setting>(test_deque1, [](auto& i) {return i == 3; }) << std::endl;
// n_dim_list_generator function usage with recursive_reduce function and recursive_count_if function
auto test_list1 = n_dim_list_generator<dims_setting, int>(input_element, element_count);
std::cout << "recursive_reduce output: " << recursive_reduce(test_list1, 0) << std::endl;
std::cout << "recursive_count_if output: " << recursive_count_if<dims_setting>(test_list1, [](auto& i) {return i == 3; }) << std::endl;
The output of this test is as follows.
recursive_reduce output: 768
recursive_count_if output: 256
recursive_reduce output: 768
recursive_count_if output: 256
recursive_reduce output: 768
recursive_count_if output: 256
recursive_reduce output: 768
recursive_count_if output: 256
Notes
The n_dim_vector_generator
function and n_dim_array_generator
function have been updated as follows. Thanks to G. Sliepen for answering the previous question.
template<std::size_t dim, class T>
auto n_dim_vector_generator(T input, std::size_t times)
{
if constexpr (dim == 0)
{
return input;
}
else
{
std::vector<decltype(n_dim_vector_generator<dim - 1>(input, times))> output;
output.reserve(times);
std::fill_n(std::back_inserter(output), times, n_dim_vector_generator<dim - 1>(input, times));
return output;
}
}
template<std::size_t dim, std::size_t times, class T>
auto n_dim_array_generator(T input)
{
if constexpr (dim == 0)
{
return input;
}
else
{
std::array<decltype(n_dim_array_generator<dim - 1, times>(input)), times> output;
std::fill(std::begin(output), std::end(output), n_dim_array_generator<dim - 1, times>(input));
return output;
}
}
All suggestions are welcome.
The summary information:
Which question it is a follow-up to?
std::array and std::vector Type Arbitrary Nested Iterable Generator Functions Implementation in C++ and
the previous questions about recursive functions, including
What changes has been made in the code since last question?
The
n_dim_deque_generator
function andn_dim_list_generator
function have been added.Why a new review is being asked for?
I am not sure if it is a good idea to use
resize
function andstd::fill
function in bothn_dim_deque_generator
andn_dim_list_generator
implementation. If there is any further possible improvement, please let me know.