You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// Represents the possible errors that can occur in the `Moving` struct.
182
+
pubenumMovingError{
183
+
/// Error indicating that a negative value was attempted to be added to an unsigned type.
184
+
NegativeValueToUnsignedType,
185
+
186
+
/// Error indicating that an overflow occurred during an operation.
187
+
/// Note: This is unlikely to occur with floating-point operations.
188
+
Overflow,
189
+
190
+
/// Error indicating that an underflow occurred during an operation.
191
+
Underflow,
192
+
193
+
/// Error indicating that the count of values has overflowed.
194
+
CountOverflow,
195
+
196
+
/// Error indicating that a value has reached or exceeded the specified threshold.
197
+
///
198
+
/// This error is triggered when a value added to the `Moving` instance meets or exceeds
199
+
/// the threshold value specified during the creation of the instance. This can be used
200
+
/// to signal that a certain limit has been reached, which might require special handling
201
+
/// or termination of further processing.
202
+
ThresholdReached,
171
203
}
172
204
173
205
impl<T>Moving<T>
174
206
where
175
-
T:FromUsize + ToFloat64 + Sign,
207
+
T:Sign + ToPrimitive,
176
208
{
209
+
/// Creates a new [`Moving<T>`] instance with default values.
210
+
///
211
+
/// # Returns
212
+
///
213
+
/// A new instance of [`Moving<T>`].
214
+
/// Values can ge added to this instance to calculate the moving average.
177
215
pubfnnew() -> Self{
178
216
Self{
179
217
count:0,
180
218
mean:0.0,
219
+
is_error:false,
220
+
threshold: f64::MAX,
221
+
phantom: std::marker::PhantomData,
222
+
}
223
+
}
224
+
225
+
/// Creates a new [`Moving<T>`] instance with a specified threshold.
226
+
///
227
+
/// This method initializes the `count` to 0, `mean` to 0.0, `is_error` to `false`,
228
+
/// and `threshold` to the provided value.
229
+
///
230
+
/// # Parameters
231
+
///
232
+
/// - `threshold`: The threshold value to be used for the new instance.
233
+
///
234
+
/// # Returns
235
+
///
236
+
/// A new instance of [`Moving<T>`] with the specified threshold.
237
+
/// Values can be added to this instance to calculate the moving average.
238
+
/// When values are greater than or equal to the threshold, the [`MovingResults::ThresholdReached`] variant is returned and no further values are added.
239
+
pubfnnew_with_threshold(threshold:f64) -> Self{
240
+
Self{
241
+
count:0,
242
+
mean:0.0,
243
+
is_error:false,
244
+
threshold,
181
245
phantom: std::marker::PhantomData,
182
246
}
183
247
}
248
+
/// Adds a value to the current statistical collection, updating the mean accordingly.
249
+
///
250
+
/// This function converts the input value to an `f64` and then updates the mean of the collection
251
+
/// based on the new value.
252
+
///
253
+
/// # Returns
254
+
/// If the mean is less than the threshold, the [`MovingResults::Value`] variant is returned with the new mean.
255
+
///
256
+
/// # Panics
257
+
///
258
+
/// Panics if the type `T` is unsigned and a negative value is attempted to be added. This is because
259
+
/// negative values are not allowed for unsigned types. If negative values are needed, it is recommended
0 commit comments