1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
// **********************************************************************
//
// Copyright (c) 2003-2017 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
namespace Ice
{
public class CollectionComparer
{
//
// Try to compare two collections efficiently, by doing a reference
// and count comparison. If equality or inequality can be determined
// this way, 'result' contains the outcome of the comparison and the
// return value is true. Otherwise, if equality or inequality cannot
// be determined this way, 'result' and return value are both false.
//
private static bool cheapComparison(System.Collections.ICollection c1,
System.Collections.ICollection c2,
out bool result)
{
if(ReferenceEquals(c1, c2))
{
result = true;
return true; // Equal references means the collections are equal.
}
if(c1 == null || c2 == null)
{
result = false;
return true; // The references are not equal and one of them is null, so c1 and c2 are not equal.
}
if(c1.Count != c2.Count)
{
result = false;
return true; // Different number of elements, so c1 and c2 are not equal.
}
if(c1.Count == 0)
{
result = true;
return true; // Same number of elements, both zero, so c1 and c2 are equal.
}
result = false; // Couldn't get a result cheaply.
return false; // c1 and c2 may still be equal, but we have to compare elements to find out.
}
//
// Compare two dictionaries for value equality (as implemented by the Equals() method of its elements).
//
public static bool Equals(System.Collections.IDictionary d1, System.Collections.IDictionary d2)
{
try
{
bool result;
if(cheapComparison(d1, d2, out result))
{
return result;
}
System.Collections.ICollection keys1 = d1.Keys;
foreach(object k in keys1)
{
if(d2.Contains(k))
{
object v1 = d1[k];
object v2 = d2[k];
if(v1 == null)
{
if(v2 != null)
{
return false;
}
}
else
{
if(!v1.Equals(v2))
{
return false;
}
}
}
else
{
return false;
}
}
}
catch(System.Exception)
{
return false;
}
return true;
}
//
// Compare two collections for equality (as implemented by the Equals() method of its elements).
// Order is significant.
//
public static bool Equals(System.Collections.ICollection c1, System.Collections.ICollection c2)
{
try
{
bool result;
if(cheapComparison(c1, c2, out result))
{
return result;
}
System.Collections.IEnumerator e = c2.GetEnumerator();
foreach(object o in c1)
{
e.MoveNext();
if(!Equals(o, e.Current))
{
return false;
}
}
return true;
}
catch(System.Exception)
{
return false;
}
}
//
// Compare two collections for equality (as implemented by the Equals() method of its elements).
// Order is significant.
//
public static bool Equals(System.Collections.IEnumerable c1, System.Collections.IEnumerable c2)
{
try
{
if(ReferenceEquals(c1, c2))
{
return true; // Equal references means the collections are equal.
}
if(c1 == null || c2 == null)
{
return false; // The references are not equal and one of them is null, so c1 and c2 are not equal.
}
System.Collections.IEnumerator e1 = c1.GetEnumerator();
System.Collections.IEnumerator e2 = c2.GetEnumerator();
while(e1.MoveNext())
{
if(!e2.MoveNext())
{
return false; // c2 has fewer elements than c1.
}
if(e1.Current == null)
{
if(e2.Current != null)
{
return false;
}
}
else
{
if(!e1.Current.Equals(e2.Current))
{
return false;
}
}
}
if(e2.MoveNext())
{
return false; // c2 has more elements than c1.
}
return true;
}
catch(System.Exception)
{
return false;
}
}
}
}
|