Chromium Code Reviews| Index: webrtc/modules/video_coding/percentile_filter.cc |
| diff --git a/webrtc/modules/video_coding/percentile_filter.cc b/webrtc/modules/video_coding/percentile_filter.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9e24f65aa7c21b667d9094f1f5052d3b6cb48c4b |
| --- /dev/null |
| +++ b/webrtc/modules/video_coding/percentile_filter.cc |
| @@ -0,0 +1,74 @@ |
| +/* |
| + * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| + |
| +#include "webrtc/modules/video_coding/percentile_filter.h" |
| + |
| +#include <iterator> |
| + |
| +#include "webrtc/base/checks.h" |
| + |
| +namespace webrtc { |
| + |
| +PercentileFilter::PercentileFilter(float percentile) : percentile_(percentile) { |
| + RTC_CHECK_GE(percentile, 0.0f); |
| + RTC_CHECK_LE(percentile, 1.0f); |
| + Clear(); |
| +} |
| + |
| +void PercentileFilter::Clear() { |
| + set_.clear(); |
| + it_ = set_.begin(); |
| + index_ = 0; |
| +} |
| + |
| +void PercentileFilter::Insert(const int32_t& value) { |
| + // Insert element at the upper bound. |
| + set_.insert(value); |
| + if (set_.size() == 1u) { |
| + // First element inserted - initialize percentile iterator and index. |
| + it_ = set_.begin(); |
| + index_ = 0; |
| + } else if (value < *it_) { |
| + // If new element is before us, increment |index_|. |
| + ++index_; |
| + } |
| + UpdatePercentileIterator(); |
| +} |
| + |
| +void PercentileFilter::Erase(const int32_t& value) { |
| + std::multiset<int32_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()) |
| + return; |
| + if (it == it_) { |
| + // If same iterator, update to the following element. Index is not affected. |
| + it_ = set_.erase(it); |
| + } else { |
| + set_.erase(it); |
| + // If erased element was before us, decrement |index_|. |
| + if (value <= *it_) |
| + --index_; |
| + } |
| + UpdatePercentileIterator(); |
| +} |
| + |
| +void PercentileFilter::UpdatePercentileIterator() { |
| + if (set_.empty()) |
| + return; |
| + const int64_t index = static_cast<int64_t>(percentile_ * (set_.size() - 1)); |
|
philipel
2016/03/03 13:34:31
Does this cast do anything?
magjed_webrtc
2016/03/04 14:19:41
It compiles without, but I prefer to do it explici
|
| + std::advance(it_, index - index_); |
| + index_ = index; |
| +} |
| + |
| +int32_t PercentileFilter::GetPercentileValue() const { |
| + return set_.empty() ? 0 : *it_; |
| +} |
| + |
| +} // namespace webrtc |