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: collections of collections 2004-11-16
\r
25 // csc /r:C5.dll CollectionCollection.cs
\r
29 using SCG = System.Collections.Generic;
\r
31 namespace CollectionCollection
\r
35 private static IList<int> col1 = new LinkedList<int>(),
\r
36 col2 = new LinkedList<int>(), col3 = new LinkedList<int>();
\r
38 public static void Main(String[] args) {
\r
39 ListEqualityComparers();
\r
44 public static void ListEqualityComparers() {
\r
45 col1.AddAll<int>(new int[] { 7, 9, 13 });
\r
46 col2.AddAll<int>(new int[] { 7, 9, 13 });
\r
47 col3.AddAll<int>(new int[] { 9, 7, 13 });
\r
49 // Default equality and hasher == sequenced equality and hasher
\r
50 HashSet<IList<int>> hs1 = new HashSet<IList<int>>();
\r
51 Equalities("Default equality (sequenced equality)", hs1.EqualityComparer);
\r
52 hs1.Add(col1); hs1.Add(col2); hs1.Add(col3);
\r
53 Console.WriteLine("hs1.Count = {0}", hs1.Count);
\r
55 // Sequenced equality and hasher
\r
56 SCG.IEqualityComparer<IList<int>> seqEqualityComparer
\r
57 = SequencedCollectionEqualityComparer<IList<int>, int>.Default;
\r
58 HashSet<IList<int>> hs2 = new HashSet<IList<int>>(seqEqualityComparer);
\r
59 Equalities("Sequenced equality", hs2.EqualityComparer);
\r
60 hs2.Add(col1); hs2.Add(col2); hs2.Add(col3);
\r
61 Console.WriteLine("hs2.Count = {0}", hs2.Count);
\r
63 // Unsequenced equality and hasher
\r
64 SCG.IEqualityComparer<IList<int>> unseqEqualityComparer
\r
65 = UnsequencedCollectionEqualityComparer<IList<int>, int>.Default;
\r
66 HashSet<IList<int>> hs3 = new HashSet<IList<int>>(unseqEqualityComparer);
\r
67 Equalities("Unsequenced equality", hs3.EqualityComparer);
\r
68 hs3.Add(col1); hs3.Add(col2); hs3.Add(col3);
\r
69 Console.WriteLine("hs3.Count = {0}", hs3.Count);
\r
71 // Reference equality and hasher
\r
72 SCG.IEqualityComparer<IList<int>> refEqEqualityComparer
\r
73 = ReferenceEqualityComparer<IList<int>>.Default;
\r
74 HashSet<IList<int>> hs4 = new HashSet<IList<int>>(refEqEqualityComparer);
\r
75 Equalities("Reference equality", hs4.EqualityComparer);
\r
76 hs4.Add(col1); hs4.Add(col2); hs4.Add(col3);
\r
77 Console.WriteLine("hs4.Count = {0}", hs4.Count);
\r
80 public static void Equalities(String msg, SCG.IEqualityComparer<IList<int>> equalityComparer)
\r
82 Console.WriteLine("\n{0}:", msg);
\r
83 Console.Write("Equals(col1,col2)={0,-5}; ", equalityComparer.Equals(col1, col2));
\r
84 Console.Write("Equals(col1,col3)={0,-5}; ", equalityComparer.Equals(col1, col3));
\r
85 Console.WriteLine("Equals(col2,col3)={0,-5}", equalityComparer.Equals(col2, col3));
\r
88 public static void IntSetSet() {
\r
89 ICollection<ISequenced<int>> outer = new HashSet<ISequenced<int>>();
\r
90 int[] ss = { 2, 3, 5, 7 };
\r
91 TreeSet<int> inner = new TreeSet<int>();
\r
92 outer.Add(inner.Snapshot());
\r
93 foreach (int i in ss) {
\r
95 outer.Add(inner.Snapshot());
\r
97 foreach (ISequenced<int> s in outer) {
\r
99 s.Apply(delegate(int x) { sum += x; });
\r
100 Console.WriteLine("Set has {0} elements and sum {1}", s.Count, sum);
\r
104 public static void CharBagSet() {
\r
106 @"three sorted streams aligned by leading masters are stored
\r
107 there; an integral triangle ends, and stable tables keep;
\r
108 being alert, they later reread the logarithm, peek at the
\r
109 recent center, then begin to send their reader algorithm.";
\r
110 String[] words = text.Split(' ', '\n', '\r', ';', ',', '.');
\r
111 ICollection<ICollection<char>> anagrams
\r
112 = new HashSet<ICollection<char>>();
\r
114 foreach (String word in words) {
\r
117 HashBag<char> anagram = new HashBag<char>();
\r
118 anagram.AddAll<char>(word.ToCharArray());
\r
119 anagrams.Add(anagram);
\r
122 Console.WriteLine("Found {0} anagrams", count - anagrams.Count);
\r