-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
executable file
·95 lines (78 loc) · 2.36 KB
/
main.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
#include <iostream>
#include <cstdio>
#include <boost/dynamic_bitset.hpp>
#include <chrono>
#include "FixedBitSet.h"
#include <boost/lexical_cast.hpp>
using namespace std;
const auto CHECK_INTERVAL = 1000000UL;
inline void step(FixedBitSet &fbs) {
if (fbs.get(0)) {
fbs.append1101();
} else {
fbs.append00();
}
fbs.removeFirst(3);
}
void runExperiment(int n) {
auto start = chrono::steady_clock::now();
auto fbs = FixedBitSet::sigma(n); // current step
auto a = FixedBitSet::sigma(n); // point a for cycle detection
auto b = FixedBitSet(INT_MAX); // point b for cycle detection
auto steps = 0UL;
auto maxWordLength = fbs.getLength();
auto nextCheckPos = CHECK_INTERVAL;
auto cycle = false;
while (fbs.getLength() > 0) {
maxWordLength = max(maxWordLength, fbs.getLength());
++steps;
step(fbs);
if (steps < CHECK_INTERVAL) {
continue;
} else if (steps == nextCheckPos) {
nextCheckPos += CHECK_INTERVAL;
if (steps > CHECK_INTERVAL) a.replaceBy(b);
b.replaceBy(fbs);
continue;
}
if (fbs == b) {
cycle = true;
break;
}
}
auto cycleLength = 0UL;
if (cycle) {
do {
++cycleLength;
step(fbs);
} while (fbs != b);
// goes back to where a is
steps = nextCheckPos - 2 * CHECK_INTERVAL;
b.replaceBy(a);
// advances b so a and b are "cycleLength" steps apart from each other
for (auto i = 0UL; i < cycleLength; ++i) {
step(b);
}
// advances a and b simultaneously to find the start of the cycle
while (a != b) {
++steps;
step(a);
step(b);
}
}
auto end = chrono::steady_clock::now();
auto time = chrono::duration<double>(end - start).count();
printf("[n=%d] finished, steps=%lu, cycleLength=%lu, maxWordLength=%d, time=%fs\n",
n, steps, cycleLength, maxWordLength, time);
}
int main(int argc, char **argv) {
if (argc != 3) {
printf("You need to have 2 arguments specifying the range of n.");
return 1;
}
int start = boost::lexical_cast<int>(argv[1]);
int end = boost::lexical_cast<int>(argv[2]);
for (int i = start; i <= end; ++i) {
runExperiment(i);
}
}