SUNphi  1.0
Filter.hpp
Go to the documentation of this file.
1 #ifndef _FILTER_HPP
2 #define _FILTER_HPP
3 
4 /// \file Filter.hpp
5 ///
6 /// \brief Filter elements of a list according to a rule
7 ///
8 /// \todo Expand as here
9 /// https://stackoverflow.com/questions/41723704/how-to-filter-a-stdinteger-sequence
10 
11 #include <ints/IntSeqCat.hpp>
12 #include <ints/Ranges.hpp>
13 
14 namespace SUNphi
15 {
16  /// Get the position of elements satisfying a rule.
17  ///
18  /// Internal implementation, forward the declaration.
19  template <template <auto> typename F, // Rule to apply
20  typename I, // IntSeq-to be
21  auto...> // List to filter
22  class _FilterVariadicList;
23 
24  /// Get the position of elements satisfying a rule.
25  ///
26  /// Internal implementation, recursive case.
27  template <template <auto> typename F, // Rule to apply
28  int...P, // Positions
29  auto...T> // Elements
30  class _FilterVariadicList<F,IntSeq<P...>,T...>
31  {
32 
33  // Force P and T to have the same number of elements
34  STATIC_ASSERT_ARE_N_TYPES(sizeof...(P),T);
35 
36  public:
37 
38  /// Returned sequence containing the true positions
39  using Res=IntSeqCat<Conditional<F<T>::res,
40  IntSeq<P>,
41  IntSeq<>>...>;
42  };
43 
44  /// Get the position of elements satisfying a rule.
45  ///
46  /// Gets a set of variables as template parameter, and a condition
47  /// F, returns the position of those which passes the filter F.
48  template <template <auto> typename F, // Rule to apply
49  auto...T> // List of variables
50  using FilterVariadicList=
51  typename _FilterVariadicList<F,
52  RangeSeq<0,
53  1,
54  sizeof...(T)>,
55  T...>::Res;
56 
57  /// Get the position of elements of a vector, satisfying a condition
58  ///
59  /// Forward declaration
60  template <template <auto> typename F, // Rule to apply
61  typename L> // Vector
62  class _FilterVariadicClass;
63 
64  /// Get the position of elements of a vector, satisfying a condition
65  template <template <auto> typename F, // Rule to apply
66  template <auto...> typename V, // Kind of vector
67  auto...List> // Elements of the vector
68  class _FilterVariadicClass<F,V<List...>>
69  {
70  public:
71 
72  /// Positions where the condition is true
73  using Pos=FilterVariadicList<F,List...>;
74 
75  };
76 
77  /// Get the position of elements of a vector, satisfying a condition
78  ///
79  /// Gets a vector-like incapsulated list of variables, and returns a
80  /// \c IntSeq containing the position of those that satisfy a
81  /// certain condition.
82  ///
83  /// Example:
84  ///
85  /// \code:
86  ///
87  /// FilterVariadicClassPos<IsNotNull,IntSeq<0,1,0,10>>; // IntSeq<0,3>;
88  ///
89  /// \endcode
90  template <template <auto> typename F, // Rule to apply
91  typename L> // Class containing the types
92  using FilterVariadicClassPos=
93  typename _FilterVariadicClass<F,L>::Pos;
94 
95  /////////////////////////////////////////////////////////////////
96 
97  /// Filterer which check non-nullity of its parameter
98  template <auto I>
99  struct IsNotNull
100  {
101  /// Result of the check
102  static constexpr bool res=
103  (I!=0);
104  };
105 
106  /// Filterer which check non-negativity of its parameter
107  template <auto I>
109  {
110  /// Result of the check
111  static constexpr bool res=
112  (I>=0);
113  };
114 
115  /// Filterer which check nothing
116  template <auto I>
118  {
119  /// Result of the check
120  static constexpr bool res=
121  true;
122  };
123 }
124 
125 #endif
static constexpr bool res
Result of the check.
Definition: Filter.hpp:102
#define STATIC_ASSERT_ARE_N_TYPES(N, UNEXP_PARPACK)
Static assert if not passing exactly N types.
Definition: TypeTraits.hpp:422
static constexpr bool res
Result of the check.
Definition: Filter.hpp:120
static constexpr bool res
Result of the check.
Definition: Filter.hpp:111