Skip to content

Commit

Permalink
and_or
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Jul 27, 2023
1 parent 5e79b8b commit 77a5874
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
48 changes: 46 additions & 2 deletions ta_lib/core/src/bool.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::series::Series;
use std::ops::{BitAnd, BitOr};

impl Series<f64> {
fn compare_series<F>(&self, rhs: &Series<f64>, f: F) -> Series<bool>
Expand Down Expand Up @@ -68,17 +69,60 @@ impl Series<f64> {
}

impl Series<bool> {
pub fn and(&self, rhs: &Series<bool>) -> Series<bool> {
pub fn and_series(&self, rhs: &Series<bool>) -> Series<bool> {
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<bool>) -> Series<bool> {
pub fn or_series(&self, rhs: &Series<bool>) -> Series<bool> {
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<bool> {
type Output = Self;

fn bitand(self, rhs: Self) -> Self::Output {
self.and_series(&rhs)
}
}

impl BitOr for Series<bool> {
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<bool> = 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<bool> = 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);
}
}
29 changes: 28 additions & 1 deletion ta_lib/core/src/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,30 @@ impl<T: AsRef<[f64]>> From<T> for Series<f64> {
}
}

impl<T> IntoIterator for Series<T> {
type Item = Option<T>;
type IntoIter = std::vec::IntoIter<Self::Item>;

fn into_iter(self) -> Self::IntoIter {
self.data.into_iter()
}
}

impl Into<Vec<f64>> for Series<f64> {
fn into(self) -> Vec<f64> {
self.data.into_iter().filter_map(|x| x).collect()
self.into_iter().filter_map(|x| x).collect()
}
}

impl Into<Vec<bool>> for Series<bool> {
fn into(self) -> Vec<bool> {
self.into_iter().filter_map(|x| x).collect()
}
}

impl Into<Series<bool>> for Series<f64> {
fn into(self) -> Series<bool> {
self.fmap(|opt| opt.map(|&f| f != 0.0))
}
}

Expand All @@ -149,6 +170,12 @@ impl PartialEq<Vec<Option<f64>>> for Series<f64> {
}
}

impl<T: PartialEq> PartialEq for Series<T> {
fn eq(&self, other: &Self) -> bool {
self.data == other.data
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 77a5874

Please sign in to comment.