diff --git a/ta_lib/core/src/bool.rs b/ta_lib/core/src/bool.rs index aa8b139f..fd0124c3 100644 --- a/ta_lib/core/src/bool.rs +++ b/ta_lib/core/src/bool.rs @@ -1,4 +1,5 @@ use crate::series::Series; +use std::ops::{BitAnd, BitOr}; impl Series { fn compare_series(&self, rhs: &Series, f: F) -> Series @@ -68,17 +69,60 @@ impl Series { } impl Series { - pub fn and(&self, rhs: &Series) -> Series { + pub fn and_series(&self, rhs: &Series) -> Series { self.zip_with(rhs, |a, b| match (a, b) { (Some(a_val), Some(b_val)) => Some(*a_val & *b_val), _ => None, }) } - pub fn or(&self, rhs: &Series) -> Series { + pub fn or_series(&self, rhs: &Series) -> Series { self.zip_with(rhs, |a, b| match (a, b) { (Some(a_val), Some(b_val)) => Some(*a_val | *b_val), _ => None, }) } } + +impl BitAnd for Series { + type Output = Self; + + fn bitand(self, rhs: Self) -> Self::Output { + self.and_series(&rhs) + } +} + +impl BitOr for Series { + type Output = Self; + + fn bitor(self, rhs: Self) -> Self::Output { + self.or_series(&rhs) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_bitand() { + let a = Series::from([1.0, 2.0, 3.0, 4.0, 5.0]); + let b = Series::from([1.0, 1.0, 6.0, 1.0, 1.0]); + let expected: Series = Series::from([0.0, 0.0, 0.0, 0.0, 0.0]).into(); + + let result = a.gt_series(&b) & a.lt_series(&b); + + assert_eq!(result, expected); + } + + #[test] + fn test_bitor() { + let a = Series::from([1.0, 2.0, 3.0, 4.0, 5.0]); + let b = Series::from([1.0, 1.0, 1.0, 1.0, 1.0]); + let expected: Series = Series::from([0.0, 1.0, 1.0, 1.0, 1.0]).into(); + + let result = a.gt_series(&b) | a.lt_series(&b); + + assert_eq!(result, expected); + } +} diff --git a/ta_lib/core/src/series.rs b/ta_lib/core/src/series.rs index 0ada2ff9..96d15bdd 100644 --- a/ta_lib/core/src/series.rs +++ b/ta_lib/core/src/series.rs @@ -137,9 +137,30 @@ impl> From for Series { } } +impl IntoIterator for Series { + type Item = Option; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.data.into_iter() + } +} + impl Into> for Series { fn into(self) -> Vec { - self.data.into_iter().filter_map(|x| x).collect() + self.into_iter().filter_map(|x| x).collect() + } +} + +impl Into> for Series { + fn into(self) -> Vec { + self.into_iter().filter_map(|x| x).collect() + } +} + +impl Into> for Series { + fn into(self) -> Series { + self.fmap(|opt| opt.map(|&f| f != 0.0)) } } @@ -149,6 +170,12 @@ impl PartialEq>> for Series { } } +impl PartialEq for Series { + fn eq(&self, other: &Self) -> bool { + self.data == other.data + } +} + #[cfg(test)] mod tests { use super::*;