feat(core): Add SignalAbs trait for numeric signals

This commit is contained in:
Anand Balakrishnan 2023-04-04 11:55:48 -07:00
parent 55b7cdd075
commit 6e41380262
No known key found for this signature in database
5 changed files with 72 additions and 23 deletions

View file

@ -1,6 +1,6 @@
use num_traits::{Num, NumCast, Signed};
use super::traits::{BaseSignal, LinearInterpolatable};
use super::traits::{BaseSignal, LinearInterpolatable, SignalAbs};
use crate::signals::utils::{apply1, apply2, apply2_const, sync_with_intersection};
use crate::signals::{ConstantSignal, Signal};
@ -282,3 +282,45 @@ where
apply2(self, other, |lhs, rhs| lhs.pow(rhs))
}
}
macro_rules! signal_abs_impl {
(const $( $ty:ty ), *) => {
$(
impl SignalAbs for ConstantSignal<$ty> {
/// Return the absolute value for the signal
fn abs(&self) -> ConstantSignal<$ty> {
ConstantSignal::new(self.value.abs())
}
}
)*
};
($( $ty:ty ), *) => {
$(
impl SignalAbs for Signal<$ty> {
/// Return the absolute value for each sample in the signal
fn abs(&self) -> Signal<$ty> {
apply1(self, |v| v.abs())
}
}
)*
};
}
signal_abs_impl!(i64, f32, f64);
impl SignalAbs for Signal<u64> {
/// Return the absolute value for each sample in the signal
fn abs(&self) -> Signal<u64> {
apply1(self, |v| v)
}
}
signal_abs_impl!(const i64, f32, f64);
impl SignalAbs for ConstantSignal<u64> {
/// Return the absolute value for the signal
fn abs(&self) -> ConstantSignal<u64> {
ConstantSignal::new(self.value)
}
}