tuple/value: add reversing operation

This commit is contained in:
Danny Robson 2018-04-05 16:06:09 +10:00
parent 1bafe204b6
commit 665c8aa49e
2 changed files with 35 additions and 0 deletions

View File

@ -29,5 +29,13 @@ main (void)
tap.expect_eq (c, util::tuple::value::zip (a, b), "tuple zipping"); tap.expect_eq (c, util::tuple::value::zip (a, b), "tuple zipping");
} }
{
std::tuple a (1, 2);
auto result = util::tuple::value::reverse (a);
std::tuple expected (2, 1);
tap.expect_eq (result, expected, "tuple reverse");
}
return tap.status (); return tap.status ();
} }

View File

@ -17,6 +17,7 @@
#ifndef CRUFT_UTIL_TUPLE_VALUE_HPP #ifndef CRUFT_UTIL_TUPLE_VALUE_HPP
#define CRUFT_UTIL_TUPLE_VALUE_HPP #define CRUFT_UTIL_TUPLE_VALUE_HPP
#include "index.hpp"
#include "../types.hpp" #include "../types.hpp"
#include <cstddef> #include <cstddef>
@ -123,6 +124,32 @@ namespace util::tuple::value {
std::forward<Args> (args)... std::forward<Args> (args)...
); );
} }
///////////////////////////////////////////////////////////////////////////
namespace detail {
template <typename TupleT, size_t ...Indices>
auto
reverse (index::indices<Indices...>, TupleT &&val)
{
return std::make_tuple (
std::get<Indices> (val)...
);
}
};
//-------------------------------------------------------------------------
template <typename TupleT>
auto
reverse (TupleT &&val)
{
return detail::reverse (
index::make_reverse_t<
std::tuple_size_v<std::decay_t<TupleT>>
> {},
std::forward<TupleT> (val)
);
}
} }