-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboxcartimer.cpp
More file actions
76 lines (59 loc) · 1.35 KB
/
boxcartimer.cpp
File metadata and controls
76 lines (59 loc) · 1.35 KB
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
#include "boxcartimer.h"
BoxcarTimer::BoxcarTimer(int numCars, int carLength) :
started(false), currentCar(0),
numCars(numCars), carLength(carLength)
{
cars = new int[numCars];
for (int i = 0; i < numCars; ++i)
cars[i] = 0;
}
BoxcarTimer::~BoxcarTimer()
{
delete[] cars;
}
void BoxcarTimer::addEvents(int count)
{
if (!started)
{
timer.start();
started = true;
carStart = 0;
}
qint64 currentTime = timer.elapsed();
while (carStart + carLength <= currentTime)
{
carStart += carLength;
currentCar += 1;
if (currentCar >= numCars)
currentCar = 0;
cars[currentCar] = 0;
}
cars[currentCar] += count;
}
double BoxcarTimer::getRate()
{
if (!started)
return 0.0;
double total = 0;
qint64 currentTime = timer.elapsed();
while (carStart + carLength <= currentTime)
{
carStart += carLength;
currentCar += 1;
if (currentCar >= numCars)
currentCar = 0;
cars[currentCar] = 0;
}
for (int i = 0; i < numCars; ++i)
{
total += cars[i];
}
if (currentTime < numCars * carLength)
total /= currentTime / 1000.0;
else
{
double dt = (numCars - 1) * carLength + currentTime % carLength;
total /= dt / 1000.0;
}
return total;
}