Repository navigation
Expand file tree
/
Copy pathclass.cpp
More file actions
78 lines (64 loc) · 1.42 KB
/
Copy pathclass.cpp
File metadata and controls
78 lines (64 loc) · 1.42 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
77
78
#include <cassert>
class Pair {
public:
int first;
int second;
void NOP() {}
int GetFirst() const { return first; }
int GetSecond() const { return second; }
int Set(int &field, int new_val) {
NOP();
int old_val = field;
field = new_val;
return old_val;
}
int SetFirst(int new_first) { return GetFirst() + Set(first, new_first); }
int SetSecond(int new_second) {
return GetSecond() + Set(second, new_second);
}
};
struct Route {
Pair path;
double cost;
double SetCost(double new_cost) {
double old_cost = cost;
cost = new_cost;
return old_cost;
}
};
struct Counter {
int v;
mutable int calls;
int Get() const {
++calls;
return v;
}
bool operator==(const Counter &o) const {
++calls;
return v == o.v;
}
};
int RandomRoute(Route &route) {
if (route.path.first % 2) {
return route.path.SetFirst(route.path.SetSecond(10));
} else {
return route.path.SetSecond(route.path.SetFirst(-10));
}
}
int main() {
Route route1 = {{0, 1}, 5};
Route route2 = {{1, 0}, 10};
double old_cost = route1.SetCost(route2.SetCost(15));
assert(RandomRoute(route1) + RandomRoute(route2) + old_cost == 9);
Counter c1{3, 0};
const Counter c2{3, 0};
const Counter *pc = &c1;
assert(c1.Get() == 3);
assert(c2.Get() == 3);
assert(pc->Get() == 3);
assert(c1 == c2);
assert(c2 == c1);
assert(c1.calls == 3);
assert(c2.calls == 2);
return 0;
}