SUNphi  1.0
IntListOperations.hpp
Go to the documentation of this file.
1 #ifndef _INT_LIST_OPERATIONS_HPP
2 #define _INT_LIST_OPERATIONS_HPP
3 
4 /// \file IntListOperations.hpp
5 ///
6 /// \brief Implements operations over list of integers
7 
8 namespace SUNphi
9 {
10  /// Sum of all integers
11  ///
12  /// Internal implementation
13  template <int...Ints>
14  constexpr int _hSum()
15  {
16  if constexpr(sizeof...(Ints)==0)
17  return 0;
18  else
19  return (Ints + ...);
20  }
21 
22  /// Sum of all integers
23  template <int...Ints>
24  [[ maybe_unused ]]
25  constexpr int hSum=
26  _hSum<Ints...>();
27 
28  /////////////////////////////////////////////////////////////////
29 
30  /// Sum of all integers up to I (excluded)
31  template <int I,
32  int Head=0,
33  int...Tail>
34  [[ maybe_unused ]]
35  constexpr int hSumFirst=
36  (I>0)*Head+hSumFirst<I-1,Tail...>;
37 
38  /// Sum of all integers up to I (excluded),unary case
39  template <int I,
40  int Head>
41  constexpr int hSumFirst<I,Head> =
42  (I>0)*Head;
43 
44  /////////////////////////////////////////////////////////////////
45 
46  /// Product of all integers
47  ///
48  /// Internal implementation
49  template <int...Ints>
50  [[ maybe_unused ]]
51  constexpr int _hMul()
52  {
53  if constexpr(sizeof...(Ints)==0)
54  return 1;
55  else
56  return (Ints * ...);
57  }
58 
59  /// Product of all integers
60  template <int...Ints>
61  [[ maybe_unused ]]
62  constexpr int hMul=
63  _hMul<Ints...>();
64 
65  /////////////////////////////////////////////////////////////////
66 
67  /// Provide a routine to search the minimal or maximal
68 #define DEFINE_SEARCH_MIN_OR_MAX(NAME,
69  DESCR,
70  OPER)
71  /*! DESCR of all integers */
72  /*! */
73  /*! Internal implementation */
74  template <int In,
75  int Head,
76  int...Tail>
77  int _ ## NAME ## OfList()
78  {
79  /* Binary comparison */
80  constexpr int Tmp=(In OPER Head)?In:Head;
81 
82  /* Decide to return or nest */
83  if constexpr(sizeof...(Tail)==0)
84  return Tmp;
85  else
86  return _ ## NAME ## OfList<Tmp,Tail...>;
87  }
88 
89  /*! DESCR of all integers */
90  template <int Head,
91  int...Tail>
92  [[ maybe_unused ]]
93  constexpr int NAME ## OfList=
94  _ ## NAME ## OfList<Head,Head,Tail...>()
95 
96  DEFINE_SEARCH_MIN_OR_MAX(max,Maximal,>);
97  DEFINE_SEARCH_MIN_OR_MAX(min,Minimal,<);
98 
99 #undef DEFINE_SEARCH_MIN_OR_MAX
100 }
101 
102 #endif
constexpr int hSumFirst
Sum of all integers up to I (excluded)
#define DEFINE_SEARCH_MIN_OR_MAX(NAME,DESCR,OPER)
Provide a routine to search the minimal or maximal.
constexpr int hMul
Product of all integers.
constexpr int _hMul()