UFO
TruncatingEquispacedBinSelector.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2019 Met Office UK
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  */
7 
8 #ifndef UFO_UTILS_TRUNCATINGEQUISPACEDBINSELECTOR_H_
9 #define UFO_UTILS_TRUNCATINGEQUISPACEDBINSELECTOR_H_
10 
11 #include <algorithm>
12 
13 #include "eckit/exception/Exceptions.h"
15 
16 namespace ufo
17 {
18 
19 /// \brief Represents a finite set of consecutive intervals (_bins_) of the same width, each closed
20 /// from the left and open from the right.
21 ///
22 /// Call the bin() function to find the bin containing a particular value.
24  public:
25  // If necessary, these could be made template parameters.
26  typedef float ValueType;
27  typedef int IndexType;
28 
29  /// \brief Partition the interval [\p lowerBound, \p upperBound) into \p numBins
30  /// bins of the same width.
32  : lowerBound_(lowerBound),
33  binWidth_((upperBound - lowerBound) / numBins),
36  {
37  ASSERT_MSG(upperBound > lowerBound, "Upper bound must be larger than lower bound");
38  ASSERT_MSG(numBins > 0, "Number of bins must be positive");
39  }
40 
41  IndexType bin(ValueType value) const override {
42  IndexType binIndex = static_cast<IndexType>((value - lowerBound_) * inverseBinWidth_);
43  binIndex = std::max(IndexType(0), binIndex);
44  binIndex = std::min(numBins_ - 1, binIndex);
45  return binIndex;
46  }
47 
48  boost::optional<IndexType> numBins() const override {
49  return numBins_;
50  }
51 
52  ValueType binWidth() const override {
53  return binWidth_;
54  }
55 
56  ValueType inverseBinWidth() const override {
57  return inverseBinWidth_;
58  }
59 
60  ValueType binCenter(IndexType bin) const override {
61  return lowerBound_ + binWidth_ * (bin + ValueType(0.5));
62  }
63 
64  private:
69 };
70 
71 } // namespace ufo
72 
73 #endif // UFO_UTILS_TRUNCATINGEQUISPACEDBINSELECTOR_H_
A finite or infinite collection of non-overlapping intervals (bins) of the same width.
Represents a finite set of consecutive intervals (bins) of the same width, each closed from the left ...
ValueType binWidth() const override
Return the width of each bin.
ValueType binCenter(IndexType bin) const override
Return the value lying at the center of the bin with index bin.
boost::optional< IndexType > numBins() const override
Return the number of bins or boost::none if the bin collection is infinite.
ValueType inverseBinWidth() const override
Return the inverse of the width of each bin.
TruncatingEquispacedBinSelector(ValueType lowerBound, ValueType upperBound, IndexType numBins)
Partition the interval [lowerBound, upperBound) into numBins bins of the same width.
IndexType bin(ValueType value) const override
Return the index of the bin containing value, or the nearest bin if value lies outside all bins.
Definition: RunCRTM.h:27