-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
337 lines (292 loc) · 7.07 KB
/
Form1.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
using System;
using System.Text;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
using Wraith.Collections.Generic;
using Wraith.Collections;
namespace TrieTest
{
public partial class Form1 : Form
{
//private TrieNode root;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender,EventArgs e)
{
/*
TrieNode root = new TrieNode(Range.Empty);
string[] words = new string[] {
"florida",
"flamed",
"flame",
"flamer"
};
foreach (string word in words)
{
root.Insert(word);
}
root.Delete("flamer");
root.Delete("flame");
bool flame = root.Lookup("flamer",0,5);
TreeNode rootNode = new TreeNode("root");
DrawNode(rootNode,root);
this.treeView1.Nodes.Add(rootNode);
*/
//TrieNode root = new TrieNode(null);
StringTrie trie = new StringTrie();
//trie.Insert("aaaaaaa");
//trie.Insert("bbbbbbb");
//trie.Delete("aaaaaaa");
//trie.Delete("bbbbbbb");
trie.Insert("aaaaaaa");
trie.Insert("aaabbbb");
trie.Delete("aaaaaaa");
trie.Delete("aaabbbb");
//this.DoFile();
//this.EnumStrings();
//SplitStrings();
}
private void DoFile()
{
string[] words = null;
string fileName = "bill.txt";
if (File.Exists(fileName))
{
using (StreamReader reader = new StreamReader(fileName))
{
string text = reader.ReadToEnd();
if (text!=null)
{
words=text.Split(new char[] { ' ','\t','\r','\n' },StringSplitOptions.RemoveEmptyEntries);
}
}
}
//TrieNode root = new TrieNode(Range.Empty);
//TrieNode root = new TrieNode(null);
StringTrie root = new StringTrie();
/*
List<string> list = new List<string>(words);
Random random = new Random();
while (list.Count>0)
{
int index = random.Next(0,list.Count-1);
root.Insert(list[index]);
list.RemoveAt(index);
}
list = new List<string>(words);
while (list.Count>0)
{
int index = random.Next(0,list.Count);
root.Delete(list[index]);
list.RemoveAt(index);
}
*/
//return;
/*
foreach (string word in words)
{
root.Insert(word);
}
foreach (string word in words)
{
root.Delete(word);
}
*/
//return;
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
if (words!=null)
{
foreach (string word in words)
{
root.Insert(word);
}
}
stopwatch.Stop();
Debug.Print(string.Format("added {0:D} in {1}",words.Length,stopwatch.Elapsed.ToString()));
bool matched = false;
int failCount=0;
TimeSpan[] times = new TimeSpan[words.Length];
for (int index=0;index<words.Length;index++)
{
matched=false;
stopwatch.Reset();
stopwatch.Start();
matched=root.Lookup(words[index],0,words[index].Length);
stopwatch.Stop();
times[index]=stopwatch.Elapsed;
if (!matched)
{
failCount+=1;
root.Lookup(words[index],0,words[index].Length);
}
}
bool failed = root.Lookup("moron",0,5);
//root.Delete("2001");
for (int index=0;index<words.Length;index++)
{
root.Delete(words[index]);
}
TimeSpan average = TimeSpan.Zero;
decimal total=0;
unchecked
{
foreach (TimeSpan time in times)
{
total+=(decimal)time.TotalMilliseconds;
}
average=new TimeSpan((Int64)(total/(Decimal)words.Length));
}
TimeSpan maximum=TimeSpan.MinValue;
TimeSpan minimum=TimeSpan.MaxValue;
TimeSpan threshold = TimeSpan.FromMilliseconds(1);
int overThreshold=0;
new List<TimeSpan>(times).ForEach(
delegate(TimeSpan time)
{
if (time>maximum)
{
maximum=time;
}
if (time<minimum)
{
minimum=time;
}
if (time>threshold)
{
overThreshold+=1;
}
}
);
Debug.Print(string.Format("average lookup {0}",average.ToString()));
Debug.Print(string.Format("maximum lookup {0}",maximum.ToString()));
Debug.Print(string.Format("minimum lookup {0}",minimum.ToString()));
TreeNode rootNode = new TreeNode("()");
DrawNode(rootNode,root.Root);
this.treeView1.Nodes.Add(rootNode);
rootNode.ExpandAll();
}
private void DrawNode(TreeNode parent,ITrieNode<string> item)
{
if (parent!=null && item!=null)
{
string text = "<EMPTY>";
if (item.Range!=null && item.Range.Container!=null)
{
text= item.Range.Container.Substring(
item.Range.Start,
item.Range.Length
);
}
else
{
Debug.WriteLine("warning: empty entry found");
}
TreeNode node = new TreeNode(text);
if (item.IsTerminator)
{
node.ForeColor=Color.Red;
}
if (item.IsBranch)
{
foreach (ITrieNode<string> child in item.Children)
{
DrawNode(node,child);
}
}
parent.Nodes.Add(node);
}
}
public void EnumStrings()
{
string[] words = null;
string fileName = "bill.txt";
if (File.Exists(fileName))
{
using (StreamReader reader = new StreamReader(fileName))
{
string text = reader.ReadToEnd();
if (text!=null)
{
words=text.Split(new char[] { ' ','\t','\r','\n' },StringSplitOptions.RemoveEmptyEntries);
}
}
}
StringTrie trie = new StringTrie();
if (words!=null)
{
foreach (string word in words)
{
trie.Insert(word);
}
}
List<string> list = new List<string>(1000);
IEnumerator<string> enumerator = trie.GetEnumerator();
if (enumerator!=null)
{
while (enumerator.MoveNext())
{
list.Add(enumerator.Current);
}
}
foreach (string word in words)
{
if (!list.Contains(word))
{
throw new InvalidOperationException("trie does not contain word");
}
}
}
//public void SplitStrings()
//{
// string[] words = null;
// string fileName = "bill.txt";
// string text=null;
// if (File.Exists(fileName))
// {
// using (StreamReader reader = new StreamReader(fileName))
// {
// text = reader.ReadToEnd();
// if (text!=null)
// {
// words=text.Split(new char[] { ' ','\t','\r','\n' },StringSplitOptions.RemoveEmptyEntries);
// }
// }
// }
// StringTrie trie = new StringTrie();
// StringTrie.Split(
// new StringRange(text),
// trie,
// new char[] { ' ','\t','\r','\n' },
// StringSplitOptions.RemoveEmptyEntries
// );
// List<string> list = new List<string>(2000);
// IEnumerator<string> enumerator = trie.GetEnumerator();
// int moved = 0;
// if (enumerator!=null)
// {
// while (enumerator.MoveNext())
// {
// //string current = enumerator.Current;
// //Debug.WriteLine(current);
// moved+=1;
// list.Add(enumerator.Current);
// }
// }
// foreach (string word in words)
// {
// if (!list.Contains(word))
// {
// throw new InvalidOperationException("trie does not contain word");
// }
// }
//}
}
}