Chunk<T>
using System;
using System.Buffers;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace DotNext.Buffers
{
internal sealed class Chunk<T> : ReadOnlySequenceSegment<T>
{
private Chunk([In] [IsReadOnly] ref ReadOnlyMemory<T> segment)
{
base.Memory = segment;
}
private Chunk<T> Next([In] [IsReadOnly] ref ReadOnlyMemory<T> segment)
{
long runningIndex = base.RunningIndex;
Chunk<T> obj = new Chunk<T>(ref segment) {
RunningIndex = runningIndex + base.Memory.Length
};
Chunk<T> result = obj;
base.Next = obj;
return result;
}
internal static void AddChunk([In] [IsReadOnly] ref ReadOnlyMemory<T> segment, [AllowNull] ref Chunk<T> first, [AllowNull] ref Chunk<T> last)
{
object chunk;
if (first != null && last != null)
chunk = last.Next(ref segment);
else {
Chunk<T> obj = new Chunk<T>(ref segment) {
RunningIndex = 0
};
Chunk<T> chunk2 = obj;
first = obj;
chunk = chunk2;
}
last = (Chunk<T>)chunk;
}
internal static ReadOnlySequence<T> CreateSequence(Chunk<T> head, Chunk<T> tail)
{
return new ReadOnlySequence<T>(head, 0, tail, tail.Memory.Length);
}
}
}