Files
SourMesen e391fd8f9a Code: Add auto-formatting rules for C++ (clang-format) and enforce in CI via "clang-format" and "dotnet format" (#78)
* Code: Add auto-formatting rules for C++ projects (clang-format) and enforce code style via "clang-format" and "dotnet format"

* fix ui format warnings
2026-04-30 06:03:55 -07:00

51 lines
1.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Mesen.Utilities
{
public class VirtualArray<T> where T : struct
{
private const int BatchSize = 0x10000;
private int _size;
private int _loadedPage = -1;
private T[] _pageData;
private Func<int, int, T[]> _loader;
public VirtualArray(int size, Func<int, int, T[]> loader)
{
_size = size;
_pageData = new T[0];
_loader = loader;
}
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public T Get(int i)
{
if(_loadedPage != (i & ~(BatchSize - 1))) {
LoadPage(i);
}
return _pageData[i - _loadedPage];
}
[MethodImpl(MethodImplOptions.NoInlining)]
private void LoadPage(int i)
{
int start = i / BatchSize * BatchSize;
int end = Math.Min(_size, start + BatchSize) - 1;
_pageData = _loader(start, end);
_loadedPage = start;
}
public T this[int i]
{
get => Get(i);
}
}
}