Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/tuple_algorithms.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ auto t = stdx::tuple{1, 2, 3};
auto sum = stdx::apply([] (auto... args) { return (args + ... + 0); }, t); // 6
----

`stdx::apply` can also be called with a variadic pack of tuples, which are
unpacked and passed to the function:
[source,cpp]
----
auto t1 = stdx::tuple{1, 2, 3};
auto t2 = stdx::tuple{4, 5, 6};
auto sum = stdx::apply([] (auto... args) { return (args + ... + 0); }, t1, t2); // 21
----

=== `cartesian_product`

`cartesian_product` takes any number of tuples and returns the tuple-of-tuples
Expand Down
23 changes: 23 additions & 0 deletions include/stdx/tuple_algorithms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@

namespace stdx {
inline namespace v1 {
template <typename F, tuplelike... Ts> constexpr auto apply(F &&f, Ts &&...ts) {
constexpr auto total_num_elements =
(std::size_t{} + ... + stdx::tuple_size_v<std::remove_cvref_t<Ts>>);

[[maybe_unused]] constexpr auto element_indices = [&] {
std::array<detail::index_pair, total_num_elements> indices{};
[[maybe_unused]] auto p = indices.data();
((p = std::remove_cvref_t<Ts>::fill_inner_indices(p)), ...);
[[maybe_unused]] auto q = indices.data();
[[maybe_unused]] std::size_t n{};
((q = std::remove_cvref_t<Ts>::fill_outer_indices(q, n++)), ...);
return indices;
}();

[[maybe_unused]] auto outer_tuple =
stdx::tuple<Ts &&...>{std::forward<Ts>(ts)...};
return [&]<std::size_t... Is>(std::index_sequence<Is...>) {
return std::forward<F>(f)(
std::move(outer_tuple)[index<element_indices[Is].outer>]
[index<element_indices[Is].inner>]...);
}(std::make_index_sequence<total_num_elements>{});
}

template <tuplelike... Ts> [[nodiscard]] constexpr auto tuple_cat(Ts &&...ts) {
if constexpr (sizeof...(Ts) == 0) {
return stdx::tuple<>{};
Expand Down
64 changes: 64 additions & 0 deletions test/tuple_algorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <catch2/catch_template_test_macros.hpp>
#include <catch2/catch_test_macros.hpp>

#include <algorithm>
#include <array>
#include <functional>
#include <type_traits>
Expand Down Expand Up @@ -77,19 +78,82 @@ TEST_CASE("apply", "[tuple_algorithms]") {
stdx::tuple{}) == 0);
static_assert(stdx::apply([](auto... xs) { return (0 + ... + xs); },
stdx::tuple{1, 2, 3}) == 6);
}

TEST_CASE("apply handles a stateful function properly", "[tuple_algorithms]") {
auto stateful = [calls = 0](auto...) mutable { return ++calls; };
CHECK(stdx::apply(stateful, stdx::tuple{1, 2, 3}) == 1);
CHECK(stdx::apply(stateful, stdx::tuple{1, 2, 3}) == 2);
}

TEST_CASE("apply handles move-only types", "[tuple_algorithms]") {
static_assert(stdx::apply([](auto x) { return x.value; },
stdx::tuple{move_only{42}}) == 42);
}

TEST_CASE("apply handles tuples of references", "[tuple_algorithms]") {
auto t = stdx::tuple{1, 2, 3};
stdx::apply([](auto &...xs) { (++xs, ...); }, t);
CHECK(t == stdx::tuple{2, 3, 4});
}

TEST_CASE("apply preserves argument order", "[tuple_algorithms]") {
int called{};
stdx::apply(
[&](auto... xs) {
++called;
auto const a = std::array{xs...};
CHECK(std::is_sorted(std::begin(a), std::end(a)));
},
stdx::tuple{1, 2, 3});
CHECK(called == 1);
}

TEST_CASE("variadic apply", "[tuple_algorithms]") {
static_assert(stdx::apply([](auto... xs) { return (0 + ... + xs); }) == 0);
static_assert(stdx::apply([](auto... xs) { return (0 + ... + xs); },
stdx::tuple{1, 2, 3},
stdx::tuple{4, 5, 6}) == 21);
}

TEST_CASE("variadic apply handles a stateful function properly",
"[tuple_algorithms]") {
auto stateful = [calls = 0](auto...) mutable { return ++calls; };
CHECK(stdx::apply(stateful) == 1);
CHECK(stdx::apply(stateful) == 2);
}

TEST_CASE("variadic apply handles move-only types", "[tuple_algorithms]") {
static_assert(stdx::apply([](auto x, auto y) { return x.value + y.value; },
stdx::tuple{move_only{42}},
stdx::tuple{move_only{17}}) == 59);
}

TEST_CASE("variadic apply handles tuples of references", "[tuple_algorithms]") {
auto t1 = stdx::tuple{1};
auto t2 = stdx::tuple{2};
stdx::apply(
[](auto &x1, auto &x2) {
x1 += x2;
x2 *= 2;
},
t1, t2);
CHECK(t1 == stdx::tuple{3});
CHECK(t2 == stdx::tuple{4});
}

TEST_CASE("variadic apply preserves argument order", "[tuple_algorithms]") {
int called{};
stdx::apply(
[&](auto... xs) {
++called;
auto const a = std::array{xs...};
CHECK(std::is_sorted(std::begin(a), std::end(a)));
},
stdx::tuple{1, 2, 3}, stdx::tuple{4, 5, 6});
CHECK(called == 1);
}

TEST_CASE("join", "[tuple_algorithms]") {
constexpr auto t = stdx::tuple{1, 2, 3};
static_assert(t.join(std::plus{}) == 6);
Expand Down