style patterns

This commit is contained in:
Miha Kralj
2026-01-25 16:01:45 -08:00
parent 2836f253c4
commit e59665c8f0
399 changed files with 6892 additions and 1323 deletions
+9 -2
View File
@@ -264,14 +264,21 @@ public class MedianTests
// Arrange
int period = 5;
double[] data = new double[20];
for (int i = 0; i < data.Length; i++) data[i] = i;
for (int i = 0; i < data.Length; i++)
{
data[i] = i;
}
// Act
double[] output = new double[data.Length];
Median.Batch(data, output, period);
var series = new TSeries();
for (int i = 0; i < data.Length; i++) series.Add(new TValue(DateTime.MinValue, data[i]));
for (int i = 0; i < data.Length; i++)
{
series.Add(new TValue(DateTime.MinValue, data[i]));
}
var batchSeries = Median.Batch(series, period);
// Assert
@@ -86,7 +86,10 @@ public sealed class MedianValidationTests : IDisposable
private static double CalculateMedian(List<double> sortedWindow)
{
int count = sortedWindow.Count;
if (count == 0) return 0; // Or NaN
if (count == 0)
{
return 0; // Or NaN
}
int mid = count / 2;
if (count % 2 != 0)
+39 -7
View File
@@ -38,7 +38,9 @@ public sealed class Median : AbstractBase, IDisposable
public Median(int period)
{
if (period <= 0)
{
throw new ArgumentException("Period must be greater than 0", nameof(period));
}
_period = period;
_buffer = new RingBuffer(period);
@@ -76,7 +78,10 @@ public sealed class Median : AbstractBase, IDisposable
/// </summary>
public override void Prime(ReadOnlySpan<double> source, TimeSpan? step = null)
{
if (source.Length == 0) return;
if (source.Length == 0)
{
return;
}
_buffer.Clear();
int warmupLength = Math.Min(source.Length, WarmupPeriod);
@@ -151,7 +156,10 @@ public sealed class Median : AbstractBase, IDisposable
public override TSeries Update(TSeries source)
{
if (source.Count == 0) return [];
if (source.Count == 0)
{
return [];
}
int len = source.Count;
var t = new List<long>(len);
@@ -178,7 +186,10 @@ public sealed class Median : AbstractBase, IDisposable
// validCount = elements in sortedBuffer BEFORE insertion
int validCount = _buffer.Count - 1;
int index = Array.BinarySearch(_sortedBuffer, 0, validCount, value);
if (index < 0) index = ~index;
if (index < 0)
{
index = ~index;
}
if (index < validCount)
{
@@ -224,12 +235,20 @@ public sealed class Median : AbstractBase, IDisposable
public static void Batch(ReadOnlySpan<double> source, Span<double> output, int period)
{
if (source.Length != output.Length)
{
throw new ArgumentException("Source and output must have the same length", nameof(output));
}
if (period <= 0)
{
throw new ArgumentException("Period must be greater than 0", nameof(period));
}
int len = source.Length;
if (len == 0) return;
if (len == 0)
{
return;
}
// Always use ArrayPool to avoid CS8353 stackalloc escape issues
double[] rentedSorted = ArrayPool<double>.Shared.Rent(period);
@@ -266,7 +285,10 @@ public sealed class Median : AbstractBase, IDisposable
windowIdx = (windowIdx + 1) % period;
int newIndex = BinarySearchSpan(sortedBuffer, count, val);
if (newIndex < 0) newIndex = ~newIndex;
if (newIndex < 0)
{
newIndex = ~newIndex;
}
if (newIndex < count)
{
@@ -300,11 +322,18 @@ public sealed class Median : AbstractBase, IDisposable
int mid = lo + ((hi - lo) >> 1);
int cmp = span[mid].CompareTo(value);
if (cmp == 0)
{
return mid;
}
if (cmp < 0)
{
lo = mid + 1;
}
else
{
hi = mid - 1;
}
}
return ~lo;
}
@@ -325,7 +354,10 @@ public sealed class Median : AbstractBase, IDisposable
/// </summary>
public new void Dispose()
{
if (_disposed) return;
if (_disposed)
{
return;
}
if (_source != null)
{
@@ -335,4 +367,4 @@ public sealed class Median : AbstractBase, IDisposable
_disposed = true;
}
}
}