Added UnsafeQueue;

This commit is contained in:
Misaki
2025-03-25 15:54:48 +09:00
parent 7bcd699eb9
commit cb69add265
5 changed files with 152 additions and 92 deletions

View File

@@ -1,14 +1,15 @@
using Misaki.HighPerformance.Unsafe.Collections.Contracts;
using Misaki.HighPerformance.Unsafe.Helpers;
using System.Collections;
using System.Collections;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Misaki.HighPerformance.Unsafe.Collections.Contracts;
using Misaki.HighPerformance.Unsafe.Helpers;
namespace Misaki.HighPerformance.Unsafe.Collections;
public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>, IEnumerable<T> where T : unmanaged
public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>, IEnumerable<T>
where T : unmanaged
{
public struct Enumerator : IEnumerator<T>
private struct Enumerator : IEnumerator<T>
{
private UnsafeArray<T> _collection;
private int _index;
@@ -44,24 +45,16 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>, IEnumerable<T> where
public T Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return _value;
}
get { return _value; }
}
object IEnumerator.Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return Current;
}
get { return Current; }
}
public void Dispose()
{
}
public void Dispose() { }
}
private T* _buffer;
@@ -70,9 +63,10 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>, IEnumerable<T> where
public readonly T* Buffer => _buffer;
public readonly int Size => _size;
public readonly ref T this[int index] => ref UnsafeUtilities.AsRef<T>(_buffer + index);
public readonly T this[int index] => UnsafeUtilities.ReadArrayElement<T>(_buffer, index);
public IEnumerator<T> GetEnumerator() => new Enumerator(ref this);
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public UnsafeArray(int size, AllocationType allocationType)
@@ -93,7 +87,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>, IEnumerable<T> where
return;
}
_buffer = (T*)NativeMemory.AlignedRealloc(_buffer, (nuint)(newSize * sizeof(T)), (nuint)AlignOf<T>());
_buffer = (T*)NativeMemory.AlignedRealloc(_buffer, (nuint)(newSize * sizeof(T)), AlignOf<T>());
_size = newSize;
}
@@ -110,4 +104,5 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>, IEnumerable<T> where
_buffer = null;
_size = 0;
}
}
}