• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef PANDA_RUNTIME_HISTOGRAM_INL_H_
17 #define PANDA_RUNTIME_HISTOGRAM_INL_H_
18 
19 #include "histogram.h"
20 #include "mem/panda_containers.h"
21 #include "mem/panda_string.h"
22 
23 namespace panda {
24 
25 template <class Value>
26 template <class ForwardIterator>
SimpleHistogram(ForwardIterator start,ForwardIterator finish,helpers::ValueType type_of_value)27 SimpleHistogram<Value>::SimpleHistogram(ForwardIterator start, ForwardIterator finish, helpers::ValueType type_of_value)
28     : type_of_value_(type_of_value)
29 {
30     for (auto it = start; it != finish; ++it) {
31         AddValue(*it);
32     }
33 }
34 
35 template <class Value>
GetGeneralStatistic()36 PandaString SimpleHistogram<Value>::GetGeneralStatistic() const
37 {
38     PandaStringStream statistic;
39     statistic << "Sum: " << helpers::ValueConverter(sum_, type_of_value_) << " ";
40     statistic << "Avg: " << helpers::ValueConverter(GetAvg(), type_of_value_) << " ";
41     statistic << "Max: " << helpers::ValueConverter(max_, type_of_value_);
42     return statistic.str();
43 }
44 
45 template <class Value>
AddValue(const Value & element,size_t number)46 void SimpleHistogram<Value>::AddValue(const Value &element, size_t number)
47 {
48     sum_ += element * number;
49     sum_of_squares_ += element * element * number;
50     if (count_ == 0) {
51         min_ = element;
52         max_ = element;
53     } else {
54         min_ = std::min(min_, element);
55         max_ = std::max(max_, element);
56     }
57     count_ += number;
58 }
59 
60 template <class Value>
61 template <class ForwardIterator>
Histogram(ForwardIterator start,ForwardIterator finish,helpers::ValueType type_of_value)62 Histogram<Value>::Histogram(ForwardIterator start, ForwardIterator finish, helpers::ValueType type_of_value)
63     : SimpleHistogram<Value>(type_of_value)
64 {
65     for (auto it = start; it != finish; ++it) {
66         AddValue(*it);
67     }
68 }
69 
70 template <class Value>
GetTopDump(size_t count_top)71 PandaString Histogram<Value>::GetTopDump(size_t count_top) const
72 {
73     PandaStringStream statistic;
74     bool first = true;
75     for (auto it : frequency_) {
76         if (count_top-- == 0) {
77             break;
78         }
79         if (!first) {
80             statistic << ",";
81         }
82         statistic << it.first << ":" << it.second;
83         first = false;
84     }
85     return statistic.str();
86 }
87 
88 template <class Value>
AddValue(const Value & element,size_t number)89 void Histogram<Value>::AddValue(const Value &element, size_t number)
90 {
91     frequency_[element] += number;
92     SimpleHistogram<Value>::AddValue(element, number);
93 }
94 
95 }  // namespace panda
96 
97 #endif  // PANDA_RUNTIME_HISTOGRAM_INL_H_
98