This repository was archived by the owner on Mar 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathEquatableList.cs
More file actions
77 lines (63 loc) · 1.85 KB
/
EquatableList.cs
File metadata and controls
77 lines (63 loc) · 1.85 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
72
73
74
75
76
77
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections;
#nullable enable
namespace NuGet.Insights
{
public class EquatableList<T> : IReadOnlyList<T>, IEquatable<EquatableList<T>>
{
private IReadOnlyList<T> _items;
private readonly int _hashCode;
public EquatableList(IEnumerable<T> items)
{
_items = items.ToList();
var hashCode = new HashCode();
foreach (var item in _items)
{
hashCode.Add(item);
}
_hashCode = hashCode.ToHashCode();
}
public T this[int index] => _items[index];
public int Count => _items.Count;
public bool Equals(EquatableList<T>? other)
{
if (other is null)
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
if (_hashCode != other._hashCode || _items.Count != other._items.Count)
{
return false;
}
for (var i = 0; i < _items.Count; i++)
{
if (!Equals(_items[i], other._items[i]))
{
return false;
}
}
return true;
}
public override bool Equals(object? obj)
{
return Equals(obj as EquatableList<T>);
}
public override int GetHashCode()
{
return _hashCode;
}
public IEnumerator<T> GetEnumerator()
{
return _items.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _items.GetEnumerator();
}
}
}