// 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. using System; using System.Collections.Immutable; using System.Runtime.CompilerServices; namespace ComputeSharp.SourceGeneration.Helpers; /// /// A helper type to build sequences of values with pooled buffers. /// /// The type of items to create sequences for. internal struct ImmutableArrayBuilder : IDisposable { /// /// The shared instance to share objects. /// private static readonly ObjectPool SharedObjectPool = new(static () => new Writer()); /// /// The rented instance to use. /// private Writer? writer; /// /// Creates a value with a pooled underlying data writer. /// /// A instance to write data to. public static ImmutableArrayBuilder Rent() { return new(SharedObjectPool.Allocate()); } /// /// Creates a new object with the specified parameters. /// /// The target data writer to use. private ImmutableArrayBuilder(Writer writer) { this.writer = writer; } /// /// Gets the data written to the underlying buffer so far, as a . /// public readonly ReadOnlySpan WrittenSpan { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => this.writer!.WrittenSpan; } /// /// Gets the number of elements currently written in the current instance. /// public readonly int Count { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => this.writer!.WrittenSpan.Length; } /// public readonly void Add(T item) { this.writer!.Add(item); } /// /// Adds the specified items to the end of the array. /// /// The items to add at the end of the array. public readonly void AddRange(ReadOnlySpan items) { this.writer!.AddRange(items); } /// /// Inserts an item to the builder at the specified index. /// /// The zero-based index at which should be inserted. /// The object to insert into the current instance. public readonly void Insert(int index, T item) { this.writer!.Insert(index, item); } /// public readonly ImmutableArray ToImmutable() { T[] array = this.writer!.WrittenSpan.ToArray(); return Unsafe.As>(ref array); } /// public readonly T[] ToArray() { return this.writer!.WrittenSpan.ToArray(); } /// public override readonly string ToString() { return this.writer!.WrittenSpan.ToString(); } /// public void Dispose() { Writer? writer = this.writer; this.writer = null; if (writer is not null) { writer.Clear(); SharedObjectPool.Free(writer); } } /// /// A class handling the actual buffer writing. /// private sealed class Writer { /// /// The underlying array. /// private T[] array; /// /// The starting offset within . /// private int index; /// /// Creates a new instance with the specified parameters. /// public Writer() { if (typeof(T) == typeof(char)) { this.array = new T[1024]; } else { this.array = new T[8]; } this.index = 0; } /// public ReadOnlySpan WrittenSpan { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => new(this.array, 0, this.index); } /// public void Add(T value) { EnsureCapacity(1); this.array[this.index++] = value; } /// public void AddRange(ReadOnlySpan items) { EnsureCapacity(items.Length); items.CopyTo(this.array.AsSpan(this.index)); this.index += items.Length; } /// public void Insert(int index, T item) { if (index < 0 || index > this.index) { ImmutableArrayBuilder.ThrowArgumentOutOfRangeExceptionForIndex(); } EnsureCapacity(1); if (index < this.index) { Array.Copy(this.array, index, this.array, index + 1, this.index - index); } this.array[index] = item; this.index++; } /// /// Clears the items in the current writer. /// public void Clear() { if (typeof(T) != typeof(char)) { this.array.AsSpan(0, this.index).Clear(); } this.index = 0; } /// /// Ensures that has enough free space to contain a given number of new items. /// /// The minimum number of items to ensure space for in . [MethodImpl(MethodImplOptions.AggressiveInlining)] private void EnsureCapacity(int requestedSize) { if (requestedSize > this.array.Length - this.index) { ResizeBuffer(requestedSize); } } /// /// Resizes to ensure it can fit the specified number of new items. /// /// The minimum number of items to ensure space for in . [MethodImpl(MethodImplOptions.NoInlining)] private void ResizeBuffer(int sizeHint) { int minimumSize = this.index + sizeHint; int requestedSize = Math.Max(this.array.Length * 2, minimumSize); T[] newArray = new T[requestedSize]; Array.Copy(this.array, newArray, this.index); this.array = newArray; } } } /// /// Private helpers for the type. /// file static class ImmutableArrayBuilder { /// /// Throws an for "index". /// public static void ThrowArgumentOutOfRangeExceptionForIndex() { throw new ArgumentOutOfRangeException("index"); } }