1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
// Copyright (C) 2010-2022 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc., 51
// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include "StatisticsCounters.hpp"
#include <assertions.hpp>
#include <algorithm>
namespace core {
StatisticsCounters::StatisticsCounters()
: m_counters(static_cast<size_t>(Statistic::END))
{
}
StatisticsCounters::StatisticsCounters(const Statistic statistic)
: StatisticsCounters({statistic})
{
}
StatisticsCounters::StatisticsCounters(
const std::initializer_list<Statistic> statistics)
: StatisticsCounters()
{
for (auto st : statistics) {
increment(st);
}
}
uint64_t
StatisticsCounters::get(Statistic statistic) const
{
const auto index = static_cast<size_t>(statistic);
ASSERT(index < static_cast<size_t>(Statistic::END));
return index < m_counters.size() ? m_counters[index] : 0;
}
uint64_t
StatisticsCounters::get_offsetted(Statistic statistic, size_t offset) const
{
return get_raw(static_cast<size_t>(statistic) + offset);
}
uint64_t
StatisticsCounters::get_raw(size_t index) const
{
ASSERT(index < size());
return m_counters[index];
}
void
StatisticsCounters::set(Statistic statistic, uint64_t value)
{
const auto index = static_cast<size_t>(statistic);
ASSERT(index < static_cast<size_t>(Statistic::END));
m_counters[index] = value;
}
void
StatisticsCounters::set_offsetted(Statistic statistic,
size_t offset,
uint64_t value)
{
set_raw(static_cast<size_t>(statistic) + offset, value);
}
void
StatisticsCounters::set_raw(size_t index, uint64_t value)
{
if (index >= m_counters.size()) {
m_counters.resize(index + 1);
}
m_counters[index] = value;
}
void
StatisticsCounters::increment(Statistic statistic, int64_t value)
{
if (value == 0) {
return;
}
const auto i = static_cast<size_t>(statistic);
if (i >= m_counters.size()) {
m_counters.resize(i + 1);
}
auto& counter = m_counters[i];
counter =
std::max(static_cast<int64_t>(0), static_cast<int64_t>(counter + value));
}
void
StatisticsCounters::increment(const StatisticsCounters& other)
{
m_counters.resize(std::max(size(), other.size()));
for (size_t i = 0; i < other.size(); ++i) {
auto& counter = m_counters[i];
counter = std::max(static_cast<int64_t>(0),
static_cast<int64_t>(counter + other.m_counters[i]));
}
}
void
StatisticsCounters::increment_offsetted(Statistic statistic,
size_t offset,
int64_t value)
{
increment(static_cast<Statistic>(static_cast<size_t>(statistic) + offset),
value);
}
size_t
StatisticsCounters::size() const
{
return m_counters.size();
}
bool
StatisticsCounters::all_zero() const
{
return !std::any_of(
m_counters.begin(), m_counters.end(), [](unsigned v) { return v != 0; });
}
} // namespace core
|