JayBeams  0.1
Another project to have fun coding.
integer_range_binning.hpp
Go to the documentation of this file.
1 #ifndef jb_integer_range_binning_hpp
2 #define jb_integer_range_binning_hpp
3 
5 
6 #include <limits>
7 #include <sstream>
8 #include <stdexcept>
9 #include <type_traits>
10 
11 namespace jb {
12 
13 /**
14  * A histogram binning_strategy for integer numbers in a known range.
15  *
16  * This class defines histogram bins for samples with integer values
17  * in a range defined at run-time. Care must be taken if the range is
18  * too big because the corresponding histogram is likely to consume a
19  * lot of memory. See jb::binning_strategy_concept.
20  *
21  * @tparam sample_type_t the type of samples, should be an integer type.
22  */
23 template <typename sample_type_t>
25 public:
26  /// type traits as required by @ref jb::binning_strategy_concept
27  typedef sample_type_t sample_type;
28 
29  /**
30  * Constructor based on the histogram range.
31  *
32  * @param h_min The value for histogram_min()
33  * @param h_max The value for histogram_max()
34  */
35  integer_range_binning(sample_type h_min, sample_type h_max)
36  : h_min_(h_min)
37  , h_max_(h_max) {
38  static_assert(
39  std::is_integral<sample_type>::value,
40  "The sample_type must be an integral type");
41  if (h_min_ >= h_max_) {
42  std::ostringstream os;
43  os << "jb::integer_range_binning requires h_min (" << h_min
44  << ") to be less than h_max (" << h_max << ")";
45  throw std::invalid_argument(os.str());
46  }
47  }
48 
49  //@{
50  /**
51  * @name Implement binning_strategy_concept interface.
52  *
53  * Please see @ref binning_strategy_concept for detailed
54  * documentation of each member function.
55  */
56  sample_type histogram_min() const {
57  return h_min_;
58  }
59  sample_type histogram_max() const {
60  return h_max_;
61  }
62  sample_type theoretical_min() const {
63  return std::numeric_limits<sample_type>::min();
64  }
65  sample_type theoretical_max() const {
66  return std::numeric_limits<sample_type>::max();
67  }
68  std::size_t sample2bin(sample_type t) const {
69  return static_cast<std::size_t>(t - histogram_min());
70  }
71  sample_type bin2sample(std::size_t i) const {
72  return histogram_min() + i;
73  }
74  sample_type interpolate(
75  sample_type x_a, sample_type x_b, double y_a, double s, double q) const {
76  return histogram_binning_linear_interpolation(x_a, x_b, y_a, s, q);
77  }
78  //@}
79 
80 private:
81  sample_type h_min_;
82  sample_type h_max_;
83 };
84 
85 } // namespace jb
86 
87 #endif // jb_integer_range_binning_hpp
std::size_t sample2bin(sample_type t) const
sample_type histogram_binning_linear_interpolation(sample_type x_a, sample_type x_b, double y_a, double s, double q)
Convenience function to implement the binning_strategy_concept.
integer_range_binning(sample_type h_min, sample_type h_max)
Constructor based on the histogram range.
sample_type theoretical_max() const
A histogram binning_strategy for integer numbers in a known range.
sample_type theoretical_min() const
sample_type interpolate(sample_type x_a, sample_type x_b, double y_a, double s, double q) const
sample_type bin2sample(std::size_t i) const
sample_type_t sample_type
type traits as required by jb::binning_strategy_concept
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7