-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.cs
312 lines (272 loc) · 7.22 KB
/
build.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Tree23
{
class Tree
{
private class TreeNode
{
public Int64 value0;
public Int64 value1;
public TreeNode child0;
public TreeNode child1;
public TreeNode child2;
public TreeNode parent;
public bool isTwoNode;
public TreeNode(Int64 value0_, TreeNode child0_ = null, TreeNode child1_ = null)
{
isTwoNode = true;
parent = null;
value0 = value0_;
value1 = 0;
SetChild(0, child0_);
SetChild(1, child1_);
SetChild(2, null);
}
public TreeNode(Int64 value0_, Int64 value1_, TreeNode child0_ = null, TreeNode child1_ = null, TreeNode child2_ = null)
{
isTwoNode = false;
parent = null;
value0 = value0_;
value1 = value1_;
SetChild(0, child0_);
SetChild(1, child1_);
SetChild(2, child2_);
}
public void SetChild(Int64 index, TreeNode newChild)
{
switch (index)
{
case 0: child0 = newChild; break;
case 1: child1 = newChild; break;
case 2: child2 = newChild; break;
}
if (newChild != null)
{
newChild.parent = this;
}
}
public bool IsDataNode()
{
return child0 == null;
}
public bool Contains(Int64 value)
{
if (isTwoNode)
{
return value0 == value;
}
else
{
return value0 == value || value1 == value;
}
}
public KeyValuePair<Int64, TreeNode> Add(Int64 value, TreeNode newNode)
{
if (isTwoNode)
{
if (value < value0)
{
value1 = value0;
value0 = value;
child2 = child1;
SetChild(1, newNode);
}
else
{
value1 = value;
SetChild(2, newNode);
}
isTwoNode = false;
return new KeyValuePair<Int64, TreeNode>(0, null);
}
else
{
Int64 overflowValue = 0;
TreeNode overflowNode = null;
if (IsDataNode())
{
if (value < value0)
{
overflowValue = value0;
overflowNode = new TreeNode(value0, value1);
value0 = value;
}
else if (value < value1)
{
overflowValue = value;
overflowNode = new TreeNode(value, value1);
}
else
{
overflowValue = value1;
overflowNode = new TreeNode(value1, value);
}
}
else
{
if (value < value0)
{
overflowValue = value0;
overflowNode = new TreeNode(value1, child1, child2);
value0 = value;
SetChild(1, newNode);
}
else if (value < value1)
{
overflowValue = value;
overflowNode = new TreeNode(value1, newNode, child2);
}
else
{
overflowValue = value1;
overflowNode = new TreeNode(value, child2, newNode);
}
}
child2 = null;
isTwoNode = true;
return new KeyValuePair<Int64, TreeNode>(overflowValue, overflowNode);
}
}
}
private TreeNode root;
private TreeNode lastInserted;
private TreeNode Find(Int64 value)
{
TreeNode current = null;
TreeNode candidate = root;
while (candidate != null)
{
current = candidate;
if (value < current.value0)
{
candidate = current.child0;
}
else if (current.isTwoNode || value < current.value1)
{
candidate = current.child1;
}
else
{
candidate = current.child2;
}
}
return current;
}
private void InsertFrom(TreeNode from, Int64 value)
{
if (from == null)
{
root = lastInserted = new TreeNode(value);
return;
}
if (from.Contains(value))
{
lastInserted = from;
return;
}
TreeNode current = from;
TreeNode candidate = from.parent;
var result = current.Add(value, null);
lastInserted = current.Contains(value) ? current : result.Value;
while (candidate != null && result.Value != null)
{
current = candidate;
result = current.Add(result.Key, result.Value);
candidate = current.parent;
}
if (candidate == null && result.Value != null)
{
root = new TreeNode(result.Key, current, result.Value);
}
}
public Tree()
{ }
public void Insert(Int64 value)
{
InsertFrom(Find(value), value);
}
public void InsertLast(Int64 value)
{
InsertFrom(lastInserted, value);
}
}
class Program
{
static KeyValuePair<double, double> Analyze(List<double> input)
{
double mean = input.Average();
double sumSq = 0.0;
input.ForEach(x => sumSq += (x - mean) * (x - mean));
double stddev = Math.Sqrt(sumSq / (input.Count - 1));
return new KeyValuePair<double, double>(mean, stddev);
}
static void Main(string[] args)
{
const int SIZE = 10000000;
int timeLimit = int.Parse(args[0]);
Stopwatch local = new Stopwatch();
Stopwatch total = new Stopwatch();
{
List<double> fingerResults = new List<double>();
total.Restart();
for (int i = 1;; i++)
{
local.Restart();
for (int j = 0; j < i; j++)
{
Tree t = new Tree();
for (Int64 k = SIZE; k >= 0; k--)
{
t.InsertLast(k);
}
}
local.Stop();
fingerResults.Add((double)local.ElapsedMilliseconds / i);
if (total.ElapsedMilliseconds > timeLimit)
break;
}
var result = Analyze(fingerResults);
Console.WriteLine("finger_mean = {0} ms", result.Key);
Console.WriteLine("finger_stddev = {0} ms", result.Value);
Console.Write("finger_results = ");
foreach (var d in fingerResults)
{
Console.Write("{0},", d);
}
Console.WriteLine();
}
{
List<double> rootResults = new List<double>();
total.Restart();
for (int i = 1;; i++)
{
local.Restart();
for (int j = 0; j < i; j++)
{
Tree t = new Tree();
for (Int64 k = SIZE; k >= 0; k--)
{
t.Insert(k);
}
}
local.Stop();
rootResults.Add((double)local.ElapsedMilliseconds / i);
if (total.ElapsedMilliseconds > timeLimit)
break;
}
var result = Analyze(rootResults);
Console.WriteLine("root_mean = {0} ms", result.Key);
Console.WriteLine("root_stddev = {0} ms", result.Value);
Console.Write("root_results = ");
foreach (var d in rootResults)
{
Console.Write("{0},", d);
}
Console.WriteLine();
}
}
}
}