Wildmeshing Toolkit
get_unique_types.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <tuple>
3 #include <type_traits>
4 #include "concatenate_types.hpp"
5 
7 
8 
9 // given a tuple, extracts a tuple with only unique types in the input
10 
11 // strategy is to recursively extract the last copy of any given type
12 
13 
14 // alternates between get_unique_types<tuple<Ts...> with
15 // main_unique_tuple_types<T,std::tuple<Ts...>> to combine Ts... and T,Ts... as necessary
16 
17 namespace detail {
18 
19 
20 // basic initializations - by default return empty in case the input is empty (which is not caught
21 // by later cases)
22 template <typename...>
24 {
25  using type = std::tuple<>;
26 };
27 // basic initialization, expects a T and a Tuple
28 template <typename T, typename RemainingTuple>
30 {
31 };
32 // convenience to avoid typename for get_unique_types...
33 template <typename... Ts>
34 using get_unique_types_t = typename get_unique_types<Ts...>::type;
35 
36 
37 // actual base case - single type is unique so we return that type
38 template <typename T>
40 {
41  using type = std::tuple<T>;
42 };
43 
44 
45 // from a list of types, check if the first is unique compared to the rest
46 template <typename T, typename... Ts>
47 struct get_unique_types<T, Ts...>
48 {
49  using type = typename get_unique_types_tuple<T, std::tuple<Ts...>>::type;
50 };
51 
52 
53 // do the meat of the implementation - check if T is unique in T,Ts...
54 // if so then add it to the unique of the rest
55 template <typename T, typename... Ts>
56 struct get_unique_types_tuple<T, std::tuple<Ts...>>
57 {
58  // is t unique among T,Ts...
59  constexpr static bool t_is_unique = (!std::is_same_v<T, Ts> && ...);
60  // if its unique then create {T} otherwise {}
61  using adding_t_type = std::conditional_t<t_is_unique, std::tuple<T>, std::tuple<>>;
62 
63  // concatenate adding_t_type \cup unique(Ts...)
65 };
66 
67 
68 } // namespace detail
69 
70 // main access point
71 template <typename... Ts>
73 
74 } // namespace wmtk::utils::metaprogramming::tuple
Definition: autodiff.h:995
typename get_unique_types< Ts... >::type get_unique_types_t
typename concatenate_types< T, U >::type concatenate_types_t
detail::get_unique_types_t< Ts... > get_unique_types_t
typename get_unique_types_tuple< T, std::tuple< Ts... > >::type type
std::conditional_t< t_is_unique, std::tuple< T >, std::tuple<> > adding_t_type
concatenate_types_t< adding_t_type, get_unique_types_t< Ts... > > type