SUNphi  1.0
Ranges.hpp
Go to the documentation of this file.
1 #ifndef _RANGES_HPP
2 #define _RANGES_HPP
3 
4 /// \file Ranges.hpp
5 ///
6 /// \brief Provide range creators
7 
8 #include <ints/IntSeq.hpp>
9 #include <ints/IntSeqCat.hpp>
10 
11 namespace SUNphi
12 {
13  /////////////////////////////////////////////////////////////////////////
14 
15  /// Defines a sequence of integer up to Max (excluded)
16  ///
17  /// Internal implementation, recursively calling itself until 0 or 1
18  template <int Max>
19  struct _IntsUpTo
20  {
21  /// Used to split the list
22  static constexpr int half=Max/2;
23 
24  /// Internal type holding the two halves
25  using type=
26  IntSeqCat<typename _IntsUpTo<half>::type,
27  typename _IntsUpTo<Max-half>::type::template Add<half>>;
28  };
29 
30  /// Defines a sequence of integer up to Max (excluded)
31  ///
32  /// Implement the terminator, considering a sequence up to 0 (empty)
33  template <>
34  struct _IntsUpTo<0>
35  {
36  /// Empty list
37  using type=
38  IntSeq<>;
39  };
40 
41  /// Defines a sequence of integer up to Max (excluded)
42  ///
43  /// Implement the terminator, considering a sequence up to 1 (including only 0)
44  template <>
45  struct _IntsUpTo<1>
46  {
47  /// Trivial list
48  using type=
49  IntSeq<0>;
50  };
51 
52  /// Defines a sequence of integer up to Max (excluded)
53  ///
54  /// Wraps the internal definition
55  template <int Max>
56  using IntsUpTo=typename _IntsUpTo<Max>::type;
57 
58  ///////////////////////////////////////////////////////////////////////
59 
60  /// Defines a sequence of integer with offset and stride (up-open interval)
61  ///
62  /// This is achieved using the Add and Mul from the IntSeq list
63  template <int Min,
64  int Shift,
65  int Max>
66  struct _RangeSeq
67  {
68  //assert if Shift is zero
69  static_assert(Shift,"Shift must be non-zero");
70 
71  /// Mask used to set to zero the Range parameters
72  static constexpr bool nonNull=
73  (Max>Min);
74 
75  /// Maximal value of the normalized range
77  nonNull*((Max-Min)/Shift);
78 
79  /// Stride among entries
80  static constexpr int stride=
81  nonNull*Shift;
82 
83  /// Offset of the sequence
85 
86  /// Shifted-strided interval
87  using type=
88  typename SUNphi::IntsUpTo<normalizedMax>
89  ::template Mul<stride>
90  ::template Add<offset>;
91  };
92 
93  /// Defines a sequence of integer with offset and stride (up-open interval)
94  ///
95  /// Example:
96  ///
97  /// \code
98  /// typedef RangeSeq<2,3,8> Range; //IntSeq<2,5>
99  /// \endcode
100  template <int Min, // Starting point
101  int Shift, // Stride
102  int Max> // End point (not included)
103  using RangeSeq=typename _RangeSeq<Min,Shift,Max>::type;
104 
105  /// Create a RangeSeq with given types
106  template <int Min, // Starting point
107  int Shift, // Stride
108  int Max> // End point (not included)
109  [[ maybe_unused ]]
110  constexpr DECLAUTO rangeSeq=
111  RangeSeq<Min,Shift,Max>{};
112 
113  /////////////////////////////////////////////////////////////////
114 
115  /// Defines a \c IntSeq of given \c Length, all containing \c Val as entry
116  template <int Length, // Number of components
117  int Val> // Value to be used
118  using IntSeqOfSameNumb=
119  typename IntsUpTo<Length>::
120  template Mul<0>::
121  template Add<Val>;
122 }
123 
124 #endif
static constexpr int offset
Offset of the sequence.
Definition: Ranges.hpp:84
static constexpr int element()
Get the I element of the sequence.
Definition: IntSeq.hpp:95
static constexpr int half
Used to split the list.
Definition: Ranges.hpp:22
#define DECLAUTO
Short name for decltype(auto)