-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputeNeighbors.cpp
More file actions
66 lines (58 loc) · 1.35 KB
/
computeNeighbors.cpp
File metadata and controls
66 lines (58 loc) · 1.35 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
#include <iostream>
#include <vector>
#include "computeNeighbors.hpp"
int power(int base, int exposant){
int cpt=0;
int res=1;
while(cpt<exposant){
res=res*2;
cpt+=1;
}
return res;
}
int logarithme(int node){
int p=0;
while(power(2,p+1)<=node){
p+=1;
}
return p;
}
// messages are received from node with highest 1-bit flipped to 0
int computePredecessor(int node) {
if(node==0){
return 0;
}
else{
int tours=logarithme(node);
int pred=node-power(2,tours);
return pred;
}
return -1;
}
// compute neighbors to communicate to
std::vector<int> computeOutNeighbors(int node, int numberNodes) {
std::vector<int> neighbors;
// first bit to flip
int flipbit = (node - computePredecessor(node)) * 2;
//std::cout << "log: " << logarithme(node) << std::endl;
int cpt=1;
// TODO
int next_pow=logarithme(node);
//std::cout <<"next_pow="<<node+power(2,next_pow) << std::endl;
while (node+power(2,next_pow)<numberNodes){
if(node==0 || cpt>1){
neighbors.push_back(node+power(2,next_pow));
}
next_pow+=1;
cpt+=1;
}
return neighbors;
}
// print neighbor list for debugging
void printNeighbors(std::vector<int> neighbors) {
for (std::vector<int>::iterator iter = neighbors.begin();
iter != neighbors.end(); iter++) {
std::cout << *iter << " ";
}
std::cout << std::endl;
}