Add ImmutableHashSetBuilder<T> type
このコミットが含まれているのは:
1 親
648f6e5c33
コミット
3439d3ec05
1ファイル変更
+102
@@ -0,0 +1,102 @@
|
||||
// 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.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace ComputeSharp.SourceGeneration.Helpers;
|
||||
|
||||
/// <summary>
|
||||
/// A helper type to build hash sets of values with pooled buffers.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to create hash sets for.</typeparam>
|
||||
internal struct ImmutableHashSetBuilder<T> : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// The shared <see cref="ObjectPool{T}"/> instance to share <see cref="HashSet{T}"/> objects.
|
||||
/// </summary>
|
||||
private static readonly ObjectPool<HashSet<T>> SharedObjectPool = new(static () => new HashSet<T>());
|
||||
|
||||
/// <summary>
|
||||
/// The rented <see cref="HashSet{T}"/> instance to use.
|
||||
/// </summary>
|
||||
private HashSet<T>? set;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="ImmutableHashSetBuilder{T}"/> value with a pooled underlying data writer.
|
||||
/// </summary>
|
||||
/// <returns>A <see cref="ImmutableHashSetBuilder{T}"/> instance to write data to.</returns>
|
||||
public static ImmutableHashSetBuilder<T> Rent()
|
||||
{
|
||||
return new(SharedObjectPool.Allocate());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ImmutableHashSetBuilder{T}"/> object with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="set">The target <see cref="HashSet{T}"/> instance to use.</param>
|
||||
private ImmutableHashSetBuilder(HashSet<T> set)
|
||||
{
|
||||
this.set = set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of elements currently written in the current instance.
|
||||
/// </summary>
|
||||
public readonly int Count
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => this.set!.Count;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ImmutableArray{T}.Builder.Add(T)"/>
|
||||
public readonly void Add(T item)
|
||||
{
|
||||
_ = this.set!.Add(item);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an <see cref="IEnumerable{T}"/> instance for the current builder.
|
||||
/// </summary>
|
||||
/// <returns>An <see cref="IEnumerable{T}"/> instance for the current builder.</returns>
|
||||
/// <remarks>
|
||||
/// The builder should not be mutated while an enumerator is in use.
|
||||
/// </remarks>
|
||||
public readonly IEnumerable<T> AsEnumerable()
|
||||
{
|
||||
return this.set!;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ImmutableArray{T}.Builder.ToImmutable"/>
|
||||
public readonly ImmutableArray<T> ToImmutable()
|
||||
{
|
||||
T[] array = this.set!.ToArray();
|
||||
|
||||
return Unsafe.As<T[], ImmutableArray<T>>(ref array);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ImmutableArray{T}.Builder.ToArray"/>
|
||||
public readonly T[] ToArray()
|
||||
{
|
||||
return this.set!.ToArray();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
{
|
||||
HashSet<T>? writer = this.set;
|
||||
|
||||
this.set = null;
|
||||
|
||||
if (writer is not null)
|
||||
{
|
||||
writer.Clear();
|
||||
|
||||
SharedObjectPool.Free(writer);
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする