-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathTimerGroup.cpp
172 lines (134 loc) · 2.86 KB
/
TimerGroup.cpp
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
DDS, a bridge double dummy solver.
Copyright (C) 2006-2014 by Bo Haglund /
2014-2018 by Bo Haglund & Soren Hein.
See LICENSE and README.
*/
#define TIMER_DEPTH 50
#include <iostream>
#include <iomanip>
#include <sstream>
#include "TimerGroup.h"
TimerGroup::TimerGroup()
{
TimerGroup::Reset();
}
void TimerGroup::Reset()
{
timers.resize(TIMER_DEPTH);
for (unsigned i = 0; i < timers.size(); i++)
timers[i].Reset();
}
void TimerGroup::SetNames(const string& baseName)
{
string st;
if (baseName == "AB")
{
// Special format emphasizing the card number within the trick.
for (unsigned i = 0; i < timers.size(); i++)
{
st = baseName + to_string(i % 4) + " " + to_string(i);
timers[i].SetName(st);
}
}
else
{
for (unsigned i = 0; i < timers.size(); i++)
{
st = baseName + to_string(i);
timers[i].SetName(st);
}
}
bname = baseName;
}
void TimerGroup::Start(const unsigned no)
{
timers[no].Start();
}
void TimerGroup::End(const unsigned no)
{
timers[no].End();
}
bool TimerGroup::Used() const
{
for (unsigned i = 0; i < timers.size(); i++)
{
if (timers[i].Used())
return true;
}
return false;
}
void TimerGroup::Differentiate()
{
for (unsigned r = 0; r < timers.size()-1; r++)
{
size_t i = timers.size() - 1 - r;
timers[i] -= timers[i-1];
}
}
void TimerGroup::Sum(Timer& sum) const
{
sum = timers[0];
for (unsigned i = 1; i < timers.size(); i++)
sum += timers[i];
}
void TimerGroup::operator -= (const TimerGroup& deduct)
{
for (unsigned i = 0; i < timers.size(); i++)
timers[i] -= deduct.timers[i];
}
string TimerGroup::Header() const
{
stringstream ss;
ss << setw(14) << left << "Name" <<
setw(9) << right << "Count" <<
setw(11) << "User" <<
setw(7) << "Avg" <<
setw(5) << "%" <<
setw(11) << "Syst" <<
setw(7) << "Avg" <<
setw(5) << "%" << "\n";
return ss.str();
}
string TimerGroup::DetailHeader() const
{
stringstream ss;
ss << setw(14) << left << "Name " <<
setw(11) << right << "Number" <<
setw(11) << "User ticks" <<
setw(11) << "Avg" <<
setw(11) << "System" <<
setw(11) << "Avg ms" << "\n";
return ss.str();
}
string TimerGroup::SumLine(const Timer& sumTotal) const
{
Timer ownSum;
TimerGroup::Sum(ownSum);
return ownSum.SumLine(sumTotal, bname);
}
string TimerGroup::TimerLines(const Timer& sumTotal) const
{
string st = "";
for (unsigned r = 0; r < timers.size(); r++)
{
size_t i = timers.size() - r - 1;
if (timers[i].Used())
st += timers[i].SumLine(sumTotal);
}
return st;
}
string TimerGroup::DetailLines() const
{
stringstream ss;
for (unsigned i = 0; i < timers.size(); i++)
{
if (timers[i].Used())
ss << timers[i].DetailLine();
}
return ss.str();
}
string TimerGroup::DashLine() const
{
return string(69, '-') + "\n";
}