< Summary

Information
Class: Hertzole.ScriptableValues.Generator.EquatableArray<T>
Assembly: Hertzole.ScriptableValues.Generator
File(s): Generator/Hertzole.ScriptableValues.Generator/Helpers/EquatableArray.cs
Tag: 52_28197466029
Line coverage
29%
Covered lines: 7
Uncovered lines: 17
Coverable lines: 24
Total lines: 182
Line coverage: 29.1%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
Equals(...)100%110%
Equals(...)0%220%
GetHashCode()0%660%
AsImmutableArray()100%11100%
FromImmutableArray(...)100%11100%
AsSpan()100%110%
ToArray()100%110%
GetEnumerator()100%110%
System.Collections.Generic.IEnumerable<T>.GetEnumerator()100%110%
System.Collections.IEnumerable.GetEnumerator()100%110%
op_Implicit(...)100%11100%
op_Implicit(...)100%110%
op_Equality(...)100%110%
op_Inequality(...)100%110%

File(s)

Generator/Hertzole.ScriptableValues.Generator/Helpers/EquatableArray.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using System.Collections.Immutable;
 5using System.Linq;
 6using System.Runtime.CompilerServices;
 7
 8namespace Hertzole.ScriptableValues.Generator;
 9
 10internal readonly struct EquatableArray<T> : IEquatable<EquatableArray<T>>, IEnumerable<T> where T : IEquatable<T>
 11{
 12    /// <summary>
 13    ///     The underlying <typeparamref name="T" /> array.
 14    /// </summary>
 15    private readonly T[]? array;
 16
 17    /// <summary>
 18    ///     Gets a value indicating whether the current array is empty.
 19    /// </summary>
 20    public bool IsEmpty
 21    {
 22        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 023        get { return AsImmutableArray().IsEmpty; }
 24    }
 25
 26    public int Length
 27    {
 28        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 54829        get { return AsImmutableArray().Length; }
 30    }
 31
 32    /// <summary>
 33    ///     Creates a new <see cref="EquatableArray{T}" /> instance.
 34    /// </summary>
 35    /// <param name="array">The input <see cref="ImmutableArray" /> to wrap.</param>
 36    public EquatableArray(ImmutableArray<T> array)
 37    {
 3638        this.array = Unsafe.As<ImmutableArray<T>, T[]?>(ref array);
 3639    }
 40
 41    /// <summary>
 42    ///     Gets a reference to an item at a specified position within the array.
 43    /// </summary>
 44    /// <param name="index">The index of the item to retrieve a reference to.</param>
 45    /// <returns>A reference to an item at a specified position within the array.</returns>
 46    public ref readonly T this[int index]
 47    {
 48        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 51149        get { return ref AsImmutableArray().ItemRef(index); }
 50    }
 51
 52    /// <sinheritdoc />
 53    public bool Equals(EquatableArray<T> array)
 54    {
 055        return AsSpan().SequenceEqual(array.AsSpan());
 56    }
 57
 58    /// <sinheritdoc />
 59    public override bool Equals(object? obj)
 60    {
 061        return obj is EquatableArray<T> array && Equals(this, array);
 62    }
 63
 64    /// <sinheritdoc />
 65    public override int GetHashCode()
 66    {
 067        if (this.array is not T[] array)
 68        {
 069            return 0;
 70        }
 71
 072        int hash = 17;
 73        unchecked
 74        {
 075            foreach (T item in array)
 76            {
 077                hash = hash * 31 + (item?.GetHashCode() ?? 0);
 78            }
 79        }
 80
 081        return hash;
 82    }
 83
 84    /// <summary>
 85    ///     Gets an <see cref="ImmutableArray{T}" /> instance from the current <see cref="EquatableArray{T}" />.
 86    /// </summary>
 87    /// <returns>The <see cref="ImmutableArray{T}" /> from the current <see cref="EquatableArray{T}" />.</returns>
 88    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 89    public ImmutableArray<T> AsImmutableArray()
 90    {
 105991        return Unsafe.As<T[]?, ImmutableArray<T>>(ref Unsafe.AsRef(in array));
 92    }
 93
 94    /// <summary>
 95    ///     Creates an <see cref="EquatableArray{T}" /> instance from a given <see cref="ImmutableArray{T}" />.
 96    /// </summary>
 97    /// <param name="array">The input <see cref="ImmutableArray{T}" /> instance.</param>
 98    /// <returns>An <see cref="EquatableArray{T}" /> instance from a given <see cref="ImmutableArray{T}" />.</returns>
 99    public static EquatableArray<T> FromImmutableArray(ImmutableArray<T> array)
 100    {
 25101        return new EquatableArray<T>(array);
 102    }
 103
 104    /// <summary>
 105    ///     Returns a <see cref="ReadOnlySpan{T}" /> wrapping the current items.
 106    /// </summary>
 107    /// <returns>A <see cref="ReadOnlySpan{T}" /> wrapping the current items.</returns>
 108    public ReadOnlySpan<T> AsSpan()
 109    {
 0110        return AsImmutableArray().AsSpan();
 111    }
 112
 113    /// <summary>
 114    ///     Copies the contents of this <see cref="EquatableArray{T}" /> instance to a mutable array.
 115    /// </summary>
 116    /// <returns>The newly instantiated array.</returns>
 117    public T[] ToArray()
 118    {
 0119        return AsImmutableArray().ToArray();
 120    }
 121
 122    /// <summary>
 123    ///     Gets an <see cref="ImmutableArray{T}.Enumerator" /> value to traverse items in the current array.
 124    /// </summary>
 125    /// <returns>An <see cref="ImmutableArray{T}.Enumerator" /> value to traverse items in the current array.</returns>
 126    public ImmutableArray<T>.Enumerator GetEnumerator()
 127    {
 0128        return AsImmutableArray().GetEnumerator();
 129    }
 130
 131    /// <sinheritdoc />
 132    IEnumerator<T> IEnumerable<T>.GetEnumerator()
 133    {
 0134        return ((IEnumerable<T>) AsImmutableArray()).GetEnumerator();
 135    }
 136
 137    /// <sinheritdoc />
 138    IEnumerator IEnumerable.GetEnumerator()
 139    {
 0140        return ((IEnumerable) AsImmutableArray()).GetEnumerator();
 141    }
 142
 143    /// <summary>
 144    ///     Implicitly converts an <see cref="ImmutableArray{T}" /> to <see cref="EquatableArray{T}" />.
 145    /// </summary>
 146    /// <returns>An <see cref="EquatableArray{T}" /> instance from a given <see cref="ImmutableArray{T}" />.</returns>
 147    public static implicit operator EquatableArray<T>(ImmutableArray<T> array)
 148    {
 25149        return FromImmutableArray(array);
 150    }
 151
 152    /// <summary>
 153    ///     Implicitly converts an <see cref="EquatableArray{T}" /> to <see cref="ImmutableArray{T}" />.
 154    /// </summary>
 155    /// <returns>An <see cref="ImmutableArray{T}" /> instance from a given <see cref="EquatableArray{T}" />.</returns>
 156    public static implicit operator ImmutableArray<T>(EquatableArray<T> array)
 157    {
 0158        return array.AsImmutableArray();
 159    }
 160
 161    /// <summary>
 162    ///     Checks whether two <see cref="EquatableArray{T}" /> values are the same.
 163    /// </summary>
 164    /// <param name="left">The first <see cref="EquatableArray{T}" /> value.</param>
 165    /// <param name="right">The second <see cref="EquatableArray{T}" /> value.</param>
 166    /// <returns>Whether <paramref name="left" /> and <paramref name="right" /> are equal.</returns>
 167    public static bool operator ==(EquatableArray<T> left, EquatableArray<T> right)
 168    {
 0169        return left.Equals(right);
 170    }
 171
 172    /// <summary>
 173    ///     Checks whether two <see cref="EquatableArray{T}" /> values are not the same.
 174    /// </summary>
 175    /// <param name="left">The first <see cref="EquatableArray{T}" /> value.</param>
 176    /// <param name="right">The second <see cref="EquatableArray{T}" /> value.</param>
 177    /// <returns>Whether <paramref name="left" /> and <paramref name="right" /> are not equal.</returns>
 178    public static bool operator !=(EquatableArray<T> left, EquatableArray<T> right)
 179    {
 0180        return !left.Equals(right);
 181    }
 182}