AxibugEmuOnline/AxibugEmuOnline.Client/Assets/VirtualNes.Core/CoreLibs/ByteArrayRef.cs

78 lines
2.0 KiB
C#
Raw Normal View History

2024-08-05 11:52:43 +08:00
using System;
namespace VirtualNes.Core
2024-07-30 18:53:36 +08:00
{
2024-08-07 17:45:38 +08:00
public class ArrayRef<T>
2024-07-30 18:53:36 +08:00
{
2024-11-13 18:42:30 +08:00
public T[] RawArray => m_rawArray;
2024-08-07 17:45:38 +08:00
private T[] m_rawArray;
2024-07-30 18:53:36 +08:00
private int m_offset;
private int m_length;
2024-08-02 16:50:16 +08:00
public int Offset
{
get => m_offset;
set
{
var gap = value - m_offset;
m_length -= gap;
}
}
2024-08-07 17:45:38 +08:00
public ArrayRef() { }
public ArrayRef(T[] array, int offset, int length)
2024-08-02 16:50:16 +08:00
{
SetArray(array, offset, length);
}
2024-08-07 17:45:38 +08:00
public ArrayRef(T[] array) : this(array, 0, array.Length) { }
public ArrayRef(T[] array, int offset) : this(array, offset, array.Length - offset) { }
2024-08-05 11:52:43 +08:00
2024-08-07 17:45:38 +08:00
public void SetArray(T[] array, int offset, int length)
2024-07-30 18:53:36 +08:00
{
m_rawArray = array;
m_offset = offset;
m_length = length;
}
2024-08-07 17:45:38 +08:00
public void SetArray(T[] array, int offset)
2024-08-06 13:49:24 +08:00
{
m_rawArray = array;
m_offset = offset;
m_length = array.Length - offset;
}
2024-08-07 17:45:38 +08:00
public T this[int index]
2024-07-30 18:53:36 +08:00
{
get
{
return m_rawArray[m_offset + index];
}
set
{
m_rawArray[(m_offset + index)] = value;
}
}
2024-08-05 11:52:43 +08:00
2024-09-13 17:39:48 +08:00
public void WriteTo(T[] source, int start, int length)
{
Array.Copy(source, 0, m_rawArray, Offset + start, length);
}
2024-08-29 17:20:01 +08:00
public Span<T> Span(int start, int length)
{
return new Span<T>(m_rawArray, start + Offset, length);
}
2024-08-07 17:45:38 +08:00
public static implicit operator ArrayRef<T>(T[] array)
2024-08-05 11:52:43 +08:00
{
2024-08-07 17:45:38 +08:00
return new ArrayRef<T>(array);
2024-08-05 11:52:43 +08:00
}
2024-08-07 17:45:38 +08:00
public static implicit operator Span<T>(ArrayRef<T> array)
2024-08-05 11:52:43 +08:00
{
2024-08-07 17:45:38 +08:00
return new Span<T>(array.m_rawArray, array.Offset, array.m_length);
2024-08-05 11:52:43 +08:00
}
2024-07-30 18:53:36 +08:00
}
}