Switched to ThumbHasher over BlurHasher
This commit is contained in:
53
ThumbHash/Models/SpanOwner.cs
Normal file
53
ThumbHash/Models/SpanOwner.cs
Normal file
@ -0,0 +1,53 @@
|
||||
using System.Buffers;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace View_by_Distance.ThumbHash.Models;
|
||||
|
||||
internal readonly ref struct SpanOwner<T>
|
||||
{
|
||||
|
||||
private static ArrayPool<T> DefaultPool
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => ArrayPool<T>.Shared;
|
||||
}
|
||||
|
||||
private readonly T[] _Buffer;
|
||||
private readonly int _Length;
|
||||
|
||||
public static SpanOwner<T> Empty
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => new(0);
|
||||
}
|
||||
|
||||
public Span<T> Span
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
ref T r0 = ref MemoryMarshal.GetArrayDataReference(_Buffer);
|
||||
return MemoryMarshal.CreateSpan(ref r0, _Length);
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public SpanOwner<T> WithLength(int length) => new(length, _Buffer);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public SpanOwner(int length) : this(length, DefaultPool.Rent(length))
|
||||
{ }
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private SpanOwner(int length, T[] buffer)
|
||||
{
|
||||
_Length = length;
|
||||
_Buffer = buffer;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Dispose() =>
|
||||
DefaultPool.Return(_Buffer);
|
||||
|
||||
}
|
Reference in New Issue
Block a user