Skip to content

Commit

Permalink
Temp
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan committed May 19, 2023
1 parent e108a1a commit 2dec12f
Show file tree
Hide file tree
Showing 9 changed files with 329 additions and 105 deletions.
2 changes: 2 additions & 0 deletions src/libraries/System.Memory/ref/System.Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ public static partial class MemoryExtensions
public static bool Contains(this System.ReadOnlySpan<char> span, System.ReadOnlySpan<char> value, System.StringComparison comparisonType) { throw null; }
public static bool Contains<T>(this System.ReadOnlySpan<T> span, T value) where T : System.IEquatable<T>? { throw null; }
public static bool Contains<T>(this System.Span<T> span, T value) where T : System.IEquatable<T>? { throw null; }
public static bool ContainsAny<T>(this System.ReadOnlySpan<T> span, System.Buffers.SearchValues<T> values) where T : System.IEquatable<T>? { throw null; }
public static bool ContainsAny<T>(this System.Span<T> span, System.Buffers.SearchValues<T> values) where T : System.IEquatable<T>? { throw null; }
public static void CopyTo<T>(this T[]? source, System.Memory<T> destination) { }
public static void CopyTo<T>(this T[]? source, System.Span<T> destination) { }
public static int Count<T>(this System.Span<T> span, T value) where T : System.IEquatable<T>? { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,24 @@ ref Unsafe.As<T, long>(ref MemoryMarshal.GetReference(span)),
return SpanHelpers.Contains(ref MemoryMarshal.GetReference(span), value, span.Length);
}

/// <summary>
/// Searches for any of the specified values and returns true if found. If not found, returns false.
/// </summary>
/// <param name="span">The span to search.</param>
/// <param name="values">The set of values to search for.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool ContainsAny<T>(this Span<T> span, SearchValues<T> values) where T : IEquatable<T>? =>
ContainsAny((ReadOnlySpan<T>)span, values);

/// <summary>
/// Searches for any of the specified values and returns true if found. If not found, returns false.
/// </summary>
/// <param name="span">The span to search.</param>
/// <param name="values">The set of values to search for.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool ContainsAny<T>(this ReadOnlySpan<T> span, SearchValues<T> values) where T : IEquatable<T>? =>
SearchValues<T>.ContainsAny(span, values);

/// <summary>
/// Searches for the specified value and returns the index of its first occurrence. If not found, returns -1. Values are compared using IEquatable{T}.Equals(T).
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public AnyByteSearchValues(ReadOnlySpan<byte> values)
internal override bool ContainsCore(byte value) =>
_lookup.Contains(value);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal override bool ContainsAny(ReadOnlySpan<byte> span)
{
return IndexOfAnyAsciiSearcher.IsVectorizationSupported && span.Length >= sizeof(ulong)
? IndexOfAnyAsciiSearcher.ContainsAnyByte(ref MemoryMarshal.GetReference(span), span.Length, ref _bitmaps)
: ContainsAnyScalar(ref MemoryMarshal.GetReference(span), span.Length);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal override int IndexOfAny(ReadOnlySpan<byte> span) =>
IndexOfAny<IndexOfAnyAsciiSearcher.DontNegate>(ref MemoryMarshal.GetReference(span), span.Length);
Expand All @@ -45,7 +53,7 @@ private int IndexOfAny<TNegator>(ref byte searchSpace, int searchSpaceLength)
where TNegator : struct, IndexOfAnyAsciiSearcher.INegator
{
return IndexOfAnyAsciiSearcher.IsVectorizationSupported && searchSpaceLength >= sizeof(ulong)
? IndexOfAnyAsciiSearcher.IndexOfAnyVectorizedAnyByte<TNegator>(ref searchSpace, searchSpaceLength, ref _bitmaps)
? IndexOfAnyAsciiSearcher.IndexOfAnyByte<TNegator>(ref searchSpace, searchSpaceLength, ref _bitmaps)
: IndexOfAnyScalar<TNegator>(ref searchSpace, searchSpaceLength);
}

Expand All @@ -54,10 +62,29 @@ private int LastIndexOfAny<TNegator>(ref byte searchSpace, int searchSpaceLength
where TNegator : struct, IndexOfAnyAsciiSearcher.INegator
{
return IndexOfAnyAsciiSearcher.IsVectorizationSupported && searchSpaceLength >= sizeof(ulong)
? IndexOfAnyAsciiSearcher.LastIndexOfAnyVectorizedAnyByte<TNegator>(ref searchSpace, searchSpaceLength, ref _bitmaps)
? IndexOfAnyAsciiSearcher.LastIndexOfAnyByte<TNegator>(ref searchSpace, searchSpaceLength, ref _bitmaps)
: LastIndexOfAnyScalar<TNegator>(ref searchSpace, searchSpaceLength);
}

private bool ContainsAnyScalar(ref byte searchSpace, int searchSpaceLength)
{
ref byte searchSpaceEnd = ref Unsafe.Add(ref searchSpace, searchSpaceLength);
ref byte cur = ref searchSpace;

while (!Unsafe.AreSame(ref cur, ref searchSpaceEnd))
{
byte b = cur;
if (_lookup.Contains(b))
{
return true;
}

cur = ref Unsafe.Add(ref cur, 1);
}

return false;
}

private int IndexOfAnyScalar<TNegator>(ref byte searchSpace, int searchSpaceLength)
where TNegator : struct, IndexOfAnyAsciiSearcher.INegator
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ public AsciiByteSearchValues(ReadOnlySpan<byte> values) =>
internal override bool ContainsCore(byte value) =>
_lookup.Contains(value);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal override bool ContainsAny(ReadOnlySpan<byte> span)
{
return IndexOfAnyAsciiSearcher.IsVectorizationSupported && span.Length >= sizeof(ulong)
? IndexOfAnyAsciiSearcher.ContainsAny(ref MemoryMarshal.GetReference(span), span.Length, ref _bitmap)
: ContainsAnyScalar(ref MemoryMarshal.GetReference(span), span.Length);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal override int IndexOfAny(ReadOnlySpan<byte> span) =>
IndexOfAny<IndexOfAnyAsciiSearcher.DontNegate>(ref MemoryMarshal.GetReference(span), span.Length);
Expand All @@ -42,7 +50,7 @@ private int IndexOfAny<TNegator>(ref byte searchSpace, int searchSpaceLength)
where TNegator : struct, IndexOfAnyAsciiSearcher.INegator
{
return IndexOfAnyAsciiSearcher.IsVectorizationSupported && searchSpaceLength >= sizeof(ulong)
? IndexOfAnyAsciiSearcher.IndexOfAnyVectorized<TNegator>(ref searchSpace, searchSpaceLength, ref _bitmap)
? IndexOfAnyAsciiSearcher.IndexOfAny<TNegator>(ref searchSpace, searchSpaceLength, ref _bitmap)
: IndexOfAnyScalar<TNegator>(ref searchSpace, searchSpaceLength);
}

Expand All @@ -51,10 +59,29 @@ private int LastIndexOfAny<TNegator>(ref byte searchSpace, int searchSpaceLength
where TNegator : struct, IndexOfAnyAsciiSearcher.INegator
{
return IndexOfAnyAsciiSearcher.IsVectorizationSupported && searchSpaceLength >= sizeof(ulong)
? IndexOfAnyAsciiSearcher.LastIndexOfAnyVectorized<TNegator>(ref searchSpace, searchSpaceLength, ref _bitmap)
? IndexOfAnyAsciiSearcher.LastIndexOfAny<TNegator>(ref searchSpace, searchSpaceLength, ref _bitmap)
: LastIndexOfAnyScalar<TNegator>(ref searchSpace, searchSpaceLength);
}

private bool ContainsAnyScalar(ref byte searchSpace, int searchSpaceLength)
{
ref byte searchSpaceEnd = ref Unsafe.Add(ref searchSpace, searchSpaceLength);
ref byte cur = ref searchSpace;

while (!Unsafe.AreSame(ref cur, ref searchSpaceEnd))
{
byte b = cur;
if (_lookup.Contains(b))
{
return true;
}

cur = ref Unsafe.Add(ref cur, 1);
}

return false;
}

private int IndexOfAnyScalar<TNegator>(ref byte searchSpace, int searchSpaceLength)
where TNegator : struct, IndexOfAnyAsciiSearcher.INegator
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public AsciiCharSearchValues(Vector256<byte> bitmap, BitVector256 lookup)
internal override bool ContainsCore(char value) =>
_lookup.Contains128(value);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal override bool ContainsAny(ReadOnlySpan<char> span)
{
return IndexOfAnyAsciiSearcher.IsVectorizationSupported && span.Length >= Vector128<short>.Count
? IndexOfAnyAsciiSearcher.ContainsAny<TOptimizations>(ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(span)), span.Length, ref _bitmap)
: ContainsAnyScalar(ref MemoryMarshal.GetReference(span), span.Length);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal override int IndexOfAny(ReadOnlySpan<char> span) =>
IndexOfAny<IndexOfAnyAsciiSearcher.DontNegate>(ref MemoryMarshal.GetReference(span), span.Length);
Expand All @@ -46,7 +54,7 @@ private int IndexOfAny<TNegator>(ref char searchSpace, int searchSpaceLength)
where TNegator : struct, IndexOfAnyAsciiSearcher.INegator
{
return IndexOfAnyAsciiSearcher.IsVectorizationSupported && searchSpaceLength >= Vector128<short>.Count
? IndexOfAnyAsciiSearcher.IndexOfAnyVectorized<TNegator, TOptimizations>(ref Unsafe.As<char, short>(ref searchSpace), searchSpaceLength, ref _bitmap)
? IndexOfAnyAsciiSearcher.IndexOfAny<TNegator, TOptimizations>(ref Unsafe.As<char, short>(ref searchSpace), searchSpaceLength, ref _bitmap)
: IndexOfAnyScalar<TNegator>(ref searchSpace, searchSpaceLength);
}

Expand All @@ -55,10 +63,29 @@ private int LastIndexOfAny<TNegator>(ref char searchSpace, int searchSpaceLength
where TNegator : struct, IndexOfAnyAsciiSearcher.INegator
{
return IndexOfAnyAsciiSearcher.IsVectorizationSupported && searchSpaceLength >= Vector128<short>.Count
? IndexOfAnyAsciiSearcher.LastIndexOfAnyVectorized<TNegator, TOptimizations>(ref Unsafe.As<char, short>(ref searchSpace), searchSpaceLength, ref _bitmap)
? IndexOfAnyAsciiSearcher.LastIndexOfAny<TNegator, TOptimizations>(ref Unsafe.As<char, short>(ref searchSpace), searchSpaceLength, ref _bitmap)
: LastIndexOfAnyScalar<TNegator>(ref searchSpace, searchSpaceLength);
}

private bool ContainsAnyScalar(ref char searchSpace, int searchSpaceLength)
{
ref char searchSpaceEnd = ref Unsafe.Add(ref searchSpace, searchSpaceLength);
ref char cur = ref searchSpace;

while (!Unsafe.AreSame(ref cur, ref searchSpaceEnd))
{
char c = cur;
if (_lookup.Contains128(c))
{
return true;
}

cur = ref Unsafe.Add(ref cur, 1);
}

return false;
}

private int IndexOfAnyScalar<TNegator>(ref char searchSpace, int searchSpaceLength)
where TNegator : struct, IndexOfAnyAsciiSearcher.INegator
{
Expand Down
Loading

0 comments on commit 2dec12f

Please sign in to comment.