public static void WriteSequence(bool copyMemory, SparseBufferGrowth growth)
        {
            using var writer = new SparseBufferWriter <byte>(128, growth);
            var sequence = ToReadOnlySequence(new ReadOnlyMemory <byte>(RandomBytes(5000)), 1000);

            writer.Write(in sequence, copyMemory);
            Equal(sequence.ToArray(), writer.ToReadOnlySequence().ToArray());
        }
示例#2
0
        /// <summary>
        /// Initializes a new builder with the specified size of memory block.
        /// </summary>
        /// <param name="chunkSize">The size of the memory block representing single segment within sequence.</param>
        /// <param name="growth">Specifies how the memory should be allocated for each subsequent chunk in this buffer.</param>
        /// <param name="allocator">The allocator used to rent the segments.</param>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="chunkSize"/> is less than or equal to zero.</exception>
        public SparseBufferWriter(int chunkSize, SparseBufferGrowth growth = SparseBufferGrowth.None, MemoryAllocator <T>?allocator = null)
        {
            if (chunkSize <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(chunkSize));
            }

            this.chunkSize = chunkSize;
            this.allocator = allocator;

            unsafe
            {
                this.growth = growth switch
                {
                    SparseBufferGrowth.Linear => & BufferHelpers.LinearGrowth,
                    SparseBufferGrowth.Exponential => & BufferHelpers.ExponentialGrowth,
                    _ => & BufferHelpers.NoGrowth,
                };
            }
        }