Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / libs / c5 / UserGuideExamples / DistinctBag.cs @ 3

1
/*
2
 Copyright (c) 2003-2007 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: A bag collection in which items that compare equal may
23
// be distinct objects or values; that is, a collection with
24
// AllowsDuplicates=true and DuplicatesByCounting=false.
25

    
26
// The collection may contain distinct objects x1 and x2 for which the
27
// equality comparer (or comparer) says that x1 and x2 are equal.
28
// This can be implemented by a dictionary whose keys are the objects
29
// themselves (using the given equality comparer or comparer), and
30
// whose values are hashsets that use reference equality for equality
31
// comparison.
32

    
33
// Such a bag-with-actual-duplicates can be used to index objects, for
34
// instance Person objects, by name and birthdate.  Several Person 
35
// objects may have the same name, or the same birthdate, or even the 
36
// same name and birthdate, yet be distinct Person objects.
37

    
38
// How much of this can be implemented on top of CollectionBase<T>?
39

    
40
// What should the meaning of Contains(x) be?
41

    
42
// Compile with 
43
//   csc /r:C5.dll DistinctBag.cs 
44

    
45
using System;                           // Console
46
using System.Text;                      // StringBuilder
47
using C5; 
48
using SCG = System.Collections.Generic;
49

    
50
namespace DistinctBag
51
{
52
  static class DistinctBagMain
53
  {
54
    static void Main(String[] args)
55
    {
56
      DistinctHashBag<Person> nameColl 
57
        = new DistinctHashBag<Person>(new Person.NameEqualityComparer());
58
      Person p1 = new Person("Peter", 19620625), 
59
        p2 = new Person("Carsten", 19640627), 
60
        p3 = new Person("Carsten", 19640628);
61
      nameColl.Add(p1);      
62
      nameColl.Add(p2);
63
      nameColl.Add(p3);
64
      Console.WriteLine("nameColl = {0}", nameColl);
65
    }
66
  }
67

    
68
  public class DistinctHashBag<T> where T : class { 
69
    private HashDictionary<T, HashSet<T>> dict;
70

    
71
    public DistinctHashBag(SCG.IEqualityComparer<T> eqc) {
72
      dict = new HashDictionary<T,HashSet<T>>(eqc);
73
    }
74

    
75
    public DistinctHashBag() : this(EqualityComparer<T>.Default) {
76
    }
77
    
78
    public bool Add(T item) { 
79
      if (!dict.Contains(item)) 
80
        dict.Add(item, new HashSet<T>(ReferenceEqualityComparer<T>.Default));
81
      return dict[item].Add(item);
82
    }
83

    
84
    public bool Remove(T item) {
85
      bool result = false;
86
      if (dict.Contains(item)) {
87
        HashSet<T> set = dict[item];
88
        result = set.Remove(item);
89
        if (set.IsEmpty) 
90
          dict.Remove(item);
91
      }
92
      return result;
93
    } 
94

    
95
    public SCG.IEnumerator<T> GetEnumerator() { 
96
      foreach (KeyValuePair<T,HashSet<T>> entry in dict) 
97
	foreach (T item in entry.Value)
98
	  yield return item;
99
    }
100

    
101
    public override String ToString() {
102
      StringBuilder sb = new StringBuilder();
103
      foreach (T item in this) 
104
        sb.Append(item).Append(" ");
105
      return sb.ToString();
106
    }
107
  }
108

    
109
  public class Person {
110
    String name;
111
    int date;
112

    
113
    public Person(String name, int date) {
114
      this.name = name;
115
      this.date = date;
116
    }
117

    
118
    public class NameEqualityComparer : SCG.IEqualityComparer<Person> {
119
      public bool Equals(Person p1, Person p2) {
120
        return p1.name == p2.name;
121
      }
122
      public int GetHashCode(Person p) { 
123
        return p.name.GetHashCode();
124
      }
125
    }
126

    
127
    public class DateComparer : SCG.IComparer<Person> {
128
      public int Compare(Person p1, Person p2) { 
129
        return p1.date.CompareTo(p2.date);
130
      }
131
    }
132

    
133
    public override String ToString() {
134
      return name + " (" + date + ")";
135
    }
136
  }
137
}