Bringing C5 1.0 into the main branch.
[mono.git] / mcs / class / Mono.C5 / current / UserGuideExamples / CollectionSanity.cs
1 /*\r
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
9  \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
12  \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
19  SOFTWARE.\r
20 */\r
21 \r
22 // C5 example: anagrams 2004-12-08\r
23 \r
24 // Compile with \r
25 //   csc /r:C5.dll CollectionSanity.cs \r
26 \r
27 using System;\r
28 using C5;\r
29 using SCG = System.Collections.Generic;\r
30 \r
31 namespace CollectionSanity {\r
32   class CollectionSanity {\r
33     public static void Main(String[] args) {\r
34       IList<int> col1 = new LinkedList<int>(),\r
35         col2 = new LinkedList<int>(), col3 = new LinkedList<int>();\r
36       col1.AddAll<int>(new int[] { 7, 9, 13 });\r
37       col2.AddAll<int>(new int[] { 7, 9, 13 });\r
38       col3.AddAll<int>(new int[] { 9, 7, 13 });\r
39 \r
40       HashSet<IList<int>> hs1 = new HashSet<IList<int>>();\r
41       hs1.Add(col1); hs1.Add(col2); hs1.Add(col3);\r
42       Console.WriteLine("hs1 is sane: {0}", EqualityComparerSanity<int,IList<int>>(hs1));\r
43     }\r
44     \r
45     // When colls is a collection of collections, this method checks\r
46     // that all `inner' collections use the exact same equalityComparer.  Note\r
47     // that two equalityComparer objects may be functionally (extensionally)\r
48     // identical, yet be distinct objects.  However, if the equalityComparers\r
49     // were obtained from EqualityComparer<T>.Default, there will be at most one\r
50     // equalityComparer for each type T.\r
51 \r
52     public static bool EqualityComparerSanity<T,U>(ICollectionValue<U> colls) \r
53       where U : IExtensible<T>\r
54     {\r
55       SCG.IEqualityComparer<T> equalityComparer = null;\r
56       foreach (IExtensible<T> coll in colls) {\r
57         if (equalityComparer == null) \r
58           equalityComparer = coll.EqualityComparer;\r
59         if (equalityComparer != coll.EqualityComparer) \r
60           return false;\r
61       }\r
62       return true;\r
63     }\r
64   }\r
65 }\r