CortidQCT  1.2.2.52
DiscreteRange.h
Go to the documentation of this file.
1 
13 #pragma once
14 
15 #include <cmath>
16 #include <limits>
17 #include <type_traits>
18 
19 namespace CortidQCT {
20 
26 template <class T> struct DiscreteRange {
28  using value_type = T;
29 
36 
38  inline constexpr DiscreteRange(value_type min_, value_type max_,
39  value_type stride_) noexcept
40  : min{min_}, max{max_}, stride{stride_} {}
41 
45  inline constexpr DiscreteRange(value_type min_, value_type max_) noexcept
46  : min{min_}, max{max_}, stride{1} {
47  static_assert(std::is_integral<value_type>::value,
48  "Default stride is only defined for integral types.");
49  }
50 
52  inline constexpr std::size_t numElements() const noexcept {
53  using std::floor;
54  auto const length = max - min;
55  return static_cast<std::size_t>(floor(length / stride)) + std::size_t{1};
56  }
57 
62  inline constexpr value_type nThElement(std::size_t n) const noexcept {
63  return min + (static_cast<value_type>(n) - value_type{1}) * stride;
64  }
65 };
66 
71 
74 
76 template <class T>
77 inline constexpr DiscreteRange<T> discreteRange(T min, T max,
78  T stride) noexcept {
79  return DiscreteRange<T>(min, max, stride);
80 }
81 
83 template <class T>
84 inline constexpr DiscreteRange<T> discreteRange(T min, T max) noexcept {
85  return DiscreteRange<T>(min, max);
86 }
87 
88 } // namespace CortidQCT
Name namespace for CortidQCT library.
Definition: CortidQCT.h:23
float value_type
type of the elements contained in the range
Definition: DiscreteRange.h:28
Type representing a discrete closed, stridable range.
Definition: DiscreteRange.h:26
value_type stride
stride, i.e. distance between two consecutive elements
Definition: DiscreteRange.h:35
constexpr value_type nThElement(std::size_t n) const noexcept
Definition: DiscreteRange.h:62
value_type max
maximum element
Definition: DiscreteRange.h:33
constexpr std::size_t numElements() const noexcept
Returns the number of elements in the range.
Definition: DiscreteRange.h:52
constexpr DiscreteRange(value_type min_, value_type max_) noexcept
Definition: DiscreteRange.h:45
constexpr DiscreteRange< T > discreteRange(T min, T max, T stride) noexcept
Convenient creator function.
Definition: DiscreteRange.h:77
value_type min
minimum element
Definition: DiscreteRange.h:31
constexpr DiscreteRange(value_type min_, value_type max_, value_type stride_) noexcept
Constructs a discrete range.
Definition: DiscreteRange.h:38