| | | 1 | | using System; |
| | | 2 | | using System.Collections; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Collections.Immutable; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Runtime.CompilerServices; |
| | | 7 | | |
| | | 8 | | namespace Hertzole.ScriptableValues.Generator; |
| | | 9 | | |
| | | 10 | | internal 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)] |
| | 0 | 23 | | get { return AsImmutableArray().IsEmpty; } |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | public int Length |
| | | 27 | | { |
| | | 28 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 548 | 29 | | 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 | | { |
| | 36 | 38 | | this.array = Unsafe.As<ImmutableArray<T>, T[]?>(ref array); |
| | 36 | 39 | | } |
| | | 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)] |
| | 511 | 49 | | get { return ref AsImmutableArray().ItemRef(index); } |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <sinheritdoc /> |
| | | 53 | | public bool Equals(EquatableArray<T> array) |
| | | 54 | | { |
| | 0 | 55 | | return AsSpan().SequenceEqual(array.AsSpan()); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <sinheritdoc /> |
| | | 59 | | public override bool Equals(object? obj) |
| | | 60 | | { |
| | 0 | 61 | | return obj is EquatableArray<T> array && Equals(this, array); |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <sinheritdoc /> |
| | | 65 | | public override int GetHashCode() |
| | | 66 | | { |
| | 0 | 67 | | if (this.array is not T[] array) |
| | | 68 | | { |
| | 0 | 69 | | return 0; |
| | | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | int hash = 17; |
| | | 73 | | unchecked |
| | | 74 | | { |
| | 0 | 75 | | foreach (T item in array) |
| | | 76 | | { |
| | 0 | 77 | | hash = hash * 31 + (item?.GetHashCode() ?? 0); |
| | | 78 | | } |
| | | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | 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 | | { |
| | 1059 | 91 | | 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 | | { |
| | 25 | 101 | | 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 | | { |
| | 0 | 110 | | 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 | | { |
| | 0 | 119 | | 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 | | { |
| | 0 | 128 | | return AsImmutableArray().GetEnumerator(); |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <sinheritdoc /> |
| | | 132 | | IEnumerator<T> IEnumerable<T>.GetEnumerator() |
| | | 133 | | { |
| | 0 | 134 | | return ((IEnumerable<T>) AsImmutableArray()).GetEnumerator(); |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | /// <sinheritdoc /> |
| | | 138 | | IEnumerator IEnumerable.GetEnumerator() |
| | | 139 | | { |
| | 0 | 140 | | 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 | | { |
| | 25 | 149 | | 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 | | { |
| | 0 | 158 | | 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 | | { |
| | 0 | 169 | | 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 | | { |
| | 0 | 180 | | return !left.Equals(right); |
| | | 181 | | } |
| | | 182 | | } |