Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Jul 13, 2023
1 parent fddde71 commit a42bd35
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 45 deletions.
24 changes: 0 additions & 24 deletions ta_lib/core/src/math.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,6 @@
use crate::series::Series;

impl Series<f64> {
pub fn window<F>(&self, period: usize, f: F) -> Self
where
F: Fn(&[f64], usize, usize) -> f64,
{
let len = self.len();
let mut result = Self::empty(len);
let mut window = vec![0.0; period];
let mut pos = 0;

for i in 0..len {
if let Some(value) = self[i] {
window[pos] = value;

let size = (i + 1).min(period);

result[i] = Some(f(&window[0..size], size, i));

pos = (pos + 1) % period;
}
}

result
}

pub fn max(&self, scalar: f64) -> Self {
self.fmap(|val| val.map(|v| v.max(scalar)).or(Some(scalar)))
}
Expand Down
59 changes: 38 additions & 21 deletions ta_lib/core/src/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl<T: Clone> Series<T> {

pub fn empty(length: usize) -> Self {
Self {
data: repeat(None).take(length).collect(),
data: vec![None; length],
}
}

Expand Down Expand Up @@ -66,28 +66,43 @@ impl<T> IndexMut<usize> for Series<T> {
}

impl Series<f64> {
fn extreme_value<F>(&self, period: usize, comparison: F) -> Self
pub fn window<F>(&self, period: usize, f: F) -> Self
where
F: Fn(&f64, &f64) -> bool,
F: Fn(&[f64], usize, usize) -> f64,
{
let len = self.len();
let mut extreme_values = Self::empty(len);
let mut indices: Vec<usize> = Vec::new();
let mut result = Self::empty(len);
let mut window = vec![0.0; period];
let mut pos = 0;

for i in 0..len {
let start = if i >= period { i - period + 1 } else { 0 };
if let Some(value) = self[i] {
window[pos] = value;

indices.retain(|&j| j >= start);
indices.retain(|&j| {
comparison(&self[j].unwrap_or(f64::NAN), &self[i].unwrap_or(f64::NAN))
});
let size = (i + 1).min(period);

indices.push(i);
result[i] = Some(f(&window[0..size], size, i));

extreme_values[i] = self[indices[0]].clone();
pos = (pos + 1) % period;
}
}

extreme_values
result
}

fn extreme_value<F>(&self, period: usize, comparison: F) -> Self
where
F: Fn(&f64, &f64) -> bool,
{
self.window(period, |window, _, _| {
window.iter().fold(f64::NAN, |acc, x| {
if acc.is_nan() || comparison(&x, &acc) {
*x
} else {
acc
}
})
})
}

pub fn nz(&self, replacement: Option<f64>) -> Self {
Expand Down Expand Up @@ -141,14 +156,6 @@ impl PartialEq<Vec<Option<f64>>> for Series<f64> {
}
}

impl Series<bool> {}

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

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -163,6 +170,16 @@ mod tests {
assert_eq!(result.len(), expected);
}

#[test]
fn test_empty() {
let len = 4;
let expected = vec![None, None, None, None];

let result = Series::empty(len);

assert_eq!(result, expected);
}

#[test]
fn test_change() {
let source = vec![
Expand Down

0 comments on commit a42bd35

Please sign in to comment.