* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / Mono.C5 / UserGuideExamples / HashCodes.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: hash codes, good and bad 2005-02-28\r
23 \r
24 // Compile with \r
25 //   csc /r:C5.dll HashCodes.cs \r
26 \r
27 using System;\r
28 using C5;\r
29 using SCG = System.Collections.Generic;\r
30 \r
31 namespace MyHashCodesTest {\r
32   class MyTest {\r
33     public static void Main(String[] args) {\r
34       int count = int.Parse(args[0]); \r
35       {\r
36         Console.Write("Good hash function: ");\r
37         Timer t = new Timer();\r
38         HashSet<int> good \r
39           = MakeRandom(count, new GoodIntegerEqualityComparer());\r
40         Console.WriteLine("({0} sec, {1} items)", t.Check(), good.Count);\r
41         ISortedDictionary<int,int> bcd = good.BucketCostDistribution();\r
42         foreach (KeyValuePair<int,int> entry in bcd) \r
43           Console.WriteLine("{0,7} bucket(s) with cost {1,5}", \r
44                             entry.Value, entry.Key);\r
45       }\r
46       {\r
47         Console.Write("Bad hash function:  ");\r
48         Timer t = new Timer();\r
49         HashSet<int> bad = MakeRandom(count, new BadIntegerEqualityComparer());\r
50         Console.WriteLine("({0} sec, {1} items)", t.Check(), bad.Count);\r
51         ISortedDictionary<int,int> bcd = bad.BucketCostDistribution();\r
52         foreach (KeyValuePair<int,int> entry in bcd) \r
53           Console.WriteLine("{0,7} bucket(s) with cost {1,5}", \r
54                             entry.Value, entry.Key);\r
55       }\r
56     }\r
57 \r
58     private static readonly C5Random rnd = new C5Random();\r
59     \r
60     public static HashSet<int> MakeRandom(int count, \r
61                                           SCG.IEqualityComparer<int> eqc) {\r
62       HashSet<int> res;\r
63       if (eqc == null) \r
64         res = new HashSet<int>();\r
65       else\r
66         res = new HashSet<int>(eqc);\r
67       for (int i=0; i<count; i++)\r
68         res.Add(rnd.Next(1000000));    \r
69       return res;\r
70     }\r
71 \r
72     private class BadIntegerEqualityComparer : SCG.IEqualityComparer<int> {\r
73       public bool Equals(int i1, int i2) {\r
74         return i1 == i2;\r
75       }\r
76       public int GetHashCode(int i) {\r
77         return i % 7;\r
78       }\r
79     }\r
80 \r
81     private class GoodIntegerEqualityComparer : SCG.IEqualityComparer<int> {\r
82       public bool Equals(int i1, int i2) {\r
83         return i1 == i2;\r
84       }\r
85       public int GetHashCode(int i) {\r
86         return i;\r
87       }\r
88     }\r
89   }\r
90 \r
91   // Crude timing utility\r
92    \r
93   public class Timer {\r
94     private DateTime start;\r
95     \r
96     public Timer() {\r
97       start = DateTime.Now;\r
98     }\r
99     \r
100     public double Check() {\r
101       TimeSpan dur = DateTime.Now - start;\r
102       return dur.TotalSeconds;\r
103     }\r
104   }\r
105 }\r
106 \r