cxtream  0.5.1
C++17 data pipeline with Python bindings.
Classes | Typedefs | Functions
Tuple and variadic template utilites.

Classes

struct  cxtream::utility::variadic_find< T1, T2, Ts >
 Get the first index of a type in a variadic template list. More...
 
struct  cxtream::utility::maybe_tuple_impl< N, Ts >
 Wrap variadic template pack in a tuple if there is more than one type. More...
 
struct  cxtream::utility::tuple_contains< T, Tuple >
 Check whether a tuple contains a given type. More...
 

Typedefs

template<std::size_t Offset, std::size_t N>
using cxtream::utility::make_offset_index_sequence = decltype(plus< Offset >(std::make_index_sequence< N >{}))
 Make std::index_sequence with the given offset. More...
 

Functions

template<std::size_t Value, std::size_t... Is>
constexpr std::index_sequence<(Value+Is)... > cxtream::utility::plus (std::index_sequence< Is... >)
 Add a number to all values in std::index_sequence. More...
 
template<typename Tuple , typename Fun >
constexpr auto cxtream::utility::tuple_for_each (Tuple &&tuple, Fun &&fun)
 Apply a function on each element of a tuple. More...
 
template<typename Tuple , typename Fun >
constexpr auto cxtream::utility::tuple_transform (Tuple &&tuple, Fun &&fun)
 Transform each element of a tuple. More...
 
template<typename... Types, typename Tuple >
constexpr auto cxtream::utility::tuple_type_view (Tuple &tuple)
 Makes a sub-tuple made of references to the original tuple (selected by type). More...
 
template<std::size_t... Idxs, typename Tuple >
constexpr auto cxtream::utility::tuple_index_view (Tuple &tuple, std::index_sequence< Idxs... >={})
 Makes a sub-tuple made of references to the original tuple (selected by index). More...
 
template<typename... Tuples>
constexpr auto cxtream::utility::tuple_cat_unique (Tuples &&... tuples)
 Concatenate two tuples and keep only the first element of each type. More...
 
template<typename Tuple , size_t... Is>
std::ostream & cxtream::utility::tuple_print (std::ostream &out, const Tuple &tuple, std::index_sequence< Is... >)
 Tuple pretty printing to std::ostream.
 
template<typename... Ts>
std::ostream & cxtream::utility::operator<< (std::ostream &out, const std::tuple< Ts... > &tuple)
 Tuple pretty printing to std::ostream.
 
template<typename... Rem, typename Tuple >
constexpr auto cxtream::utility::tuple_remove (Tuple tuple)
 Remove types from a tuple. More...
 
template<typename Rng , CONCEPT_REQUIRES_(ranges::Range< Rng >() &&!ranges::View< Rng >()) >
auto cxtream::utility::unzip (Rng range_of_tuples)
 Unzips a range of tuples to a tuple of ranges. More...
 
template<bool Enable, typename RangeT >
decltype(auto) cxtream::utility::unzip_if (RangeT &&range)
 Unzips a range of tuples to a tuple of ranges if a constexpr condition holds. More...
 
template<typename Tuple >
decltype(auto) cxtream::utility::maybe_untuple (Tuple &&tuple)
 Extract a value from a tuple if the tuple contains only a single value. More...
 
template<std::size_t N, typename RARng >
constexpr auto cxtream::utility::range_to_tuple (RARng &&rng)
 Converts a range to a tuple. More...
 
template<std::size_t N, typename Fun >
constexpr Fun cxtream::utility::times_with_index (Fun &&fun)
 Repeat a function N times in compile time. More...
 
template<typename Tuple , typename Fun >
constexpr auto cxtream::utility::tuple_for_each_with_index (Tuple &&tuple, Fun &&fun)
 Similar to tuple_for_each(), but with index available. More...
 
template<typename Tuple , typename Fun >
constexpr auto cxtream::utility::tuple_transform_with_index (Tuple &&tuple, Fun &&fun)
 Similar to tuple_transform(), but with index available. More...
 

Detailed Description

Typedef Documentation

◆ make_offset_index_sequence

template<std::size_t Offset, std::size_t N>
using cxtream::utility::make_offset_index_sequence = typedef decltype(plus<Offset>(std::make_index_sequence<N>{}))

Make std::index_sequence with the given offset.

Example:

std::is_same<decltype(make_offset_index_sequence<3, 4>()),
std::index_sequence<3, 4, 5, 6>>;

Definition at line 92 of file tuple.hpp.

Function Documentation

◆ maybe_untuple()

template<typename Tuple >
decltype(auto) cxtream::utility::maybe_untuple ( Tuple &&  tuple)

Extract a value from a tuple if the tuple contains only a single value.

If the tuple contains zero or more than one element, this is an identity.

Example:

std::tuple<int, double> t1{1, 3.};
auto t2 = maybe_untuple(t1);
static_assert(std::is_same_v<decltype(t2), std::tuple<int, double>>);
std::tuple<int> t3{1};
auto t4 = maybe_untuple(t3);
static_assert(std::is_same_v<decltype(t4), int>);
int i = 1;
std::tuple<int&> t5{i};
auto& t6 = maybe_untuple(t5);
static_assert(std::is_same_v<decltype(t6), int&>);
t6 = 2;
BOOST_TEST(i == 2);

Definition at line 522 of file tuple.hpp.

◆ plus()

template<std::size_t Value, std::size_t... Is>
constexpr std::index_sequence<(Value + Is)...> cxtream::utility::plus ( std::index_sequence< Is... >  )

Add a number to all values in std::index_sequence.

Example:

std::is_same<decltype(plus<2>(std::index_sequence<1, 3, 4>{})),
std::index_sequence<3, 5, 6>>;

Definition at line 78 of file tuple.hpp.

◆ range_to_tuple()

template<std::size_t N, typename RARng >
constexpr auto cxtream::utility::range_to_tuple ( RARng &&  rng)

Converts a range to a tuple.

Example:

std::vector<std::unique_ptr<int>> data;
data.emplace_back(std::make_unique<int>(5));
data.emplace_back(std::make_unique<int>(6));
data.emplace_back(std::make_unique<int>(7));
auto tpl = range_to_tuple<3>(std::move(data));
assert(tpl == std::make_tuple(5, 6, 7));

Definition at line 554 of file tuple.hpp.

◆ times_with_index()

template<std::size_t N, typename Fun >
constexpr Fun cxtream::utility::times_with_index ( Fun &&  fun)

Repeat a function N times in compile time.

Example:

auto tpl = std::make_tuple(1, 0.25, 'a');
times_with_index<3>([&tpl](auto index) {
++std::get<index>(tpl);
});
assert(tpl == std::make_tuple(2, 1.25, 'b'));

Definition at line 586 of file tuple.hpp.

◆ tuple_cat_unique()

template<typename... Tuples>
constexpr auto cxtream::utility::tuple_cat_unique ( Tuples &&...  tuples)

Concatenate two tuples and keep only the first element of each type.

Note: All the reference types are decayed during this operation.

Example:

auto t1 = std::make_tuple(0, '1');
auto t2 = std::make_tuple(2, '3', 5.);
auto t3 = tuple_cat_unique(t1, t2);
static_assert(std::is_same<std::tuple<int, char, double>, decltype(t3)>{});
assert(t2 == std::make_tuple(0, '1', 5.));

Definition at line 291 of file tuple.hpp.

◆ tuple_for_each()

template<typename Tuple , typename Fun >
constexpr auto cxtream::utility::tuple_for_each ( Tuple &&  tuple,
Fun &&  fun 
)

Apply a function on each element of a tuple.

The order of application is from the first to the last element.

Example:

auto tpl = std::make_tuple(5, 2.);
tuple_for_each(tpl, [](auto& val) { std::cout << val << '\n'; });
Returns
The function after application.

Definition at line 120 of file tuple.hpp.

◆ tuple_for_each_with_index()

template<typename Tuple , typename Fun >
constexpr auto cxtream::utility::tuple_for_each_with_index ( Tuple &&  tuple,
Fun &&  fun 
)

Similar to tuple_for_each(), but with index available.

Example:

auto tpl = std::make_tuple(1, 2.);
tuple_for_each_with_index(tpl, [](auto& val, auto index) {
val += index;
});
assert(tpl == std::make_tuple(1, 3.));

Definition at line 605 of file tuple.hpp.

◆ tuple_index_view()

template<std::size_t... Idxs, typename Tuple >
constexpr auto cxtream::utility::tuple_index_view ( Tuple &  tuple,
std::index_sequence< Idxs... >  = {} 
)

Makes a sub-tuple made of references to the original tuple (selected by index).

Example:

auto tpl = std::make_tuple(0, 5., 'c');
auto subtpl = tuple_index_view<2, 0>(t1);
// or equivalently: auto subtpl = tuple_index_view(t1, std::index_sequence<2, 0>{});
static_assert(std::is_same<std::tuple<char&, int&>, decltype(subtpl)>{});
assert(subtpl == std::tuple<char, int>{'c', 0});
Returns
The view of the original tuple.

Definition at line 205 of file tuple.hpp.

◆ tuple_remove()

template<typename... Rem, typename Tuple >
constexpr auto cxtream::utility::tuple_remove ( Tuple  tuple)

Remove types from a tuple.

Note: All the reference types are decayed during this operation.

Example:

auto t1 = std::make_tuple(0, '1');
auto t2 = tuple_remove<int>(t1);
static_assert(std::is_same<std::tuple<char>, decltype(t2)>{});
assert(t2 == std::make_tuple('1'));
auto t3 = tuple_remove<int, char>(std::make_tuple(0, 'a', 3L, 'b'));
static_assert(std::is_same<std::tuple<long>, decltype(t3)>{});
assert(t3 == std::make_tuple(3L));

Definition at line 334 of file tuple.hpp.

◆ tuple_transform()

template<typename Tuple , typename Fun >
constexpr auto cxtream::utility::tuple_transform ( Tuple &&  tuple,
Fun &&  fun 
)

Transform each element of a tuple.

The order of application is unspecified.

Example:

auto t1 = std::make_tuple(0, 10L, 5.);
auto t2 = tuple_transform(t1, [](const auto &v) { return v + 1; });
static_assert(std::is_same<std::tuple<int, long, double>, decltype(t2)>{});
assert(t2 == std::make_tuple(0 + 1, 10L + 1, 5. + 1));
Returns
The transformed tuple.

Definition at line 155 of file tuple.hpp.

◆ tuple_transform_with_index()

template<typename Tuple , typename Fun >
constexpr auto cxtream::utility::tuple_transform_with_index ( Tuple &&  tuple,
Fun &&  fun 
)

Similar to tuple_transform(), but with index available.

Example:

auto tpl = std::make_tuple(1, 0.25, 'a');
auto tpl2 = tuple_transform_with_index(tpl, [](auto&& elem, auto index) {
return elem + index;
});
assert(tpl2 == std::make_tuple(1, 1.25, 'c'));

Definition at line 641 of file tuple.hpp.

◆ tuple_type_view()

template<typename... Types, typename Tuple >
constexpr auto cxtream::utility::tuple_type_view ( Tuple &  tuple)

Makes a sub-tuple made of references to the original tuple (selected by type).

Example:

auto tpl = std::make_tuple(0, 5., 'c');
auto subtpl = tuple_type_view<char, int>(t1);
static_assert(std::is_same<std::tuple<char&, int&>, decltype(subtpl)>{});
assert(subtpl == std::tuple<char, int>{'c', 0});
Returns
The view of the original tuple.

Definition at line 186 of file tuple.hpp.

◆ unzip()

template<typename Rng , CONCEPT_REQUIRES_(ranges::Range< Rng >() &&!ranges::View< Rng >()) >
auto cxtream::utility::unzip ( Rng  range_of_tuples)

Unzips a range of tuples to a tuple of ranges.

Specialization of unzip function for views.

Example:

std::vector<std::tuple<int, double>> data{};
data.emplace_back(1, 5.);
data.emplace_back(2, 6.);
data.emplace_back(3, 7.);
std::vector<int> va;
std::vector<double> vb;
std::tie(va, vb) = unzip(data);

Definition at line 408 of file tuple.hpp.

◆ unzip_if()

template<bool Enable, typename RangeT >
decltype(auto) cxtream::utility::unzip_if ( RangeT &&  range)

Unzips a range of tuples to a tuple of ranges if a constexpr condition holds.

This method is enabled or disabled by its first template parameter. If disabled, it returns identity. If enabled, it returns the same thing as unzip() would return.

Example:

std::vector<std::tuple<int, double>> data{};
data.emplace_back(1, 5.);
data.emplace_back(2, 6.);
data.emplace_back(3, 7.);
std::vector<int> va;
std::vector<double> vb;
std::tie(va, vb) = unzip_if<true>(data);
std::vector<int> vc = unzip_if<false>(va);

Definition at line 468 of file tuple.hpp.