tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

histogram.cc (1703B)


      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 "modules/video_coding/histogram.h"
     12 
     13 #include <algorithm>
     14 #include <cstddef>
     15 
     16 #include "rtc_base/checks.h"
     17 
     18 namespace webrtc {
     19 namespace video_coding {
     20 Histogram::Histogram(size_t num_buckets, size_t max_num_values) {
     21  RTC_DCHECK_GT(num_buckets, 0);
     22  RTC_DCHECK_GT(max_num_values, 0);
     23  buckets_.resize(num_buckets);
     24  values_.reserve(max_num_values);
     25  index_ = 0;
     26 }
     27 
     28 void Histogram::Add(size_t value) {
     29  value = std::min<size_t>(value, buckets_.size() - 1);
     30  if (index_ < values_.size()) {
     31    --buckets_[values_[index_]];
     32    RTC_DCHECK_LT(values_[index_], buckets_.size());
     33    values_[index_] = value;
     34  } else {
     35    values_.emplace_back(value);
     36  }
     37 
     38  ++buckets_[value];
     39  index_ = (index_ + 1) % values_.capacity();
     40 }
     41 
     42 size_t Histogram::InverseCdf(float probability) const {
     43  RTC_DCHECK_GE(probability, 0.f);
     44  RTC_DCHECK_LE(probability, 1.f);
     45  RTC_DCHECK_GT(values_.size(), 0ul);
     46 
     47  size_t bucket = 0;
     48  float accumulated_probability = 0;
     49  while (accumulated_probability < probability && bucket < buckets_.size()) {
     50    accumulated_probability +=
     51        static_cast<float>(buckets_[bucket]) / values_.size();
     52    ++bucket;
     53  }
     54  return bucket;
     55 }
     56 
     57 size_t Histogram::NumValues() const {
     58  return values_.size();
     59 }
     60 
     61 }  // namespace video_coding
     62 }  // namespace webrtc