Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / libs / c5 / C5 / hashing / HashDictionary.cs @ 3

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
using System;
23
using System.Diagnostics;
24
using SCG = System.Collections.Generic;
25

    
26
namespace C5
27
{
28
  /// <summary>
29
  /// A generic dictionary class based on a hash set class <see cref="T:C5.HashSet`1"/>. 
30
  /// </summary>
31
  [Serializable]
32
  public class HashDictionary<K, V> : DictionaryBase<K, V>, IDictionary<K, V>
33
  {
34
    /// <summary>
35
    /// Create a hash dictionary using a default equalityComparer for the keys.
36
    /// Initial capacity of internal table will be 16 entries and threshold for 
37
    /// expansion is 66% fill.
38
    /// </summary>
39
    public HashDictionary() : this(EqualityComparer<K>.Default) { }
40

    
41
    /// <summary>
42
    /// Create a hash dictionary using a custom equalityComparer for the keys.
43
    /// Initial capacity of internal table will be 16 entries and threshold for 
44
    /// expansion is 66% fill.
45
    /// </summary>
46
    /// <param name="keyequalityComparer">The external key equalityComparer</param>
47
    public HashDictionary(SCG.IEqualityComparer<K> keyequalityComparer) : base(keyequalityComparer)
48
    {
49
      pairs = new HashSet<KeyValuePair<K, V>>(new KeyValuePairEqualityComparer<K, V>(keyequalityComparer));
50
    }
51

    
52
    /// <summary>
53
    /// Create a hash dictionary using a custom equalityComparer and prescribing the 
54
    /// initial size of the dictionary and a non-default threshold for internal table expansion.
55
    /// </summary>
56
    /// <param name="capacity">The initial capacity. Will be rounded upwards to nearest
57
    /// power of 2, at least 16.</param>
58
    /// <param name="fill">The expansion threshold. Must be between 10% and 90%.</param>
59
    /// <param name="keyequalityComparer">The external key equalityComparer</param>
60
    public HashDictionary(int capacity, double fill, SCG.IEqualityComparer<K> keyequalityComparer): base(keyequalityComparer)
61
    {
62
      pairs = new HashSet<KeyValuePair<K, V>>(capacity, fill, new KeyValuePairEqualityComparer<K, V>(keyequalityComparer));
63
    }
64

    
65
    /// <summary>
66
    /// 
67
    /// </summary>
68
    /// <returns></returns>
69
    public override object Clone()
70
    {
71
      HashDictionary<K, V> clone = new HashDictionary<K, V>(EqualityComparer);
72
      clone.pairs.AddAll(pairs);
73
      return clone;
74
    }
75

    
76
  }
77
}