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