-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathSequenceEqual.cs
More file actions
71 lines (59 loc) · 2.38 KB
/
Copy pathSequenceEqual.cs
File metadata and controls
71 lines (59 loc) · 2.38 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
namespace System.Linq
{
public static partial class Enumerable
{
public static bool SequenceEqual<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second) =>
SequenceEqual(first, second, null);
public static bool SequenceEqual<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource>? comparer)
{
if (first == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.first);
}
if (second == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.second);
}
if (first is ICollection<TSource> firstCol && second is ICollection<TSource> secondCol)
{
if (first.TryGetSpan(out ReadOnlySpan<TSource> firstSpan) && second.TryGetSpan(out ReadOnlySpan<TSource> secondSpan))
{
return firstSpan.SequenceEqual(secondSpan, comparer);
}
if (firstCol.Count != secondCol.Count)
{
return false;
}
if (firstCol is IList<TSource> firstList && secondCol is IList<TSource> secondList)
{
comparer ??= EqualityComparer<TSource>.Default;
int count = firstCol.Count;
for (int i = 0; i < count; i++)
{
if (!comparer.Equals(firstList[i], secondList[i]))
{
return false;
}
}
return true;
}
}
using (IEnumerator<TSource> e1 = first.GetEnumerator())
using (IEnumerator<TSource> e2 = second.GetEnumerator())
{
comparer ??= EqualityComparer<TSource>.Default;
while (e1.MoveNext())
{
if (!(e2.MoveNext() && comparer.Equals(e1.Current, e2.Current)))
{
return false;
}
}
return !e2.MoveNext();
}
}
}
}