-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcuckatoo.h
More file actions
344 lines (243 loc) · 11.7 KB
/
cuckatoo.h
File metadata and controls
344 lines (243 loc) · 11.7 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Header guard
#ifndef CUCKATOO_H
#define CUCKATOO_H
// Header files
using namespace std;
// Structures
// Cuckatoo node connections link structure
struct CuckatooNodeConnectionsLink {
// Previous node connection link
const CuckatooNodeConnectionsLink *previousNodeConnectionLink;
// Node
uint32_t node;
// Edge index
uint32_t edgeIndex;
};
// Global variables
// Cuckatoo newest node connections
thread_local static HashTable<CuckatooNodeConnectionsLink, MAX_NUMBER_OF_EDGES_AFTER_TRIMMING> cuckatooUNewestNodeConnections;
thread_local static HashTable<CuckatooNodeConnectionsLink, MAX_NUMBER_OF_EDGES_AFTER_TRIMMING> cuckatooVNewestNodeConnections;
// Cuckatoo visited pairs
thread_local static HashTable<uint32_t, SOLUTION_SIZE / 2> cuckatooUVisitedNodePairs;
thread_local static HashTable<uint32_t, SOLUTION_SIZE / 2> cuckatooVVisitedNodePairs;
// Cuckatoo root node
thread_local static uint32_t cuckatooRootNode;
// Function prototypes
// Initialize cuckatoo thread local global variables
static inline bool initializeCuckatooThreadLocalGlobalVariables() noexcept;
// Get cuckatoo solution
static inline bool getCuckatooSolution(uint32_t solution[SOLUTION_SIZE], CuckatooNodeConnectionsLink *__restrict__ nodeConnections, const uint32_t *__restrict__ edges, const uint64_t numberOfEdges) noexcept;
// Search node connections for cuckatoo solution first partition
static inline bool searchNodeConnectionsForCuckatooSolutionFirstPartition(const uint_fast8_t cycleSize, const uint32_t node, const uint32_t *index) noexcept;
// Search node connections for cuckatoo solution second partition
static inline bool searchNodeConnectionsForCuckatooSolutionSecondPartition(const uint_fast8_t cycleSize, const uint32_t node, const uint32_t *index) noexcept;
// Supporting function implementation
// Initialize cuckatoo thread local global variables
bool initializeCuckatooThreadLocalGlobalVariables() noexcept {
// Return if creating thread local global variables was successful
cuckatooRootNode = 0;
return cuckatooUNewestNodeConnections && cuckatooVNewestNodeConnections && cuckatooUVisitedNodePairs && cuckatooVVisitedNodePairs;
}
// Get cuckatoo solution
bool getCuckatooSolution(uint32_t solution[SOLUTION_SIZE], CuckatooNodeConnectionsLink *__restrict__ nodeConnections, const uint32_t *__restrict__ edges, const uint64_t numberOfEdges) noexcept {
// Go through all edges
for(uint64_t nodeConnectionsIndex = 0, edgesIndex = 0; nodeConnectionsIndex < numberOfEdges * 2; nodeConnectionsIndex += 2, edgesIndex += EDGE_NUMBER_OF_COMPONENTS) {
// Get edge's index and nodes
const uint32_t *index = &edges[edgesIndex];
uint32_t node = edges[edgesIndex + 1];
cuckatooRootNode = edges[edgesIndex + 2];
// Replace newest node connection for the node on the first partition and add node connection to list
nodeConnections[nodeConnectionsIndex] = {cuckatooUNewestNodeConnections.replace(node, &nodeConnections[nodeConnectionsIndex]), node, edges[edgesIndex]};
// Replace newest node connection for the node on the second partition and add node connection to list
nodeConnections[nodeConnectionsIndex + 1] = {cuckatooVNewestNodeConnections.replace(cuckatooRootNode, &nodeConnections[nodeConnectionsIndex + 1]), cuckatooRootNode, edges[edgesIndex]};
// Check if both nodes have a pair
if(cuckatooUNewestNodeConnections.contains(node ^ 1) && cuckatooVNewestNodeConnections.contains(cuckatooRootNode ^ 1)) {
// Reset visited nodes
cuckatooUVisitedNodePairs.clear();
cuckatooVVisitedNodePairs.clear();
// Go through all nodes in the cycle
for(uint_fast8_t cycleSize = 1;; cycleSize += 2) {
// Set that node pair has been visited
cuckatooUVisitedNodePairs.setUnique(node >> 1, index);
// Check if node's pair has more than one connection
const CuckatooNodeConnectionsLink *nodeConnection = cuckatooUNewestNodeConnections.get(node ^ 1);
if(nodeConnection->previousNodeConnectionLink) {
// Go through all of the node's pair's connections
for(; nodeConnection; nodeConnection = nodeConnection->previousNodeConnectionLink) {
// Check if the connected node's pair wasn't already visited
if(!cuckatooVVisitedNodePairs.contains((nodeConnection + 1)->node >> 1)) {
// Check if cycle is complete
if(((nodeConnection + 1)->node ^ 1) == cuckatooRootNode) {
// Check if cycle is a solution
if(cycleSize == SOLUTION_SIZE - 1) {
// Get solution from visited nodes
cuckatooUVisitedNodePairs.getValues(solution);
cuckatooVVisitedNodePairs.getValues(&solution[SOLUTION_SIZE / 2]);
solution[SOLUTION_SIZE - 1] = (nodeConnection + 1)->edgeIndex;
// Sort solution in ascending order
sort(solution, solution + SOLUTION_SIZE);
// Return true
return true;
}
}
// Otherwise check if cycle could be as solution
else if(cycleSize != SOLUTION_SIZE - 1) {
// Check if the connected node has a pair
if(cuckatooVNewestNodeConnections.contains((nodeConnection + 1)->node ^ 1)) {
// Check if solution was found at the connected node's pair
if(searchNodeConnectionsForCuckatooSolutionSecondPartition(cycleSize + 1, (nodeConnection + 1)->node ^ 1, &(nodeConnection + 1)->edgeIndex)) {
// Get solution from visited nodes
cuckatooUVisitedNodePairs.getValues(solution);
cuckatooVVisitedNodePairs.getValues(&solution[SOLUTION_SIZE / 2]);
// Sort solution in ascending order
sort(solution, solution + SOLUTION_SIZE);
// Return true
return true;
}
}
}
}
}
// Break
break;
}
// Go to node's pair opposite end and get its edge index
index = &(nodeConnection + 1)->edgeIndex;
node = (nodeConnection + 1)->node;
// Check if node pair was already visited
if(cuckatooVVisitedNodePairs.contains(node >> 1)) {
// Break
break;
}
// Check if cycle is complete
if((node ^ 1) == cuckatooRootNode) {
// Check if cycle is a solution
if(cycleSize == SOLUTION_SIZE - 1) {
// Get solution from visited nodes
cuckatooUVisitedNodePairs.getValues(solution);
cuckatooVVisitedNodePairs.getValues(&solution[SOLUTION_SIZE / 2]);
solution[SOLUTION_SIZE - 1] = *index;
// Sort solution in ascending order
sort(solution, solution + SOLUTION_SIZE);
// Return true
return true;
}
// Break
break;
}
// Check if cycle isn't a solution
if(cycleSize == SOLUTION_SIZE - 1) {
// Break
break;
}
// Check if node doesn't have a pair
if(!cuckatooVNewestNodeConnections.contains(node ^ 1)) {
// break
break;
}
// Set that node pair has been visited
cuckatooVVisitedNodePairs.setUnique(node >> 1, index);
// Check if node's pair has more than one connection
nodeConnection = cuckatooVNewestNodeConnections.get(node ^ 1);
if(nodeConnection->previousNodeConnectionLink) {
// Go through all of the node's pair's connections
for(; nodeConnection; nodeConnection = nodeConnection->previousNodeConnectionLink) {
// Check if the connected node has a pair
if(cuckatooUNewestNodeConnections.contains((nodeConnection - 1)->node ^ 1)) {
// Check if the connected node's pair wasn't already visited
if(!cuckatooUVisitedNodePairs.contains((nodeConnection - 1)->node >> 1)) {
// Check if solution was found at the connected node's pair
if(searchNodeConnectionsForCuckatooSolutionFirstPartition(cycleSize + 2, (nodeConnection - 1)->node ^ 1, &(nodeConnection - 1)->edgeIndex)) {
// Get solution from visited nodes
cuckatooUVisitedNodePairs.getValues(solution);
cuckatooVVisitedNodePairs.getValues(&solution[SOLUTION_SIZE / 2]);
// Sort solution in ascending order
sort(solution, solution + SOLUTION_SIZE);
// Return true
return true;
}
}
}
}
// Break
break;
}
// Go to node's pair opposite end and get its edge index
index = &(nodeConnection - 1)->edgeIndex;
node = (nodeConnection - 1)->node;
// Check if node pair was already visited
if(cuckatooUVisitedNodePairs.contains(node >> 1)) {
// Break
break;
}
// Check if node doesn't have a pair
if(!cuckatooUNewestNodeConnections.contains(node ^ 1)) {
// break
break;
}
}
}
}
// Return false
return false;
}
// Search node connections for cuckatoo solution first partition
bool searchNodeConnectionsForCuckatooSolutionFirstPartition(const uint_fast8_t cycleSize, const uint32_t node, const uint32_t *index) noexcept {
// Set that node pair has been visited
const uint32_t visitedNodePairIndex = cuckatooUVisitedNodePairs.setUniqueAndGetIndex(node >> 1, index);
// Go through all of the node's connections
for(const CuckatooNodeConnectionsLink *nodeConnection = cuckatooUNewestNodeConnections.get(node); nodeConnection; nodeConnection = nodeConnection->previousNodeConnectionLink) {
// Check if the connected node's pair wasn't already visited
if(!cuckatooVVisitedNodePairs.contains((nodeConnection + 1)->node >> 1)) {
// Check if cycle is complete
if(((nodeConnection + 1)->node ^ 1) == cuckatooRootNode) {
// Check if cycle is a solution
if(cycleSize == SOLUTION_SIZE - 1) {
// Set that the connected node's pair has been visited
cuckatooVVisitedNodePairs.setUnique((nodeConnection + 1)->node >> 1, &(nodeConnection + 1)->edgeIndex);
// Return true
return true;
}
}
// Otherwise check if cycle could be as solution
else if(cycleSize != SOLUTION_SIZE - 1) {
// Check if the connected node has a pair
if(cuckatooVNewestNodeConnections.contains((nodeConnection + 1)->node ^ 1)) {
// Check if solution was found at the connected node's pair
if(searchNodeConnectionsForCuckatooSolutionSecondPartition(cycleSize + 1, (nodeConnection + 1)->node ^ 1, &(nodeConnection + 1)->edgeIndex)) {
// Return true
return true;
}
}
}
}
}
// Set that node pair hasn't been visited
cuckatooUVisitedNodePairs.removeMostRecentSetUique(visitedNodePairIndex);
// Return false
return false;
}
// Search node connections for cuckatoo solution second partition
bool searchNodeConnectionsForCuckatooSolutionSecondPartition(const uint_fast8_t cycleSize, const uint32_t node, const uint32_t *index) noexcept {
// Set that node pair has been visited
const uint32_t visitedNodePairIndex = cuckatooVVisitedNodePairs.setUniqueAndGetIndex(node >> 1, index);
// Go through all of the node's connections
for(const CuckatooNodeConnectionsLink *nodeConnection = cuckatooVNewestNodeConnections.get(node); nodeConnection; nodeConnection = nodeConnection->previousNodeConnectionLink) {
// Check if the connected node has a pair
if(cuckatooUNewestNodeConnections.contains((nodeConnection - 1)->node ^ 1)) {
// Check if the connected node's pair wasn't already visited
if(!cuckatooUVisitedNodePairs.contains((nodeConnection - 1)->node >> 1)) {
// Check if solution was found at the connected node's pair
if(searchNodeConnectionsForCuckatooSolutionFirstPartition(cycleSize + 1, (nodeConnection - 1)->node ^ 1, &(nodeConnection - 1)->edgeIndex)) {
// Return true
return true;
}
}
}
}
// Set that node pair hasn't been visited
cuckatooVVisitedNodePairs.removeMostRecentSetUique(visitedNodePairIndex);
// Return false
return false;
}
#endif