matrix enumeration full available

This commit is contained in:
Youmu Liu
2025-08-29 21:50:24 +08:00
parent 78f97fb314
commit 6bebe1ca85
5 changed files with 110 additions and 80 deletions
+53 -38
View File
@@ -32,6 +32,7 @@ using StgSharp.HighPerformance;
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using hlsfAllocator = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocator;
using hlsfHandle = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocationHandle;
@@ -57,18 +58,45 @@ namespace StgSharp.Mathematics
set;
}
public int ColumnCount { get; init; }
public static hlsfAllocator? DefaultAllocator
{
get
{
ArgumentNullException.ThrowIfNull(field, nameof(DefaultAllocator));
return null;
}
set;
} = null;
public int RowCount { get; init; }
public int ColumnLength { get; init; }
protected int KernelColumnCount { get; init; }
public int RowLength { get; init; }
protected int KernelRowCount { get; init; }
public static MatrixAllocation DefaultAllocation { get; set; } = MatrixAllocation.GC;
protected int KernelColumnLength { get; init; }
protected int KernelRowLength { get; init; }
public static Matrix<T> FromDefault(int columnLength, int rowLength)
{
return DefaultAllocation switch
{
MatrixAllocation.GC => FromGC(columnLength, rowLength),
MatrixAllocation.HLSF => FromHlsf(columnLength, rowLength, DefaultAllocator!),
_ => throw new NotSupportedException("This allocation is not supported."),
};
}
public static Matrix<T> FromGC(int columnLength, int rowLength)
{
return new GCMatrix<T>(columnLength, rowLength);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix<T> FromHlsf(int column, int row, hlsfAllocator allocator)
public static Matrix<T> FromHlsf(int columnLength, int rowLength, hlsfAllocator allocator)
{
return new HlsfMatrix<T>(column, row, allocator);
return new HlsfMatrix<T>(columnLength, rowLength, allocator);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -86,66 +114,61 @@ namespace StgSharp.Mathematics
private protected void AssertIndex(int column, int row)
{
if (column < 0 || column >= ColumnCount) {
if (column < 0 || column >= ColumnLength) {
throw new ArgumentOutOfRangeException(nameof(column));
}
if (row < 0 || row >= RowCount) {
if (row < 0 || row >= RowLength) {
throw new ArgumentOutOfRangeException(nameof(row));
}
}
public readonly unsafe ref struct MatrixSegmentEnumeration(
MatrixKernel* source,
int size,
int stride,
int secondaryStride)
int size,
int secondaryStride,
int secondarySize)
{
private readonly MatrixKernel* _source = source;
private readonly int _secondarySize = secondarySize;
private readonly int _secondaryStride = secondaryStride;
private readonly int _size = size;
private readonly int _stride = stride;
public Enumerator GetEnumerator()
{
return new Enumerator(_source, _size, _stride, _size, _secondaryStride);
return new Enumerator(in this);
}
public ref struct Enumerator(MatrixKernel* source, int end, int stride, int size, int secondaryStride)
public ref struct Enumerator(in MatrixSegmentEnumeration enumeration)
{
private MatrixKernel* _current = source - stride;
private readonly MatrixKernel* _end = source + end;
private readonly MatrixKernel* _source = source;
private readonly int _secondaryStride = secondaryStride;
private readonly int _size = size;
private readonly int _stride = stride;
private MatrixKernel* _current = enumeration._source - enumeration._stride;
private readonly MatrixKernel* _end = enumeration._source + enumeration._size;
private readonly int _secondarySize = enumeration._secondarySize;
private readonly int _secondaryStride = enumeration._secondaryStride;
private readonly int _stride = enumeration._stride;
public readonly MatrixSegment Current
public readonly Segment Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new(_current, _size, _secondaryStride);
get => new(_current, _secondarySize, _secondaryStride);
}
public readonly void Dispose() { }
public bool MoveNext()
{
_current += _stride;
return _current < _end;
}
public void Reset()
{
_current = _source - _stride;
}
}
}
public readonly unsafe ref struct MatrixSegment(MatrixKernel* source, int size, int stride)
public readonly unsafe ref struct Segment(MatrixKernel* source, int size, int stride)
{
private readonly MatrixKernel* _source = source;
@@ -168,11 +191,10 @@ namespace StgSharp.Mathematics
return new Enumerator(_source, _size, _stride);
}
public ref struct Enumerator(MatrixKernel* source, int end, int stride)
public ref struct Enumerator(MatrixKernel* source, int count, int stride)
{
private readonly MatrixKernel* _end = source + end;
private readonly MatrixKernel* _source = source;
private readonly MatrixKernel* _end = source + (count * stride);
private readonly int _stride = stride;
public MatrixKernel* Current
@@ -183,19 +205,12 @@ namespace StgSharp.Mathematics
private set;
} = source - stride;
public void Dispose() { }
public bool MoveNext()
{
Current += _stride;
return Current < _end;
}
public void Reset()
{
Current = _source - _stride;
}
}
}