-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPieceGraph.cpp
More file actions
194 lines (153 loc) · 4.84 KB
/
Copy pathPieceGraph.cpp
File metadata and controls
194 lines (153 loc) · 4.84 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
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
#include <vector>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include "PieceGraph.h"
void PieceGraph::insert(Bitboard& newBitboard) {
if (!(allPieces.containsAny(newBitboard))) {
PieceNode * newPieceNode = new PieceNode();
Bitboard perimeter = newBitboard.getPerimeter();
perimeter.intersectionWith(allPieces);
list <PieceNode*> neighbors;
for (auto& bitboard: perimeter.splitIntoBitboards()){
neighbors.push_front(bitboardHashTable.at(bitboard.hash()));
}
newPieceNode -> insert(neighbors, newBitboard);
bitboardHashTable[newBitboard.hash()] = newPieceNode;
allPieces.unionWith(newBitboard);
}
}
void PieceGraph::remove(Bitboard& oldBitboard) {
if (bitboardHashTable.find(oldBitboard.hash() ) == bitboardHashTable.end()) {
cout << "attempting to remove piece that does exist in piece graph" << endl;
oldBitboard.print();
throw 15;
}
PieceNode * p = bitboardHashTable[oldBitboard.hash()];
p -> remove();
delete p;
bitboardHashTable.erase(oldBitboard.hash());
allPieces.notIntersectionWith(oldBitboard);
//TODO: xor is faster but not as safe?
}
void PieceGraph::reposition(Bitboard& oldBitboard, Bitboard& newBitboard) {
remove(oldBitboard);
insert(newBitboard);
}
void PieceGraph::getArticulationNodes(PieceNode * n, int& counter,
unordered_set<PieceNode*>& visited) {
visited.insert(n);
n -> visitedNum = counter++;
//initially assume that this node is the lowest link
n -> lowLink = n -> visitedNum;
for(auto neighbor: n -> neighbors){
//if not yet visited
if (visited.find(neighbor) == visited.end() ){
(*neighbor).parent = n;
//see if node has a path to lower node
getArticulationNodes(neighbor, counter, visited);
//if neighbor points to a lower node
//update lowest Link
n -> lowLink = (n -> lowLink < neighbor -> lowLink) ?
n -> lowLink : neighbor -> lowLink;
//if neighbor cannot reach a lower node
//that means this node is not a leaf in a DFS tree
if ( neighbor -> lowLink >= n -> visitedNum && n )
articulationNodes.insert(n);
}
//if the node is a back edge
else if (!(n -> parent == neighbor)) {
// set lowest link to min(lowlink, neighbor.num)
n -> lowLink = (n -> lowLink < (*neighbor).visitedNum) ?
n -> lowLink : (*neighbor).visitedNum;
}
}
}
void PieceGraph::checkArticulationRoot(PieceNode * root) {
int children = 0;
for (auto potentialChild : root->neighbors){
if (potentialChild -> parent == root) {
children++;
if (children > 1) {articulationNodes.insert(root); return;}
}
}
articulationNodes.erase(root);
}
//TODO: optimize (recalculate O(k) for inserted leaves)
Bitboard PieceGraph::getPinnedPieces(Bitboard root) {
articulationNodes.clear();
int counter = 0;
if (bitboardHashTable.size() == 0) return Bitboard();
PieceNode * firstPieceNode;
if (root == Bitboard()){
firstPieceNode = bitboardHashTable.begin() -> second;
} else {
firstPieceNode = bitboardHashTable.at(root.hash());
}
unordered_set<PieceNode*> visited;
getArticulationNodes(firstPieceNode, counter, visited);
checkArticulationRoot(firstPieceNode);
Bitboard pinned;
Bitboard nextPiece;
for (auto pinnedPiece: articulationNodes) {
nextPiece.initialize({{pinnedPiece->boardIndex, pinnedPiece->location}});
pinned.unionWith(nextPiece);
}
return pinned;
}
void PieceGraph::destroy() {
for (auto element : bitboardHashTable ) {
delete element.second;
}
bitboardHashTable.clear();
articulationNodes.clear();
allPieces.clear();
}
void PieceGraph::DFS( PieceNode * root,
unordered_set<PieceNode*>& visited) {
visited.insert(root);
for (auto neighbor : root -> neighbors) {
if (visited.find(neighbor) != visited.end() ) continue;
DFS(neighbor, visited);
}
}
unordered_set <PieceNode*> PieceGraph::DFS() {
unordered_set <PieceNode*> visited;
if (bitboardHashTable.size() != 0)
DFS(bitboardHashTable.begin() -> second, visited);
return visited;
}
bool PieceGraph::checkBiDirectional(Bitboard a, Bitboard b) {
if (bitboardHashTable.find(a.hash()) == bitboardHashTable.end() ||
bitboardHashTable.find(b.hash()) == bitboardHashTable.end())
{
cout << "node does not exist in hash table" << endl;
throw 2;
}
PieceNode * ap = bitboardHashTable[a.hash()];
PieceNode * bp = bitboardHashTable[b.hash()];
bool forward = false;
bool backward = false;
for (PieceNode * next : ap ->neighbors) {
forward |= next == bp;
}
for (PieceNode * back : bp ->neighbors) {
backward |= back == ap;
}
return forward && backward;
}
void MoveGraph::getArticulationNodes(Bitboard root) {
articulationLocations.clear();
PieceGraph::getPinnedPieces(root);
for (auto node: articulationNodes){
Bitboard newNode({{node->boardIndex, node->location}});
articulationLocations.unionWith(newNode);
}
}
void MoveGraph::destroy(){
PieceGraph::destroy();
articulationLocations.clear();
}
Bitboard MoveGraph::getMoves(){
return allPieces;
}