salsa 0.7.1
Loading...
Searching...
No Matches
HyperCube.cc
1#pragma clang diagnostic push
2#pragma clang diagnostic ignored "-Weverything"
3// Next time please sanitize your damned code.
4
5#include "HyperCube.hh"
6
7namespace Salsa {
8
9int nVertex; // number vertex
10
11HyperCube::HyperCube(int power, int start) : Object(), mPower(power), mStart(start)
12{
13 nVertex = pow(2, power);
14}
15
22
24{
25 std::bitset<64> b;
26 std::vector<std::string> bitsVector(nVertex);
27
28 for (int i = 0; i < nVertex; i++) {
29 std::bitset<64> b(i);
30 std::string bits = b.to_string<char, std::char_traits<char>, std::allocator<char>>();
31 bits.erase(0, 64 - (mPower + 1));
32
33 bitsVector[i] = bits;
34 }
35
36 mAdjMatrix.resize(nVertex);
37
38 for (std::size_t i = 0; i < bitsVector.size(); i++) {
39 mAdjMatrix[i].resize(nVertex);
40 for (std::size_t j = 0; j < bitsVector.size(); j++) {
41 int diff = 0;
42 for (std::size_t k = 0; k < bitsVector[i].length(); k++) {
43 if (bitsVector[i][k] != bitsVector[j][k]) diff++;
44 }
45 if (diff == 1) mAdjMatrix[i][j] = 1;
46 }
47 }
48}
49
50void HyperCube::addNode(std::string nodeName)
51{
52 auto it = _nodeMap.begin();
53
54 while (it != _nodeMap.end()) {
55 if (it->second == nodeName) {
56 SPD_INFO("a node with that name has already been added");
57 return;
58 }
59
60 it++;
61 }
62
63 int i = _nodeMap.size() + 1;
64 _nodeMap.insert(std::pair<int, std::string>(i, nodeName));
65}
66
67void HyperCube::removeNode(std::string nodeName)
68{
69 auto it = _nodeMap.begin();
70
71 while (it != _nodeMap.end()) {
72 if (it->second == nodeName) {
73 _nodeMap.erase(it);
74
75 std::map<int, std::string> newMap;
76 int i = 1;
77
78 for (std::map<int, std::string>::iterator mi = _nodeMap.begin(); mi != _nodeMap.end(); ++mi) {
79 newMap[i] = mi->second;
80 i++;
81 }
82 _nodeMap = newMap;
83 it--;
84 }
85
86 it++;
87 }
88}
89
91{
92 bool flag = false;
93
94 std::vector<int> initVector = {mStart};
95 mPaths.push_back(initVector);
96 mPassedNodes.push_back(mStart);
97
98 do {
99 std::vector<int> tmpVector;
100 for (std::size_t i = 0; i < mPaths[mPaths.size() - 1].size(); i++) {
101
102 std::size_t pos = mPaths[mPaths.size() - 1][i] - 1;
103 for (std::size_t j = 0; j < mAdjMatrix[pos].size(); j++) {
104
105 bool passed = std::find(mPassedNodes.begin(), mPassedNodes.end(), j + 1) != mPassedNodes.end();
106
107 if (mAdjMatrix[pos][j] == 1 && !passed) {
108 tmpVector.push_back(j + 1);
109 mPassedNodes.push_back(j + 1);
110 }
111 }
112 }
113
114 if (tmpVector.size() > 0) {
115 mPaths.push_back(tmpVector);
116 flag = true;
117 }
118 else
119 flag = false;
120
121 } while (flag);
122}
123
125{
126 for (std::size_t i = 0; i < mAdjMatrix.size(); ++i) {
127 std::string s;
128 for (std::size_t j = 0; j < mAdjMatrix[i].size(); ++j) {
129 s.append(std::to_string(mAdjMatrix[i][j]));
130 }
131 SPD_INFO("{}", s);
132 }
133
134 for (std::size_t i = 0; i < mPaths.size(); ++i) {
135 std::string s;
136 for (std::size_t j = 0; j < mPaths[i].size(); ++j) {
137 if (_nodeMap.find(mPaths[i][j]) != _nodeMap.end()) {
138 auto position = _nodeMap.find(mPaths[i][j]);
139
140 s.append(std::to_string(mPaths[i][j]) + "(" + position->second + ") ");
141 }
142 else
143 s.append(std::to_string(mPaths[i][j]) + " ");
144 }
145 SPD_INFO("{}", s);
146 }
147}
148} // namespace Salsa
149
150#pragma clang diagnostic pop
void createAdjMatrix()
create matrix adjacency
Definition HyperCube.cc:23
void removeNode(std::string nodeName)
remove node from HC
Definition HyperCube.cc:67
HyperCube(int power=3, int start=1)
Create HyperCube.
Definition HyperCube.cc:11
std::vector< std::vector< int > > mAdjMatrix
Matrix adjacency.
Definition HyperCube.hh:41
std::vector< std::vector< int > > mPaths
Output paths.
Definition HyperCube.hh:42
std::map< int, std::string > _nodeMap
avalible nodes and their numbers
Definition HyperCube.hh:35
void addNode(std::string nodeName)
add new node in HC
Definition HyperCube.cc:50
void print() const
Printing Hyper cube paths.
Definition HyperCube.cc:124
void createPaths()
Creat outPut vectors.
Definition HyperCube.cc:90
int mPower
Power.
Definition HyperCube.hh:38
virtual ~HyperCube()
Definition HyperCube.cc:16
std::vector< int > mPassedNodes
Passed nodes.
Definition HyperCube.hh:40
int mStart
Starting point.
Definition HyperCube.hh:39
Base Salsa Object class.
Definition Object.hh:15