* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / Mono.C5 / UserGuideExamples / KeywordRecognition.cs
1 /*\r
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
9  \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
12  \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
19  SOFTWARE.\r
20 */\r
21 \r
22 // C5 example: Keyword recognition 2004-12-20\r
23 \r
24 // Compile with \r
25 //   csc /r:C5.dll KeywordRecognition.cs \r
26 \r
27 using System;\r
28 using C5;\r
29 using SCG = System.Collections.Generic;\r
30 \r
31 namespace KeywordRecognition {\r
32 \r
33 class KeywordRecognition {\r
34   // Array of 77 keywords:\r
35 \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
48   \r
49   private static readonly ICollection<String> kw1;\r
50 \r
51   private static readonly ICollection<String> kw2;\r
52 \r
53   private static readonly ICollection<String> kw3;\r
54 \r
55   private static readonly SCG.IDictionary<String,bool> kw4 = \r
56     new SCG.Dictionary<String,bool>();\r
57 \r
58 \r
59   class SC : SCG.IComparer<string>\r
60   {\r
61     public int Compare(string a, string b)\r
62     {\r
63       return StringComparer.InvariantCulture.Compare(a,b);\r
64     }\r
65   }\r
66 \r
67   class SH : SCG.IEqualityComparer<string>\r
68   {\r
69     public int GetHashCode(string item)\r
70     {\r
71       return item.GetHashCode();\r
72     }\r
73 \r
74     public bool Equals(string i1, string i2)\r
75     {\r
76       return i1 == null ? i2 == null : i1.Equals(i2,StringComparison.InvariantCulture);\r
77     }\r
78   }\r
79 \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
90   }\r
91 \r
92   public static bool IsKeyword1(String s) {\r
93     return kw1.Contains(s);\r
94   }\r
95 \r
96   public static bool IsKeyword2(String s) {\r
97     return kw2.Contains(s);\r
98   }\r
99 \r
100   public static bool IsKeyword3(String s) {\r
101     return kw3.Contains(s);\r
102   }\r
103 \r
104   public static bool IsKeyword4(String s) { \r
105     return kw4.ContainsKey(s); \r
106   }\r
107 \r
108   public static bool IsKeyword5(String s) { \r
109     return Array.BinarySearch(keywordArray, s) >= 0; \r
110   }\r
111 \r
112   public static void Main(String[] args) {\r
113     if (args.Length != 2) \r
114       Console.WriteLine("Usage: KeywordRecognition <iterations> <word>\n");\r
115     else {\r
116       int count = int.Parse(args[0]);\r
117       String id = args[1];\r
118 \r
119       {\r
120         Console.Write("HashSet.Contains ");\r
121         Timer t = new Timer();\r
122         for (int i=0; i<count; i++)\r
123           IsKeyword1(id);\r
124         Console.WriteLine(t.Check());      \r
125       }\r
126 \r
127       {\r
128         Console.Write("TreeSet.Contains ");\r
129         Timer t = new Timer();\r
130         for (int i=0; i<count; i++)\r
131           IsKeyword2(id);\r
132         Console.WriteLine(t.Check());      \r
133       }\r
134 \r
135       {\r
136         Console.Write("SortedArray.Contains ");\r
137         Timer t = new Timer();\r
138         for (int i=0; i<count; i++)\r
139           IsKeyword3(id);\r
140         Console.WriteLine(t.Check());      \r
141       }\r
142 \r
143       {\r
144         Console.Write("SCG.Dictionary.ContainsKey ");\r
145         Timer t = new Timer();\r
146         for (int i=0; i<count; i++)\r
147           IsKeyword4(id);\r
148         Console.WriteLine(t.Check());      \r
149       }\r
150 \r
151       {\r
152         Console.Write("Array.BinarySearch ");\r
153         Timer t = new Timer();\r
154         for (int i=0; i<count; i++)\r
155           IsKeyword5(id);\r
156         Console.WriteLine(t.Check());      \r
157       }\r
158     }\r
159   }\r
160 }\r
161 \r
162 // Crude timing utility ----------------------------------------\r
163    \r
164 public class Timer {\r
165   private DateTime start;\r
166 \r
167   public Timer() {\r
168     start = DateTime.Now;\r
169   }\r
170 \r
171   public double Check() {\r
172     TimeSpan dur = DateTime.Now - start;\r
173     return dur.TotalSeconds;\r
174   }\r
175 }\r
176 \r
177 }\r