Add CopyTo method in RingBuffer and optimize JMA and SMA implementations

This commit is contained in:
Miha Kralj
2025-12-30 22:30:47 -08:00
parent fa6cb1d623
commit 3f17a9a236
3 changed files with 108 additions and 97 deletions
+23
View File
@@ -412,6 +412,29 @@ public sealed class RingBuffer : IEnumerable<double>
}
}
/// <summary>
/// Copies elements to a destination span in chronological order.
/// Destination must have at least Count elements.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CopyTo(Span<double> destination)
{
if (_count == 0) return;
int start = _count == _capacity ? _head : 0;
if (start + _count <= _capacity)
{
_buffer.AsSpan(start, _count).CopyTo(destination);
}
else
{
int firstPartLength = _capacity - start;
_buffer.AsSpan(start, firstPartLength).CopyTo(destination);
_buffer.AsSpan(0, _count - firstPartLength).CopyTo(destination.Slice(firstPartLength));
}
}
/// <summary>
/// Creates a copy of the current state for bar correction support.
/// </summary>