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 16 matching lines...) Expand all Loading... | |
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. The complexity of this operation is logarithmic in |
37 // the size of the container. | 37 // the size of the container. |
stefan-webrtc
2016/11/28 13:01:58
Comment on what this returns
terelius
2016/12/02 16:45:52
Done.
| |
38 void Erase(const T& value); | 38 bool Erase(const T& value); |
39 | 39 |
40 // Get the percentile value. The complexity of this operation is constant. | 40 // Get the percentile value. The complexity of this operation is constant. |
41 T GetPercentileValue() const; | 41 T GetPercentileValue() const; |
42 | 42 |
43 private: | 43 private: |
44 // Update iterator and index to point at target percentile value. | 44 // Update iterator and index to point at target percentile value. |
45 void UpdatePercentileIterator(); | 45 void UpdatePercentileIterator(); |
46 | 46 |
47 const float percentile_; | 47 const float percentile_; |
48 std::multiset<T> set_; | 48 std::multiset<T> set_; |
(...skipping 20 matching lines...) Expand all Loading... | |
69 percentile_it_ = set_.begin(); | 69 percentile_it_ = set_.begin(); |
70 percentile_index_ = 0; | 70 percentile_index_ = 0; |
71 } else if (value < *percentile_it_) { | 71 } else if (value < *percentile_it_) { |
72 // If new element is before us, increment |percentile_index_|. | 72 // If new element is before us, increment |percentile_index_|. |
73 ++percentile_index_; | 73 ++percentile_index_; |
74 } | 74 } |
75 UpdatePercentileIterator(); | 75 UpdatePercentileIterator(); |
76 } | 76 } |
77 | 77 |
78 template <typename T> | 78 template <typename T> |
79 void PercentileFilter<T>::Erase(const T& value) { | 79 bool PercentileFilter<T>::Erase(const T& value) { |
80 typename std::multiset<T>::const_iterator it = set_.lower_bound(value); | 80 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. | 81 // Ignore erase operation if the element is not present in the current set. |
82 if (it == set_.end() || *it != value) | 82 if (it == set_.end() || *it != value) |
83 return; | 83 return false; |
84 if (it == percentile_it_) { | 84 if (it == percentile_it_) { |
85 // If same iterator, update to the following element. Index is not | 85 // If same iterator, update to the following element. Index is not |
86 // affected. | 86 // affected. |
87 percentile_it_ = set_.erase(it); | 87 percentile_it_ = set_.erase(it); |
88 } else { | 88 } else { |
89 set_.erase(it); | 89 set_.erase(it); |
90 // If erased element was before us, decrement |percentile_index_|. | 90 // If erased element was before us, decrement |percentile_index_|. |
91 if (value <= *percentile_it_) | 91 if (value <= *percentile_it_) |
92 --percentile_index_; | 92 --percentile_index_; |
93 } | 93 } |
94 UpdatePercentileIterator(); | 94 UpdatePercentileIterator(); |
95 return true; | |
95 } | 96 } |
96 | 97 |
97 template <typename T> | 98 template <typename T> |
98 void PercentileFilter<T>::UpdatePercentileIterator() { | 99 void PercentileFilter<T>::UpdatePercentileIterator() { |
99 if (set_.empty()) | 100 if (set_.empty()) |
100 return; | 101 return; |
101 const int64_t index = static_cast<int64_t>(percentile_ * (set_.size() - 1)); | 102 const int64_t index = static_cast<int64_t>(percentile_ * (set_.size() - 1)); |
102 std::advance(percentile_it_, index - percentile_index_); | 103 std::advance(percentile_it_, index - percentile_index_); |
103 percentile_index_ = index; | 104 percentile_index_ = index; |
104 } | 105 } |
105 | 106 |
106 template <typename T> | 107 template <typename T> |
107 T PercentileFilter<T>::GetPercentileValue() const { | 108 T PercentileFilter<T>::GetPercentileValue() const { |
108 return set_.empty() ? 0 : *percentile_it_; | 109 return set_.empty() ? 0 : *percentile_it_; |
109 } | 110 } |
110 | 111 |
111 } // namespace webrtc | 112 } // namespace webrtc |
112 | 113 |
113 #endif // WEBRTC_BASE_ANALYTICS_PERCENTILE_FILTER_H_ | 114 #endif // WEBRTC_BASE_ANALYTICS_PERCENTILE_FILTER_H_ |
OLD | NEW |