|
| 1 | +using System.Collections; |
| 2 | + |
| 3 | +namespace Refit.Generator; |
| 4 | + |
| 5 | +internal static class ImmutableEquatableArray |
| 6 | +{ |
| 7 | + public static ImmutableEquatableArray<T> Empty<T>() |
| 8 | + where T : IEquatable<T> => ImmutableEquatableArray<T>.Empty; |
| 9 | + |
| 10 | + public static ImmutableEquatableArray<T> ToImmutableEquatableArray<T>( |
| 11 | + this IEnumerable<T>? values |
| 12 | + ) |
| 13 | + where T : IEquatable<T> => values == null ? Empty<T>() : new(values); |
| 14 | +} |
| 15 | + |
| 16 | +/// <summary> |
| 17 | +/// Provides an immutable list implementation which implements sequence equality. |
| 18 | +/// </summary> |
| 19 | +internal sealed class ImmutableEquatableArray<T> |
| 20 | + : IEquatable<ImmutableEquatableArray<T>>, |
| 21 | + IReadOnlyList<T> |
| 22 | + where T : IEquatable<T> |
| 23 | +{ |
| 24 | + public static ImmutableEquatableArray<T> Empty { get; } = new(Array.Empty<T>()); |
| 25 | + |
| 26 | + private readonly T[] _values; |
| 27 | + public T this[int index] => _values[index]; |
| 28 | + public int Count => _values.Length; |
| 29 | + |
| 30 | + public ImmutableEquatableArray(T[] values) => _values = values; |
| 31 | + |
| 32 | + public ImmutableEquatableArray(IEnumerable<T> values) => _values = values.ToArray(); |
| 33 | + |
| 34 | + public T[] AsArray() => _values; |
| 35 | + |
| 36 | + public bool Equals(ImmutableEquatableArray<T>? other) => |
| 37 | + other != null && ((ReadOnlySpan<T>)_values).SequenceEqual(other._values); |
| 38 | + |
| 39 | + public override bool Equals(object? obj) => |
| 40 | + obj is ImmutableEquatableArray<T> other && Equals(other); |
| 41 | + |
| 42 | + public override int GetHashCode() |
| 43 | + { |
| 44 | + var hash = 0; |
| 45 | + foreach (T value in _values) |
| 46 | + { |
| 47 | + hash = Combine(hash, value.GetHashCode()); |
| 48 | + } |
| 49 | + |
| 50 | + static int Combine(int h1, int h2) |
| 51 | + { |
| 52 | + // RyuJIT optimizes this to use the ROL instruction |
| 53 | + // Related GitHub pull request: https://github.com/dotnet/coreclr/pull/1830 |
| 54 | + uint rol5 = ((uint)h1 << 5) | ((uint)h1 >> 27); |
| 55 | + return ((int)rol5 + h1) ^ h2; |
| 56 | + } |
| 57 | + |
| 58 | + return hash; |
| 59 | + } |
| 60 | + |
| 61 | + public Enumerator GetEnumerator() => new(_values); |
| 62 | + |
| 63 | + IEnumerator<T> IEnumerable<T>.GetEnumerator() => ((IEnumerable<T>)_values).GetEnumerator(); |
| 64 | + |
| 65 | + IEnumerator IEnumerable.GetEnumerator() => _values.GetEnumerator(); |
| 66 | + |
| 67 | + public struct Enumerator |
| 68 | + { |
| 69 | + private readonly T[] _values; |
| 70 | + private int _index; |
| 71 | + |
| 72 | + internal Enumerator(T[] values) |
| 73 | + { |
| 74 | + _values = values; |
| 75 | + _index = -1; |
| 76 | + } |
| 77 | + |
| 78 | + public bool MoveNext() |
| 79 | + { |
| 80 | + var newIndex = _index + 1; |
| 81 | + |
| 82 | + if ((uint)newIndex < (uint)_values.Length) |
| 83 | + { |
| 84 | + _index = newIndex; |
| 85 | + return true; |
| 86 | + } |
| 87 | + |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + public readonly T Current => _values[_index]; |
| 92 | + } |
| 93 | +} |
0 commit comments