-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
2,153 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
namespace System.Buffers.Current | ||
{ | ||
/// <summary> | ||
/// Extension methods for <see cref="ReadOnlySequence{T}"/> | ||
/// </summary> | ||
public static class BuffersExtensions | ||
{ | ||
/// <summary> | ||
/// Returns position of first occurrence of item in the <see cref="ReadOnlySequence{T}"/> | ||
/// </summary> | ||
public static SequencePosition? PositionOf<T>(this ReadOnlySequence<T> sequence, T value) where T : IEquatable<T> | ||
{ | ||
SequencePosition position = sequence.Start; | ||
SequencePosition result = position; | ||
while (sequence.TryGet(ref position, out var memory)) | ||
{ | ||
var index = memory.Span.IndexOf(value); | ||
if (index != -1) | ||
{ | ||
return sequence.GetPosition(result, index); | ||
} | ||
result = position; | ||
} | ||
return null; | ||
} | ||
|
||
/// <summary> | ||
/// Copy the <see cref="ReadOnlySequence{T}"/> to the specified <see cref="Span{Byte}"/>. | ||
/// </summary> | ||
/// <param name="sequence">The source <see cref="ReadOnlySequence{T}"/>.</param> | ||
/// <param name="destination">The destination <see cref="Span{Byte}"/>.</param> | ||
public static void CopyTo<T>(this ReadOnlySequence<T> sequence, Span<T> destination) | ||
{ | ||
if (sequence.Length > destination.Length) | ||
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.destination); | ||
|
||
foreach (var segment in sequence) | ||
{ | ||
segment.Span.CopyTo(destination); | ||
destination = destination.Slice(segment.Length); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Converts the <see cref="ReadOnlySequence{T}"/> to an array | ||
/// </summary> | ||
public static T[] ToArray<T>(this ReadOnlySequence<T> sequence) | ||
{ | ||
var array = new T[sequence.Length]; | ||
sequence.CopyTo(array); | ||
return array; | ||
} | ||
|
||
/// <summary> | ||
/// Writes contents of <paramref name="source"/> to <paramref name="bufferWriter"/> | ||
/// </summary> | ||
public static void Write<T>(this IBufferWriter<T> bufferWriter, ReadOnlySpan<T> source) | ||
{ | ||
Span<T> destination = bufferWriter.GetSpan(); | ||
|
||
// Fast path, try copying to the available memory directly | ||
if (source.Length <= destination.Length) | ||
{ | ||
source.CopyTo(destination); | ||
bufferWriter.Advance(source.Length); | ||
return; | ||
} | ||
|
||
WriteMultiSegment(bufferWriter, source, destination); | ||
} | ||
|
||
private static void WriteMultiSegment<T>(IBufferWriter<T> bufferWriter, ReadOnlySpan<T> source, Span<T> destination) | ||
{ | ||
while (true) | ||
{ | ||
int writeSize = Math.Min(destination.Length, source.Length); | ||
source.Slice(0, writeSize).CopyTo(destination); | ||
bufferWriter.Advance(writeSize); | ||
source = source.Slice(writeSize); | ||
if (source.Length > 0) | ||
{ | ||
destination = bufferWriter.GetSpan(source.Length); | ||
continue; | ||
} | ||
|
||
return; | ||
} | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...ryLookupBenchmark/corefx/IBufferWriter.cs → ...pBenchmark/corefxCurrent/IBufferWriter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
namespace System.Buffers.Current | ||
{ | ||
/// <summary> | ||
/// Represents a linked list of <see cref="Memory{T}"/> nodes. | ||
/// </summary> | ||
public interface IMemoryList<T> | ||
{ | ||
/// <summary> | ||
/// The <see cref="Memory{T}"/> value for current node. | ||
/// </summary> | ||
Memory<T> Memory { get; } | ||
|
||
/// <summary> | ||
/// The next node. | ||
/// </summary> | ||
IMemoryList<T> Next { get; } | ||
|
||
/// <summary> | ||
/// The sum of node length before current. | ||
/// </summary> | ||
long RunningIndex { get; } | ||
} | ||
} |
Oops, something went wrong.