parent
af72792875
commit
5e956f5989
3 changed files with 128 additions and 3 deletions
@ -0,0 +1,51 @@ |
||||
using System; |
||||
using Unity.Collections; |
||||
using Unity.Collections.LowLevel.Unsafe; |
||||
|
||||
namespace GameCore.LowLevel |
||||
{ |
||||
public ref struct TypedBuffer<T> where T : unmanaged |
||||
{ |
||||
public NativeList<byte> buffer; |
||||
|
||||
public int Length => buffer.Length / UnsafeUtility.SizeOf<T>(); |
||||
|
||||
public TypedBuffer(NativeList<byte> buffer) |
||||
{ |
||||
this.buffer = buffer; |
||||
} |
||||
|
||||
public unsafe void Add(T data) |
||||
{ |
||||
var oldLength = buffer.Length; |
||||
buffer.Resize(oldLength + sizeof(T), NativeArrayOptions.UninitializedMemory); |
||||
UnsafeUtility.CopyStructureToPtr(ref data, (byte*)buffer.GetUnsafePtr() + oldLength); |
||||
} |
||||
|
||||
public unsafe T this[int index] |
||||
{ |
||||
get |
||||
{ |
||||
if (index < 0 || index >= Length) |
||||
throw new IndexOutOfRangeException(nameof(index)); |
||||
var p = (T*)buffer.GetUnsafeReadOnlyPtr(); |
||||
return p[index]; |
||||
} |
||||
} |
||||
} |
||||
|
||||
public static class TypedBufferExtensions |
||||
{ |
||||
public static TypedBuffer<T> ToTypedBuffer<T>(this NativeList<byte> buffer) |
||||
where T : unmanaged |
||||
=> new(buffer); |
||||
|
||||
public static unsafe void Sort<T>(this TypedBuffer<T> typedBuffer) |
||||
where T : unmanaged, IComparable<T> |
||||
{ |
||||
var p = (T*)typedBuffer.buffer.GetUnsafePtr(); |
||||
var l = typedBuffer.buffer.Length / sizeof(T); |
||||
NativeSortExtension.Sort(p, l); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,3 @@ |
||||
fileFormatVersion: 2 |
||||
guid: edfdc737cb64451ebdd0dc178e8c07af |
||||
timeCreated: 1767469202 |
||||
Loading…
Reference in new issue