normalization of methods

This commit is contained in:
Miha Kralj
2026-02-10 21:33:16 -08:00
parent 915d7a007b
commit 6d6259a47d
527 changed files with 10525 additions and 2123 deletions
+5 -5
View File
@@ -111,7 +111,7 @@ public class HanmaTests
double[] input = series.Values.ToArray();
double[] output = new double[input.Length];
Hanma.Calculate(input.AsSpan(), output.AsSpan(), 10);
Hanma.Batch(input.AsSpan(), output.AsSpan(), 10);
for (int i = 0; i < input.Length; i++)
{
@@ -292,7 +292,7 @@ public class HanmaTests
var tValues = series.Values.ToArray();
var spanInput = new ReadOnlySpan<double>(tValues);
var spanOutput = new double[tValues.Length];
Hanma.Calculate(spanInput, spanOutput, period);
Hanma.Batch(spanInput, spanOutput, period);
double spanResult = spanOutput[^1];
// 3. Streaming Mode
@@ -325,8 +325,8 @@ public class HanmaTests
double[] output = new double[5];
double[] wrongSizeOutput = new double[3];
Assert.Throws<ArgumentException>(() => Hanma.Calculate(source.AsSpan(), output.AsSpan(), 0));
Assert.Throws<ArgumentException>(() => Hanma.Calculate(source.AsSpan(), wrongSizeOutput.AsSpan(), 3));
Assert.Throws<ArgumentException>(() => Hanma.Batch(source.AsSpan(), output.AsSpan(), 0));
Assert.Throws<ArgumentException>(() => Hanma.Batch(source.AsSpan(), wrongSizeOutput.AsSpan(), 3));
}
[Fact]
@@ -335,7 +335,7 @@ public class HanmaTests
double[] source = [100, 110, double.NaN, 120, 130];
double[] output = new double[5];
Hanma.Calculate(source.AsSpan(), output.AsSpan(), 3);
Hanma.Batch(source.AsSpan(), output.AsSpan(), 3);
foreach (var val in output)
{
@@ -104,7 +104,7 @@ public class HanmaValidationTests
// Span
double[] input = series.Values.ToArray();
double[] spanOutput = new double[input.Length];
Hanma.Calculate(input.AsSpan(), spanOutput.AsSpan(), period);
Hanma.Batch(input.AsSpan(), spanOutput.AsSpan(), period);
// All should match
for (int i = 0; i < series.Count; i++)
+18 -6
View File
@@ -24,6 +24,7 @@ public sealed class Hanma : AbstractBase
private readonly ITValuePublisher? _source;
private readonly TValuePublishedHandler? _pubHandler;
private bool _isNew = true;
private bool _disposed;
[StructLayout(LayoutKind.Auto)]
private record struct State(double LastValidValue, bool IsInitialized);
@@ -56,7 +57,7 @@ public sealed class Hanma : AbstractBase
}
/// <summary>
///
/// Creates HANMA chained to a source publisher for event-based updates.
/// </summary>
/// <param name="source">Data source for event-based updates</param>
/// <param name="period">Lookback period for the Hanning window (default: 10)</param>
@@ -73,9 +74,13 @@ public sealed class Hanma : AbstractBase
protected override void Dispose(bool disposing)
{
if (disposing && _source != null && _pubHandler != null)
if (!_disposed)
{
_source.Pub -= _pubHandler;
if (disposing && _source != null && _pubHandler != null)
{
_source.Pub -= _pubHandler;
}
_disposed = true;
}
base.Dispose(disposing);
}
@@ -177,7 +182,7 @@ public sealed class Hanma : AbstractBase
var tSpan = CollectionsMarshal.AsSpan(t);
var vSpan = CollectionsMarshal.AsSpan(v);
Calculate(source.Values, vSpan, _period);
Batch(source.Values, vSpan, _period);
source.Times.CopyTo(tSpan);
// Restore state
@@ -277,7 +282,7 @@ public sealed class Hanma : AbstractBase
/// <param name="period">Lookback period for the Hanning window (default: 10)</param>
/// <exception cref="ArgumentException">Thrown when output length doesn't match source length.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Calculate(ReadOnlySpan<double> source, Span<double> output, int period = 10)
public static void Batch(ReadOnlySpan<double> source, Span<double> output, int period = 10)
{
if (period <= 0)
{
@@ -425,6 +430,13 @@ public sealed class Hanma : AbstractBase
}
}
public static (TSeries Results, Hanma Indicator) Calculate(TSeries source, int period = 10)
{
var indicator = new Hanma(period);
TSeries results = indicator.Update(source);
return (results, indicator);
}
public override void Reset()
{
_buffer.Clear();
@@ -432,4 +444,4 @@ public sealed class Hanma : AbstractBase
_p_state = _state;
Last = default;
}
}
}