-
Notifications
You must be signed in to change notification settings - Fork 0
/
customDs.cpp
64 lines (58 loc) · 1.21 KB
/
customDs.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
#include <iostream>
#include <unordered_map>
using namespace std;
class CustomDs{
public:
CustomDs():data(), set(-1), curr_ver(0){}
int getIdx(int idx) {
unordered_map<int,pair<int,int> >::iterator it = data.find(idx);
if (it != data.end()){
if ((it->second).first == curr_ver)
return (it->second).second;
else
return set;
}
return -1;
}
void setIdx(int idx, int val) {
unordered_map<int,pair<int,int> >::iterator it = data.find(idx);
if (it != data.end()) {
(it->second).first = curr_ver;
(it->second).second = val;
}
else {
data.insert(make_pair(idx,make_pair(curr_ver, val)));
}
}
void setAll(int val) {
set = val;
curr_ver++;
}
private:
unordered_map<int,pair<int,int> > data;
int set;
int curr_ver;
};
int main(int argc, char const *argv[])
{
CustomDs d;
d.setIdx(3,2);
d.setIdx(4,3);
d.setIdx(6,4);
cout<<d.getIdx(3)<<endl;
cout<<d.getIdx(4)<<endl;
cout<<d.getIdx(6)<<endl;
d.setAll(1);
cout<<d.getIdx(3)<<endl;
cout<<d.getIdx(4)<<endl;
cout<<d.getIdx(6)<<endl;
d.setIdx(4,10);
cout<<d.getIdx(3)<<endl;
cout<<d.getIdx(4)<<endl;
cout<<d.getIdx(6)<<endl;
d.setAll(10);
cout<<d.getIdx(3)<<endl;
cout<<d.getIdx(4)<<endl;
cout<<d.getIdx(6)<<endl;
return 0;
}