Empirical
tuple_utils.h
Go to the documentation of this file.
1 
11 #ifndef EMP_TUPLE_UTILS_H
12 #define EMP_TUPLE_UTILS_H
13 
14 #include <functional>
15 #include <tuple>
16 
17 #include "../meta/IntPack.h"
18 #include "../meta/meta.h"
19 
20 namespace emp {
21 
23  template <typename TUPLE_T>
24  constexpr inline int tuple_size() { return std::tuple_size<TUPLE_T>::value; }
25 
26 
28  template <typename... Ts, int... Ps>
29  auto shuffle_tuple(const std::tuple<Ts...> & tup, IntPack<Ps...>) {
30  return std::make_tuple( std::get<Ps>(tup)... );
31  }
32 
33 
36  template < typename FUN_T, typename TUPLE_T, int... N > // Specify positions to apply...
37  auto ApplyTuple(const FUN_T & fun, const TUPLE_T & tup, IntPack<N...>) {
38  return fun(std::get<N>(tup)...);
39  }
40 
42  template <typename FUN_T, typename TUPLE_T> // Apply whole tuple
43  auto ApplyTuple(const FUN_T & fun, const TUPLE_T & tup) {
44  return ApplyTuple(fun, tup, IntPackRange<0,tuple_size<TUPLE_T>()>());
45  }
46 
47 
48 
50  template <typename... TYPES>
51  struct TupleHash {
52  using tuple_t = std::tuple<TYPES...>;
53  using fun_t = std::function<std::size_t(TYPES...)>;
54 
55  std::size_t operator()( const tuple_t & tup ) const {
56  return ApplyTuple<fun_t, tuple_t> (emp::CombineHash<TYPES...>, tup);
57  }
58  };
59 
60 }
61 
62 #endif
Definition: IntPack.h:26
Setup tuples to be able to be used in hash tables.
Definition: tuple_utils.h:51
std::size_t CombineHash(const T &x)
Definition: meta.h:207
std::function< std::size_t(TYPES...)> fun_t
Definition: tuple_utils.h:53
std::tuple< TYPES... > tuple_t
Definition: tuple_utils.h:52
constexpr int tuple_size()
Quick way to calculate tuple size.
Definition: tuple_utils.h:24
typename internal::ip_range<(START >=END), START, END, STEP >::type IntPackRange
Definition: IntPack.h:117
auto ApplyTuple(const FUN_T &fun, const TUPLE_T &tup, IntPack< N... >)
Definition: tuple_utils.h:37
If we are in emscripten, make sure to include the header.
Definition: array.h:37
std::size_t operator()(const tuple_t &tup) const
Definition: tuple_utils.h:55
auto shuffle_tuple(const std::tuple< Ts... > &tup, IntPack< Ps... >)
Reorganize the entries in tuple; the provided int pack must specify the new ordering.
Definition: tuple_utils.h:29