1 /**
2  * Copyright (c) 2021-2024 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 ark {
24 
25 template <class Value>
26 template <class ForwardIterator>
SimpleHistogram(ForwardIterator start, ForwardIterator finish, helpers::ValueType typeOfValue)27 SimpleHistogram<Value>::SimpleHistogram(ForwardIterator start, ForwardIterator finish, helpers::ValueType typeOfValue)
28     : typeOfValue_(typeOfValue)
29 {
30     for (auto it = start; it != finish; ++it) {
31         AddValue(*it);
32     }
33 }
34 
35 template <class Value>
GetGeneralStatistic() const36 PandaString SimpleHistogram<Value>::GetGeneralStatistic() const
37 {
38     PandaStringStream statistic;
39     statistic << "Sum: " << helpers::ValueConverter(sum_, typeOfValue_) << " ";
40     statistic << "Avg: " << helpers::ValueConverter(GetAvg(), typeOfValue_) << " ";
41     statistic << "Max: " << helpers::ValueConverter(max_, typeOfValue_);
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 * Value(number);
49     sumOfSquares_ += element * element * Value(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 typeOfValue)62 Histogram<Value>::Histogram(ForwardIterator start, ForwardIterator finish, helpers::ValueType typeOfValue)
63     : SimpleHistogram<Value>(typeOfValue)
64 {
65     for (auto it = start; it != finish; ++it) {
66         AddValue(*it);
67     }
68 }
69 
70 template <class Value>
GetTopDump(size_t countTop) const71 PandaString Histogram<Value>::GetTopDump(size_t countTop) const
72 {
73     PandaStringStream statistic;
74     bool first = true;
75     for (auto it : frequency_) {
76         if (countTop-- == 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 ark
96 
97 #endif  // PANDA_RUNTIME_HISTOGRAM_INL_H_
98