-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventTimer.cpp
executable file
·78 lines (63 loc) · 1.61 KB
/
EventTimer.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
/*
* EventTimer.cpp
*
* Li-Yi Wei
* 9/23/2003
*
*/
#include <iostream>
using namespace std;
#include "EventTimer.hpp"
const float EventTimer::_ILLEGAL_VALUE = -1;
EventTimer::EventTimer(const int numRecordableEvents) : _eventTable(numRecordableEvents), _eventPointer(0)
{
Clear();
}
EventTimer::~EventTimer(void)
{
// nothing to do
}
void EventTimer::Clear(void)
{
for(unsigned int i = 0; i < _eventTable.size(); i++)
{
_eventTable[i] = _ILLEGAL_VALUE;
}
_eventPointer = 0;
}
int EventTimer::RecordTime(const float eventTime)
{
_eventTable[_eventPointer] = eventTime;
_eventPointer = (_eventPointer + 1)%_eventTable.size();
return 1;
}
float EventTimer::ElapsedTime(const int inputWhichEvent,
const int lookBackWindowSize) const
{
float result = -1;
// toroidally map whichEvent into the input range
int whichEvent = inputWhichEvent;
if(whichEvent < 0)
{
// use most recent event
whichEvent = \
(_eventPointer - 1 + _eventTable.size())%_eventTable.size();
}
else
{
whichEvent = whichEvent%_eventTable.size();
}
if((lookBackWindowSize >= 0) && (lookBackWindowSize < _eventTable.size()))
{
int previousEvent =
(whichEvent - lookBackWindowSize +
_eventTable.size())%_eventTable.size();
if( (_eventTable[whichEvent] != _ILLEGAL_VALUE) &&
(_eventTable[previousEvent] != _ILLEGAL_VALUE) )
{
result = _eventTable[whichEvent] - _eventTable[previousEvent];
}
}
// done
return result;
}