| 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 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 template <typename T> | 26 template <typename T> |
| 27 class PercentileFilter { | 27 class PercentileFilter { |
| 28 public: | 28 public: |
| 29 // Construct filter. |percentile| should be between 0 and 1. | 29 // Construct filter. |percentile| should be between 0 and 1. |
| 30 explicit PercentileFilter(float percentile); | 30 explicit PercentileFilter(float percentile); |
| 31 | 31 |
| 32 // Insert one observation. The complexity of this operation is logarithmic in | 32 // Insert one observation. The complexity of this operation is logarithmic in |
| 33 // the size of the container. | 33 // the size of the container. |
| 34 void Insert(const T& value); | 34 void Insert(const T& value); |
| 35 | 35 |
| 36 // Remove one observation. The complexity of this operation is logarithmic in | 36 // Remove one observation or return false if |value| doesn't exist in the |
| 37 // the size of the container. | 37 // container. The complexity of this operation is logarithmic in the size of |
| 38 void Erase(const T& value); | 38 // the container. |
| 39 bool Erase(const T& value); |
| 39 | 40 |
| 40 // Get the percentile value. The complexity of this operation is constant. | 41 // Get the percentile value. The complexity of this operation is constant. |
| 41 T GetPercentileValue() const; | 42 T GetPercentileValue() const; |
| 42 | 43 |
| 43 private: | 44 private: |
| 44 // Update iterator and index to point at target percentile value. | 45 // Update iterator and index to point at target percentile value. |
| 45 void UpdatePercentileIterator(); | 46 void UpdatePercentileIterator(); |
| 46 | 47 |
| 47 const float percentile_; | 48 const float percentile_; |
| 48 std::multiset<T> set_; | 49 std::multiset<T> set_; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 69 percentile_it_ = set_.begin(); | 70 percentile_it_ = set_.begin(); |
| 70 percentile_index_ = 0; | 71 percentile_index_ = 0; |
| 71 } else if (value < *percentile_it_) { | 72 } else if (value < *percentile_it_) { |
| 72 // If new element is before us, increment |percentile_index_|. | 73 // If new element is before us, increment |percentile_index_|. |
| 73 ++percentile_index_; | 74 ++percentile_index_; |
| 74 } | 75 } |
| 75 UpdatePercentileIterator(); | 76 UpdatePercentileIterator(); |
| 76 } | 77 } |
| 77 | 78 |
| 78 template <typename T> | 79 template <typename T> |
| 79 void PercentileFilter<T>::Erase(const T& value) { | 80 bool PercentileFilter<T>::Erase(const T& value) { |
| 80 typename std::multiset<T>::const_iterator it = set_.lower_bound(value); | 81 typename std::multiset<T>::const_iterator it = set_.lower_bound(value); |
| 81 // Ignore erase operation if the element is not present in the current set. | 82 // Ignore erase operation if the element is not present in the current set. |
| 82 if (it == set_.end() || *it != value) | 83 if (it == set_.end() || *it != value) |
| 83 return; | 84 return false; |
| 84 if (it == percentile_it_) { | 85 if (it == percentile_it_) { |
| 85 // If same iterator, update to the following element. Index is not | 86 // If same iterator, update to the following element. Index is not |
| 86 // affected. | 87 // affected. |
| 87 percentile_it_ = set_.erase(it); | 88 percentile_it_ = set_.erase(it); |
| 88 } else { | 89 } else { |
| 89 set_.erase(it); | 90 set_.erase(it); |
| 90 // If erased element was before us, decrement |percentile_index_|. | 91 // If erased element was before us, decrement |percentile_index_|. |
| 91 if (value <= *percentile_it_) | 92 if (value <= *percentile_it_) |
| 92 --percentile_index_; | 93 --percentile_index_; |
| 93 } | 94 } |
| 94 UpdatePercentileIterator(); | 95 UpdatePercentileIterator(); |
| 96 return true; |
| 95 } | 97 } |
| 96 | 98 |
| 97 template <typename T> | 99 template <typename T> |
| 98 void PercentileFilter<T>::UpdatePercentileIterator() { | 100 void PercentileFilter<T>::UpdatePercentileIterator() { |
| 99 if (set_.empty()) | 101 if (set_.empty()) |
| 100 return; | 102 return; |
| 101 const int64_t index = static_cast<int64_t>(percentile_ * (set_.size() - 1)); | 103 const int64_t index = static_cast<int64_t>(percentile_ * (set_.size() - 1)); |
| 102 std::advance(percentile_it_, index - percentile_index_); | 104 std::advance(percentile_it_, index - percentile_index_); |
| 103 percentile_index_ = index; | 105 percentile_index_ = index; |
| 104 } | 106 } |
| 105 | 107 |
| 106 template <typename T> | 108 template <typename T> |
| 107 T PercentileFilter<T>::GetPercentileValue() const { | 109 T PercentileFilter<T>::GetPercentileValue() const { |
| 108 return set_.empty() ? 0 : *percentile_it_; | 110 return set_.empty() ? 0 : *percentile_it_; |
| 109 } | 111 } |
| 110 | 112 |
| 111 } // namespace webrtc | 113 } // namespace webrtc |
| 112 | 114 |
| 113 #endif // WEBRTC_BASE_ANALYTICS_PERCENTILE_FILTER_H_ | 115 #endif // WEBRTC_BASE_ANALYTICS_PERCENTILE_FILTER_H_ |
| OLD | NEW |