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