Skip to content

Commit 6881e02

Browse files
authored
Adjust MovingAverage UT to use MeanAccumulator and be @nogc (#308)
* Adjust moving average UT * Address review * Remove %= and extra imports
1 parent 661f2d8 commit 6881e02

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

source/mir/math/sum.d

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -190,48 +190,55 @@ nothrow @nogc unittest
190190

191191
/// Moving mean
192192
version(mir_test)
193+
@safe pure nothrow @nogc
193194
unittest
194195
{
195-
import mir.ndslice.topology: linspace;
196+
import mir.internal.utility: isFloatingPoint;
196197
import mir.math.sum;
197-
import mir.array.allocation: array;
198+
import mir.ndslice.topology: linspace;
199+
import mir.rc.array: rcarray;
198200

199-
class MovingAverage
201+
struct MovingAverage(T)
202+
if (isFloatingPoint!T)
200203
{
201-
Summator!(double, Summation.precise) summator;
204+
import mir.math.stat: MeanAccumulator;
205+
206+
MeanAccumulator!(T, Summation.precise) meanAccumulator;
202207
double[] circularBuffer;
203208
size_t frontIndex;
204209

205-
double avg() @property const
210+
@disable this(this);
211+
212+
auto avg() @property const
206213
{
207-
return summator.sum / circularBuffer.length;
214+
return meanAccumulator.mean;
208215
}
209216

210217
this(double[] buffer)
211218
{
212219
assert(buffer.length);
213220
circularBuffer = buffer;
214-
summator = 0;
215-
summator.put(buffer);
221+
meanAccumulator.put(buffer);
216222
}
217223

218224
///operation without rounding
219-
void put(double x)
225+
void put(T x)
220226
{
221227
import mir.utility: swap;
222-
summator += x;
228+
meanAccumulator.summator += x;
223229
swap(circularBuffer[frontIndex++], x);
224-
summator -= x;
225-
frontIndex %= circularBuffer.length;
230+
frontIndex = frontIndex == circularBuffer.length ? 0 : frontIndex;
231+
meanAccumulator.summator -= x;
226232
}
227233
}
228234

229235
/// ma always keeps precise average of last 1000 elements
230-
auto ma = new MovingAverage(linspace!double([1000], [0.0, 999]).array);
236+
auto x = linspace!double([1000], [0.0, 999]).rcarray;
237+
auto ma = MovingAverage!double(x[]);
231238
assert(ma.avg == (1000 * 999 / 2) / 1000.0);
232239
/// move by 10 elements
233-
foreach(x; linspace!double([10], [1000.0, 1009.0]))
234-
ma.put(x);
240+
foreach(e; linspace!double([10], [1000.0, 1009.0]))
241+
ma.put(e);
235242
assert(ma.avg == (1010 * 1009 / 2 - 10 * 9 / 2) / 1000.0);
236243
}
237244

0 commit comments

Comments
 (0)