| 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 |
| 44 // Remove all observations from the container and reset the state to that of |
| 45 // a newly contructed object. |
| 46 void Clear(); |
| 47 |
| 43 private: | 48 private: |
| 44 // Update iterator and index to point at target percentile value. | 49 // Update iterator and index to point at target percentile value. |
| 45 void UpdatePercentileIterator(); | 50 void UpdatePercentileIterator(); |
| 46 | 51 |
| 47 const float percentile_; | 52 const float percentile_; |
| 48 std::multiset<T> set_; | 53 std::multiset<T> set_; |
| 49 // Maintain iterator and index of current target percentile value. | 54 // Maintain iterator and index of current target percentile value. |
| 50 typename std::multiset<T>::iterator percentile_it_; | 55 typename std::multiset<T>::iterator percentile_it_; |
| 51 int64_t percentile_index_; | 56 int64_t percentile_index_; |
| 52 }; | 57 }; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 69 percentile_it_ = set_.begin(); | 74 percentile_it_ = set_.begin(); |
| 70 percentile_index_ = 0; | 75 percentile_index_ = 0; |
| 71 } else if (value < *percentile_it_) { | 76 } else if (value < *percentile_it_) { |
| 72 // If new element is before us, increment |percentile_index_|. | 77 // If new element is before us, increment |percentile_index_|. |
| 73 ++percentile_index_; | 78 ++percentile_index_; |
| 74 } | 79 } |
| 75 UpdatePercentileIterator(); | 80 UpdatePercentileIterator(); |
| 76 } | 81 } |
| 77 | 82 |
| 78 template <typename T> | 83 template <typename T> |
| 79 void PercentileFilter<T>::Erase(const T& value) { | 84 bool PercentileFilter<T>::Erase(const T& value) { |
| 80 typename std::multiset<T>::const_iterator it = set_.lower_bound(value); | 85 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. | 86 // Ignore erase operation if the element is not present in the current set. |
| 82 if (it == set_.end() || *it != value) | 87 if (it == set_.end() || *it != value) |
| 83 return; | 88 return false; |
| 84 if (it == percentile_it_) { | 89 if (it == percentile_it_) { |
| 85 // If same iterator, update to the following element. Index is not | 90 // If same iterator, update to the following element. Index is not |
| 86 // affected. | 91 // affected. |
| 87 percentile_it_ = set_.erase(it); | 92 percentile_it_ = set_.erase(it); |
| 88 } else { | 93 } else { |
| 89 set_.erase(it); | 94 set_.erase(it); |
| 90 // If erased element was before us, decrement |percentile_index_|. | 95 // If erased element was before us, decrement |percentile_index_|. |
| 91 if (value <= *percentile_it_) | 96 if (value <= *percentile_it_) |
| 92 --percentile_index_; | 97 --percentile_index_; |
| 93 } | 98 } |
| 94 UpdatePercentileIterator(); | 99 UpdatePercentileIterator(); |
| 100 return true; |
| 95 } | 101 } |
| 96 | 102 |
| 97 template <typename T> | 103 template <typename T> |
| 98 void PercentileFilter<T>::UpdatePercentileIterator() { | 104 void PercentileFilter<T>::UpdatePercentileIterator() { |
| 99 if (set_.empty()) | 105 if (set_.empty()) |
| 100 return; | 106 return; |
| 101 const int64_t index = static_cast<int64_t>(percentile_ * (set_.size() - 1)); | 107 const int64_t index = static_cast<int64_t>(percentile_ * (set_.size() - 1)); |
| 102 std::advance(percentile_it_, index - percentile_index_); | 108 std::advance(percentile_it_, index - percentile_index_); |
| 103 percentile_index_ = index; | 109 percentile_index_ = index; |
| 104 } | 110 } |
| 105 | 111 |
| 106 template <typename T> | 112 template <typename T> |
| 107 T PercentileFilter<T>::GetPercentileValue() const { | 113 T PercentileFilter<T>::GetPercentileValue() const { |
| 108 return set_.empty() ? 0 : *percentile_it_; | 114 return set_.empty() ? 0 : *percentile_it_; |
| 109 } | 115 } |
| 110 | 116 |
| 117 template <typename T> |
| 118 void PercentileFilter<T>::Clear() { |
| 119 set_.clear(); |
| 120 percentile_index_ = 0; |
| 121 percentile_it_ = set_.begin(); |
| 122 } |
| 123 |
| 111 } // namespace webrtc | 124 } // namespace webrtc |
| 112 | 125 |
| 113 #endif // WEBRTC_BASE_ANALYTICS_PERCENTILE_FILTER_H_ | 126 #endif // WEBRTC_BASE_ANALYTICS_PERCENTILE_FILTER_H_ |
| OLD | NEW |