moving_max_counter.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_NUMERICS_MOVING_MAX_COUNTER_H_
  11. #define RTC_BASE_NUMERICS_MOVING_MAX_COUNTER_H_
  12. #include <stdint.h>
  13. #include <deque>
  14. #include <limits>
  15. #include <utility>
  16. #include "absl/types/optional.h"
  17. #include "rtc_base/checks.h"
  18. #include "rtc_base/constructor_magic.h"
  19. namespace rtc {
  20. // Implements moving max: can add samples to it and calculate maximum over some
  21. // fixed moving window.
  22. //
  23. // Window size is configured at constructor.
  24. // Samples can be added with |Add()| and max over current window is returned by
  25. // |MovingMax|. |current_time_ms| in successive calls to Add and MovingMax
  26. // should never decrease as if it's a wallclock time.
  27. template <class T>
  28. class MovingMaxCounter {
  29. public:
  30. explicit MovingMaxCounter(int64_t window_length_ms);
  31. // Advances the current time, and adds a new sample. The new current time must
  32. // be at least as large as the old current time.
  33. void Add(const T& sample, int64_t current_time_ms);
  34. // Advances the current time, and returns the maximum sample in the time
  35. // window ending at the current time. The new current time must be at least as
  36. // large as the old current time.
  37. absl::optional<T> Max(int64_t current_time_ms);
  38. void Reset();
  39. private:
  40. // Throws out obsolete samples.
  41. void RollWindow(int64_t new_time_ms);
  42. const int64_t window_length_ms_;
  43. // This deque stores (timestamp, sample) pairs in chronological order; new
  44. // pairs are only ever added at the end. However, because they can't affect
  45. // the Max() calculation, pairs older than window_length_ms_ are discarded,
  46. // and if an older pair has a sample that's smaller than that of a younger
  47. // pair, the older pair is discarded. As a result, the sequence of timestamps
  48. // is strictly increasing, and the sequence of samples is strictly decreasing.
  49. std::deque<std::pair<int64_t, T>> samples_;
  50. #if RTC_DCHECK_IS_ON
  51. int64_t last_call_time_ms_ = std::numeric_limits<int64_t>::min();
  52. #endif
  53. RTC_DISALLOW_COPY_AND_ASSIGN(MovingMaxCounter);
  54. };
  55. template <class T>
  56. MovingMaxCounter<T>::MovingMaxCounter(int64_t window_length_ms)
  57. : window_length_ms_(window_length_ms) {}
  58. template <class T>
  59. void MovingMaxCounter<T>::Add(const T& sample, int64_t current_time_ms) {
  60. RollWindow(current_time_ms);
  61. // Remove samples that will never be maximum in any window: newly added sample
  62. // will always be in all windows the previous samples are. Thus, smaller or
  63. // equal samples could be removed. This will maintain the invariant - deque
  64. // contains strictly decreasing sequence of values.
  65. while (!samples_.empty() && samples_.back().second <= sample) {
  66. samples_.pop_back();
  67. }
  68. // Add the new sample but only if there's no existing sample at the same time.
  69. // Due to checks above, the already existing element will be larger, so the
  70. // new sample will never be the maximum in any window.
  71. if (samples_.empty() || samples_.back().first < current_time_ms) {
  72. samples_.emplace_back(std::make_pair(current_time_ms, sample));
  73. }
  74. }
  75. template <class T>
  76. absl::optional<T> MovingMaxCounter<T>::Max(int64_t current_time_ms) {
  77. RollWindow(current_time_ms);
  78. absl::optional<T> res;
  79. if (!samples_.empty()) {
  80. res.emplace(samples_.front().second);
  81. }
  82. return res;
  83. }
  84. template <class T>
  85. void MovingMaxCounter<T>::Reset() {
  86. samples_.clear();
  87. }
  88. template <class T>
  89. void MovingMaxCounter<T>::RollWindow(int64_t new_time_ms) {
  90. #if RTC_DCHECK_IS_ON
  91. RTC_DCHECK_GE(new_time_ms, last_call_time_ms_);
  92. last_call_time_ms_ = new_time_ms;
  93. #endif
  94. const int64_t window_begin_ms = new_time_ms - window_length_ms_;
  95. auto it = samples_.begin();
  96. while (it != samples_.end() && it->first < window_begin_ms) {
  97. ++it;
  98. }
  99. samples_.erase(samples_.begin(), it);
  100. }
  101. } // namespace rtc
  102. #endif // RTC_BASE_NUMERICS_MOVING_MAX_COUNTER_H_