[asp.net] Added code to handle local resources copying in the test suite
[mono.git] / mcs / class / Mono.C5 / UserGuideExamples / CollectionCollection.cs
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 // C5 example: collections of collections 2004-11-16
23
24 // Compile with 
25 //   csc /r:C5.dll CollectionCollection.cs 
26
27 using System;
28 using C5;
29 using SCG = System.Collections.Generic;
30
31 namespace CollectionCollection
32 {
33   class MyTest
34   {
35     private static IList<int> col1 = new LinkedList<int>(),
36       col2 = new LinkedList<int>(), col3 = new LinkedList<int>();
37
38     public static void Main(String[] args) {
39       ListEqualityComparers();
40       IntSetSet();
41       CharBagSet();
42     }
43       
44     public static void ListEqualityComparers() {
45       col1.AddAll<int>(new int[] { 7, 9, 13 });
46       col2.AddAll<int>(new int[] { 7, 9, 13 });
47       col3.AddAll<int>(new int[] { 9, 7, 13 });
48
49       // Default equality and hasher == sequenced equality and hasher
50       HashSet<IList<int>> hs1 = new HashSet<IList<int>>();
51       Equalities("Default equality (sequenced equality)", hs1.EqualityComparer);
52       hs1.Add(col1); hs1.Add(col2); hs1.Add(col3);
53       Console.WriteLine("hs1.Count = {0}", hs1.Count);
54
55       // Sequenced equality and hasher 
56       SCG.IEqualityComparer<IList<int>> seqEqualityComparer
57         = SequencedCollectionEqualityComparer<IList<int>, int>.Default;
58       HashSet<IList<int>> hs2 = new HashSet<IList<int>>(seqEqualityComparer);
59       Equalities("Sequenced equality", hs2.EqualityComparer);
60       hs2.Add(col1); hs2.Add(col2); hs2.Add(col3);
61       Console.WriteLine("hs2.Count = {0}", hs2.Count);
62
63       // Unsequenced equality and hasher
64       SCG.IEqualityComparer<IList<int>> unseqEqualityComparer
65         = UnsequencedCollectionEqualityComparer<IList<int>, int>.Default;
66       HashSet<IList<int>> hs3 = new HashSet<IList<int>>(unseqEqualityComparer);
67       Equalities("Unsequenced equality", hs3.EqualityComparer);
68       hs3.Add(col1); hs3.Add(col2); hs3.Add(col3);
69       Console.WriteLine("hs3.Count = {0}", hs3.Count);
70
71       // Reference equality and hasher
72       SCG.IEqualityComparer<IList<int>> refEqEqualityComparer 
73         = ReferenceEqualityComparer<IList<int>>.Default;
74       HashSet<IList<int>> hs4 = new HashSet<IList<int>>(refEqEqualityComparer);
75       Equalities("Reference equality", hs4.EqualityComparer);
76       hs4.Add(col1); hs4.Add(col2); hs4.Add(col3);
77       Console.WriteLine("hs4.Count = {0}", hs4.Count);
78     }
79
80     public static void Equalities(String msg, SCG.IEqualityComparer<IList<int>> equalityComparer)
81     {
82       Console.WriteLine("\n{0}:", msg);
83       Console.Write("Equals(col1,col2)={0,-5}; ", equalityComparer.Equals(col1, col2));
84       Console.Write("Equals(col1,col3)={0,-5}; ", equalityComparer.Equals(col1, col3));
85       Console.WriteLine("Equals(col2,col3)={0,-5}", equalityComparer.Equals(col2, col3));
86     }
87
88     public static void IntSetSet() {
89       ICollection<ISequenced<int>> outer = new HashSet<ISequenced<int>>();
90       int[] ss = { 2, 3, 5, 7 };
91       TreeSet<int> inner = new TreeSet<int>();
92       outer.Add(inner.Snapshot());
93       foreach (int i in ss) { 
94         inner.Add(i);
95         outer.Add(inner.Snapshot());
96       }
97       foreach (ISequenced<int> s in outer) {
98         int sum = 0;
99         s.Apply(delegate(int x) { sum += x; });
100         Console.WriteLine("Set has {0} elements and sum {1}", s.Count, sum);
101       }
102     }
103
104     public static void CharBagSet() { 
105       String text = 
106         @"three sorted streams aligned by leading masters are stored 
107           there; an integral triangle ends, and stable tables keep;
108           being alert, they later reread the logarithm, peek at the
109           recent center, then begin to send their reader algorithm.";
110       String[] words = text.Split(' ', '\n', '\r', ';', ',', '.');
111       ICollection<ICollection<char>> anagrams 
112         = new HashSet<ICollection<char>>();
113       int count = 0;
114       foreach (String word in words) {
115         if (word != "") {
116           count++;
117           HashBag<char> anagram = new HashBag<char>();
118           anagram.AddAll<char>(word.ToCharArray());
119           anagrams.Add(anagram);
120         }
121       }
122       Console.WriteLine("Found {0} anagrams", count - anagrams.Count);
123     }
124   }
125 }