Some checks failed
Publish NuGet Packages / publish (pull_request) Failing after 1m5s
- Updated `.gitignore` to include `.vscode/` and clarified comments. - Introduced `SafeHandle` for managing memory alignment and safe access. - Refactored `UnsafeArray<T>` to add bounds checking and alignment logic. - Added `IUnsafeHashCollection<T>` for specialized hash-based collections. - Refactored `UnsafeHashMap<TKey, TValue>` and `UnsafeHashSet<T>` to use `HashMapHelper<TKey>` with alignment support. - Made `UnsafeSlotMap<T>` methods `readonly` for immutability. - Enhanced `HashMapHelper<TKey>` with alignment-aware buffer management and validation. - Updated benchmarks to use `UnsafeArray<Vector256<int>>` and added capacity checks. - Incremented assembly version to `1.1.3` in `Misaki.HighPerformance.LowLevel.csproj`. - Updated `Program.cs` to run `CollectionBenchmark` and demonstrate safe disposal handling.
134 lines
4.3 KiB
C#
134 lines
4.3 KiB
C#
using Misaki.HighPerformance.LowLevel.Buffer;
|
|
using Misaki.HighPerformance.LowLevel.Collections.Contracts;
|
|
using Misaki.HighPerformance.LowLevel.Contracts;
|
|
using Misaki.HighPerformance.LowLevel.Utilities;
|
|
using System.Collections;
|
|
using System.Runtime.CompilerServices;
|
|
|
|
namespace Misaki.HighPerformance.LowLevel.Collections;
|
|
|
|
/// <summary>
|
|
/// A collection that provides fast, unsafe operations for managing a set of unmanaged types. It supports adding,
|
|
/// 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>
|
|
where T : unmanaged, IEquatable<T>
|
|
{
|
|
public struct Enumerator : IEnumerator<T>
|
|
{
|
|
internal HashMapHelper<T>.Enumerator _enumerator;
|
|
|
|
public readonly T Current => _enumerator.buffer->_keys[_enumerator.index];
|
|
readonly object IEnumerator.Current => Current;
|
|
|
|
public Enumerator(HashMapHelper<T>* hashMap)
|
|
{
|
|
_enumerator = new HashMapHelper<T>.Enumerator(hashMap);
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public bool MoveNext() => _enumerator.MoveNext();
|
|
|
|
public void Reset() => _enumerator.Reset();
|
|
|
|
public readonly void Dispose()
|
|
{
|
|
}
|
|
}
|
|
|
|
private HashMapHelper<T> _helper;
|
|
|
|
public readonly int Count => _helper.Count;
|
|
public readonly int Capacity => _helper.Capacity;
|
|
public readonly bool IsCreated => _helper.IsCreated;
|
|
|
|
public Enumerator GetEnumerator() => new((HashMapHelper<T>*)UnsafeUtility.AddressOf(ref this));
|
|
IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator();
|
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
|
|
|
/// <summary>
|
|
/// Invalid constructor. Use <see cref="UnsafeHashSet(int, Allocator, AllocationOption)"/> or <see cref="UnsafeHashSet(int, ref AllocationHandle, AllocationOption)"/> instead."/>
|
|
/// </summary>
|
|
public UnsafeHashSet()
|
|
: this(0, Allocator.Invalid)
|
|
{
|
|
}
|
|
|
|
public UnsafeHashSet(int capacity, ref AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
|
|
{
|
|
_helper = new HashMapHelper<T>(capacity, 0, 0, HashMapHelper<T>.MINIMAL_CAPACITY, ref handle, allocationOption);
|
|
}
|
|
|
|
public UnsafeHashSet(int capacity, Allocator allocator, AllocationOption allocationOption = AllocationOption.None)
|
|
: this(capacity, ref AllocationManager.GetAllocationHandle(allocator), allocationOption)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a new value (unless it is already present).
|
|
/// </summary>
|
|
/// <param name="item">The value to add.</param>
|
|
/// <returns>True if the value was not already present.</returns>
|
|
public bool Add(T item)
|
|
{
|
|
return -1 != _helper.TryAdd(item);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes a particular value.
|
|
/// </summary>
|
|
/// <param name="item">The value to remove.</param>
|
|
/// <returns>True if the value was present.</returns>
|
|
public bool Remove(T item)
|
|
{
|
|
return -1 != _helper.TryRemove(item);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns true if a particular value is present.
|
|
/// </summary>
|
|
/// <param name="item">The value to check for.</param>
|
|
/// <returns>True if the value was present.</returns>
|
|
public bool Contains(T item)
|
|
{
|
|
return -1 != _helper.Find(item);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the capacity to match what it would be if it had been originally initialized with all its entries.
|
|
/// </summary>
|
|
public void TrimExcess() => _helper.TrimExcess();
|
|
|
|
/// <summary>
|
|
/// Returns an array with a copy of this set's values (in no particular order).
|
|
/// </summary>
|
|
/// <param name="allocator">The allocator to use.</param>
|
|
/// <returns>An array with a copy of the set's values.</returns>
|
|
public UnsafeArray<T> ToNativeArray(Allocator allocator)
|
|
{
|
|
return _helper.GetKeyArray(allocator);
|
|
}
|
|
|
|
public void Resize(int newSize, AllocationOption option = AllocationOption.None)
|
|
{
|
|
_helper.Resize(newSize);
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
_helper.Clear();
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public readonly void* GetUnsafePtr()
|
|
{
|
|
return _helper.Buffer;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_helper.Dispose();
|
|
}
|
|
}
|