-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathEasyArray.cs
More file actions
55 lines (45 loc) · 1.77 KB
/
EasyArray.cs
File metadata and controls
55 lines (45 loc) · 1.77 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
using System.Collections;
namespace StackExchange.Redis.Build;
/// <summary>
/// Think <c>ImmutableArray{T}</c>, but with structural equality.
/// </summary>
/// <typeparam name="T">The data being wrapped.</typeparam>
internal readonly struct EasyArray<T>(T[]? array) : IEquatable<EasyArray<T>>, IEnumerable<T>
{
public static readonly EasyArray<T> Empty = new([]);
private readonly T[]? _array = array ?? [];
public int Length => _array?.Length ?? 0;
public ref readonly T this[int index] => ref _array![index];
public ReadOnlySpan<T> Span => _array.AsSpan();
public bool IsEmpty => Length == 0;
public static bool operator ==(EasyArray<T> x, EasyArray<T> y)
=> x.Equals(y);
public static bool operator !=(EasyArray<T> x, EasyArray<T> y)
=> x.Equals(y);
public bool Equals(EasyArray<T> other)
{
T[]? tArr = this._array, oArr = other._array;
if (tArr is null) return oArr is null || oArr.Length == 0;
if (oArr is null) return tArr.Length == 0;
if (tArr.Length != oArr.Length) return false;
for (int i = 0; i < tArr.Length; i++)
{
if (ReferenceEquals(tArr[i], oArr[i]))
return false;
}
return true;
}
public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>)(_array ?? [])).GetEnumerator();
public override bool Equals(object? obj)
=> obj is EasyArray<T> other && Equals(other);
public override int GetHashCode()
{
var arr = _array;
if (arr is null) return 0;
// use length and first item for a quick hash
return arr.Length == 0
? 0
: arr.Length ^ EqualityComparer<T>.Default.GetHashCode(arr[0]);
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}