Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cargo-features = ["edition2024"]

[package]
name = "sliding_features"
version = "9.1.0"
version = "9.2.0"
authors = ["MathisWellmann <wellmannmathis@gmail.com>"]
edition = "2024"
license-file = "LICENSE"
Expand Down
Binary file modified img/trend_flex.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions src/rolling/first.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! Retains the first value it sees and never updates again.

use crate::View;

/// Retains the first value it ever sees and never updates it again.
#[derive(Default, Clone, Debug)]
pub struct First<T> {
out: Option<T>,
}

impl<T> First<T> {
/// Create a new First View
#[inline(always)]
pub fn new() -> First<T> {
First { out: None }
}
}

impl<T: num::Float> View<T> for First<T> {
fn update(&mut self, val: T) {
debug_assert!(val.is_finite(), "value must be finite");
if self.out.is_none() {
self.out = Some(val);
}
}

fn last(&self) -> Option<T> {
self.out
}
}

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

#[test]
fn first() {
let mut f = First::new();
assert_eq!(f.last(), None);
f.update(42.0);
assert_eq!(f.last(), Some(42.0));
f.update(7.0);
assert_eq!(f.last(), Some(42.0));
f.update(99.0);
assert_eq!(f.last(), Some(42.0));
}
}
2 changes: 2 additions & 0 deletions src/rolling/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//! This module contains `View` implementations that are updated on a rolling basis, but don't maintain a sliding window with history.

mod drawdown;
mod first;
mod ln_return;
mod welford_rolling;

pub use drawdown::Drawdown;
pub use first::First;
pub use ln_return::LnReturn;
pub use welford_rolling::WelfordRolling;
Loading