Table of Contents

MovingMinMax<T> Class

Definition

Namespace
Bodu.Numerics
Assembly
Bodu.Numerics.dll
Package
Bodu.Numerics 1.0.0
Source
MovingMinMax{T}.cs

Maintains the minimum and maximum of the most recent N samples of a stream, updating in amortized O(1) as each new sample displaces the oldest.

public sealed class MovingMinMax<T> where T : INumber<T>

Type Parameters

T

The numeric type of the samples.

Inheritance
MovingMinMax<T>
Inherited Members
Extension Methods

Examples

var window = new MovingMinMax<int>(3);
window.Add(5);   // window: {5}          Min = 5, Max = 5
window.Add(1);   // window: {5, 1}       Min = 1, Max = 5
window.Add(4);   // window: {5, 1, 4}    Min = 1, Max = 5
window.Add(2);   // window: {1, 4, 2}    Min = 1, Max = 4

Remarks

This is the sliding-window extrema idiom: the window covers the last Capacity samples, Minimum and Maximum always describe exactly those samples, and until the window fills (IsFull) they describe the samples received so far. Both extrema are tracked exactly in T using the classic pair of monotonic deques, so each sample is pushed and popped at most once per deque - amortized O(1) per Add(T) with no per-sample allocation.

Samples must be finite: NaN and infinite values are rejected by Add(T). For the sum and mean over the same window, pair this type with MovingSum<T>; for whole-stream extrema use RunningStatistics<T>.

Constructors

MovingMinMax(int)

Initializes a new instance of the MovingMinMax<T> class with the given window size.

public MovingMinMax(int capacity)

Parameters

capacity int

The number of most-recent samples the window covers.

Exceptions

ArgumentOutOfRangeException

capacity ≤ 0.

Properties

Capacity

Gets the number of most-recent samples the window covers once full.

public int Capacity { get; }

Property Value

int

The fixed window size supplied at construction.

Count

Gets the number of samples currently in the window.

public int Count { get; }

Property Value

int

The sample count, at most Capacity.

IsEmpty

Gets a value indicating whether the window contains no samples.

public bool IsEmpty { get; }

Property Value

bool

true when Count is zero; otherwise false.

IsFull

Gets a value indicating whether the window has filled to Capacity and every new sample now displaces the oldest.

public bool IsFull { get; }

Property Value

bool

true when Count equals Capacity.

Maximum

Gets the largest sample currently in the window, tracked exactly in T.

public T Maximum { get; }

Property Value

T

The window maximum.

Exceptions

InvalidOperationException

The window is empty.

Minimum

Gets the smallest sample currently in the window, tracked exactly in T.

public T Minimum { get; }

Property Value

T

The window minimum.

Exceptions

InvalidOperationException

The window is empty.

Methods

Add(T)

Adds a sample to the window, displacing the oldest sample once the window is full.

public void Add(T value)

Parameters

value T

The sample to add. Must be finite.

Exceptions

ArgumentException

value is NaN or infinite.

Reset()

Resets the window to the empty state, discarding every tracked sample.

public void Reset()

ToString()

Returns a culture-invariant summary of the window state for diagnostics.

public override string ToString()

Returns

string

A string such as "Capacity = 3, Count = 2, Min = 1, Max = 5", or "Capacity = 3, Count = 0" when empty.

Applies to

ProductVersions
.NET8, 10