-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobservable.cpp
172 lines (136 loc) · 4.54 KB
/
observable.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
// Copyright 2020 Joren Brunekreef and Andrzej Görlich
#include <fstream>
#include <vector>
#include <algorithm>
#include "observable.hpp"
std::default_random_engine Observable::rng(0); // TODO(JorenB): seed properly
void Observable::write() {
std::string filename = data_dir + name + "-" + identifier + extension;
std::ifstream infile(filename);
if (!infile.good()) { printf("output file deleted\n"); exit(1); }
infile.close();
std::ofstream file;
file.open(filename, std::ios::out | std::ios::app);
assert(file.is_open());
file << output << "\n";
file.close();
}
void Observable::clear() {
std::string filename = data_dir + name + "-" + identifier + extension;
std::ofstream file;
file.open(filename, std::ios::out | std::ios::trunc);
assert(file.is_open());
file.close();
}
std::vector<Vertex::Label> Observable::sphere(Vertex::Label origin, int radius) {
std::vector<bool> done;
std::vector<Vertex::Label> thisDepth;
std::vector<Vertex::Label> nextDepth;
int vmax = 0;
for (auto v : Universe::vertices) {
if (v > vmax) vmax = v;
}
done.resize(vmax + 1, false);
done.at(origin) = true;
thisDepth.push_back(origin);
std::vector<Vertex::Label> vertexList;
for (int currentDepth = 0; currentDepth < radius; currentDepth++) {
for (auto v : thisDepth) {
for (auto neighbor : Universe::vertexNeighbors[v]) {
if (!done.at(neighbor)) {
nextDepth.push_back(neighbor);
done.at(neighbor) = true;
if (currentDepth == radius - 1) vertexList.push_back(neighbor);
}
}
}
thisDepth = nextDepth;
nextDepth.clear();
}
return vertexList;
}
std::vector<Triangle::Label> Observable::sphereDual(Triangle::Label origin, int radius) {
std::vector<bool> done;
std::vector<Triangle::Label> thisDepth;
std::vector<Triangle::Label> nextDepth;
int tmax = 0;
for (auto t : Universe::triangles) {
if (t > tmax) tmax = t;
}
done.resize(tmax + 1, false);
done.at(origin) = true;
thisDepth.push_back(origin);
std::vector<Triangle::Label> triangleList;
for (int currentDepth = 0; currentDepth < radius; currentDepth++) {
for (auto t : thisDepth) {
for (auto neighbor : Universe::triangleNeighbors[t]) {
if (!done.at(neighbor)) {
nextDepth.push_back(neighbor);
done.at(neighbor) = true;
if (currentDepth == radius-1) triangleList.push_back(neighbor);
}
}
}
thisDepth = nextDepth;
nextDepth.clear();
}
return triangleList;
}
int Observable::distance(Vertex::Label v1, Vertex::Label v2) {
if (v1 == v2) return 0;
std::vector<bool> done;
std::vector<Vertex::Label> thisDepth;
std::vector<Vertex::Label> nextDepth;
int vmax = 0;
for (auto v : Universe::vertices) {
if (v > vmax) vmax = v;
}
done.resize(vmax + 1, false);
done.at(v1) = true;
thisDepth.push_back(v1);
int currentDepth = 0;
do {
for (auto v : thisDepth) {
for (auto neighbor : Universe::vertexNeighbors[v]) {
if (neighbor == v2) return currentDepth+1;
if (!done.at(neighbor)) {
nextDepth.push_back(neighbor);
done.at(neighbor) = true;
}
}
}
thisDepth = nextDepth;
nextDepth.clear();
currentDepth++;
} while (thisDepth.size() > 0);
return -1; // should not happen
}
int Observable::distanceDual(Triangle::Label t1, Triangle::Label t2) {
if (t1 == t2) return 0;
std::vector<bool> done;
std::vector<Triangle::Label> thisDepth;
std::vector<Triangle::Label> nextDepth;
int tmax = 0;
for (auto t : Universe::triangles) {
if (t > tmax) tmax = t;
}
done.resize(tmax + 1, false);
done.at(t1) = true;
thisDepth.push_back(t1);
int currentDepth = 0;
do {
for (auto t : thisDepth) {
for (auto neighbor : Universe::triangleNeighbors[t]) {
if (neighbor == t2) return currentDepth + 1;
if (!done.at(neighbor)) {
nextDepth.push_back(neighbor);
done.at(neighbor) = true;
}
}
}
thisDepth = nextDepth;
nextDepth.clear();
currentDepth++;
} while (thisDepth.size() > 0);
return -1; // should not happen
}