-
Notifications
You must be signed in to change notification settings - Fork 58
/
test_graph.cpp
178 lines (147 loc) · 3.39 KB
/
test_graph.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "deferred_allocator.h"
using namespace gcpp;
#include <iostream>
#include <iomanip>
#include <memory>
#include <vector>
#include <algorithm>
using namespace std;
class Counter {
public:
Counter() { ++count_; }
~Counter() { --count_; }
static int count() { return count_; }
private:
static int count_;
};
int Counter::count_ = 0;
/*--- Solution 1 (default) ----------------------------------------------------
class MyGraph {
public:
class Node : public Counter {
vector<shared_ptr<Node>> children;
public:
void AddChild(const shared_ptr<Node>& node) {
children.push_back(node);
}
void RemoveChild(const shared_ptr<Node>& node) {
auto it = find(children.begin(), children.end(), node);
Expects(it != children.end() && "trying to remove a child that was never added");
children.erase(it);
}
};
void SetRoot(const shared_ptr<Node>& node) {
root = node;
}
void ShrinkToFit() {
}
static auto MakeNode() { return make_shared<MyGraph::Node>(); }
private:
shared_ptr<Node> root;
};
//--- Solution 2 (deferred_ptr) -----------------------------------------------
/*/
static deferred_heap heap;
class MyGraph {
public:
class Node : public Counter {
deferred_vector<deferred_ptr<Node>> children{ heap };
public:
void AddChild(const deferred_ptr<Node>& node) {
children.push_back(node);
}
void RemoveChild(const deferred_ptr<Node>& node) {
auto it = find(children.begin(), children.end(), node);
Expects(it != children.end() && "trying to remove a child that was never added");
//children.erase(it);
*it = nullptr;
}
};
void SetRoot(const deferred_ptr<Node>& node) {
root = node;
}
void ShrinkToFit() {
heap.collect();
}
static auto MakeNode() { return heap.make<MyGraph::Node>(); }
private:
deferred_ptr<Node> root;
};
// ----------------------------------------------------------------------------
//*/
bool TestCase1() {
MyGraph g;
{
auto a = MyGraph::MakeNode();
g.SetRoot(a);
auto b = MyGraph::MakeNode();
a->AddChild(b);
auto c = MyGraph::MakeNode();
b->AddChild(c);
a->RemoveChild(b);
}
g.ShrinkToFit();
return Counter::count() == 1;
}
bool TestCase2() {
MyGraph g;
{
auto a = MyGraph::MakeNode();
g.SetRoot(a);
auto b = MyGraph::MakeNode();
a->AddChild(b);
auto c = MyGraph::MakeNode();
b->AddChild(c);
auto d = MyGraph::MakeNode();
b->AddChild(d);
d->AddChild(b);
a->RemoveChild(b);
}
g.ShrinkToFit();
return Counter::count() == 1;
}
bool TestCase3() {
MyGraph g;
{
auto a = MyGraph::MakeNode();
g.SetRoot(a);
auto b = MyGraph::MakeNode();
a->AddChild(b);
auto c = MyGraph::MakeNode();
b->AddChild(c);
auto d = MyGraph::MakeNode();
b->AddChild(d);
d->AddChild(b);
}
g.ShrinkToFit();
return Counter::count() == 4;
}
bool TestCase4() {
MyGraph g;
{
auto a = MyGraph::MakeNode();
g.SetRoot(a);
auto b = MyGraph::MakeNode();
a->AddChild(b);
auto c = MyGraph::MakeNode();
b->AddChild(c);
auto d = MyGraph::MakeNode();
b->AddChild(d);
d->AddChild(b);
d->RemoveChild(b);
}
g.ShrinkToFit();
return Counter::count() == 4;
}
int main() {
cout.setf(ios::boolalpha);
bool passed1 = TestCase1();
cout << passed1 << endl;
bool passed2 = TestCase2();
cout << passed2 << endl;
bool passed3 = TestCase3();
cout << passed3 << endl;
bool passed4 = TestCase4();
cout << passed4 << endl;
return 0;
}