Index: webrtc/base/analytics/percentile_filter.h |
diff --git a/webrtc/modules/video_coding/percentile_filter.cc b/webrtc/base/analytics/percentile_filter.h |
similarity index 50% |
rename from webrtc/modules/video_coding/percentile_filter.cc |
rename to webrtc/base/analytics/percentile_filter.h |
index 6495567540961c86a721b58de9ba99a51694973e..b3c8f8d1775f8df42b22b9a42d802ce454b9c1cb 100644 |
--- a/webrtc/modules/video_coding/percentile_filter.cc |
+++ b/webrtc/base/analytics/percentile_filter.h |
@@ -8,15 +8,51 @@ |
* be found in the AUTHORS file in the root of the source tree. |
*/ |
-#include "webrtc/modules/video_coding/percentile_filter.h" |
+#ifndef WEBRTC_BASE_ANALYTICS_PERCENTILE_FILTER_H_ |
+#define WEBRTC_BASE_ANALYTICS_PERCENTILE_FILTER_H_ |
+ |
+#include <stdint.h> |
magjed_webrtc
2016/11/30 14:22:43
this include looks unused
terelius
2016/11/30 14:32:16
It is included for int64_t percentile_index_;
|
#include <iterator> |
+#include <set> |
#include "webrtc/base/checks.h" |
namespace webrtc { |
-PercentileFilter::PercentileFilter(float percentile) |
+// Class to efficiently get the percentile value from a group of observations. |
+// The percentile is the value below which a given percentage of the |
+// observations fall. |
+template <typename T> |
+class PercentileFilter { |
+ public: |
+ // Construct filter. |percentile| should be between 0 and 1. |
+ explicit PercentileFilter(float percentile); |
+ |
+ // Insert one observation. The complexity of this operation is logarithmic in |
+ // the size of the container. |
+ void Insert(const T& value); |
+ |
+ // Remove one observation. The complexity of this operation is logarithmic in |
+ // the size of the container. |
+ void Erase(const T& value); |
+ |
+ // Get the percentile value. The complexity of this operation is constant. |
+ T GetPercentileValue() const; |
+ |
+ private: |
+ // Update iterator and index to point at target percentile value. |
+ void UpdatePercentileIterator(); |
+ |
+ const float percentile_; |
+ std::multiset<T> set_; |
+ // Maintain iterator and index of current target percentile value. |
+ typename std::multiset<T>::iterator percentile_it_; |
+ int64_t percentile_index_; |
+}; |
+ |
+template <typename T> |
+PercentileFilter<T>::PercentileFilter(float percentile) |
: percentile_(percentile), |
percentile_it_(set_.begin()), |
percentile_index_(0) { |
@@ -24,7 +60,8 @@ PercentileFilter::PercentileFilter(float percentile) |
RTC_CHECK_LE(percentile, 1.0f); |
} |
-void PercentileFilter::Insert(const int64_t& value) { |
+template <typename T> |
+void PercentileFilter<T>::Insert(const T& value) { |
// Insert element at the upper bound. |
set_.insert(value); |
if (set_.size() == 1u) { |
@@ -38,13 +75,15 @@ void PercentileFilter::Insert(const int64_t& value) { |
UpdatePercentileIterator(); |
} |
-void PercentileFilter::Erase(const int64_t& value) { |
- std::multiset<int64_t>::const_iterator it = set_.lower_bound(value); |
+template <typename T> |
+void PercentileFilter<T>::Erase(const T& value) { |
+ typename std::multiset<T>::const_iterator it = set_.lower_bound(value); |
// Ignore erase operation if the element is not present in the current set. |
if (it == set_.end() || *it != value) |
return; |
if (it == percentile_it_) { |
- // If same iterator, update to the following element. Index is not affected. |
+ // If same iterator, update to the following element. Index is not |
+ // affected. |
percentile_it_ = set_.erase(it); |
} else { |
set_.erase(it); |
@@ -55,7 +94,8 @@ void PercentileFilter::Erase(const int64_t& value) { |
UpdatePercentileIterator(); |
} |
-void PercentileFilter::UpdatePercentileIterator() { |
+template <typename T> |
+void PercentileFilter<T>::UpdatePercentileIterator() { |
if (set_.empty()) |
return; |
const int64_t index = static_cast<int64_t>(percentile_ * (set_.size() - 1)); |
@@ -63,8 +103,11 @@ void PercentileFilter::UpdatePercentileIterator() { |
percentile_index_ = index; |
} |
-int64_t PercentileFilter::GetPercentileValue() const { |
+template <typename T> |
+T PercentileFilter<T>::GetPercentileValue() const { |
return set_.empty() ? 0 : *percentile_it_; |
} |
} // namespace webrtc |
+ |
+#endif // WEBRTC_BASE_ANALYTICS_PERCENTILE_FILTER_H_ |