Skip to content

Commit f9455c0

Browse files
Simplify implementation of old ArrayEquals
All its current usages are done on type where using the default comparer will not yield different results, excepted in some cases better performances.
1 parent 81dc1d2 commit f9455c0

File tree

1 file changed

+8
-28
lines changed

1 file changed

+8
-28
lines changed

src/NHibernate/Util/ArrayHelper.cs

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public static int CountTrue(bool[] array)
179179

180180
public static bool ArrayEquals<T>(T[] a, T[] b)
181181
{
182-
return ArrayComparer<T>.DefaultWithoutComparer.Equals(a, b);
182+
return ArrayComparer<T>.Default.Equals(a, b);
183183
}
184184

185185
public static bool ArrayEquals(byte[] a, byte[] b)
@@ -197,21 +197,20 @@ public static bool ArrayEquals(byte[] a, byte[] b)
197197
/// <returns></returns>
198198
public static int ArrayGetHashCode<T>(T[] array)
199199
{
200-
return ArrayComparer<T>.DefaultWithoutComparer.GetHashCode(array);
200+
return ArrayComparer<T>.Default.GetHashCode(array);
201201
}
202202

203203
internal class ArrayComparer<T> : IEqualityComparer<T[]>
204204
{
205205
private readonly IEqualityComparer<T> _elementComparer;
206206

207207
internal static ArrayComparer<T> Default { get; } = new ArrayComparer<T>();
208-
internal static ArrayComparer<T> DefaultWithoutComparer { get; } = new ArrayComparer<T>(null);
209208

210209
internal ArrayComparer() : this(EqualityComparer<T>.Default) { }
211210

212211
internal ArrayComparer(IEqualityComparer<T> elementComparer)
213212
{
214-
_elementComparer = elementComparer;
213+
_elementComparer = elementComparer ?? throw new ArgumentNullException(nameof(elementComparer));
215214
}
216215

217216
public bool Equals(T[] a, T[] b)
@@ -225,21 +224,10 @@ public bool Equals(T[] a, T[] b)
225224
if (a.Length != b.Length)
226225
return false;
227226

228-
if (_elementComparer != null)
227+
for (var i = 0; i < a.Length; i++)
229228
{
230-
for (var i = 0; i < a.Length; i++)
231-
{
232-
if (!_elementComparer.Equals(a[i], b[i]))
233-
return false;
234-
}
235-
}
236-
else
237-
{
238-
for (var i = 0; i < a.Length; i++)
239-
{
240-
if (!Equals(a[i], b[i]))
241-
return false;
242-
}
229+
if (!_elementComparer.Equals(a[i], b[i]))
230+
return false;
243231
}
244232

245233
return true;
@@ -252,16 +240,8 @@ public int GetHashCode(T[] array)
252240

253241
var hc = array.Length;
254242

255-
if (_elementComparer != null)
256-
{
257-
foreach (var e in array)
258-
hc = unchecked(hc * 31 + _elementComparer.GetHashCode(e));
259-
}
260-
else
261-
{
262-
foreach (var e in array)
263-
hc = unchecked(hc * 31 + (e?.GetHashCode() ?? 0));
264-
}
243+
foreach (var e in array)
244+
hc = unchecked(hc * 31 + _elementComparer.GetHashCode(e));
265245

266246
return hc;
267247
}

0 commit comments

Comments
 (0)