Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / libs / c5 / C5 / trees / RedBlackTreeDictionary.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 SCG = System.Collections.Generic;
24

    
25
namespace C5
26
{
27
  /// <summary>
28
  /// A sorted generic dictionary based on a red-black tree set.
29
  /// </summary>
30
  [Serializable]
31
  public class TreeDictionary<K, V> : SortedDictionaryBase<K, V>, IDictionary<K, V>, ISortedDictionary<K, V>
32
  {
33

    
34
    #region Constructors
35

    
36
    /// <summary>
37
    /// Create a red-black tree dictionary using the natural comparer for keys.
38
    /// <exception cref="ArgumentException"/> if the key type K is not comparable.
39
    /// </summary>
40
    public TreeDictionary() : this(Comparer<K>.Default, EqualityComparer<K>.Default) { }
41

    
42
    /// <summary>
43
    /// Create a red-black tree dictionary using an external comparer for keys.
44
    /// </summary>
45
    /// <param name="comparer">The external comparer</param>
46
    public TreeDictionary(SCG.IComparer<K> comparer) : this(comparer, new ComparerZeroHashCodeEqualityComparer<K>(comparer)) { }
47

    
48
    TreeDictionary(SCG.IComparer<K> comparer, SCG.IEqualityComparer<K> equalityComparer) : base(comparer,equalityComparer)
49
    {
50
      pairs = sortedpairs = new TreeSet<KeyValuePair<K, V>>(new KeyValuePairComparer<K, V>(comparer));
51
    }
52

    
53
    #endregion
54

    
55
    //TODO: put in interface
56
    /// <summary>
57
    /// Make a snapshot of the current state of this dictionary
58
    /// </summary>
59
    /// <returns>The snapshot</returns>
60
    [Tested]
61
    public SCG.IEnumerable<KeyValuePair<K, V>> Snapshot()
62
    {
63
      TreeDictionary<K, V> res = (TreeDictionary<K, V>)MemberwiseClone();
64

    
65
      res.pairs = (TreeSet<KeyValuePair<K, V>>)((TreeSet<KeyValuePair<K, V>>)sortedpairs).Snapshot();
66
      return res;
67
    }
68

    
69
    /// <summary>
70
    /// 
71
    /// </summary>
72
    /// <returns></returns>
73
    public override object Clone()
74
    {
75
      TreeDictionary<K, V> clone = new TreeDictionary<K, V>(Comparer, EqualityComparer);
76
      clone.sortedpairs.AddSorted(sortedpairs);
77
      return clone;
78
    }
79

    
80
  }
81
}