feat(collections)!: switch to ref struct enumerators

Refactor all unsafe collection enumerators to use ref struct types,
removing support for boxing and standard .NET enumeration interfaces.
GetEnumerator methods now return stack-only, more efficient enumerators
with [UnscopedRef] and inlining attributes. IEnumerable<T> and
IEnumerable implementations are removed from affected types. Interfaces
now require unmanaged types. Also includes minor doc and bug fixes.

BREAKING CHANGE: Enumerators are no longer compatible with LINQ, and collections no longer implement IEnumerable/IEnumerator.
This commit is contained in:
2026-04-10 02:51:37 +09:00
parent a108f39cbe
commit dea8de60d0
12 changed files with 175 additions and 268 deletions

View File

@@ -1,7 +1,6 @@
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections.Contracts;
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Collections;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.LowLevel.Collections;
@@ -11,19 +10,18 @@ namespace Misaki.HighPerformance.LowLevel.Collections;
/// removing, and checking for values.
/// </summary>
/// <typeparam name="T">Represents an unmanaged type that can be compared for equality.</typeparam>
public unsafe struct UnsafeHashSet<T> : IUnsafeHashCollection<T>, IEnumerable<T>
public unsafe struct UnsafeHashSet<T> : IUnsafeHashCollection<T>
where T : unmanaged, IEquatable<T>
{
public struct Enumerator : IEnumerator<T>
public ref struct Enumerator
{
internal HashMapHelper<T>.Enumerator _enumerator;
public readonly T Current => _enumerator.buffer->_keys[_enumerator.index];
readonly object IEnumerator.Current => Current;
public readonly T Current => _enumerator.helper._keys[_enumerator.index];
public Enumerator(HashMapHelper<T>* hashMap)
public Enumerator(ref HashMapHelper<T> hashMap)
{
_enumerator = new HashMapHelper<T>.Enumerator(hashMap);
_enumerator = new HashMapHelper<T>.Enumerator(ref hashMap);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -36,10 +34,6 @@ public unsafe struct UnsafeHashSet<T> : IUnsafeHashCollection<T>, IEnumerable<T>
{
_enumerator.Reset();
}
public readonly void Dispose()
{
}
}
private HashMapHelper<T> _helper;
@@ -48,21 +42,6 @@ public unsafe struct UnsafeHashSet<T> : IUnsafeHashCollection<T>, IEnumerable<T>
public readonly int Capacity => _helper.Capacity;
public readonly bool IsCreated => _helper.IsCreated;
public Enumerator GetEnumerator()
{
return new((HashMapHelper<T>*)UnsafeUtility.AddressOf(ref this));
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
/// <summary>
/// Invalid constructor. Use <see cref="UnsafeHashSet(int, Allocator, AllocationOption)"/> or <see cref="UnsafeHashSet(int, AllocationHandle, AllocationOption)"/> instead."/>
/// </summary>
@@ -81,6 +60,13 @@ public unsafe struct UnsafeHashSet<T> : IUnsafeHashCollection<T>, IEnumerable<T>
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[UnscopedRef]
public Enumerator GetEnumerator()
{
return new Enumerator(ref _helper);
}
/// <summary>
/// Adds a new value (unless it is already present).
/// </summary>