Removes values outside the time window. This will ensure at least one value remains. Note that this is called automatically when writing to the time series, so this is only needed when one wants to explicitly trigger a truncation.
| 122 | // when writing to the time series, so this is only needed when |
| 123 | // one wants to explicitly trigger a truncation. |
| 124 | void truncate() |
| 125 | { |
| 126 | Time expired = Clock::now() - window; |
| 127 | typename std::map<Time, T>::iterator upper_bound = |
| 128 | values.upper_bound(expired); |
| 129 | |
| 130 | // Ensure at least 1 value remains. |
| 131 | if (values.size() <= 1 || upper_bound == values.end()) { |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | // When truncating and there exists a next value considered |
| 136 | // for sparsification, there are two cases to consider for |
| 137 | // updating the index: |
| 138 | // |
| 139 | // Case 1: upper_bound < next |
| 140 | // ---------------------------------------------------------- |
| 141 | // upper_bound index, next |
| 142 | // v v |
| 143 | // Before: 0 1 2 3 4 5 6 7 ... |
| 144 | // ---------------------------------------------------------- |
| 145 | // next index After truncating, index is |
| 146 | // v v must be adjusted: |
| 147 | // Truncate: 3 4 5 6 7 ... index -= # elements removed |
| 148 | // ---------------------------------------------------------- |
| 149 | // index, next |
| 150 | // v |
| 151 | // After: 3 4 5 6 7 ... |
| 152 | // ---------------------------------------------------------- |
| 153 | // |
| 154 | // Case 2: upper_bound >= next |
| 155 | // ---------------------------------------------------------- |
| 156 | // upper_bound, index, next |
| 157 | // v |
| 158 | // Before: 0 1 2 3 4 5 6 7 ... |
| 159 | // ---------------------------------------------------------- |
| 160 | // After truncating, we must |
| 161 | // After: 4 5 6 7 ... reset index to None(). |
| 162 | // ---------------------------------------------------------- |
| 163 | if (index.isSome() && upper_bound->first < next->first) { |
| 164 | size_t size = values.size(); |
| 165 | values.erase(values.begin(), upper_bound); |
| 166 | index = index.get() - (size - values.size()); |
| 167 | } else { |
| 168 | index = None(); |
| 169 | values.erase(values.begin(), upper_bound); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | private: |
| 174 | // Performs "sparsification" to limit the size of the time series |