1 /*
2  *  Copyright (c) 2017 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 #ifndef RTC_BASE_NUMERICS_HISTOGRAM_PERCENTILE_COUNTER_H_
12 #define RTC_BASE_NUMERICS_HISTOGRAM_PERCENTILE_COUNTER_H_
13 
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 #include <map>
18 #include <vector>
19 
20 #include "absl/types/optional.h"
21 
22 namespace rtc {
23 // Calculates percentiles on the stream of data. Use |Add| methods to add new
24 // values. Use |GetPercentile| to get percentile of the currently added values.
25 class HistogramPercentileCounter {
26  public:
27   // Values below |long_tail_boundary| are stored as the histogram in an array.
28   // Values above - in a map.
29   explicit HistogramPercentileCounter(uint32_t long_tail_boundary);
30   ~HistogramPercentileCounter();
31   void Add(uint32_t value);
32   void Add(uint32_t value, size_t count);
33   void Add(const HistogramPercentileCounter& other);
34   // Argument should be from 0 to 1.
35   absl::optional<uint32_t> GetPercentile(float fraction);
36 
37  private:
38   std::vector<size_t> histogram_low_;
39   std::map<uint32_t, size_t> histogram_high_;
40   const uint32_t long_tail_boundary_;
41   size_t total_elements_;
42   size_t total_elements_low_;
43 };
44 }  // namespace rtc
45 #endif  // RTC_BASE_NUMERICS_HISTOGRAM_PERCENTILE_COUNTER_H_
46