1
|
/*
|
2
|
Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
|
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
of this software and associated documentation files (the "Software"), to deal
|
5
|
in the Software without restriction, including without limitation the rights
|
6
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
copies of the Software, and to permit persons to whom the Software is
|
8
|
furnished to do so, subject to the following conditions:
|
9
|
|
10
|
The above copyright notice and this permission notice shall be included in
|
11
|
all copies or substantial portions of the Software.
|
12
|
|
13
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
SOFTWARE.
|
20
|
*/
|
21
|
|
22
|
// C5 example: Keyword recognition 2004-12-20
|
23
|
|
24
|
// Compile with
|
25
|
// csc /r:C5.dll KeywordRecognition.cs
|
26
|
|
27
|
using System;
|
28
|
using C5;
|
29
|
using SCG = System.Collections.Generic;
|
30
|
|
31
|
namespace KeywordRecognition {
|
32
|
|
33
|
class KeywordRecognition {
|
34
|
// Array of 77 keywords:
|
35
|
|
36
|
static readonly String[] keywordArray =
|
37
|
{ "abstract", "as", "base", "bool", "break", "byte", "case", "catch",
|
38
|
"char", "checked", "class", "const", "continue", "decimal", "default",
|
39
|
"delegate", "do", "double", "else", "enum", "event", "explicit",
|
40
|
"extern", "false", "finally", "fixed", "float", "for", "foreach",
|
41
|
"goto", "if", "implicit", "in", "int", "interface", "internal", "is",
|
42
|
"lock", "long", "namespace", "new", "null", "object", "operator",
|
43
|
"out", "override", "params", "private", "protected", "public",
|
44
|
"readonly", "ref", "return", "sbyte", "sealed", "short", "sizeof",
|
45
|
"stackalloc", "static", "string", "struct", "switch", "this", "throw",
|
46
|
"true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe",
|
47
|
"ushort", "using", "virtual", "void", "volatile", "while" };
|
48
|
|
49
|
private static readonly ICollection<String> kw1;
|
50
|
|
51
|
private static readonly ICollection<String> kw2;
|
52
|
|
53
|
private static readonly ICollection<String> kw3;
|
54
|
|
55
|
private static readonly SCG.IDictionary<String,bool> kw4 =
|
56
|
new SCG.Dictionary<String,bool>();
|
57
|
|
58
|
|
59
|
class SC : SCG.IComparer<string>
|
60
|
{
|
61
|
public int Compare(string a, string b)
|
62
|
{
|
63
|
return StringComparer.InvariantCulture.Compare(a,b);
|
64
|
}
|
65
|
}
|
66
|
|
67
|
class SH : SCG.IEqualityComparer<string>
|
68
|
{
|
69
|
public int GetHashCode(string item)
|
70
|
{
|
71
|
return item.GetHashCode();
|
72
|
}
|
73
|
|
74
|
public bool Equals(string i1, string i2)
|
75
|
{
|
76
|
return i1 == null ? i2 == null : i1.Equals(i2,StringComparison.InvariantCulture);
|
77
|
}
|
78
|
}
|
79
|
|
80
|
static KeywordRecognition() {
|
81
|
kw1 = new HashSet<String>();
|
82
|
kw1.AddAll<string>(keywordArray);
|
83
|
kw2 = new TreeSet<String>(new SC());
|
84
|
kw2.AddAll<string>(keywordArray);
|
85
|
kw3 = new SortedArray<String>(new SC());
|
86
|
kw3.AddAll<string>(keywordArray);
|
87
|
kw4 = new SCG.Dictionary<String,bool>();
|
88
|
foreach (String keyword in keywordArray)
|
89
|
kw4.Add(keyword, false);
|
90
|
}
|
91
|
|
92
|
public static bool IsKeyword1(String s) {
|
93
|
return kw1.Contains(s);
|
94
|
}
|
95
|
|
96
|
public static bool IsKeyword2(String s) {
|
97
|
return kw2.Contains(s);
|
98
|
}
|
99
|
|
100
|
public static bool IsKeyword3(String s) {
|
101
|
return kw3.Contains(s);
|
102
|
}
|
103
|
|
104
|
public static bool IsKeyword4(String s) {
|
105
|
return kw4.ContainsKey(s);
|
106
|
}
|
107
|
|
108
|
public static bool IsKeyword5(String s) {
|
109
|
return Array.BinarySearch(keywordArray, s) >= 0;
|
110
|
}
|
111
|
|
112
|
public static void Main(String[] args) {
|
113
|
if (args.Length != 2)
|
114
|
Console.WriteLine("Usage: KeywordRecognition <iterations> <word>\n");
|
115
|
else {
|
116
|
int count = int.Parse(args[0]);
|
117
|
String id = args[1];
|
118
|
|
119
|
{
|
120
|
Console.Write("HashSet.Contains ");
|
121
|
Timer t = new Timer();
|
122
|
for (int i=0; i<count; i++)
|
123
|
IsKeyword1(id);
|
124
|
Console.WriteLine(t.Check());
|
125
|
}
|
126
|
|
127
|
{
|
128
|
Console.Write("TreeSet.Contains ");
|
129
|
Timer t = new Timer();
|
130
|
for (int i=0; i<count; i++)
|
131
|
IsKeyword2(id);
|
132
|
Console.WriteLine(t.Check());
|
133
|
}
|
134
|
|
135
|
{
|
136
|
Console.Write("SortedArray.Contains ");
|
137
|
Timer t = new Timer();
|
138
|
for (int i=0; i<count; i++)
|
139
|
IsKeyword3(id);
|
140
|
Console.WriteLine(t.Check());
|
141
|
}
|
142
|
|
143
|
{
|
144
|
Console.Write("SCG.Dictionary.ContainsKey ");
|
145
|
Timer t = new Timer();
|
146
|
for (int i=0; i<count; i++)
|
147
|
IsKeyword4(id);
|
148
|
Console.WriteLine(t.Check());
|
149
|
}
|
150
|
|
151
|
{
|
152
|
Console.Write("Array.BinarySearch ");
|
153
|
Timer t = new Timer();
|
154
|
for (int i=0; i<count; i++)
|
155
|
IsKeyword5(id);
|
156
|
Console.WriteLine(t.Check());
|
157
|
}
|
158
|
}
|
159
|
}
|
160
|
}
|
161
|
|
162
|
// Crude timing utility ----------------------------------------
|
163
|
|
164
|
public class Timer {
|
165
|
private DateTime start;
|
166
|
|
167
|
public Timer() {
|
168
|
start = DateTime.Now;
|
169
|
}
|
170
|
|
171
|
public double Check() {
|
172
|
TimeSpan dur = DateTime.Now - start;
|
173
|
return dur.TotalSeconds;
|
174
|
}
|
175
|
}
|
176
|
|
177
|
}
|