/// <summary> /// Determines whether two spans are structurally (byte-wise) equal by comparing the elements by using memcmp /// </summary> /// <param name="first">A span, of type T to compare to second.</param> /// <param name="second">A span, of type U to compare to first.</param> public static bool BlockEquals <[Primitive] T, [Primitive] U>(this ReadOnlySpan <T> first, ReadOnlySpan <U> second) where T : struct where U : struct { var bytesCount = (ulong)first.Length * (ulong)Unsafe.SizeOf <T>(); if (bytesCount != (ulong)second.Length * (ulong)Unsafe.SizeOf <U>()) { return(false); } // perf: it is cheaper to compare 'n' long elements than 'n*8' bytes (in a loop) if ((bytesCount & 0x00000007) == 0) // fast % sizeof(long) { return(SequenceEqual(Cast <T, long>(first), Cast <U, long>(second))); } if ((bytesCount & 0x00000003) == 0) // fast % sizeof(int) { return(SequenceEqual(Cast <T, int>(first), Cast <U, int>(second))); } if ((bytesCount & 0x00000001) == 0) // fast % sizeof(short) { return(SequenceEqual(Cast <T, short>(first), Cast <U, short>(second))); } return(ReadOnlySpanExtensions.SequenceEqual(Cast <T, byte>(first), Cast <U, byte>(second))); }
/// <summary> /// Determines whether two read-only spans are equal (byte-wise) by comparing the elements by using memcmp /// </summary> /// <param name="first">A span of long integers to compare to second.</param> /// <param name="second">A span of long integers T to compare to first.</param> public static bool SequenceEqual(this ReadOnlySpan <long> first, ReadOnlySpan <long> second) { return(first.Length >= 256 ? MemoryEqual(first, second) : ReadOnlySpanExtensions.SequenceEqual(first, second)); }
/// <summary> /// Determines whether two read-only spans are equal (byte-wise) by comparing the elements by using memcmp /// </summary> /// <param name="first">A span of shorts to compare to second.</param> /// <param name="second">A span of shorts T to compare to first.</param> public static bool SequenceEqual(this ReadOnlySpan <short> first, ReadOnlySpan <short> second) { return(first.Length >= 512 ? MemoryEqual(first, second) : ReadOnlySpanExtensions.SequenceEqual <short>(first, second)); }