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:
10 The above copyright notice and this permission notice shall be included in
11 all copies or substantial portions of the Software.
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
22 // C5 example: anagrams 2004-12-08
25 // csc /r:C5.dll CollectionSanity.cs
29 using SCG
= System
.Collections
.Generic
;
31 namespace CollectionSanity
{
32 class CollectionSanity
{
33 public static void Main(String
[] args
) {
34 IList
<int> col1
= new LinkedList
<int>(),
35 col2
= new LinkedList
<int>(), col3
= new LinkedList
<int>();
36 col1
.AddAll
<int>(new int[] { 7, 9, 13 }
);
37 col2
.AddAll
<int>(new int[] { 7, 9, 13 }
);
38 col3
.AddAll
<int>(new int[] { 9, 7, 13 }
);
40 HashSet
<IList
<int>> hs1
= new HashSet
<IList
<int>>();
41 hs1
.Add(col1
); hs1
.Add(col2
); hs1
.Add(col3
);
42 Console
.WriteLine("hs1 is sane: {0}", EqualityComparerSanity
<int,IList
<int>>(hs1
));
45 // When colls is a collection of collections, this method checks
46 // that all `inner' collections use the exact same equalityComparer. Note
47 // that two equalityComparer objects may be functionally (extensionally)
48 // identical, yet be distinct objects. However, if the equalityComparers
49 // were obtained from EqualityComparer<T>.Default, there will be at most one
50 // equalityComparer for each type T.
52 public static bool EqualityComparerSanity
<T
,U
>(ICollectionValue
<U
> colls
)
53 where U
: IExtensible
<T
>
55 SCG
.IEqualityComparer
<T
> equalityComparer
= null;
56 foreach (IExtensible
<T
> coll
in colls
) {
57 if (equalityComparer
== null)
58 equalityComparer
= coll
.EqualityComparer
;
59 if (equalityComparer
!= coll
.EqualityComparer
)