Performs "sparsification" to limit the size of the time series to be within the capacity. The sparsifying technique is to iteratively halve the granularity of the older half of the time series. Once sparsification reaches the midpoint of the time series, it begins again from the beginning. Sparsification results in the following granularity over time: Initial: | ------------------------ A ------
| 189 | // Each stage halves the size and granularity of time series prior |
| 190 | // to sparsifying. |
| 191 | void sparsify() |
| 192 | { |
| 193 | // We remove every other element up to the halfway point of the |
| 194 | // time series, until we're within the capacity. If we reach the |
| 195 | // half-way point of the time series, we'll start another |
| 196 | // sparsification cycle from the beginning, for example: |
| 197 | // |
| 198 | // next Time series with a capacity of 7. |
| 199 | // v Initial state with 7 entries |
| 200 | // 0 1 2 3 4 5 6 |
| 201 | // |
| 202 | // next Insert '7'. |
| 203 | // v Capacity is exceeded, we remove '1' and |
| 204 | // 0 2 3 4 5 6 7 advance to remove '3' next. |
| 205 | // |
| 206 | // next Insert '8'. |
| 207 | // v Capacity is exceeded, we remove '3' and |
| 208 | // 0 2 4 5 6 7 8 advance to remove '5' next. |
| 209 | // |
| 210 | // next Insert '9'. |
| 211 | // v Capacity is exceeded, we remove '5' and now |
| 212 | // 0 2 4 6 7 8 9 '7' is past the halfway mark, so we will reset |
| 213 | // reset to the beginning and consider '2'. |
| 214 | |
| 215 | while (values.size() > capacity) { |
| 216 | // If the index is uninitialized, or past the half-way point, |
| 217 | // we set it back to the beginning. |
| 218 | if (index.isNone() || index.get() > values.size() / 2) { |
| 219 | // The second element is the initial deletion candidate. |
| 220 | next = values.begin(); |
| 221 | ++next; |
| 222 | index = 1; |
| 223 | } |
| 224 | |
| 225 | next = values.erase(next); |
| 226 | next++; // Skip one element. |
| 227 | index = index.get() + 1; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // Non-const for assignability. |
| 232 | Duration window; |