add 1.6 support and fix prepatch regressions
This commit is contained in:
@@ -7,13 +7,15 @@ namespace PerformanceFish.Cache;
|
||||
|
||||
public sealed class BitCellGrid
|
||||
{
|
||||
private readonly Map _map;
|
||||
private long[] _innerArray;
|
||||
private CellIndices _cellIndices;
|
||||
|
||||
public BitCellGrid(Map map)
|
||||
{
|
||||
_innerArray = null!;
|
||||
_cellIndices = null!;
|
||||
_map = map;
|
||||
_innerArray = Array.Empty<long>();
|
||||
_cellIndices = default;
|
||||
map.InvokeWhenCellIndicesReady(Initialize);
|
||||
}
|
||||
|
||||
@@ -23,12 +25,26 @@ public sealed class BitCellGrid
|
||||
_innerArray = new long[(Math.Max(0, _cellIndices.NumGridCells - 1) >> 6) + 1];
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private void EnsureInitialized()
|
||||
{
|
||||
if (_cellIndices.NumGridCells != _map.cellIndices.NumGridCells)
|
||||
Initialize(_map);
|
||||
}
|
||||
|
||||
public bool this[int index]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => (_innerArray[index >> 6] & (1L << (index & 63))) != 0L;
|
||||
get
|
||||
{
|
||||
EnsureInitialized();
|
||||
ValidateIndex(index);
|
||||
return (_innerArray[index >> 6] & (1L << (index & 63))) != 0L;
|
||||
}
|
||||
set
|
||||
{
|
||||
EnsureInitialized();
|
||||
ValidateIndex(index);
|
||||
ref var bucket = ref _innerArray[index >> 6];
|
||||
bucket ^= (-(long)value.AsInt() ^ bucket) & (1L << (index & 63));
|
||||
}
|
||||
@@ -37,7 +53,24 @@ public sealed class BitCellGrid
|
||||
public bool this[in IntVec3 c]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => this[c.CellToIndex(_cellIndices)];
|
||||
set => this[c.CellToIndex(_cellIndices)] = value;
|
||||
get
|
||||
{
|
||||
EnsureInitialized();
|
||||
return this[c.CellToIndex(_cellIndices)];
|
||||
}
|
||||
set
|
||||
{
|
||||
EnsureInitialized();
|
||||
this[c.CellToIndex(_cellIndices)] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private void ValidateIndex(int index)
|
||||
{
|
||||
if ((uint)index >= (uint)_cellIndices.NumGridCells)
|
||||
{
|
||||
throw new IndexOutOfRangeException($"BitCellGrid index {index} out of bounds for {_cellIndices.NumGridCells} cells on map size {_map.Size}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,38 +9,60 @@ namespace PerformanceFish.Cache;
|
||||
|
||||
public sealed class CellGrid<T>
|
||||
{
|
||||
private readonly Map _map;
|
||||
private T[] _innerArray;
|
||||
private CellIndices _cellIndices;
|
||||
|
||||
public CellGrid(Map map)
|
||||
{
|
||||
_innerArray = null!;
|
||||
_cellIndices = null!;
|
||||
_map = map;
|
||||
_innerArray = Array.Empty<T>();
|
||||
_cellIndices = default;
|
||||
map.InvokeWhenCellIndicesReady(Initialize);
|
||||
}
|
||||
|
||||
[MemberNotNull(nameof(_innerArray), nameof(_cellIndices))]
|
||||
[MemberNotNull(nameof(_innerArray))]
|
||||
private void Initialize(Map map)
|
||||
{
|
||||
_cellIndices = map.cellIndices;
|
||||
_innerArray = new T[_cellIndices.NumGridCells];
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private void EnsureInitialized()
|
||||
{
|
||||
if (_cellIndices.NumGridCells != _map.cellIndices.NumGridCells)
|
||||
Initialize(_map);
|
||||
}
|
||||
|
||||
public ref T this[int index]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => ref _innerArray[index];
|
||||
get
|
||||
{
|
||||
EnsureInitialized();
|
||||
return ref _innerArray[index];
|
||||
}
|
||||
}
|
||||
|
||||
public ref T this[in IntVec3 c]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => ref _innerArray[c.CellToIndex(_cellIndices)];
|
||||
get
|
||||
{
|
||||
EnsureInitialized();
|
||||
var index = c.CellToIndex(_cellIndices);
|
||||
return ref _innerArray[index];
|
||||
}
|
||||
}
|
||||
|
||||
public ref T this[CellIndex cellIndex]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => ref _innerArray[cellIndex.Value];
|
||||
get
|
||||
{
|
||||
EnsureInitialized();
|
||||
return ref _innerArray[cellIndex.Value];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user