This repository has been archived by the owner on Jan 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmlrp.cpp
198 lines (158 loc) · 4.68 KB
/
bmlrp.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "bmlrp.h"
#include <algorithm>
#include <vector>
#include <queue>
#include "debug.h"
#include "graph.h"
#include "misc.h"
#include "sim.h"
#include "test.h"
using std::cerr;
using std::endl;
using std::vector;
const Addr Msb = ((Addr)-1 >> 1) + 1;
void connectInside(const vector<int>& circle, const vector<Addr>& addrs,
int l, int r, Addr bit, Graph* appendTo) {
myassert(bit != 0);
if (r - l < 2) {
return;
}
int m = l;
while ((m < r) && ((addrs[circle[m]] & bit) == 0)) {
++m;
}
if ((m - l > 0) && (r - m > 0)) {
int best_i = l;
int best_j = m;
Addr best_dist = addrs[circle[l]] ^ addrs[circle[m]];
for (int i = l; i < m; ++i) {
for (int j = m; j < r; ++j) {
Addr dist = addrs[circle[i]] ^ addrs[circle[j]];
if (dist < best_dist) {
best_dist = dist;
best_i = i;
best_j = j;
}
}
}
appendTo->AddEdgeBidirectional(circle[best_i], circle[best_j]);
}
bit >>= 1;
connectInside(circle, addrs, l, m, bit, appendTo);
connectInside(circle, addrs, m, r, bit, appendTo);
}
// Connect nodes inside circle. circle must be sorted.
void connectInside(const vector<int>& circle, const vector<Addr>& addrs,
Graph* appendTo) {
connectInside(circle, addrs, 0, circle.size(), Msb, appendTo);
}
// Connect 2 circles together.
void connectCircles(const vector<int>& circle0, const vector<int>& circle1,
const vector<Addr>& addrs, Graph* appendTo) {
if (circle0.empty()) {
return;
}
myassert(!circle1.empty());
Addr best_dist = addrs[circle0[0]] ^ addrs[circle1[0]];
int best_i = 0;
int best_j = 0;
for (uint i = 0; i < circle0.size(); ++i) {
for (uint j = 0; j < circle1.size(); ++j) {
Addr dist = addrs[circle0[i]] ^ addrs[circle1[j]];
if (dist < best_dist) {
best_dist = dist;
best_i = i;
best_j = j;
}
}
}
if (circle0[best_i] != circle1[best_j]) {
appendTo->AddEdgeBidirectional(circle0[best_i], circle1[best_j]);
}
}
bool DiffColor(Addr a, Addr b) {
return (a ^ b) & Msb;
}
bool SameColor(Addr a, Addr b) {
return !DiffColor(a, b);
}
// Returns the next level graph.
// clGraph: current level graph
// addrs: vector of addresses shifted left by i, if clGraph is level i.
Graph NextLevel(const Graph& clGraph, const vector<Addr>& addrs) {
const int n = clGraph.n;
vector< vector<int> > circles(n);
vector<int> radius(n, 0);
struct BfsState {
int node; // the tracked node
int location; // the current location (vertex at which node is located)
};
std::queue<BfsState> q;
for (int i = 0; i < n; ++i) {
q.push({i, i});
}
// distance from the origin
int dist = 0;
while (!q.empty()) {
std::queue<BfsState> qn; // queue new
while (!q.empty()) {
int node = q.front().node;
int loc = q.front().location;
q.pop();
vector<int> succ = clGraph.GetDirectSuccessors(loc);
for (uint i = 0; i < succ.size(); ++i) {
const int to = succ[i];
if (DiffColor(addrs[node], addrs[to])) {
if ((radius[to] == 0) || (radius[to] == dist + 1)) {
radius[to] = dist + 1;
circles[to].push_back(node);
qn.push({node, to});
}
}
}
}
q = qn;
++dist;
}
Graph res(n);
// connect nodes in each circle
for (int i = 0; i < n; ++i) {
auto& circle = circles[i];
// sort nodes by address
sort(circle.begin(), circle.end(), [&addrs](int node1, int node2) {
return addrs[node1] < addrs[node2];
});
// make each element unique
auto it = std::unique(circle.begin(), circle.end());
circle.resize(std::distance(circle.begin(), it));
connectInside(circle, addrs, &res);
}
// connect same color neighbors and connect nodes in neighboring circles
for (int i = 0; i < n; ++i) {
vector<int> succ = clGraph.GetDirectSuccessors(i);
for (uint j = 0; j < succ.size(); ++j) {
const int to = succ[j];
if (SameColor(addrs[i], addrs[to])) {
// connect same color neighbors
res.AddEdge(i, to);
// connect nodes in neighboring circles
connectCircles(circles[i], circles[to], addrs, &res);
}
}
}
return res;
}
Graph GetLevel(const Graph& level0, const vector<Addr>& addrs, int level) {
myassert(level <= static_cast<int>(sizeof(Addr) * 8));
myassert(IsGraphConnected(level0));
Graph res = level0;
vector<Addr> addrs_copy = addrs;
for (int level_i = 1; level_i <= level; ++level_i) {
res = NextLevel(res, addrs_copy);
TestNextLevel(res, addrs, level_i);
for (uint i = 0; i < addrs_copy.size(); ++i) {
addrs_copy[i] <<= 1;
}
}
return res;
}