</ItemGroup>
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
- <PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
+ <PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
</ItemGroup>
<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<ProjectReference Include="$(LibrariesProjectRoot)System.Collections.Immutable\src\System.Collections.Immutable.csproj" />
</ItemGroup>
+ <ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
+ <PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
+ </ItemGroup>
+
<ItemGroup Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'">
<Reference Include="System.Collections" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.MemoryMappedFiles" />
+ <Reference Include="System.Memory" />
<Reference Include="System.Runtime" />
<Reference Include="System.Runtime.InteropServices" />
<Reference Include="System.Text.Encoding.Extensions" />
internal static bool Equals(ImmutableArray<byte> x, ImmutableArray<byte> y)
{
- if (x == y)
- {
- return true;
- }
-
- if (x.IsDefault || y.IsDefault || x.Length != y.Length)
- {
- return false;
- }
-
- for (var i = 0; i < x.Length; i++)
- {
- if (x[i] != y[i])
- {
- return false;
- }
- }
-
- return true;
+ return x.AsSpan().SequenceEqual(y.AsSpan());
}
internal static bool Equals(byte[] left, int leftStart, byte[] right, int rightStart, int length)
{
- if (left == null || right == null)
- {
- return ReferenceEquals(left, right);
- }
-
- if (ReferenceEquals(left, right) && leftStart == rightStart)
- {
- return true;
- }
-
- for (var i = 0; i < length; i++)
- {
- if (left[leftStart + i] != right[rightStart + i])
- {
- return false;
- }
- }
-
- return true;
+ return left.AsSpan(leftStart, length).SequenceEqual(right.AsSpan(rightStart, length));
}
internal static bool Equals(byte[]? left, byte[]? right)
{
- if (ReferenceEquals(left, right))
- {
- return true;
- }
-
- if (left == null || right == null || left.Length != right.Length)
- {
- return false;
- }
-
- for (var i = 0; i < left.Length; i++)
- {
- if (left[i] != right[i])
- {
- return false;
- }
- }
-
- return true;
+ return left.AsSpan().SequenceEqual(right.AsSpan());
}
// Both hash computations below use the FNV-1a algorithm (http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function).
internal static int GetHashCode(ImmutableArray<byte> x)
{
Debug.Assert(!x.IsDefault);
- return Hash.GetFNVHashCode(x);
+ return Hash.GetFNVHashCode(x.AsSpan());
}
bool IEqualityComparer<byte[]>.Equals(byte[]? x, byte[]? y)
/// </summary>
/// <param name="data">The sequence of bytes</param>
/// <returns>The FNV-1a hash of <paramref name="data"/></returns>
- internal static int GetFNVHashCode(byte[] data)
- {
- int hashCode = Hash.FnvOffsetBias;
-
- for (int i = 0; i < data.Length; i++)
- {
- hashCode = unchecked((hashCode ^ data[i]) * Hash.FnvPrime);
- }
-
- return hashCode;
- }
-
- /// <summary>
- /// Compute the FNV-1a hash of a sequence of bytes
- /// See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
- /// </summary>
- /// <param name="data">The sequence of bytes</param>
- /// <returns>The FNV-1a hash of <paramref name="data"/></returns>
- internal static int GetFNVHashCode(ImmutableArray<byte> data)
+ internal static int GetFNVHashCode(ReadOnlySpan<byte> data)
{
int hashCode = Hash.FnvOffsetBias;
[Fact]
public void GetFNVHashCodeImmutableByteTest()
{
- Assert.Equal(-1088511923, Hash.GetFNVHashCode(ImmutableArray.Create((byte)0xFF, (byte)0xD1)));
+ Assert.Equal(-1088511923, Hash.GetFNVHashCode(ImmutableArray.Create((byte)0xFF, (byte)0xD1).AsSpan()));
}
[Fact]