-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathch2-orbit_structure.cpp
186 lines (156 loc) · 4.52 KB
/
ch2-orbit_structure.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
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
#include <cassert>
#include <iostream>
#include <set>
#include <vector>
#include "my_orbit_structure.h"
#include "my_intrinsics.h"
#include "my_type_functions.h"
using namespace std;
template<typename F, typename P>
requires(Transformation(F) && UnaryPredicate(P) &&
Domain(F) == Domain(P))
Domain(F) connection_point_naive(Domain(F) x, F f, P p)
{
set<Domain(F)> seen;
while (p(x) && seen.find(x) == seen.end()) {
seen.insert(x);
x = f(x);
}
return x;
}
class Node {
public:
int value;
Node* next;
Node() : value(0), next(NULL), deleting(false) {
}
Node(int value, Node* next) : value(value), next(next), deleting(false) {
}
~Node() {
Node* toDelete = next;
next = NULL;
deleting = true;
if (toDelete != NULL && !toDelete->deleting) delete toDelete;
}
private:
bool deleting;
};
template<typename InputIterator>
pair<Node*, Node*> make_linked_list(InputIterator start, InputIterator stop)
{
Node* head = new Node;
Node* current = head;
while (start != stop) {
current->next = new Node;
current->next->value = *start;
current = current->next;
++start;
}
Node* result = head->next;
head->next = NULL;
delete head;
return make_pair(result, current);
}
template<typename InputIterator>
Node* new_terminating_list(InputIterator start, InputIterator stop)
{
return make_linked_list(start, stop).first;
}
template<typename InputIterator>
Node* new_circular_list(InputIterator start, InputIterator stop)
{
pair<Node*, Node*> list = make_linked_list(start, stop);
list.second->next = list.first;
return list.first;
}
template<typename InputIterator>
pair<Node*, Node*> make_p_shaped_list(InputIterator handle_start,
InputIterator handle_stop,
InputIterator cycle_start,
InputIterator cycle_stop)
{
pair<Node*, Node*> handle = make_linked_list(handle_start, handle_stop);
handle.second->next = new_circular_list(cycle_start, cycle_stop);
return make_pair(handle.first, handle.second->next);
}
Node* traverse(Node* node)
{
return node->next;
}
bool can_traverse(Node* node)
{
return node != NULL;
}
void test_linked_list_cycle_detection() {
triple<unsigned long, unsigned long, Node*> structure;
vector<int> v;
size_t n = 10;
for (size_t i = 1; i <= n; i++) v.push_back(i);
Node* terminating = new_terminating_list(v.begin(), v.end());
structure = orbit_structure(terminating, traverse, can_traverse);
assert(structure.m0 == n);
assert(structure.m1 == 0);
assert(structure.m2 == NULL);
delete terminating;
Node* circular = new_circular_list(v.begin(), v.end());
structure = orbit_structure(circular, traverse, can_traverse);
assert(structure.m0 == 0);
assert(structure.m1 == n - 1);
assert(structure.m2 == circular);
delete circular;
pair<Node*, Node*> p_shaped = make_p_shaped_list(v.begin(), v.end(), v.begin(), v.end());
structure = orbit_structure(p_shaped.first, traverse, can_traverse);
assert(structure.m0 == n);
assert(structure.m1 == n - 1);
assert(structure.m2 == p_shaped.second);
delete p_shaped.first;
cout << "Linked list test passed!" << endl;
}
class LcgPredicate {
public:
LcgPredicate(unsigned long m) : m(m) {
}
bool operator()(unsigned long x) {
return x < m;
}
private:
unsigned long m;
};
template<>
struct input_type<LcgPredicate, 0> {
typedef unsigned long type;
};
class LcgTransformation {
public:
LcgTransformation(unsigned long a,
unsigned long b,
unsigned long m) :
a(a), b(b), m(m) {
}
unsigned long operator()(unsigned long x) {
return (a * x + b) % m;
}
private:
unsigned long a;
unsigned long b;
unsigned long m;
};
template<>
struct input_type<LcgTransformation, 0> {
typedef unsigned long type;
};
void test_prng_period_analysis() {
// https://en.wikipedia.org/wiki/Linear_congruential_generator
LcgTransformation f(1140671485 , 12820163, 1 << 24);
LcgPredicate p(1 << 24);
triple<unsigned long, unsigned long, unsigned long> structure =
orbit_structure(65535, f, p);
assert(structure.m0 == 0);
assert(structure.m1 == (1 << 24) - 1);
assert(structure.m2 == 65535);
cout << "PRNG test passed!" << endl;
}
int main() {
test_linked_list_cycle_detection();
test_prng_period_analysis();
}