| OLD | NEW | 
| (Empty) |  | 
 |   1 /* | 
 |   2  *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 
 |   3  * | 
 |   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 | 
 |   6  *  tree. An additional intellectual property rights grant can be found | 
 |   7  *  in the file PATENTS.  All contributing project authors may | 
 |   8  *  be found in the AUTHORS file in the root of the source tree. | 
 |   9  */ | 
 |  10  | 
 |  11 #include "webrtc/modules/audio_processing/echo_detector/sliding_window_minimum.h
    " | 
 |  12  | 
 |  13 #include <algorithm> | 
 |  14 #include <limits> | 
 |  15  | 
 |  16 #include "webrtc/base/checks.h" | 
 |  17  | 
 |  18 namespace webrtc { | 
 |  19  | 
 |  20 SlidingWindowMinimum::SlidingWindowMinimum(size_t window_size) | 
 |  21     : values_(window_size), right_to_left_min_(window_size) {} | 
 |  22 SlidingWindowMinimum::SlidingWindowMinimum(SlidingWindowMinimum&& other) = | 
 |  23     default; | 
 |  24 SlidingWindowMinimum::~SlidingWindowMinimum() = default; | 
 |  25  | 
 |  26 void SlidingWindowMinimum::AddValue(size_t new_value) { | 
 |  27   left_to_right_min_ = std::min(left_to_right_min_, new_value); | 
 |  28  | 
 |  29   RTC_DCHECK(window_index_ < values_.size()); | 
 |  30   values_[window_index_] = new_value; | 
 |  31  | 
 |  32   window_index_++; | 
 |  33   if (window_index_ == right_to_left_min_.size()) { | 
 |  34     // Update right to left values. | 
 |  35     RTC_DCHECK(values_.size() == right_to_left_min_.size()); | 
 |  36     auto i = right_to_left_min_.rbegin(); | 
 |  37     size_t rl = std::numeric_limits<size_t>::max(); | 
 |  38     // This loop is O(n), but it is only executed once every n iterations. | 
 |  39     for (auto val = values_.rbegin(); val != values_.rend(); ++val, ++i) { | 
 |  40       rl = std::min(rl, *val); | 
 |  41       *i = rl; | 
 |  42     } | 
 |  43     left_to_right_min_ = std::numeric_limits<size_t>::max(); | 
 |  44     window_index_ = 0; | 
 |  45   } | 
 |  46 } | 
 |  47  | 
 |  48 size_t SlidingWindowMinimum::GetMinimum() { | 
 |  49   return std::min(left_to_right_min_, right_to_left_min_[window_index_]); | 
 |  50 } | 
 |  51  | 
 |  52 }  // namespace webrtc | 
| OLD | NEW |