Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/modules/video_coding/percentile_filter.h" | 11 #ifndef WEBRTC_BASE_ANALYTICS_PERCENTILE_FILTER_H_ |
| 12 #define WEBRTC_BASE_ANALYTICS_PERCENTILE_FILTER_H_ | |
| 13 | |
| 14 #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_;
| |
| 12 | 15 |
| 13 #include <iterator> | 16 #include <iterator> |
| 17 #include <set> | |
| 14 | 18 |
| 15 #include "webrtc/base/checks.h" | 19 #include "webrtc/base/checks.h" |
| 16 | 20 |
| 17 namespace webrtc { | 21 namespace webrtc { |
| 18 | 22 |
| 19 PercentileFilter::PercentileFilter(float percentile) | 23 // Class to efficiently get the percentile value from a group of observations. |
| 24 // The percentile is the value below which a given percentage of the | |
| 25 // observations fall. | |
| 26 template <typename T> | |
| 27 class PercentileFilter { | |
| 28 public: | |
| 29 // Construct filter. |percentile| should be between 0 and 1. | |
| 30 explicit PercentileFilter(float percentile); | |
| 31 | |
| 32 // Insert one observation. The complexity of this operation is logarithmic in | |
| 33 // the size of the container. | |
| 34 void Insert(const T& value); | |
| 35 | |
| 36 // Remove one observation. The complexity of this operation is logarithmic in | |
| 37 // the size of the container. | |
| 38 void Erase(const T& value); | |
| 39 | |
| 40 // Get the percentile value. The complexity of this operation is constant. | |
| 41 T GetPercentileValue() const; | |
| 42 | |
| 43 private: | |
| 44 // Update iterator and index to point at target percentile value. | |
| 45 void UpdatePercentileIterator(); | |
| 46 | |
| 47 const float percentile_; | |
| 48 std::multiset<T> set_; | |
| 49 // Maintain iterator and index of current target percentile value. | |
| 50 typename std::multiset<T>::iterator percentile_it_; | |
| 51 int64_t percentile_index_; | |
| 52 }; | |
| 53 | |
| 54 template <typename T> | |
| 55 PercentileFilter<T>::PercentileFilter(float percentile) | |
| 20 : percentile_(percentile), | 56 : percentile_(percentile), |
| 21 percentile_it_(set_.begin()), | 57 percentile_it_(set_.begin()), |
| 22 percentile_index_(0) { | 58 percentile_index_(0) { |
| 23 RTC_CHECK_GE(percentile, 0.0f); | 59 RTC_CHECK_GE(percentile, 0.0f); |
| 24 RTC_CHECK_LE(percentile, 1.0f); | 60 RTC_CHECK_LE(percentile, 1.0f); |
| 25 } | 61 } |
| 26 | 62 |
| 27 void PercentileFilter::Insert(const int64_t& value) { | 63 template <typename T> |
| 64 void PercentileFilter<T>::Insert(const T& value) { | |
| 28 // Insert element at the upper bound. | 65 // Insert element at the upper bound. |
| 29 set_.insert(value); | 66 set_.insert(value); |
| 30 if (set_.size() == 1u) { | 67 if (set_.size() == 1u) { |
| 31 // First element inserted - initialize percentile iterator and index. | 68 // First element inserted - initialize percentile iterator and index. |
| 32 percentile_it_ = set_.begin(); | 69 percentile_it_ = set_.begin(); |
| 33 percentile_index_ = 0; | 70 percentile_index_ = 0; |
| 34 } else if (value < *percentile_it_) { | 71 } else if (value < *percentile_it_) { |
| 35 // If new element is before us, increment |percentile_index_|. | 72 // If new element is before us, increment |percentile_index_|. |
| 36 ++percentile_index_; | 73 ++percentile_index_; |
| 37 } | 74 } |
| 38 UpdatePercentileIterator(); | 75 UpdatePercentileIterator(); |
| 39 } | 76 } |
| 40 | 77 |
| 41 void PercentileFilter::Erase(const int64_t& value) { | 78 template <typename T> |
| 42 std::multiset<int64_t>::const_iterator it = set_.lower_bound(value); | 79 void PercentileFilter<T>::Erase(const T& value) { |
| 80 typename std::multiset<T>::const_iterator it = set_.lower_bound(value); | |
| 43 // Ignore erase operation if the element is not present in the current set. | 81 // Ignore erase operation if the element is not present in the current set. |
| 44 if (it == set_.end() || *it != value) | 82 if (it == set_.end() || *it != value) |
| 45 return; | 83 return; |
| 46 if (it == percentile_it_) { | 84 if (it == percentile_it_) { |
| 47 // If same iterator, update to the following element. Index is not affected. | 85 // If same iterator, update to the following element. Index is not |
| 86 // affected. | |
| 48 percentile_it_ = set_.erase(it); | 87 percentile_it_ = set_.erase(it); |
| 49 } else { | 88 } else { |
| 50 set_.erase(it); | 89 set_.erase(it); |
| 51 // If erased element was before us, decrement |percentile_index_|. | 90 // If erased element was before us, decrement |percentile_index_|. |
| 52 if (value <= *percentile_it_) | 91 if (value <= *percentile_it_) |
| 53 --percentile_index_; | 92 --percentile_index_; |
| 54 } | 93 } |
| 55 UpdatePercentileIterator(); | 94 UpdatePercentileIterator(); |
| 56 } | 95 } |
| 57 | 96 |
| 58 void PercentileFilter::UpdatePercentileIterator() { | 97 template <typename T> |
| 98 void PercentileFilter<T>::UpdatePercentileIterator() { | |
| 59 if (set_.empty()) | 99 if (set_.empty()) |
| 60 return; | 100 return; |
| 61 const int64_t index = static_cast<int64_t>(percentile_ * (set_.size() - 1)); | 101 const int64_t index = static_cast<int64_t>(percentile_ * (set_.size() - 1)); |
| 62 std::advance(percentile_it_, index - percentile_index_); | 102 std::advance(percentile_it_, index - percentile_index_); |
| 63 percentile_index_ = index; | 103 percentile_index_ = index; |
| 64 } | 104 } |
| 65 | 105 |
| 66 int64_t PercentileFilter::GetPercentileValue() const { | 106 template <typename T> |
| 107 T PercentileFilter<T>::GetPercentileValue() const { | |
| 67 return set_.empty() ? 0 : *percentile_it_; | 108 return set_.empty() ? 0 : *percentile_it_; |
| 68 } | 109 } |
| 69 | 110 |
| 70 } // namespace webrtc | 111 } // namespace webrtc |
| 112 | |
| 113 #endif // WEBRTC_BASE_ANALYTICS_PERCENTILE_FILTER_H_ | |
| OLD | NEW |