-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtraverse.cs
258 lines (223 loc) · 5.33 KB
/
traverse.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Traverse
{
class Tree
{
private class TreeNode
{
public TreeNode parent;
public TreeNode left;
public TreeNode right;
public Int64 value;
public TreeNode(Int64 value_, TreeNode left_ = null, TreeNode right_ = null)
{
parent = null;
left = left_;
if (left != null)
left.parent = this;
right = right_;
if (right != null)
right.parent = this;
value = value_;
}
}
private TreeNode root;
private TreeNode finger;
private TreeNode BuildFullInternal(Int64 depth, Int64 value)
{
if (depth == 0)
{
return null;
}
TreeNode left = BuildFullInternal(depth - 1, value * 2);
TreeNode right = BuildFullInternal(depth - 1, value * 2 + 1);
return new TreeNode(value, left, right);
}
public Tree()
{ }
public void BuildFull(int depth)
{
root = finger = BuildFullInternal(depth, 1);
}
public void FingerUp()
{
if (finger.parent == null)
{
return;
}
finger = finger.parent;
}
public void FingerLeft()
{
if (finger.left == null)
{
return;
}
finger = finger.left;
}
public void FingerRight()
{
if (finger.right == null)
{
return;
}
finger = finger.right;
}
public void FingerSet(Int64 value)
{
finger.value = value;
}
public void FingerReset()
{
finger = root;
}
}
class Program
{
static void Convert(List<Int64> input, List<Int64> output)
{
Stack<Int64> stack = new Stack<Int64>();
foreach (var v in input)
{
switch (v)
{
case 0:
stack.Pop();
break;
case 1:
case 2:
stack.Push(v);
break;
default:
output.AddRange(stack);
output.Add(v);
break;
}
}
}
static void InterpretFinger(Tree t, List<Int64> input)
{
foreach (var v in input)
{
switch (v)
{
case 0:
t.FingerUp();
break;
case 1:
t.FingerLeft();
break;
case 2:
t.FingerRight();
break;
default:
t.FingerSet(v);
break;
}
}
}
static void InterpretRoot(Tree t, List<Int64> input)
{
foreach (var v in input)
{
switch (v)
{
case 0:
break;
case 1:
t.FingerLeft();
break;
case 2:
t.FingerRight();
break;
default:
t.FingerSet(v);
t.FingerReset();
break;
}
}
}
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)
{
int timeLimit = int.Parse(args[0]);
List<Int64> inFinger = new List<Int64>();
int depth = int.Parse(Console.ReadLine());
{
Int64 i;
while (Int64.TryParse(Console.ReadLine(), out i))
{
inFinger.Add(i);
}
}
List<Int64> inRoot = new List<Int64>();
Convert(inFinger, inRoot);
Tree t = new Tree();
t.BuildFull(depth);
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++)
{
t.FingerReset();
InterpretFinger(t, inFinger);
}
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++)
{
t.FingerReset();
InterpretRoot(t, inRoot);
}
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();
}
}
}
}