diff --git a/crates/brk_computer/src/states/realized.rs b/crates/brk_computer/src/states/realized.rs index 851178f1e..b2d160c86 100644 --- a/crates/brk_computer/src/states/realized.rs +++ b/crates/brk_computer/src/states/realized.rs @@ -28,28 +28,16 @@ impl RealizedState { if supply_state.value.is_not_zero() { if self.realized_cap == Dollars::NAN { self.realized_cap = Dollars::ZERO; + // self.realized_profit = Dollars::ZERO; + // self.realized_loss = Dollars::ZERO; + // self.value_created = Dollars::ZERO; + // self.adjusted_value_created = Dollars::ZERO; + // self.value_destroyed = Dollars::ZERO; + // self.adjusted_value_destroyed = Dollars::ZERO; } + self.realized_cap += price * Bitcoin::from(supply_state.value); } - - // if self.realized_profit == Dollars::NAN { - // self.realized_profit = Dollars::ZERO; - // } - // if self.realized_loss == Dollars::NAN { - // self.realized_loss = Dollars::ZERO; - // } - // if self.value_created == Dollars::NAN { - // self.value_created = Dollars::ZERO; - // } - // if self.adjusted_value_created == Dollars::NAN { - // self.adjusted_value_created = Dollars::ZERO; - // } - // if self.value_destroyed == Dollars::NAN { - // self.value_destroyed = Dollars::ZERO; - // } - // if self.adjusted_value_destroyed == Dollars::NAN { - // self.adjusted_value_destroyed = Dollars::ZERO; - // } } pub fn decrement(&mut self, supply_state: &SupplyState, price: Dollars) { diff --git a/crates/brk_computer/src/vecs/grouped/ratio_from_dateindex.rs b/crates/brk_computer/src/vecs/grouped/ratio_from_dateindex.rs index 9d50b1520..937fbceb7 100644 --- a/crates/brk_computer/src/vecs/grouped/ratio_from_dateindex.rs +++ b/crates/brk_computer/src/vecs/grouped/ratio_from_dateindex.rs @@ -7,7 +7,6 @@ use brk_vec::{ AnyCollectableVec, AnyIterableVec, AnyVec, CollectableVec, Compressed, EagerVec, Result, StoredIndex, VecIterator, Version, }; -// use rayon::prelude::*; use crate::{ utils::get_percentile, diff --git a/crates/brk_core/src/structs/bitcoin.rs b/crates/brk_core/src/structs/bitcoin.rs index b61812e8f..045c0e112 100644 --- a/crates/brk_core/src/structs/bitcoin.rs +++ b/crates/brk_core/src/structs/bitcoin.rs @@ -1,23 +1,14 @@ -use std::ops::{Add, Div, Mul}; +use std::{ + cmp::Ordering, + ops::{Add, Div, Mul}, +}; use serde::Serialize; use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout}; use super::{Sats, StoredF64}; -#[derive( - Debug, - Default, - Clone, - Copy, - PartialEq, - PartialOrd, - FromBytes, - Immutable, - IntoBytes, - KnownLayout, - Serialize, -)] +#[derive(Debug, Default, Clone, Copy, FromBytes, Immutable, IntoBytes, KnownLayout, Serialize)] pub struct Bitcoin(f64); impl Add for Bitcoin { @@ -71,11 +62,34 @@ impl From for Bitcoin { } } +impl PartialEq for Bitcoin { + fn eq(&self, other: &Self) -> bool { + match (self.0.is_nan(), other.0.is_nan()) { + (true, true) => true, + (true, false) => false, + (false, true) => false, + (false, false) => self.0 == other.0, + } + } +} + impl Eq for Bitcoin {} #[allow(clippy::derive_ord_xor_partial_ord)] -impl Ord for Bitcoin { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - self.0.partial_cmp(&other.0).unwrap() +impl PartialOrd for Bitcoin { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +#[allow(clippy::derive_ord_xor_partial_ord)] +impl Ord for Bitcoin { + fn cmp(&self, other: &Self) -> Ordering { + match (self.0.is_nan(), other.0.is_nan()) { + (true, true) => Ordering::Equal, + (true, false) => Ordering::Less, + (false, true) => Ordering::Greater, + (false, false) => self.0.partial_cmp(&other.0).unwrap(), + } } } diff --git a/crates/brk_core/src/structs/dollars.rs b/crates/brk_core/src/structs/dollars.rs index d4ad32d74..7095b7e93 100644 --- a/crates/brk_core/src/structs/dollars.rs +++ b/crates/brk_core/src/structs/dollars.rs @@ -13,18 +13,7 @@ use crate::CheckedSub; use super::{Bitcoin, Cents, Close, Sats, StoredF32, StoredF64}; #[derive( - Debug, - Default, - Clone, - Copy, - PartialEq, - PartialOrd, - Deref, - FromBytes, - Immutable, - IntoBytes, - KnownLayout, - Serialize, + Debug, Default, Clone, Copy, Deref, FromBytes, Immutable, IntoBytes, KnownLayout, Serialize, )] pub struct Dollars(f64); @@ -141,20 +130,6 @@ impl Div for Dollars { } } -impl Eq for Dollars {} - -#[allow(clippy::derive_ord_xor_partial_ord)] -impl Ord for Dollars { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - match (self.0.is_nan(), other.0.is_nan()) { - (true, true) => Ordering::Equal, - (true, false) => Ordering::Less, - (false, true) => Ordering::Greater, - (false, false) => self.0.partial_cmp(&other.0).unwrap(), - } - } -} - impl Mul for Dollars { type Output = Self; fn mul(self, rhs: Bitcoin) -> Self::Output { @@ -232,3 +207,35 @@ impl CheckedSub for Dollars { Some(Self(self.0 - rhs as f64)) } } + +impl PartialEq for Dollars { + fn eq(&self, other: &Self) -> bool { + match (self.0.is_nan(), other.0.is_nan()) { + (true, true) => true, + (true, false) => false, + (false, true) => false, + (false, false) => self.0 == other.0, + } + } +} + +impl Eq for Dollars {} + +#[allow(clippy::derive_ord_xor_partial_ord)] +impl PartialOrd for Dollars { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +#[allow(clippy::derive_ord_xor_partial_ord)] +impl Ord for Dollars { + fn cmp(&self, other: &Self) -> Ordering { + match (self.0.is_nan(), other.0.is_nan()) { + (true, true) => Ordering::Equal, + (true, false) => Ordering::Less, + (false, true) => Ordering::Greater, + (false, false) => self.0.partial_cmp(&other.0).unwrap(), + } + } +} diff --git a/crates/brk_core/src/structs/feerate.rs b/crates/brk_core/src/structs/feerate.rs index dfd34f33e..35b1cd767 100644 --- a/crates/brk_core/src/structs/feerate.rs +++ b/crates/brk_core/src/structs/feerate.rs @@ -1,22 +1,14 @@ -use std::ops::{Add, Div}; +use std::{ + cmp::Ordering, + ops::{Add, Div}, +}; use serde::Serialize; use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout}; use super::{Sats, StoredUsize}; -#[derive( - Debug, - Clone, - Copy, - Serialize, - PartialEq, - PartialOrd, - FromBytes, - Immutable, - IntoBytes, - KnownLayout, -)] +#[derive(Debug, Clone, Copy, Serialize, FromBytes, Immutable, IntoBytes, KnownLayout)] pub struct Feerate(f32); impl From<(Sats, StoredUsize)> for Feerate { @@ -56,11 +48,34 @@ impl From for Feerate { } } +impl PartialEq for Feerate { + fn eq(&self, other: &Self) -> bool { + match (self.0.is_nan(), other.0.is_nan()) { + (true, true) => true, + (true, false) => false, + (false, true) => false, + (false, false) => self.0 == other.0, + } + } +} + impl Eq for Feerate {} #[allow(clippy::derive_ord_xor_partial_ord)] -impl Ord for Feerate { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - self.0.partial_cmp(&other.0).unwrap() +impl PartialOrd for Feerate { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +#[allow(clippy::derive_ord_xor_partial_ord)] +impl Ord for Feerate { + fn cmp(&self, other: &Self) -> Ordering { + match (self.0.is_nan(), other.0.is_nan()) { + (true, true) => Ordering::Equal, + (true, false) => Ordering::Less, + (false, true) => Ordering::Greater, + (false, false) => self.0.partial_cmp(&other.0).unwrap(), + } } } diff --git a/crates/brk_core/src/structs/stored_f32.rs b/crates/brk_core/src/structs/stored_f32.rs index 4116782a8..d65748e08 100644 --- a/crates/brk_core/src/structs/stored_f32.rs +++ b/crates/brk_core/src/structs/stored_f32.rs @@ -12,18 +12,7 @@ use crate::CheckedSub; use super::{Dollars, StoredF64}; #[derive( - Debug, - Deref, - Default, - Clone, - Copy, - PartialEq, - PartialOrd, - FromBytes, - Immutable, - IntoBytes, - KnownLayout, - Serialize, + Debug, Deref, Default, Clone, Copy, FromBytes, Immutable, IntoBytes, KnownLayout, Serialize, )] pub struct StoredF32(f32); @@ -77,20 +66,6 @@ impl From for f32 { } } -impl Eq for StoredF32 {} - -#[allow(clippy::derive_ord_xor_partial_ord)] -impl Ord for StoredF32 { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - match (self.0.is_nan(), other.0.is_nan()) { - (true, true) => Ordering::Equal, - (true, false) => Ordering::Less, - (false, true) => Ordering::Greater, - (false, false) => self.0.partial_cmp(&other.0).unwrap(), - } - } -} - impl Div for StoredF32 { type Output = Self; fn div(self, rhs: Dollars) -> Self::Output { @@ -125,3 +100,35 @@ impl Sub for StoredF32 { Self(self.0 - rhs.0) } } + +impl PartialEq for StoredF32 { + fn eq(&self, other: &Self) -> bool { + match (self.0.is_nan(), other.0.is_nan()) { + (true, true) => true, + (true, false) => false, + (false, true) => false, + (false, false) => self.0 == other.0, + } + } +} + +impl Eq for StoredF32 {} + +#[allow(clippy::derive_ord_xor_partial_ord)] +impl PartialOrd for StoredF32 { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +#[allow(clippy::derive_ord_xor_partial_ord)] +impl Ord for StoredF32 { + fn cmp(&self, other: &Self) -> Ordering { + match (self.0.is_nan(), other.0.is_nan()) { + (true, true) => Ordering::Equal, + (true, false) => Ordering::Less, + (false, true) => Ordering::Greater, + (false, false) => self.0.partial_cmp(&other.0).unwrap(), + } + } +} diff --git a/crates/brk_core/src/structs/stored_f64.rs b/crates/brk_core/src/structs/stored_f64.rs index 95f82726e..752551f10 100644 --- a/crates/brk_core/src/structs/stored_f64.rs +++ b/crates/brk_core/src/structs/stored_f64.rs @@ -10,18 +10,7 @@ use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout}; use crate::CheckedSub; #[derive( - Debug, - Deref, - Default, - Clone, - Copy, - PartialEq, - PartialOrd, - FromBytes, - Immutable, - IntoBytes, - KnownLayout, - Serialize, + Debug, Deref, Default, Clone, Copy, FromBytes, Immutable, IntoBytes, KnownLayout, Serialize, )] pub struct StoredF64(f64); @@ -70,11 +59,35 @@ impl From for f64 { } } +impl CheckedSub for StoredF64 { + fn checked_sub(self, rhs: usize) -> Option { + Some(Self(self.0 - rhs as f64)) + } +} + +impl PartialEq for StoredF64 { + fn eq(&self, other: &Self) -> bool { + match (self.0.is_nan(), other.0.is_nan()) { + (true, true) => true, + (true, false) => false, + (false, true) => false, + (false, false) => self.0 == other.0, + } + } +} + impl Eq for StoredF64 {} +#[allow(clippy::derive_ord_xor_partial_ord)] +impl PartialOrd for StoredF64 { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + #[allow(clippy::derive_ord_xor_partial_ord)] impl Ord for StoredF64 { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { + fn cmp(&self, other: &Self) -> Ordering { match (self.0.is_nan(), other.0.is_nan()) { (true, true) => Ordering::Equal, (true, false) => Ordering::Less, @@ -83,9 +96,3 @@ impl Ord for StoredF64 { } } } - -impl CheckedSub for StoredF64 { - fn checked_sub(self, rhs: usize) -> Option { - Some(Self(self.0 - rhs as f64)) - } -}