-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData.cpp
More file actions
39 lines (31 loc) · 911 Bytes
/
Data.cpp
File metadata and controls
39 lines (31 loc) · 911 Bytes
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
#include "Data.h"
using namespace std;
Data::Data() : varName() {}
Data::Data(const string &name, int initialValue, int initialTime)
: varName(name) {
setValue(initialValue, initialTime);
}
bool Data::getValueAt(int time, int &value, int &commitTime) const {
bool found = false;
int bestTime = -1;
for (const auto &v : versions) {
if (v.commitTime <= time && v.commitTime >= bestTime) {
bestTime = v.commitTime;
value = v.value;
commitTime = v.commitTime;
found = true;
}
}
return found;
}
void Data::setValue(int value, int commitTime) {
versions.push_back({value, commitTime});
}
int Data::getLatestCommitTime() const {
if (versions.empty()) return -1;
return versions.back().commitTime;
}
int Data::getLatestValue() const {
if (versions.empty()) return 0;
return versions.back().value;
}