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: hash codes, good and bad 2005-02-28
\r
25 // csc /r:C5.dll HashCodes.cs
\r
29 using SCG = System.Collections.Generic;
\r
31 namespace MyHashCodesTest {
\r
33 public static void Main(String[] args) {
\r
34 int count = int.Parse(args[0]);
\r
36 Console.Write("Good hash function: ");
\r
37 Timer t = new Timer();
\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
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
58 private static readonly C5Random rnd = new C5Random();
\r
60 public static HashSet<int> MakeRandom(int count,
\r
61 SCG.IEqualityComparer<int> eqc) {
\r
64 res = new HashSet<int>();
\r
66 res = new HashSet<int>(eqc);
\r
67 for (int i=0; i<count; i++)
\r
68 res.Add(rnd.Next(1000000));
\r
72 private class BadIntegerEqualityComparer : SCG.IEqualityComparer<int> {
\r
73 public bool Equals(int i1, int i2) {
\r
76 public int GetHashCode(int i) {
\r
81 private class GoodIntegerEqualityComparer : SCG.IEqualityComparer<int> {
\r
82 public bool Equals(int i1, int i2) {
\r
85 public int GetHashCode(int i) {
\r
91 // Crude timing utility
\r
93 public class Timer {
\r
94 private DateTime start;
\r
97 start = DateTime.Now;
\r
100 public double Check() {
\r
101 TimeSpan dur = DateTime.Now - start;
\r
102 return dur.TotalSeconds;
\r