-/*\r
- Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft\r
- Permission is hereby granted, free of charge, to any person obtaining a copy\r
- of this software and associated documentation files (the "Software"), to deal\r
- in the Software without restriction, including without limitation the rights\r
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
- copies of the Software, and to permit persons to whom the Software is\r
- furnished to do so, subject to the following conditions:\r
- \r
- The above copyright notice and this permission notice shall be included in\r
- all copies or substantial portions of the Software.\r
- \r
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
- SOFTWARE.\r
-*/\r
-\r
-// C5 example: Keyword recognition 2004-12-20\r
-\r
-// Compile with \r
-// csc /r:C5.dll KeywordRecognition.cs \r
-\r
-using System;\r
-using C5;\r
-using SCG = System.Collections.Generic;\r
-\r
-namespace KeywordRecognition {\r
-\r
-class KeywordRecognition {\r
- // Array of 77 keywords:\r
-\r
- static readonly String[] keywordArray = \r
- { "abstract", "as", "base", "bool", "break", "byte", "case", "catch",\r
- "char", "checked", "class", "const", "continue", "decimal", "default",\r
- "delegate", "do", "double", "else", "enum", "event", "explicit",\r
- "extern", "false", "finally", "fixed", "float", "for", "foreach",\r
- "goto", "if", "implicit", "in", "int", "interface", "internal", "is",\r
- "lock", "long", "namespace", "new", "null", "object", "operator",\r
- "out", "override", "params", "private", "protected", "public",\r
- "readonly", "ref", "return", "sbyte", "sealed", "short", "sizeof",\r
- "stackalloc", "static", "string", "struct", "switch", "this", "throw",\r
- "true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe",\r
- "ushort", "using", "virtual", "void", "volatile", "while" };\r
- \r
- private static readonly ICollection<String> kw1;\r
-\r
- private static readonly ICollection<String> kw2;\r
-\r
- private static readonly ICollection<String> kw3;\r
-\r
- private static readonly SCG.IDictionary<String,bool> kw4 = \r
- new SCG.Dictionary<String,bool>();\r
-\r
-\r
- class SC : SCG.IComparer<string>\r
- {\r
- public int Compare(string a, string b)\r
- {\r
- return StringComparer.InvariantCulture.Compare(a,b);\r
- }\r
- }\r
-\r
- class SH : SCG.IEqualityComparer<string>\r
- {\r
- public int GetHashCode(string item)\r
- {\r
- return item.GetHashCode();\r
- }\r
-\r
- public bool Equals(string i1, string i2)\r
- {\r
- return i1 == null ? i2 == null : i1.Equals(i2,StringComparison.InvariantCulture);\r
- }\r
- }\r
-\r
- static KeywordRecognition() { \r
- kw1 = new HashSet<String>();\r
- kw1.AddAll<string>(keywordArray); \r
- kw2 = new TreeSet<String>(new SC());\r
- kw2.AddAll<string>(keywordArray);\r
- kw3 = new SortedArray<String>(new SC());\r
- kw3.AddAll<string>(keywordArray);\r
- kw4 = new SCG.Dictionary<String,bool>();\r
- foreach (String keyword in keywordArray) \r
- kw4.Add(keyword, false);\r
- }\r
-\r
- public static bool IsKeyword1(String s) {\r
- return kw1.Contains(s);\r
- }\r
-\r
- public static bool IsKeyword2(String s) {\r
- return kw2.Contains(s);\r
- }\r
-\r
- public static bool IsKeyword3(String s) {\r
- return kw3.Contains(s);\r
- }\r
-\r
- public static bool IsKeyword4(String s) { \r
- return kw4.ContainsKey(s); \r
- }\r
-\r
- public static bool IsKeyword5(String s) { \r
- return Array.BinarySearch(keywordArray, s) >= 0; \r
- }\r
-\r
- public static void Main(String[] args) {\r
- if (args.Length != 2) \r
- Console.WriteLine("Usage: KeywordRecognition <iterations> <word>\n");\r
- else {\r
- int count = int.Parse(args[0]);\r
- String id = args[1];\r
-\r
- {\r
- Console.Write("HashSet.Contains ");\r
- Timer t = new Timer();\r
- for (int i=0; i<count; i++)\r
- IsKeyword1(id);\r
- Console.WriteLine(t.Check()); \r
- }\r
-\r
- {\r
- Console.Write("TreeSet.Contains ");\r
- Timer t = new Timer();\r
- for (int i=0; i<count; i++)\r
- IsKeyword2(id);\r
- Console.WriteLine(t.Check()); \r
- }\r
-\r
- {\r
- Console.Write("SortedArray.Contains ");\r
- Timer t = new Timer();\r
- for (int i=0; i<count; i++)\r
- IsKeyword3(id);\r
- Console.WriteLine(t.Check()); \r
- }\r
-\r
- {\r
- Console.Write("SCG.Dictionary.ContainsKey ");\r
- Timer t = new Timer();\r
- for (int i=0; i<count; i++)\r
- IsKeyword4(id);\r
- Console.WriteLine(t.Check()); \r
- }\r
-\r
- {\r
- Console.Write("Array.BinarySearch ");\r
- Timer t = new Timer();\r
- for (int i=0; i<count; i++)\r
- IsKeyword5(id);\r
- Console.WriteLine(t.Check()); \r
- }\r
- }\r
- }\r
-}\r
-\r
-// Crude timing utility ----------------------------------------\r
- \r
-public class Timer {\r
- private DateTime start;\r
-\r
- public Timer() {\r
- start = DateTime.Now;\r
- }\r
-\r
- public double Check() {\r
- TimeSpan dur = DateTime.Now - start;\r
- return dur.TotalSeconds;\r
- }\r
-}\r
-\r
-}\r
+/*
+ Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
+// C5 example: Keyword recognition 2004-12-20
+
+// Compile with
+// csc /r:C5.dll KeywordRecognition.cs
+
+using System;
+using C5;
+using SCG = System.Collections.Generic;
+
+namespace KeywordRecognition {
+
+class KeywordRecognition {
+ // Array of 77 keywords:
+
+ static readonly String[] keywordArray =
+ { "abstract", "as", "base", "bool", "break", "byte", "case", "catch",
+ "char", "checked", "class", "const", "continue", "decimal", "default",
+ "delegate", "do", "double", "else", "enum", "event", "explicit",
+ "extern", "false", "finally", "fixed", "float", "for", "foreach",
+ "goto", "if", "implicit", "in", "int", "interface", "internal", "is",
+ "lock", "long", "namespace", "new", "null", "object", "operator",
+ "out", "override", "params", "private", "protected", "public",
+ "readonly", "ref", "return", "sbyte", "sealed", "short", "sizeof",
+ "stackalloc", "static", "string", "struct", "switch", "this", "throw",
+ "true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe",
+ "ushort", "using", "virtual", "void", "volatile", "while" };
+
+ private static readonly ICollection<String> kw1;
+
+ private static readonly ICollection<String> kw2;
+
+ private static readonly ICollection<String> kw3;
+
+ private static readonly SCG.IDictionary<String,bool> kw4 =
+ new SCG.Dictionary<String,bool>();
+
+
+ class SC : SCG.IComparer<string>
+ {
+ public int Compare(string a, string b)
+ {
+ return StringComparer.InvariantCulture.Compare(a,b);
+ }
+ }
+
+ class SH : SCG.IEqualityComparer<string>
+ {
+ public int GetHashCode(string item)
+ {
+ return item.GetHashCode();
+ }
+
+ public bool Equals(string i1, string i2)
+ {
+ return i1 == null ? i2 == null : i1.Equals(i2,StringComparison.InvariantCulture);
+ }
+ }
+
+ static KeywordRecognition() {
+ kw1 = new HashSet<String>();
+ kw1.AddAll<string>(keywordArray);
+ kw2 = new TreeSet<String>(new SC());
+ kw2.AddAll<string>(keywordArray);
+ kw3 = new SortedArray<String>(new SC());
+ kw3.AddAll<string>(keywordArray);
+ kw4 = new SCG.Dictionary<String,bool>();
+ foreach (String keyword in keywordArray)
+ kw4.Add(keyword, false);
+ }
+
+ public static bool IsKeyword1(String s) {
+ return kw1.Contains(s);
+ }
+
+ public static bool IsKeyword2(String s) {
+ return kw2.Contains(s);
+ }
+
+ public static bool IsKeyword3(String s) {
+ return kw3.Contains(s);
+ }
+
+ public static bool IsKeyword4(String s) {
+ return kw4.ContainsKey(s);
+ }
+
+ public static bool IsKeyword5(String s) {
+ return Array.BinarySearch(keywordArray, s) >= 0;
+ }
+
+ public static void Main(String[] args) {
+ if (args.Length != 2)
+ Console.WriteLine("Usage: KeywordRecognition <iterations> <word>\n");
+ else {
+ int count = int.Parse(args[0]);
+ String id = args[1];
+
+ {
+ Console.Write("HashSet.Contains ");
+ Timer t = new Timer();
+ for (int i=0; i<count; i++)
+ IsKeyword1(id);
+ Console.WriteLine(t.Check());
+ }
+
+ {
+ Console.Write("TreeSet.Contains ");
+ Timer t = new Timer();
+ for (int i=0; i<count; i++)
+ IsKeyword2(id);
+ Console.WriteLine(t.Check());
+ }
+
+ {
+ Console.Write("SortedArray.Contains ");
+ Timer t = new Timer();
+ for (int i=0; i<count; i++)
+ IsKeyword3(id);
+ Console.WriteLine(t.Check());
+ }
+
+ {
+ Console.Write("SCG.Dictionary.ContainsKey ");
+ Timer t = new Timer();
+ for (int i=0; i<count; i++)
+ IsKeyword4(id);
+ Console.WriteLine(t.Check());
+ }
+
+ {
+ Console.Write("Array.BinarySearch ");
+ Timer t = new Timer();
+ for (int i=0; i<count; i++)
+ IsKeyword5(id);
+ Console.WriteLine(t.Check());
+ }
+ }
+ }
+}
+
+// Crude timing utility ----------------------------------------
+
+public class Timer {
+ private DateTime start;
+
+ public Timer() {
+ start = DateTime.Now;
+ }
+
+ public double Check() {
+ TimeSpan dur = DateTime.Now - start;
+ return dur.TotalSeconds;
+ }
+}
+
+}