Health-GPS  1.2.2.0
Global Health Policy Simulation model (Health-GPS)
monotonic_vector.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdexcept>
4 #include <vector>
5 
7 
8 namespace hgps {
9 
14 template <core::Numerical TYPE> bool is_strict_monotonic(const std::vector<TYPE> &values) noexcept {
15  auto compare = [](TYPE a, TYPE b) { return a < b ? -1 : (a == b) ? 0 : 1; };
16 
17  int previous = 0;
18  int offset = 1;
19  auto count = values.size();
20  if (count > 0) {
21  count--;
22  }
23 
24  for (std::size_t i = 0; i < count; ++i) {
25  int current = compare(values[i], values[i + offset]);
26  if (current == 0) {
27  return false;
28  }
29 
30  if (current != previous && previous != 0) {
31  return false;
32  }
33 
34  previous = current;
35  }
36 
37  return true;
38 }
39 
42 template <core::Numerical TYPE> class MonotonicVector {
43  public:
45  using IteratorType = typename std::vector<TYPE>::iterator;
47  using ConstIteratorType = typename std::vector<TYPE>::const_iterator;
48 
52  MonotonicVector(std::vector<TYPE> &values) : data_{values} {
53  if (!is_strict_monotonic(values)) {
54  throw std::invalid_argument("Values must be strict monotonic.");
55  }
56  }
57 
60  std::size_t size() const noexcept { return data_.size(); }
61 
65  const TYPE &at(std::size_t index) const { return data_.at(index); }
66 
70  TYPE &operator[](std::size_t index) { return data_.at(index); }
71 
75  const TYPE &operator[](std::size_t index) const { return data_.at(index); }
76 
79  IteratorType begin() noexcept { return data_.begin(); }
80 
83  IteratorType end() noexcept { return data_.end(); }
84 
87  ConstIteratorType begin() const noexcept { return data_.cbegin(); }
88 
91  ConstIteratorType end() const noexcept { return data_.cend(); }
92 
95  ConstIteratorType cbegin() const noexcept { return data_.cbegin(); }
96 
99  ConstIteratorType cend() const noexcept { return data_.cend(); }
100 
101  private:
102  std::vector<TYPE> data_;
103 };
104 } // namespace hgps
Defines a monotonic vector container type.
Definition: monotonic_vector.h:42
typename std::vector< TYPE >::iterator IteratorType
Element iterator.
Definition: monotonic_vector.h:45
std::size_t size() const noexcept
Gets the number of elements.
Definition: monotonic_vector.h:60
TYPE & operator[](std::size_t index)
Gets a specified element with bounds checking.
Definition: monotonic_vector.h:70
ConstIteratorType begin() const noexcept
Gets an read-only iterator to the beginning of the elements.
Definition: monotonic_vector.h:87
ConstIteratorType cend() const noexcept
Gets an read-only iterator to the element following the element of the vector.
Definition: monotonic_vector.h:99
ConstIteratorType cbegin() const noexcept
Gets an read-only iterator to the beginning of the elements.
Definition: monotonic_vector.h:95
const TYPE & operator[](std::size_t index) const
Gets a specified read-only element with bounds checking.
Definition: monotonic_vector.h:75
IteratorType begin() noexcept
Gets an iterator to the beginning of the elements.
Definition: monotonic_vector.h:79
typename std::vector< TYPE >::const_iterator ConstIteratorType
Read-only element iterator.
Definition: monotonic_vector.h:47
IteratorType end() noexcept
Gets an iterator to the element following the element of the vector.
Definition: monotonic_vector.h:83
const TYPE & at(std::size_t index) const
Gets a specified element with bounds checking.
Definition: monotonic_vector.h:65
MonotonicVector(std::vector< TYPE > &values)
Initialises a new instance of the MonotonicVector class.
Definition: monotonic_vector.h:52
ConstIteratorType end() const noexcept
Gets an read-only iterator to the element following the element of the vector.
Definition: monotonic_vector.h:91
Top-level namespace for Health-GPS C++ API.
Definition: analysis_definition.h:8
bool is_strict_monotonic(const std::vector< TYPE > &values) noexcept
Determine whether a vector data is strict monotonic.
Definition: monotonic_vector.h:14