TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
Tuple and variant tools

Detailed Description

Helpers for manipulating and serializing std::tuple and std::variant objects.

Classes

struct  triqs::utility::overloaded< Fs >
 Lambda-overload helper for std::visit. More...

Typedefs

template<int N, int... Is>
using triqs::tuple::complement_sequence = typename complement_sequence_impl<N, all_indices<Is...>>::type
 Index sequence containing all integers of \( [0, N] \) that are not in Is....
template<typename T, int... Is>
using triqs::tuple::filter_out_t = std::decay_t<decltype(filter_out<Is...>(std::declval<T>()))>
 Result type of filter_out() for tuple type T dropping positions Is....
template<typename T, size_t... Is>
using triqs::tuple::filter_t = std::tuple<std::tuple_element_t<Is, std::decay_t<T>>...>
 Tuple type made of the elements at the given positions of given tuple type T.

Functions

template<typename F, typename T>
decltype(auto) triqs::tuple::apply (F &&f, T &&t)
 Call a function with the elements of a tuple as its arguments.
template<typename C, typename T>
decltype(auto) triqs::tuple::apply_construct (T &&t)
 Brace-construct an object from the elements of a tuple.
template<typename C, typename T>
decltype(auto) triqs::tuple::apply_construct_parenthesis (T &&t)
 Parenthesis-construct an object from the elements of a tuple.
template<typename F>
auto triqs::tuple::called_on_tuple (F &&f)
 Wrap a callable so that it can be invoked with a single tuple argument.
template<size_t... Is, typename T>
filter_t< T, Is... > triqs::tuple::filter (T &&t)
 Keep only the elements of a tuple at the given positions.
template<size_t... Is, typename T>
filter_t< T, Is... > triqs::tuple::filter (T &&t, std::index_sequence< Is... >)
 Keep only the elements of a tuple at the given positions (as an index-sequence).
template<int... Is, typename T>
decltype(auto) triqs::tuple::filter_out (T &&t)
 Drop the elements of a tuple at the given positions.
template<typename F, typename T, typename R>
decltype(auto) triqs::tuple::fold (F &&f, T &&t, R &&r)
 Left-fold a callable over the elements of a tuple.
template<typename F, typename T0, typename T1, typename R>
decltype(auto) triqs::tuple::fold (F &&f, T0 &&t1, T1 &&t2, R &&r)
 Left-fold a callable over the elements of two equally-sized tuples.
template<typename T, typename F>
void triqs::tuple::for_each (T &&t, F &&f)
 Apply a callable to every element of a tuple, in order.
template<typename T, typename F>
void triqs::tuple::for_each_enumerate (T &&t, F &&f)
 Apply a callable to every (index, element) pair of a tuple.
template<typename F, typename... Ts>
void triqs::tuple::for_each_zip (F &&f, Ts &&...ts)
 Apply an N-ary callable across \( N \) tuples zipped together.
template<typename Archive, typename... Ts>
void boost::serialization::load (Archive &ar, std::variant< Ts... > &v, const unsigned int version)
 Deserialize a std::variant.
template<int N, typename X>
auto triqs::tuple::make_tuple_repeat (X const &x)
 Build a tuple with \( N \) copies of a value.
template<typename F, typename T>
decltype(auto) triqs::tuple::map (F &&f, T &&t)
 Map a callable over the elements of a tuple.
template<typename... Ts, typename F>
auto triqs::tuple::map_on_zip (F &&f, Ts &&...ts)
 Map an N-ary callable across N tuples zipped together.
template<typename T1, typename T2>
std::ostream & std::operator<< (std::ostream &os, std::pair< T1, T2 > const &x)
 Write a std::pair to an output stream.
template<typename... T>
std::ostream & std::operator<< (std::ostream &os, std::tuple< T... > const &t)
 Write a std::tuple to an output stream.
template<typename T, typename... Ts>
std::ostream & std::operator<< (std::ostream &os, std::variant< T, Ts... > const &v)
 Write a std::variant to an output stream by visiting the held alternative and forwarding it to the stream.
template<typename T, typename... Ts>
std::ostream & std::operator<< (std::ostream &os, std::vector< std::variant< T, Ts... > > const &vec)
 Write a vector of std::variant as a comma-separated list of its elements.
template<typename T>
auto triqs::tuple::pop_front (T &&t)
 Remove the first element of a tuple.
template<typename T, typename X>
auto triqs::tuple::push_back (T &&t, X &&x)
 Append an element to the end of a tuple.
template<typename T, typename X>
auto triqs::tuple::push_front (T &&t, X &&x)
 Prepend an element to the front of a tuple.
template<int... Is, typename T, typename R>
auto triqs::tuple::replace (T &&t, R &&r)
 Return a copy of a tuple with the elements at the given positions replaced by a given value.
template<typename Archive, typename... Ts>
void boost::serialization::save (Archive &ar, std::variant< Ts... > const &v, const unsigned int version)
 Serialize a std::variant.
template<typename Archive, typename... Ts>
void boost::serialization::serialize (Archive &ar, std::tuple< Ts... > &t, const unsigned int version)
 Boost.Serialization entry point for std::tuple.
string std::to_string (string const &str)
 Identity overload for std::string.
template<typename T, typename... Ts>
string std::to_string (variant< T, Ts... > const &var)
 Convert a variant into a string.

Typedef Documentation

◆ filter_out_t

template<typename T, int... Is>
using triqs::tuple::filter_out_t = std::decay_t<decltype(filter_out<Is...>(std::declval<T>()))>

#include <triqs/utility/tuple_tools.hpp>

Result type of filter_out() for tuple type T dropping positions Is....

Template Parameters
TTuple type.
IsPositions to drop.

Definition at line 434 of file tuple_tools.hpp.

◆ filter_t

template<typename T, size_t... Is>
using triqs::tuple::filter_t = std::tuple<std::tuple_element_t<Is, std::decay_t<T>>...>

#include <triqs/utility/tuple_tools.hpp>

Tuple type made of the elements at the given positions of given tuple type T.

Template Parameters
TTuple type.
IsPositions to keep.

Definition at line 392 of file tuple_tools.hpp.

Function Documentation

◆ apply()

template<typename F, typename T>
decltype(auto) triqs::tuple::apply ( F && f,
T && t )

#include <triqs/utility/tuple_tools.hpp>

Call a function with the elements of a tuple as its arguments.

Equivalent to the C++17 std::apply.

Template Parameters
FCallable type.
TTuple type.
Parameters
fCallable to invoke.
tTuple of arguments, i.e. t = (t0, t1, ...).
Returns
The result of the function call to f(t0, t1, ...).

Definition at line 159 of file tuple_tools.hpp.

◆ apply_construct()

template<typename C, typename T>
decltype(auto) triqs::tuple::apply_construct ( T && t)

#include <triqs/utility/tuple_tools.hpp>

Brace-construct an object from the elements of a tuple.

Template Parameters
CType to construct.
TTuple type.
Parameters
tTuple of constructor arguments, i.e. t = (t0, t1, ...).
Returns
An object of type C constructed as C{t0, t1, ...}.

Definition at line 174 of file tuple_tools.hpp.

◆ apply_construct_parenthesis()

template<typename C, typename T>
decltype(auto) triqs::tuple::apply_construct_parenthesis ( T && t)

#include <triqs/utility/tuple_tools.hpp>

Parenthesis-construct an object from the elements of a tuple.

Template Parameters
CType to construct.
TTuple type.
Parameters
tTuple of constructor arguments, i.e. t = (t0, t1, ...).
Returns
An object of type C constructed as C(t0, t1, ...).

Definition at line 189 of file tuple_tools.hpp.

◆ called_on_tuple()

template<typename F>
auto triqs::tuple::called_on_tuple ( F && f)

#include <triqs/utility/tuple_tools.hpp>

Wrap a callable so that it can be invoked with a single tuple argument.

Template Parameters
FCallable type.
Parameters
fCallable to wrap.
Returns
A new callable that takes a single tuple argument and unpacks it to call f.

Definition at line 206 of file tuple_tools.hpp.

◆ filter() [1/2]

template<size_t... Is, typename T>
filter_t< T, Is... > triqs::tuple::filter ( T && t)

#include <triqs/utility/tuple_tools.hpp>

Keep only the elements of a tuple at the given positions.

Template Parameters
IsPositions to keep.
TTuple type.
Parameters
tSource tuple.
Returns
A new tuple holding the elements of the source tuple whose positions are in Is....

Definition at line 402 of file tuple_tools.hpp.

◆ filter() [2/2]

template<size_t... Is, typename T>
filter_t< T, Is... > triqs::tuple::filter ( T && t,
std::index_sequence< Is... >  )

#include <triqs/utility/tuple_tools.hpp>

Keep only the elements of a tuple at the given positions (as an index-sequence).

Template Parameters
IsPositions to keep, supplied through the index-sequence argument.
TTuple type.
Parameters
tSource tuple.
Returns
A new tuple holding the elements of the source tuple whose positions are in Is....

Definition at line 412 of file tuple_tools.hpp.

◆ filter_out()

template<int... Is, typename T>
decltype(auto) triqs::tuple::filter_out ( T && t)

#include <triqs/utility/tuple_tools.hpp>

Drop the elements of a tuple at the given positions.

Template Parameters
IsPositions to drop.
TTuple type.
Parameters
tSource tuple.
Returns
A new tuple holding the elements of the source tuple whose positions are not in Is....

Definition at line 424 of file tuple_tools.hpp.

◆ fold() [1/2]

template<typename F, typename T, typename R>
decltype(auto) triqs::tuple::fold ( F && f,
T && t,
R && r )

#include <triqs/utility/tuple_tools.hpp>

Left-fold a callable over the elements of a tuple.

Computes f(x_N, f(x_{N-1}, ... f(x_1, r))) for a tuple t = (x_1, ..., x_N).

Template Parameters
FCallable type.
TTuple type.
RAccumulator type.
Parameters
fFolding callable with signature f(x, r) -> r'.
tTuple to fold.
rInitial accumulator value.
Returns
The folded result.

Definition at line 324 of file tuple_tools.hpp.

◆ fold() [2/2]

template<typename F, typename T0, typename T1, typename R>
decltype(auto) triqs::tuple::fold ( F && f,
T0 && t1,
T1 && t2,
R && r )

#include <triqs/utility/tuple_tools.hpp>

Left-fold a callable over the elements of two equally-sized tuples.

Computes f(x_N, y_N, f(x_{N-1}, y_{N-1}, ... f(x_0, y_0, r))) for tuples t1 = (x_1, ..., x_N) and t2 = (y_1, ..., y_N).

Template Parameters
FCallable type.
T0First tuple type.
T1Second tuple type.
RAccumulator type.
Parameters
fFolding callable with signature f(x, y, r) -> r'.
t1First tuple to fold.
t2Second tuple to fold.
rInitial accumulator value.
Returns
The folded result.

Definition at line 354 of file tuple_tools.hpp.

◆ for_each()

template<typename T, typename F>
void triqs::tuple::for_each ( T && t,
F && f )

#include <triqs/utility/tuple_tools.hpp>

Apply a callable to every element of a tuple, in order.

Template Parameters
TTuple type.
FCallable type.
Parameters
tTuple to iterate over.
fCallable invoked as f(x) for each element x of the tuple t.

Definition at line 239 of file tuple_tools.hpp.

◆ for_each_enumerate()

template<typename T, typename F>
void triqs::tuple::for_each_enumerate ( T && t,
F && f )

#include <triqs/utility/tuple_tools.hpp>

Apply a callable to every (index, element) pair of a tuple.

Mimics Python's for n, x in enumerate(t): f(n, x).

Template Parameters
TTuple type.
FCallable type.
Parameters
tTuple to iterate over.
fCallable invoked as f(n, x) for each element x at index n of the tuple t.

Definition at line 256 of file tuple_tools.hpp.

◆ for_each_zip()

template<typename F, typename... Ts>
void triqs::tuple::for_each_zip ( F && f,
Ts &&... ts )

#include <triqs/utility/tuple_tools.hpp>

Apply an N-ary callable across \( N \) tuples zipped together.

Mimics Python's for i1, ..., iN in zip(t1, ..., tN): f(i1, ..., iN). The tuples must have equal size.

Template Parameters
FCallable type taking \( N \) arguments.
TsTuple types.
Parameters
fCallable invoked on each zipped tuple of elements.
tsThe \( N \) tuples to zip and iterate over.

Definition at line 270 of file tuple_tools.hpp.

◆ load()

template<typename Archive, typename... Ts>
void boost::serialization::load ( Archive & ar,
std::variant< Ts... > & v,
const unsigned int version )

#include <triqs/utility/variant_serialize.hpp>

Deserialize a std::variant.

Warning
This is unused. It might be removed in the future.
Template Parameters
ArchiveArchive type.
TsAlternative types of the variant.
Parameters
arBoost archive.
vVariant to deserialize into.
versionUnused version required by the Boost.Serialization protocol.

Definition at line 84 of file variant_serialize.hpp.

◆ make_tuple_repeat()

template<int N, typename X>
auto triqs::tuple::make_tuple_repeat ( X const & x)

#include <triqs/utility/tuple_tools.hpp>

Build a tuple with \( N \) copies of a value.

Template Parameters
NNumber of elements (non-negative).
XElement type.
Parameters
xValue to repeat.
Returns
A tuple with \( N \) elements, each equal to x.

Definition at line 141 of file tuple_tools.hpp.

◆ map()

template<typename F, typename T>
decltype(auto) triqs::tuple::map ( F && f,
T && t )

#include <triqs/utility/tuple_tools.hpp>

Map a callable over the elements of a tuple.

Template Parameters
FCallable type.
TTuple type.
Parameters
fCallable applied to each element.
tTuple to map over.
Returns
A new tuple containing the result of applying the callable to each element of the input tuple.

Definition at line 288 of file tuple_tools.hpp.

◆ map_on_zip()

template<typename... Ts, typename F>
auto triqs::tuple::map_on_zip ( F && f,
Ts &&... ts )

#include <triqs/utility/tuple_tools.hpp>

Map an N-ary callable across N tuples zipped together.

Template Parameters
TsTuple types.
FCallable type.
Parameters
fCallable applied to each zipped tuple of elements.
tsThe \( N \) tuples to zip and map over.
Returns
A new tuple containing the result of applying the callable to each zipped tuple of elements.

Definition at line 299 of file tuple_tools.hpp.

◆ operator<<() [1/4]

template<typename T1, typename T2>
std::ostream & std::operator<< ( std::ostream & os,
std::pair< T1, T2 > const & x )

#include <triqs/utility/tuple_tools.hpp>

Write a std::pair to an output stream.

Template Parameters
T1Type of the first element.
T2Type of the second element.
Parameters
osOutput stream.
xPair to write.
Returns
Reference to the output stream.

Definition at line 504 of file tuple_tools.hpp.

◆ operator<<() [2/4]

template<typename... T>
std::ostream & std::operator<< ( std::ostream & os,
std::tuple< T... > const & t )

#include <triqs/utility/tuple_tools.hpp>

Write a std::tuple to an output stream.

Template Parameters
TTypes of the tuple elements.
Parameters
osOutput stream.
tTuple to write.
Returns
Reference to the output stream.

Definition at line 486 of file tuple_tools.hpp.

◆ operator<<() [3/4]

template<typename T, typename... Ts>
std::ostream & std::operator<< ( std::ostream & os,
std::variant< T, Ts... > const & v )

#include <triqs/utility/variant_extensions.hpp>

Write a std::variant to an output stream by visiting the held alternative and forwarding it to the stream.

Template Parameters
TFirst alternative type.
TsRemaining alternative types.
Parameters
osOutput stream.
vVariant to write.
Returns
Reference to the output stream.

Definition at line 66 of file variant_extensions.hpp.

◆ operator<<() [4/4]

template<typename T, typename... Ts>
std::ostream & std::operator<< ( std::ostream & os,
std::vector< std::variant< T, Ts... > > const & vec )

#include <triqs/utility/variant_extensions.hpp>

Write a vector of std::variant as a comma-separated list of its elements.

Template Parameters
TFirst alternative type.
TsRemaining alternative types.
Parameters
osOutput stream.
vecVector of variants to write.
Returns
Reference to the output stream.

Definition at line 80 of file variant_extensions.hpp.

◆ pop_front()

template<typename T>
auto triqs::tuple::pop_front ( T && t)

#include <triqs/utility/tuple_tools.hpp>

Remove the first element of a tuple.

Template Parameters
TTuple type.
Parameters
tSource tuple.
Returns
A new tuple equal to the source tuple with its first element removed.

Definition at line 465 of file tuple_tools.hpp.

◆ push_back()

template<typename T, typename X>
auto triqs::tuple::push_back ( T && t,
X && x )

#include <triqs/utility/tuple_tools.hpp>

Append an element to the end of a tuple.

Template Parameters
TTuple type.
XElement type.
Parameters
tSource tuple.
xElement to append.
Returns
A new tuple equal to the source tuple with x added at the end.

Definition at line 445 of file tuple_tools.hpp.

◆ push_front()

template<typename T, typename X>
auto triqs::tuple::push_front ( T && t,
X && x )

#include <triqs/utility/tuple_tools.hpp>

Prepend an element to the front of a tuple.

Template Parameters
TTuple type.
XElement type.
Parameters
tSource tuple.
xElement to prepend.
Returns
A new tuple equal to the source tuple with x added at the front.

Definition at line 456 of file tuple_tools.hpp.

◆ replace()

template<int... Is, typename T, typename R>
auto triqs::tuple::replace ( T && t,
R && r )

#include <triqs/utility/tuple_tools.hpp>

Return a copy of a tuple with the elements at the given positions replaced by a given value.

Template Parameters
IsPositions to replace.
TTuple type.
RReplacement type.
Parameters
tSource tuple.
rReplacement value.
Returns
A tuple with the selected positions replaced.

Definition at line 382 of file tuple_tools.hpp.

◆ save()

template<typename Archive, typename... Ts>
void boost::serialization::save ( Archive & ar,
std::variant< Ts... > const & v,
const unsigned int version )

#include <triqs/utility/variant_serialize.hpp>

Serialize a std::variant.

Warning
This is unused. It might be removed in the future.
Template Parameters
ArchiveArchive type.
TsAlternative types of the variant.
Parameters
arBoost archive.
vVariant to serialize.
versionUnused version required by the Boost.Serialization protocol.

Definition at line 68 of file variant_serialize.hpp.

◆ serialize()

template<typename Archive, typename... Ts>
void boost::serialization::serialize ( Archive & ar,
std::tuple< Ts... > & t,
const unsigned int version )

#include <triqs/utility/tuple_serialize.hpp>

Boost.Serialization entry point for std::tuple.

Template Parameters
ArchiveArchive type.
TsTuple element types.
Parameters
arBoost archive.
tTuple to serialize.
versionUnused version required by the Boost.Serialization protocol.

Definition at line 54 of file tuple_serialize.hpp.

◆ to_string()

template<typename T, typename... Ts>
string std::to_string ( variant< T, Ts... > const & var)
inline

#include <triqs/utility/variant_extensions.hpp>

Convert a variant into a string.

It uses a stringstream and the std::operator<<(std::ostream&, std::variant<T, Ts...> const&) overload.

Template Parameters
TFirst alternative type.
TsRemaining alternative types.
Parameters
varVariant to convert.
Returns
String representation of the variant.

Definition at line 102 of file variant_extensions.hpp.