UFO
ConstrainedRange.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2021 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_DATAEXTRACTOR_CONSTRAINEDRANGE_H_
9 #define UFO_UTILS_DATAEXTRACTOR_CONSTRAINEDRANGE_H_
10 
11 #include <cassert>
12 
13 namespace ufo {
14 
15 /// \brief A range of indices.
17  public:
18  /// \brief Create an unconstrained range of size `size`.
19  explicit ConstrainedRange(int size = 0) : size_(size) {
20  reset();
21  }
22 
23  /// \brief Return the index of the first element in the range.
24  int begin() const { return begin_; }
25  /// \brief Return the index of the element past the end of the range.
26  int end() const { return end_; }
27 
28  /// \brief Return the range length.
29  int size() const { return end_ - begin_; }
30  /// \brief Return true if the range is empty, false otherwise.
31  bool empty() const { return end_ == begin_; }
32 
33  /// \brief Constrain the range.
34  void constrain(int newBegin, int newEnd) {
35  assert(newBegin >= begin_);
36  assert(newEnd <= end_);
37  begin_ = newBegin;
38  end_ = newEnd;
39  }
40 
41  /// \brief Remove any constraints, resetting the range to its original size.
42  void reset() {
43  begin_ = 0;
44  end_ = size_;
45  }
46 
47  private:
48  int size_;
49  int begin_;
50  int end_;
51 };
52 
53 } // namespace ufo
54 
55 #endif // UFO_UTILS_DATAEXTRACTOR_CONSTRAINEDRANGE_H_
A range of indices.
bool empty() const
Return true if the range is empty, false otherwise.
int end() const
Return the index of the element past the end of the range.
ConstrainedRange(int size=0)
Create an unconstrained range of size size.
void reset()
Remove any constraints, resetting the range to its original size.
int size() const
Return the range length.
void constrain(int newBegin, int newEnd)
Constrain the range.
int begin() const
Return the index of the first element in the range.
Definition: RunCRTM.h:27