-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersistent_stack.cpp
189 lines (148 loc) · 5.19 KB
/
persistent_stack.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
187
188
189
#include "test_utils.hpp"
#include "struct/persistent_stack.hpp"
void stress_test_persistent_stack() {
vector<vector<int>> stacks(1);
persistent_stack<int> ps;
int V = ps.versions();
boold coind(0.5);
LOOP_FOR_DURATION_OR_RUNS (1s, 150000) {
if (coind(mt)) { // push element
int v = rand_wide<int>(0, V - 1, +3);
int n = rand_unif<int>(-1000, 1000);
int v0 = ps.push(v, n);
int v1 = stacks.size();
stacks.push_back(stacks[v]);
stacks.back().push_back(n);
assert(V == v0 && V == v1);
assert(ps.top(V) == stacks[V].back());
V++;
}
if (coind(mt)) { // pop element
int v = rand_unif<int>(0, V - 1);
int v0 = ps.pop(v);
int v1 = stacks.size();
stacks.push_back(stacks[v]);
if (!stacks.back().empty()) {
stacks.back().pop_back();
}
assert(V == v0 && V == v1);
assert(stacks[V].empty() || ps.top(V) == stacks[V].back());
V++;
}
if (coind(mt) && V > 1) { // push inplace
int v = rand_wide<int>(1, V - 1, +3);
int n = rand_unif<int>(-1000, 1000);
ps.push_inplace(v, n);
stacks[v].push_back(n);
assert(ps.top(v) == stacks[v].back());
}
if (coind(mt) && V > 1) { // pop element inplace
int v = rand_unif<int>(1, V - 1);
ps.pop_inplace(v);
if (!stacks[v].empty()) {
stacks[v].pop_back();
}
assert(stacks[v].empty() || ps.top(v) == stacks[v].back());
}
}
int S = 0, M = 0;
for (int i = 0; i < V; i++) {
S += stacks[i].size();
M = max<int>(M, stacks[i].size());
}
println("final versions: {}", V);
println("final total size: {}", S);
println("final max size: {}", M);
}
void stress_test_persistent_jump_stack() {
vector<vector<int>> stacks(1);
persistent_jump_stack<int> ps;
int V = ps.versions();
boold coind(0.5);
LOOP_FOR_DURATION_OR_RUNS (1s, 150000) {
if (coind(mt) && V > 1) { // query kth from the bottom
int v = rand_unif<int>(1, V - 1);
int S = ps.size(v);
assert(S == int(stacks[v].size()));
if (S > 0) {
int k = rand_unif<int>(0, S - 1);
int x = ps.find_from_bottom(v, k);
int y = stacks[v][k];
assert(x == y);
}
}
if (coind(mt) && V > 1) { // query kth from the top
int v = rand_unif<int>(1, V - 1);
int S = ps.size(v);
assert(S == int(stacks[v].size()));
if (S > 0) {
int k = rand_unif<int>(0, S - 1);
int x = ps.find_from_top(v, k);
int y = stacks[v][S - k - 1];
assert(x == y);
}
}
if (coind(mt)) { // push element
int v = rand_wide<int>(0, V - 1, +3);
int n = rand_unif<int>(-1000, 1000);
int v0 = ps.push(v, n);
int v1 = stacks.size();
stacks.push_back(stacks[v]);
stacks.back().push_back(n);
assert(V == v0 && V == v1);
assert(ps.top(V) == stacks[V].back());
V++;
}
if (coind(mt)) { // pop element
int v = rand_unif<int>(0, V - 1);
assert(int(stacks[v].size()) == ps.size(v));
int v0 = ps.pop(v);
int v1 = stacks.size();
stacks.push_back(stacks[v]);
if (!stacks.back().empty()) {
stacks.back().pop_back();
}
assert(V == v0 && V == v1);
assert(stacks[V].empty() || ps.top(V) == stacks[V].back());
V++;
}
if (coind(mt) && V > 1) { // push inplace
int v = rand_wide<int>(1, V - 1, +3);
int n = rand_unif<int>(-1000, 1000);
ps.push_inplace(v, n);
stacks[v].push_back(n);
assert(ps.top(v) == stacks[v].back());
}
if (coind(mt) && V > 1) { // pop element inplace
int v = rand_unif<int>(1, V - 1);
ps.pop_inplace(v);
if (!stacks[v].empty()) {
stacks[v].pop_back();
}
assert(stacks[v].empty() || ps.top(v) == stacks[v].back());
}
if (true) { // verify elements in a random version
int v = rand_unif<int>(0, V - 1);
int S = ps.size(v);
int S_actual = stacks[v].size();
assert(S == S_actual);
for (int i = 0; i < S; i++) {
assert(ps.find_from_bottom(v, i) == stacks[v][i]);
assert(ps.find_from_top(v, i) == stacks[v][S - i - 1]);
}
}
}
int S = 0, M = 0;
for (int i = 0; i < V; i++) {
S += stacks[i].size();
M = max<int>(M, stacks[i].size());
}
println("final versions: {}", V);
println("final total size: {}", S);
println("final max size: {}", M);
}
int main() {
RUN_BLOCK(stress_test_persistent_stack());
RUN_BLOCK(stress_test_persistent_jump_stack());
return 0;
}