OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | |
ilnik
2017/08/18 13:31:17
changed to 2017. Will upload in the next patch set
| |
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 | |
11 #ifndef WEBRTC_RTC_BASE_MOVING_MAX_COUNTER_H_ | |
12 #define WEBRTC_RTC_BASE_MOVING_MAX_COUNTER_H_ | |
13 | |
14 #include <stdint.h> | |
15 | |
16 #include <deque> | |
17 #include <limits> | |
18 #include <utility> | |
19 | |
20 #include "webrtc/rtc_base/checks.h" | |
21 #include "webrtc/rtc_base/constructormagic.h" | |
22 #include "webrtc/rtc_base/optional.h" | |
23 | |
24 namespace rtc { | |
25 | |
26 // Implements moving max: can add samples to it and calculate maximum over some | |
27 // fixed moving window. | |
28 // | |
29 // Window size is configured at constructor. | |
30 // Samples can be added with |Add()| and max over current window is returned by | |
31 // |MovingMax|. |time_ms| in consecutive calls to Add and MovingMax should never | |
32 // decrease as if it's a current time. | |
33 // Not thread-safe. | |
34 template <class T> | |
35 class MovingMaxCounter { | |
36 public: | |
37 explicit MovingMaxCounter(int window_length_ms); | |
sprang_webrtc
2017/08/21 14:41:09
For consistency, maybe use int64_t for times
ilnik
2017/08/21 14:59:27
Done.
| |
38 // Adds new sample at a given current time. | |
39 void Add(const T& sample, int64_t time_ms); | |
40 // Returns max sample in window ending at a given current time. | |
41 rtc::Optional<T> MovingMax(int64_t time_ms) const; | |
sprang_webrtc
2017/08/21 14:41:09
Remove Moving from name
ilnik
2017/08/21 14:59:27
Done.
| |
42 void Reset(); | |
43 | |
44 private: | |
45 // Throws out obsolete samples. Is |const| because it is always safe to throw | |
46 // out old elements as long as |time_ms| parameters to public methods are | |
47 // always increasing. | |
48 void RollWindow(int64_t time_ms) const; | |
49 int window_length_ms_; | |
sprang_webrtc
2017/08/21 14:41:09
const
ilnik
2017/08/21 14:59:27
Done.
| |
50 // Samples in the window are stored as pairs <time, sample> in this deque in | |
51 // chronological order. Samples what can't be maximum in any window are | |
52 // thrown out - deque always contains non-increasing sequence of samples. | |
53 // Is |mutable| because |MovingMax()| may be called on const instances yet it | |
54 // will remove obsolete samples to speed up consecutive calls. | |
55 mutable std::deque<std::pair<int64_t, T>> samples_; | |
56 mutable int64_t last_call_time_ms_; | |
57 | |
58 RTC_DISALLOW_COPY_AND_ASSIGN(MovingMaxCounter); | |
59 }; | |
60 | |
61 template <class T> | |
62 MovingMaxCounter<T>::MovingMaxCounter(int window_length_ms) | |
63 : window_length_ms_(window_length_ms), | |
64 last_call_time_ms_(std::numeric_limits<int64_t>::min()) {} | |
65 | |
66 template <class T> | |
67 void MovingMaxCounter<T>::Add(const T& sample, int64_t time_ms) { | |
68 RTC_DCHECK_GE(time_ms, last_call_time_ms_); | |
69 last_call_time_ms_ = time_ms; | |
70 RollWindow(time_ms); | |
71 // Remove samples what will never be maximum in any window: newly added sample | |
72 // will always be in all windows the previous samples are. Thus, smaller | |
73 // samples could be removed. This will maintain the invariant - deque contains | |
74 // non-increasing sequence of values. | |
75 while (!samples_.empty() && samples_.back().second < sample) { | |
76 samples_.pop_back(); | |
77 } | |
78 samples_.emplace_back(std::make_pair(time_ms, sample)); | |
sprang_webrtc
2017/08/21 14:41:09
if samples_[time_ms] exists, you can just update t
ilnik
2017/08/21 14:59:27
Seems like an unneeded optimization to me. If the
sprang_webrtc
2017/08/22 08:44:50
Agree that it should not matter for the use case i
ilnik
2017/08/22 09:06:39
Fair enough. I am adding the checks now.
| |
79 } | |
80 | |
81 template <class T> | |
82 rtc::Optional<T> MovingMaxCounter<T>::MovingMax(int64_t time_ms) const { | |
83 RTC_DCHECK_GE(time_ms, last_call_time_ms_); | |
84 last_call_time_ms_ = time_ms; | |
85 RollWindow(time_ms); | |
86 rtc::Optional<T> res; | |
87 if (!samples_.empty()) { | |
88 res.emplace(samples_.front().second); | |
89 } | |
90 return res; | |
91 } | |
92 | |
93 template <class T> | |
94 void MovingMaxCounter<T>::Reset() { | |
95 samples_.clear(); | |
96 } | |
97 | |
98 template <class T> | |
99 void MovingMaxCounter<T>::RollWindow(int64_t time_ms) const { | |
100 int64_t window_begin_ms = time_ms - window_length_ms_; | |
101 while (!samples_.empty() && samples_.front().first < window_begin_ms) { | |
102 samples_.pop_front(); | |
103 } | |
104 } | |
105 | |
106 } // namespace rtc | |
107 | |
108 #endif // WEBRTC_RTC_BASE_MOVING_MAX_COUNTER_H_ | |
OLD | NEW |