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