-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.cpp
321 lines (279 loc) · 7.23 KB
/
build.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
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
#include <algorithm>
#include <array>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <numeric>
#include <sstream>
#include <vector>
struct tree_t
{
private:
struct node_t
{
std::array<int64_t, 2> values;
std::array<node_t*, 3> children;
node_t* parent;
bool is_two_node;
node_t(int64_t value,
node_t* child0 = nullptr,
node_t* child1 = nullptr) :
values(),
children(),
parent(),
is_two_node(true)
{
values[0] = value;
set_child(0, child0);
set_child(1, child1);
}
node_t(int64_t value1,
int64_t value2,
node_t* child0 = nullptr,
node_t* child1 = nullptr,
node_t* child2 = nullptr) :
values(),
children(),
parent(),
is_two_node(false)
{
values[0] = value1;
values[1] = value2;
set_child(0, child0);
set_child(1, child1);
set_child(2, child2);
}
void set_child(int64_t i, node_t* new_child)
{
if (new_child)
new_child->parent = this;
children[i] = new_child;
}
bool is_data_node() const
{
return children[0] == nullptr;
}
bool contains(int64_t value) const
{
if (is_two_node)
return values[0] == value;
else
return values[0] == value || values[1] == value;
}
// Extend the node with a new value and subtree.
//
// If the node wasn't full, return {0, nullptr}.
// If the node was full, create a new node, distribute the
// values and subtrees between the two nodes and return the
// overflowed value and the right node.
std::pair<int64_t, node_t*> add(int64_t value, node_t* new_node)
{
if (is_two_node)
{
if (value < values[0])
{
values[1] = values[0];
values[0] = value;
children[2] = children[1];
set_child(1, new_node);
}
else
{
values[1] = value;
set_child(2, new_node);
}
is_two_node = false;
return {0, nullptr};
}
else
{
int64_t overflow_value;
node_t* overflow_node;
if (is_data_node())
{
if (value < values[0])
{
overflow_value = values[0];
overflow_node = new node_t(values[0], values[1]);
values[0] = value;
}
else if (value < values[1])
{
overflow_value = value;
overflow_node = new node_t(value, values[1]);
}
else
{
overflow_value = values[1];
overflow_node = new node_t(values[1], value);
}
}
else
{
if (value < values[0])
{
overflow_value = values[0];
overflow_node = new node_t(values[1], children[1], children[2]);
values[0] = value;
set_child(1, new_node);
}
else if (value < values[1])
{
overflow_value = value;
overflow_node = new node_t(values[1], new_node, children[2]);
}
else
{
overflow_value = values[1];
overflow_node = new node_t(value, children[2], new_node);
}
}
children[2] = nullptr;
is_two_node = true;
return {overflow_value, overflow_node};
}
}
};
node_t* root;
node_t* last_inserted;
void destroy(node_t* from)
{
if (!from)
return;
for (auto& n : from->children)
destroy(n);
delete from;
}
node_t* find(int64_t value) const
{
node_t* current = nullptr;
node_t* candidate = root;
while (candidate)
{
current = candidate;
if (value < current->values[0])
candidate = current->children[0];
else if (current->is_two_node || value < current->values[1])
candidate = current->children[1];
else
candidate = current->children[2];
}
return current;
}
void insert_from(node_t* from, int64_t value)
{
if (!from)
{
root = last_inserted = new node_t(value);
return;
}
if (from->contains(value))
{
last_inserted = from;
return;
}
node_t* current = from;
node_t* candidate = from->parent;
auto result = current->add(value, nullptr);
last_inserted = current->contains(value) ? current : result.second;
while (candidate && result.second)
{
current = candidate;
result = current->add(result.first, result.second);
candidate = current->parent;
}
if (!candidate && result.second)
root = new node_t(result.first, current, result.second);
}
public:
tree_t() :
root(),
last_inserted()
{ }
~tree_t()
{
destroy(root);
}
void insert(int64_t value)
{
insert_from(find(value), value);
}
void insert_last(int64_t value)
{
insert_from(last_inserted, value);
}
};
template <typename It>
std::pair<double, double> analyze(It begin, It end)
{
int size = std::distance(begin, end);
double sum = std::accumulate(begin, end, 0.0);
double mean = sum / size;
double sum_sq = 0.0;
std::for_each(begin, end, [&] (double x) { sum_sq += (x - mean) * (x - mean); });
double stddev = std::sqrt(sum_sq / (size - 1));
return {mean, stddev};
}
int main(int argc, char** argv)
{
using namespace std::chrono;
constexpr int SIZE = 10000000;
int time_limit;
{
std::stringstream s(argv[1]);
s >> time_limit;
}
{
std::vector<double> finger_results;
auto measurement_start = high_resolution_clock::now();
for (int i = 1;; i++)
{
auto start = high_resolution_clock::now();
for (int j = 0; j < i; j++)
{
tree_t t;
for (int64_t k = SIZE; k >= 0; k--)
t.insert_last(k);
}
auto end = high_resolution_clock::now();
duration<double, std::milli> diff = end - start;
finger_results.push_back(diff.count() / i);
if (duration_cast<milliseconds>(end - measurement_start).count() > time_limit)
break;
}
auto result = analyze(finger_results.begin(), finger_results.end());
std::cout << "finger_mean = " << result.first << " ms\n";
std::cout << "finger_stddev = " << result.second << " ms\n";
std::cout << "finger_results = ";
for (auto d : finger_results)
std::cout << d << ",";
std::cout << "\n";
}
{
std::vector<double> root_results;
auto measurement_start = high_resolution_clock::now();
for (int i = 1;; i++)
{
auto start = high_resolution_clock::now();
for (int j = 0; j < i; j++)
{
tree_t t;
for (int64_t k = SIZE; k >= 0; k--)
t.insert(k);
}
auto end = high_resolution_clock::now();
duration<double, std::milli> diff = end - start;
root_results.push_back(diff.count() / i);
if (duration_cast<milliseconds>(end - measurement_start).count() > time_limit)
break;
}
auto result = analyze(root_results.begin(), root_results.end());
std::cout << "root_mean = " << result.first << " ms\n";
std::cout << "root_stddev = " << result.second << " ms\n";
std::cout << "root_results = ";
for (auto d : root_results)
std::cout << d << ",";
std::cout << "\n";
}
}