computer: percentiles snapshot

This commit is contained in:
nym21
2025-12-02 19:40:47 +01:00
parent b6ec133368
commit 24248215e9
29 changed files with 2535 additions and 2425 deletions

View File

@@ -2,7 +2,7 @@ use brk_traversable::Traversable;
use rayon::prelude::*;
use vecdb::AnyExportableVec;
use crate::Filtered;
use crate::Filter;
use super::{ByAmountRange, ByGreatEqualAmount, ByLowerThanAmount};
@@ -14,6 +14,24 @@ pub struct AddressGroups<T> {
}
impl<T> AddressGroups<T> {
pub fn new<F>(mut create: F) -> Self
where
F: FnMut(Filter) -> T,
{
Self {
ge_amount: ByGreatEqualAmount::new(&mut create),
amount_range: ByAmountRange::new(&mut create),
lt_amount: ByLowerThanAmount::new(&mut create),
}
}
pub fn iter(&self) -> impl Iterator<Item = &T> {
self.ge_amount
.iter()
.chain(self.amount_range.iter())
.chain(self.lt_amount.iter())
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
self.ge_amount
.iter_mut()
@@ -37,26 +55,6 @@ impl<T> AddressGroups<T> {
}
}
impl<T> AddressGroups<Filtered<T>> {
pub fn iter_right(&self) -> impl Iterator<Item = &T> {
self.amount_range
.iter_right()
.chain(self.lt_amount.iter_right())
.chain(self.ge_amount.iter_right())
}
}
impl<T> From<AddressGroups<T>> for AddressGroups<Filtered<T>> {
#[inline]
fn from(value: AddressGroups<T>) -> Self {
Self {
amount_range: ByAmountRange::from(value.amount_range),
lt_amount: ByLowerThanAmount::from(value.lt_amount),
ge_amount: ByGreatEqualAmount::from(value.ge_amount),
}
}
}
impl<T> Traversable for AddressGroups<T>
where
ByGreatEqualAmount<T>: brk_traversable::Traversable,

View File

@@ -0,0 +1,68 @@
use std::ops::Range;
use brk_types::Sats;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AmountFilter {
LowerThan(Sats),
Range(Range<Sats>),
GreaterOrEqual(Sats),
}
impl AmountFilter {
pub fn contains(&self, sats: Sats) -> bool {
match self {
AmountFilter::LowerThan(max) => sats < *max,
AmountFilter::Range(r) => sats >= r.start && sats < r.end,
AmountFilter::GreaterOrEqual(min) => sats >= *min,
}
}
pub fn includes(&self, other: &AmountFilter) -> bool {
match self {
AmountFilter::LowerThan(max) => match other {
AmountFilter::LowerThan(max2) => max >= max2,
AmountFilter::Range(range) => range.end <= *max,
AmountFilter::GreaterOrEqual(_) => false,
},
AmountFilter::GreaterOrEqual(min) => match other {
AmountFilter::Range(range) => range.start >= *min,
AmountFilter::GreaterOrEqual(min2) => min <= min2,
AmountFilter::LowerThan(_) => false,
},
AmountFilter::Range(_) => false,
}
}
pub fn to_name_suffix(&self) -> String {
match self {
AmountFilter::LowerThan(s) if *s == Sats::_1 => "with_0sats".to_string(),
AmountFilter::LowerThan(s) => format!("under_{}", format_sats(*s)),
AmountFilter::GreaterOrEqual(s) => format!("above_{}", format_sats(*s)),
AmountFilter::Range(r) => {
format!("{}_{}", format_sats(r.start), format_sats(r.end))
}
}
}
}
fn format_sats(sats: Sats) -> String {
match sats {
s if s == Sats::ZERO => "0sats".to_string(),
s if s == Sats::_1 => "1sat".to_string(),
s if s == Sats::_10 => "10sats".to_string(),
s if s == Sats::_100 => "100sats".to_string(),
s if s == Sats::_1K => "1k_sats".to_string(),
s if s == Sats::_10K => "10k_sats".to_string(),
s if s == Sats::_100K => "100k_sats".to_string(),
s if s == Sats::_1M => "1m_sats".to_string(),
s if s == Sats::_10M => "10m_sats".to_string(),
s if s == Sats::_1BTC => "1btc".to_string(),
s if s == Sats::_10BTC => "10btc".to_string(),
s if s == Sats::_100BTC => "100btc".to_string(),
s if s == Sats::_1K_BTC => "1k_btc".to_string(),
s if s == Sats::_10K_BTC => "10k_btc".to_string(),
s if s == Sats::_100K_BTC => "100k_btc".to_string(),
_ => format!("{}sats", u64::from(sats)),
}
}

View File

@@ -6,7 +6,7 @@ use brk_types::OutputType;
use rayon::prelude::*;
use vecdb::AnyExportableVec;
use super::{Filter, Filtered};
use super::Filter;
pub const P2PK65: &str = "p2pk65";
pub const P2PK33: &str = "p2pk33";
@@ -30,6 +30,22 @@ pub struct ByAddressType<T> {
}
impl<T> ByAddressType<T> {
pub fn new<F>(mut create: F) -> Self
where
F: FnMut(Filter) -> T,
{
Self {
p2pk65: create(Filter::Type(OutputType::P2PK65)),
p2pk33: create(Filter::Type(OutputType::P2PK33)),
p2pkh: create(Filter::Type(OutputType::P2PKH)),
p2sh: create(Filter::Type(OutputType::P2SH)),
p2wpkh: create(Filter::Type(OutputType::P2WPKH)),
p2wsh: create(Filter::Type(OutputType::P2WSH)),
p2tr: create(Filter::Type(OutputType::P2TR)),
p2a: create(Filter::Type(OutputType::P2A)),
}
}
pub fn new_with_name<F>(f: F) -> Result<Self>
where
F: Fn(&'static str) -> Result<T>,
@@ -215,38 +231,6 @@ impl<T> ByAddressType<T> {
}
}
impl<T> ByAddressType<Filtered<T>> {
pub fn iter_right(&self) -> impl Iterator<Item = &T> {
[
&self.p2pk65.1,
&self.p2pk33.1,
&self.p2pkh.1,
&self.p2sh.1,
&self.p2wpkh.1,
&self.p2wsh.1,
&self.p2tr.1,
&self.p2a.1,
]
.into_iter()
}
}
impl<T> From<ByAddressType<T>> for ByAddressType<Filtered<T>> {
#[inline]
fn from(value: ByAddressType<T>) -> Self {
Self {
p2pk65: (Filter::Type(OutputType::P2PK65), value.p2pk65).into(),
p2pk33: (Filter::Type(OutputType::P2PK33), value.p2pk33).into(),
p2pkh: (Filter::Type(OutputType::P2PKH), value.p2pkh).into(),
p2sh: (Filter::Type(OutputType::P2SH), value.p2sh).into(),
p2wpkh: (Filter::Type(OutputType::P2WPKH), value.p2wpkh).into(),
p2wsh: (Filter::Type(OutputType::P2WSH), value.p2wsh).into(),
p2tr: (Filter::Type(OutputType::P2TR), value.p2tr).into(),
p2a: (Filter::Type(OutputType::P2A), value.p2a).into(),
}
}
}
impl<T> Add for ByAddressType<T>
where
T: Add<Output = T>,

View File

@@ -1,9 +1,7 @@
use brk_traversable::Traversable;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use crate::Filtered;
use super::Filter;
use super::{Filter, TimeFilter};
#[derive(Default, Clone, Traversable)]
pub struct ByAgeRange<T> {
@@ -29,35 +27,35 @@ pub struct ByAgeRange<T> {
pub from_15y: T,
}
impl<T> From<ByAgeRange<T>> for ByAgeRange<Filtered<T>> {
#[inline]
fn from(value: ByAgeRange<T>) -> Self {
impl<T> ByAgeRange<T> {
pub fn new<F>(mut create: F) -> Self
where
F: FnMut(Filter) -> T,
{
Self {
up_to_1d: (Filter::LowerThan(1), value.up_to_1d).into(),
_1d_to_1w: (Filter::Range(1..7), value._1d_to_1w).into(),
_1w_to_1m: (Filter::Range(7..30), value._1w_to_1m).into(),
_1m_to_2m: (Filter::Range(30..2 * 30), value._1m_to_2m).into(),
_2m_to_3m: (Filter::Range(2 * 30..3 * 30), value._2m_to_3m).into(),
_3m_to_4m: (Filter::Range(3 * 30..4 * 30), value._3m_to_4m).into(),
_4m_to_5m: (Filter::Range(4 * 30..5 * 30), value._4m_to_5m).into(),
_5m_to_6m: (Filter::Range(5 * 30..6 * 30), value._5m_to_6m).into(),
_6m_to_1y: (Filter::Range(6 * 30..365), value._6m_to_1y).into(),
_1y_to_2y: (Filter::Range(365..2 * 365), value._1y_to_2y).into(),
_2y_to_3y: (Filter::Range(2 * 365..3 * 365), value._2y_to_3y).into(),
_3y_to_4y: (Filter::Range(3 * 365..4 * 365), value._3y_to_4y).into(),
_4y_to_5y: (Filter::Range(4 * 365..5 * 365), value._4y_to_5y).into(),
_5y_to_6y: (Filter::Range(5 * 365..6 * 365), value._5y_to_6y).into(),
_6y_to_7y: (Filter::Range(6 * 365..7 * 365), value._6y_to_7y).into(),
_7y_to_8y: (Filter::Range(7 * 365..8 * 365), value._7y_to_8y).into(),
_8y_to_10y: (Filter::Range(8 * 365..10 * 365), value._8y_to_10y).into(),
_10y_to_12y: (Filter::Range(10 * 365..12 * 365), value._10y_to_12y).into(),
_12y_to_15y: (Filter::Range(12 * 365..15 * 365), value._12y_to_15y).into(),
from_15y: (Filter::GreaterOrEqual(15 * 365), value.from_15y).into(),
up_to_1d: create(Filter::Time(TimeFilter::Range(0..1))),
_1d_to_1w: create(Filter::Time(TimeFilter::Range(1..7))),
_1w_to_1m: create(Filter::Time(TimeFilter::Range(7..30))),
_1m_to_2m: create(Filter::Time(TimeFilter::Range(30..2 * 30))),
_2m_to_3m: create(Filter::Time(TimeFilter::Range(2 * 30..3 * 30))),
_3m_to_4m: create(Filter::Time(TimeFilter::Range(3 * 30..4 * 30))),
_4m_to_5m: create(Filter::Time(TimeFilter::Range(4 * 30..5 * 30))),
_5m_to_6m: create(Filter::Time(TimeFilter::Range(5 * 30..6 * 30))),
_6m_to_1y: create(Filter::Time(TimeFilter::Range(6 * 30..365))),
_1y_to_2y: create(Filter::Time(TimeFilter::Range(365..2 * 365))),
_2y_to_3y: create(Filter::Time(TimeFilter::Range(2 * 365..3 * 365))),
_3y_to_4y: create(Filter::Time(TimeFilter::Range(3 * 365..4 * 365))),
_4y_to_5y: create(Filter::Time(TimeFilter::Range(4 * 365..5 * 365))),
_5y_to_6y: create(Filter::Time(TimeFilter::Range(5 * 365..6 * 365))),
_6y_to_7y: create(Filter::Time(TimeFilter::Range(6 * 365..7 * 365))),
_7y_to_8y: create(Filter::Time(TimeFilter::Range(7 * 365..8 * 365))),
_8y_to_10y: create(Filter::Time(TimeFilter::Range(8 * 365..10 * 365))),
_10y_to_12y: create(Filter::Time(TimeFilter::Range(10 * 365..12 * 365))),
_12y_to_15y: create(Filter::Time(TimeFilter::Range(12 * 365..15 * 365))),
from_15y: create(Filter::Time(TimeFilter::GreaterOrEqual(15 * 365))),
}
}
}
impl<T> ByAgeRange<T> {
pub fn iter(&self) -> impl Iterator<Item = &T> {
[
&self.up_to_1d,
@@ -140,30 +138,3 @@ impl<T> ByAgeRange<T> {
}
}
impl<T> ByAgeRange<Filtered<T>> {
pub fn iter_right(&self) -> impl Iterator<Item = &T> {
[
&self.up_to_1d.1,
&self._1d_to_1w.1,
&self._1w_to_1m.1,
&self._1m_to_2m.1,
&self._2m_to_3m.1,
&self._3m_to_4m.1,
&self._4m_to_5m.1,
&self._5m_to_6m.1,
&self._6m_to_1y.1,
&self._1y_to_2y.1,
&self._2y_to_3y.1,
&self._3y_to_4y.1,
&self._4y_to_5y.1,
&self._5y_to_6y.1,
&self._6y_to_7y.1,
&self._7y_to_8y.1,
&self._8y_to_10y.1,
&self._10y_to_12y.1,
&self._12y_to_15y.1,
&self.from_15y.1,
]
.into_iter()
}
}

View File

@@ -4,7 +4,7 @@ use brk_traversable::Traversable;
use brk_types::Sats;
use rayon::prelude::*;
use super::{Filter, Filtered};
use super::{AmountFilter, Filter};
#[derive(Debug, Default, Clone, Traversable)]
pub struct ByAmountRange<T> {
@@ -25,87 +25,30 @@ pub struct ByAmountRange<T> {
pub _100k_btc_or_more: T,
}
impl<T> From<ByAmountRange<T>> for ByAmountRange<Filtered<T>> {
#[inline]
fn from(value: ByAmountRange<T>) -> Self {
#[allow(clippy::inconsistent_digit_grouping)]
impl<T> ByAmountRange<T> {
pub fn new<F>(mut create: F) -> Self
where
F: FnMut(Filter) -> T,
{
Self {
_0sats: (Filter::LowerThan(Sats::_1.into()), value._0sats).into(),
_1sat_to_10sats: (
Filter::Range(Sats::_1.into()..Sats::_10.into()),
value._1sat_to_10sats,
)
.into(),
_10sats_to_100sats: (
Filter::Range(Sats::_10.into()..Sats::_100.into()),
value._10sats_to_100sats,
)
.into(),
_100sats_to_1k_sats: (
Filter::Range(Sats::_100.into()..Sats::_1K.into()),
value._100sats_to_1k_sats,
)
.into(),
_1k_sats_to_10k_sats: (
Filter::Range(Sats::_1K.into()..Sats::_10K.into()),
value._1k_sats_to_10k_sats,
)
.into(),
_10k_sats_to_100k_sats: (
Filter::Range(Sats::_10K.into()..Sats::_100K.into()),
value._10k_sats_to_100k_sats,
)
.into(),
_100k_sats_to_1m_sats: (
Filter::Range(Sats::_100K.into()..Sats::_1M.into()),
value._100k_sats_to_1m_sats,
)
.into(),
_1m_sats_to_10m_sats: (
Filter::Range(Sats::_1M.into()..Sats::_10M.into()),
value._1m_sats_to_10m_sats,
)
.into(),
_10m_sats_to_1btc: (
Filter::Range(Sats::_10M.into()..Sats::_1BTC.into()),
value._10m_sats_to_1btc,
)
.into(),
_1btc_to_10btc: (
Filter::Range(Sats::_1BTC.into()..Sats::_10BTC.into()),
value._1btc_to_10btc,
)
.into(),
_10btc_to_100btc: (
Filter::Range(Sats::_10BTC.into()..Sats::_100BTC.into()),
value._10btc_to_100btc,
)
.into(),
_100btc_to_1k_btc: (
Filter::Range(Sats::_100BTC.into()..Sats::_1K_BTC.into()),
value._100btc_to_1k_btc,
)
.into(),
_1k_btc_to_10k_btc: (
Filter::Range(Sats::_1K_BTC.into()..Sats::_10K_BTC.into()),
value._1k_btc_to_10k_btc,
)
.into(),
_10k_btc_to_100k_btc: (
Filter::Range(Sats::_10K_BTC.into()..Sats::_100K_BTC.into()),
value._10k_btc_to_100k_btc,
)
.into(),
_100k_btc_or_more: (
Filter::GreaterOrEqual(Sats::_100K_BTC.into()),
value._100k_btc_or_more,
)
.into(),
_0sats: create(Filter::Amount(AmountFilter::LowerThan(Sats::_1))),
_1sat_to_10sats: create(Filter::Amount(AmountFilter::Range(Sats::_1..Sats::_10))),
_10sats_to_100sats: create(Filter::Amount(AmountFilter::Range(Sats::_10..Sats::_100))),
_100sats_to_1k_sats: create(Filter::Amount(AmountFilter::Range(Sats::_100..Sats::_1K))),
_1k_sats_to_10k_sats: create(Filter::Amount(AmountFilter::Range(Sats::_1K..Sats::_10K))),
_10k_sats_to_100k_sats: create(Filter::Amount(AmountFilter::Range(Sats::_10K..Sats::_100K))),
_100k_sats_to_1m_sats: create(Filter::Amount(AmountFilter::Range(Sats::_100K..Sats::_1M))),
_1m_sats_to_10m_sats: create(Filter::Amount(AmountFilter::Range(Sats::_1M..Sats::_10M))),
_10m_sats_to_1btc: create(Filter::Amount(AmountFilter::Range(Sats::_10M..Sats::_1BTC))),
_1btc_to_10btc: create(Filter::Amount(AmountFilter::Range(Sats::_1BTC..Sats::_10BTC))),
_10btc_to_100btc: create(Filter::Amount(AmountFilter::Range(Sats::_10BTC..Sats::_100BTC))),
_100btc_to_1k_btc: create(Filter::Amount(AmountFilter::Range(Sats::_100BTC..Sats::_1K_BTC))),
_1k_btc_to_10k_btc: create(Filter::Amount(AmountFilter::Range(Sats::_1K_BTC..Sats::_10K_BTC))),
_10k_btc_to_100k_btc: create(Filter::Amount(AmountFilter::Range(Sats::_10K_BTC..Sats::_100K_BTC))),
_100k_btc_or_more: create(Filter::Amount(AmountFilter::GreaterOrEqual(Sats::_100K_BTC))),
}
}
}
impl<T> ByAmountRange<T> {
#[allow(clippy::inconsistent_digit_grouping)]
pub fn get_mut(&mut self, value: Sats) -> &mut T {
if value == Sats::ZERO {
@@ -229,29 +172,6 @@ impl<T> ByAmountRange<T> {
}
}
impl<T> ByAmountRange<Filtered<T>> {
pub fn iter_right(&self) -> impl Iterator<Item = &T> {
[
&self._0sats.1,
&self._1sat_to_10sats.1,
&self._10sats_to_100sats.1,
&self._100sats_to_1k_sats.1,
&self._1k_sats_to_10k_sats.1,
&self._10k_sats_to_100k_sats.1,
&self._100k_sats_to_1m_sats.1,
&self._1m_sats_to_10m_sats.1,
&self._10m_sats_to_1btc.1,
&self._1btc_to_10btc.1,
&self._10btc_to_100btc.1,
&self._100btc_to_1k_btc.1,
&self._1k_btc_to_10k_btc.1,
&self._10k_btc_to_100k_btc.1,
&self._100k_btc_or_more.1,
]
.into_iter()
}
}
impl<T> Add for ByAmountRange<T>
where
T: Add<Output = T>,

View File

@@ -2,7 +2,7 @@ use brk_traversable::Traversable;
use brk_types::{HalvingEpoch, Height};
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use super::{Filter, Filtered};
use super::Filter;
#[derive(Default, Clone, Traversable)]
pub struct ByEpoch<T> {
@@ -13,20 +13,24 @@ pub struct ByEpoch<T> {
pub _4: T,
}
impl<T> From<ByEpoch<T>> for ByEpoch<Filtered<T>> {
#[inline]
fn from(value: ByEpoch<T>) -> Self {
impl<T> ByEpoch<T> {
pub fn new<F>(mut create: F) -> Self
where
F: FnMut(Filter) -> T,
{
Self {
_0: (Filter::Epoch(HalvingEpoch::new(0)), value._0).into(),
_1: (Filter::Epoch(HalvingEpoch::new(1)), value._1).into(),
_2: (Filter::Epoch(HalvingEpoch::new(2)), value._2).into(),
_3: (Filter::Epoch(HalvingEpoch::new(3)), value._3).into(),
_4: (Filter::Epoch(HalvingEpoch::new(4)), value._4).into(),
_0: create(Filter::Epoch(HalvingEpoch::new(0))),
_1: create(Filter::Epoch(HalvingEpoch::new(1))),
_2: create(Filter::Epoch(HalvingEpoch::new(2))),
_3: create(Filter::Epoch(HalvingEpoch::new(3))),
_4: create(Filter::Epoch(HalvingEpoch::new(4))),
}
}
}
impl<T> ByEpoch<T> {
pub fn iter(&self) -> impl Iterator<Item = &T> {
[&self._0, &self._1, &self._2, &self._3, &self._4].into_iter()
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
[
&mut self._0,
@@ -69,9 +73,3 @@ impl<T> ByEpoch<T> {
}
}
}
impl<T> ByEpoch<Filtered<T>> {
pub fn iter_right(&self) -> impl Iterator<Item = &T> {
[&self._0.1, &self._1.1, &self._2.1, &self._3.1, &self._4.1].into_iter()
}
}

View File

@@ -1,7 +1,7 @@
use brk_traversable::Traversable;
use brk_types::Sats;
use super::{Filter, Filtered};
use super::{AmountFilter, Filter};
#[derive(Default, Clone, Traversable)]
pub struct ByGreatEqualAmount<T> {
@@ -21,6 +21,46 @@ pub struct ByGreatEqualAmount<T> {
}
impl<T> ByGreatEqualAmount<T> {
pub fn new<F>(mut create: F) -> Self
where
F: FnMut(Filter) -> T,
{
Self {
_1sat: create(Filter::Amount(AmountFilter::GreaterOrEqual(Sats::_1))),
_10sats: create(Filter::Amount(AmountFilter::GreaterOrEqual(Sats::_10))),
_100sats: create(Filter::Amount(AmountFilter::GreaterOrEqual(Sats::_100))),
_1k_sats: create(Filter::Amount(AmountFilter::GreaterOrEqual(Sats::_1K))),
_10k_sats: create(Filter::Amount(AmountFilter::GreaterOrEqual(Sats::_10K))),
_100k_sats: create(Filter::Amount(AmountFilter::GreaterOrEqual(Sats::_100K))),
_1m_sats: create(Filter::Amount(AmountFilter::GreaterOrEqual(Sats::_1M))),
_10m_sats: create(Filter::Amount(AmountFilter::GreaterOrEqual(Sats::_10M))),
_1btc: create(Filter::Amount(AmountFilter::GreaterOrEqual(Sats::_1BTC))),
_10btc: create(Filter::Amount(AmountFilter::GreaterOrEqual(Sats::_10BTC))),
_100btc: create(Filter::Amount(AmountFilter::GreaterOrEqual(Sats::_100BTC))),
_1k_btc: create(Filter::Amount(AmountFilter::GreaterOrEqual(Sats::_1K_BTC))),
_10k_btc: create(Filter::Amount(AmountFilter::GreaterOrEqual(Sats::_10K_BTC))),
}
}
pub fn iter(&self) -> impl Iterator<Item = &T> {
[
&self._1sat,
&self._10sats,
&self._100sats,
&self._1k_sats,
&self._10k_sats,
&self._100k_sats,
&self._1m_sats,
&self._10m_sats,
&self._1btc,
&self._10btc,
&self._100btc,
&self._1k_btc,
&self._10k_btc,
]
.into_iter()
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
[
&mut self._1sat,
@@ -40,49 +80,3 @@ impl<T> ByGreatEqualAmount<T> {
.into_iter()
}
}
impl<T> ByGreatEqualAmount<Filtered<T>> {
pub fn iter_right(&self) -> impl Iterator<Item = &T> {
[
&self._1sat.1,
&self._10sats.1,
&self._100sats.1,
&self._1k_sats.1,
&self._10k_sats.1,
&self._100k_sats.1,
&self._1m_sats.1,
&self._10m_sats.1,
&self._1btc.1,
&self._10btc.1,
&self._100btc.1,
&self._1k_btc.1,
&self._10k_btc.1,
]
.into_iter()
}
}
impl<T> From<ByGreatEqualAmount<T>> for ByGreatEqualAmount<Filtered<T>> {
#[inline]
fn from(value: ByGreatEqualAmount<T>) -> Self {
Self {
_1sat: (Filter::GreaterOrEqual(Sats::_1.into()), value._1sat).into(),
_10sats: (Filter::GreaterOrEqual(Sats::_10.into()), value._10sats).into(),
_100sats: (Filter::GreaterOrEqual(Sats::_100.into()), value._100sats).into(),
_1k_sats: (Filter::GreaterOrEqual(Sats::_1K.into()), value._1k_sats).into(),
_10k_sats: (Filter::GreaterOrEqual(Sats::_10K.into()), value._10k_sats).into(),
_100k_sats: (Filter::GreaterOrEqual(Sats::_100K.into()), value._100k_sats).into(),
_1m_sats: (Filter::GreaterOrEqual(Sats::_1M.into()), value._1m_sats).into(),
_10m_sats: (Filter::GreaterOrEqual(Sats::_10M.into()), value._10m_sats).into(),
_1btc: (Filter::GreaterOrEqual(Sats::_1BTC.into()), value._1btc).into(),
_10btc: (Filter::GreaterOrEqual(Sats::_10BTC.into()), value._10btc).into(),
_100btc: (Filter::GreaterOrEqual(Sats::_100BTC.into()), value._100btc).into(),
_1k_btc: (Filter::GreaterOrEqual(Sats::_1K_BTC.into()), value._1k_btc).into(),
_10k_btc: (
Filter::GreaterOrEqual(Sats::_10K_BTC.into()),
value._10k_btc,
)
.into(),
}
}
}

View File

@@ -1,7 +1,7 @@
use brk_traversable::Traversable;
use brk_types::Sats;
use super::{Filter, Filtered};
use super::{AmountFilter, Filter};
#[derive(Default, Clone, Traversable)]
pub struct ByLowerThanAmount<T> {
@@ -21,6 +21,46 @@ pub struct ByLowerThanAmount<T> {
}
impl<T> ByLowerThanAmount<T> {
pub fn new<F>(mut create: F) -> Self
where
F: FnMut(Filter) -> T,
{
Self {
_10sats: create(Filter::Amount(AmountFilter::LowerThan(Sats::_10))),
_100sats: create(Filter::Amount(AmountFilter::LowerThan(Sats::_100))),
_1k_sats: create(Filter::Amount(AmountFilter::LowerThan(Sats::_1K))),
_10k_sats: create(Filter::Amount(AmountFilter::LowerThan(Sats::_10K))),
_100k_sats: create(Filter::Amount(AmountFilter::LowerThan(Sats::_100K))),
_1m_sats: create(Filter::Amount(AmountFilter::LowerThan(Sats::_1M))),
_10m_sats: create(Filter::Amount(AmountFilter::LowerThan(Sats::_10M))),
_1btc: create(Filter::Amount(AmountFilter::LowerThan(Sats::_1BTC))),
_10btc: create(Filter::Amount(AmountFilter::LowerThan(Sats::_10BTC))),
_100btc: create(Filter::Amount(AmountFilter::LowerThan(Sats::_100BTC))),
_1k_btc: create(Filter::Amount(AmountFilter::LowerThan(Sats::_1K_BTC))),
_10k_btc: create(Filter::Amount(AmountFilter::LowerThan(Sats::_10K_BTC))),
_100k_btc: create(Filter::Amount(AmountFilter::LowerThan(Sats::_100K_BTC))),
}
}
pub fn iter(&self) -> impl Iterator<Item = &T> {
[
&self._10sats,
&self._100sats,
&self._1k_sats,
&self._10k_sats,
&self._100k_sats,
&self._1m_sats,
&self._10m_sats,
&self._1btc,
&self._10btc,
&self._100btc,
&self._1k_btc,
&self._10k_btc,
&self._100k_btc,
]
.into_iter()
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
[
&mut self._10sats,
@@ -40,45 +80,3 @@ impl<T> ByLowerThanAmount<T> {
.into_iter()
}
}
impl<T> ByLowerThanAmount<Filtered<T>> {
pub fn iter_right(&self) -> impl Iterator<Item = &T> {
[
&self._10sats.1,
&self._100sats.1,
&self._1k_sats.1,
&self._10k_sats.1,
&self._100k_sats.1,
&self._1m_sats.1,
&self._10m_sats.1,
&self._1btc.1,
&self._10btc.1,
&self._100btc.1,
&self._1k_btc.1,
&self._10k_btc.1,
&self._100k_btc.1,
]
.into_iter()
}
}
impl<T> From<ByLowerThanAmount<T>> for ByLowerThanAmount<Filtered<T>> {
#[inline]
fn from(value: ByLowerThanAmount<T>) -> Self {
Self {
_10sats: (Filter::LowerThan(Sats::_10.into()), value._10sats).into(),
_100sats: (Filter::LowerThan(Sats::_100.into()), value._100sats).into(),
_1k_sats: (Filter::LowerThan(Sats::_1K.into()), value._1k_sats).into(),
_10k_sats: (Filter::LowerThan(Sats::_10K.into()), value._10k_sats).into(),
_100k_sats: (Filter::LowerThan(Sats::_100K.into()), value._100k_sats).into(),
_1m_sats: (Filter::LowerThan(Sats::_1M.into()), value._1m_sats).into(),
_10m_sats: (Filter::LowerThan(Sats::_10M.into()), value._10m_sats).into(),
_1btc: (Filter::LowerThan(Sats::_1BTC.into()), value._1btc).into(),
_10btc: (Filter::LowerThan(Sats::_10BTC.into()), value._10btc).into(),
_100btc: (Filter::LowerThan(Sats::_100BTC.into()), value._100btc).into(),
_1k_btc: (Filter::LowerThan(Sats::_1K_BTC.into()), value._1k_btc).into(),
_10k_btc: (Filter::LowerThan(Sats::_10K_BTC.into()), value._10k_btc).into(),
_100k_btc: (Filter::LowerThan(Sats::_100K_BTC.into()), value._100k_btc).into(),
}
}
}

View File

@@ -1,6 +1,4 @@
use crate::Filtered;
use super::Filter;
use super::{Filter, TimeFilter};
use brk_traversable::Traversable;
#[derive(Default, Clone, Traversable)]
@@ -26,6 +24,56 @@ pub struct ByMaxAge<T> {
}
impl<T> ByMaxAge<T> {
pub fn new<F>(mut create: F) -> Self
where
F: FnMut(Filter) -> T,
{
Self {
_1w: create(Filter::Time(TimeFilter::LowerThan(7))),
_1m: create(Filter::Time(TimeFilter::LowerThan(30))),
_2m: create(Filter::Time(TimeFilter::LowerThan(2 * 30))),
_3m: create(Filter::Time(TimeFilter::LowerThan(3 * 30))),
_4m: create(Filter::Time(TimeFilter::LowerThan(4 * 30))),
_5m: create(Filter::Time(TimeFilter::LowerThan(5 * 30))),
_6m: create(Filter::Time(TimeFilter::LowerThan(6 * 30))),
_1y: create(Filter::Time(TimeFilter::LowerThan(365))),
_2y: create(Filter::Time(TimeFilter::LowerThan(2 * 365))),
_3y: create(Filter::Time(TimeFilter::LowerThan(3 * 365))),
_4y: create(Filter::Time(TimeFilter::LowerThan(4 * 365))),
_5y: create(Filter::Time(TimeFilter::LowerThan(5 * 365))),
_6y: create(Filter::Time(TimeFilter::LowerThan(6 * 365))),
_7y: create(Filter::Time(TimeFilter::LowerThan(7 * 365))),
_8y: create(Filter::Time(TimeFilter::LowerThan(8 * 365))),
_10y: create(Filter::Time(TimeFilter::LowerThan(10 * 365))),
_12y: create(Filter::Time(TimeFilter::LowerThan(12 * 365))),
_15y: create(Filter::Time(TimeFilter::LowerThan(15 * 365))),
}
}
pub fn iter(&self) -> impl Iterator<Item = &T> {
[
&self._1w,
&self._1m,
&self._2m,
&self._3m,
&self._4m,
&self._5m,
&self._6m,
&self._1y,
&self._2y,
&self._3y,
&self._4y,
&self._5y,
&self._6y,
&self._7y,
&self._8y,
&self._10y,
&self._12y,
&self._15y,
]
.into_iter()
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
[
&mut self._1w,
@@ -50,55 +98,3 @@ impl<T> ByMaxAge<T> {
.into_iter()
}
}
impl<T> ByMaxAge<Filtered<T>> {
pub fn iter_right(&self) -> impl Iterator<Item = &T> {
[
&self._1w.1,
&self._1m.1,
&self._2m.1,
&self._3m.1,
&self._4m.1,
&self._5m.1,
&self._6m.1,
&self._1y.1,
&self._2y.1,
&self._3y.1,
&self._4y.1,
&self._5y.1,
&self._6y.1,
&self._7y.1,
&self._8y.1,
&self._10y.1,
&self._12y.1,
&self._15y.1,
]
.into_iter()
}
}
impl<T> From<ByMaxAge<T>> for ByMaxAge<Filtered<T>> {
#[inline]
fn from(value: ByMaxAge<T>) -> Self {
Self {
_1w: (Filter::LowerThan(7), value._1w).into(),
_1m: (Filter::LowerThan(30), value._1m).into(),
_2m: (Filter::LowerThan(2 * 30), value._2m).into(),
_3m: (Filter::LowerThan(3 * 30), value._3m).into(),
_4m: (Filter::LowerThan(4 * 30), value._4m).into(),
_5m: (Filter::LowerThan(5 * 30), value._5m).into(),
_6m: (Filter::LowerThan(6 * 30), value._6m).into(),
_1y: (Filter::LowerThan(365), value._1y).into(),
_2y: (Filter::LowerThan(2 * 365), value._2y).into(),
_3y: (Filter::LowerThan(3 * 365), value._3y).into(),
_4y: (Filter::LowerThan(4 * 365), value._4y).into(),
_5y: (Filter::LowerThan(5 * 365), value._5y).into(),
_6y: (Filter::LowerThan(6 * 365), value._6y).into(),
_7y: (Filter::LowerThan(7 * 365), value._7y).into(),
_8y: (Filter::LowerThan(8 * 365), value._8y).into(),
_10y: (Filter::LowerThan(10 * 365), value._10y).into(),
_12y: (Filter::LowerThan(12 * 365), value._12y).into(),
_15y: (Filter::LowerThan(15 * 365), value._15y).into(),
}
}
}

View File

@@ -1,8 +1,6 @@
use brk_traversable::Traversable;
use crate::Filtered;
use super::Filter;
use super::{Filter, TimeFilter};
#[derive(Default, Clone, Traversable)]
pub struct ByMinAge<T> {
@@ -27,6 +25,56 @@ pub struct ByMinAge<T> {
}
impl<T> ByMinAge<T> {
pub fn new<F>(mut create: F) -> Self
where
F: FnMut(Filter) -> T,
{
Self {
_1d: create(Filter::Time(TimeFilter::GreaterOrEqual(1))),
_1w: create(Filter::Time(TimeFilter::GreaterOrEqual(7))),
_1m: create(Filter::Time(TimeFilter::GreaterOrEqual(30))),
_2m: create(Filter::Time(TimeFilter::GreaterOrEqual(2 * 30))),
_3m: create(Filter::Time(TimeFilter::GreaterOrEqual(3 * 30))),
_4m: create(Filter::Time(TimeFilter::GreaterOrEqual(4 * 30))),
_5m: create(Filter::Time(TimeFilter::GreaterOrEqual(5 * 30))),
_6m: create(Filter::Time(TimeFilter::GreaterOrEqual(6 * 30))),
_1y: create(Filter::Time(TimeFilter::GreaterOrEqual(365))),
_2y: create(Filter::Time(TimeFilter::GreaterOrEqual(2 * 365))),
_3y: create(Filter::Time(TimeFilter::GreaterOrEqual(3 * 365))),
_4y: create(Filter::Time(TimeFilter::GreaterOrEqual(4 * 365))),
_5y: create(Filter::Time(TimeFilter::GreaterOrEqual(5 * 365))),
_6y: create(Filter::Time(TimeFilter::GreaterOrEqual(6 * 365))),
_7y: create(Filter::Time(TimeFilter::GreaterOrEqual(7 * 365))),
_8y: create(Filter::Time(TimeFilter::GreaterOrEqual(8 * 365))),
_10y: create(Filter::Time(TimeFilter::GreaterOrEqual(10 * 365))),
_12y: create(Filter::Time(TimeFilter::GreaterOrEqual(12 * 365))),
}
}
pub fn iter(&self) -> impl Iterator<Item = &T> {
[
&self._1d,
&self._1w,
&self._1m,
&self._2m,
&self._3m,
&self._4m,
&self._5m,
&self._6m,
&self._1y,
&self._2y,
&self._3y,
&self._4y,
&self._5y,
&self._6y,
&self._7y,
&self._8y,
&self._10y,
&self._12y,
]
.into_iter()
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
[
&mut self._1d,
@@ -51,55 +99,3 @@ impl<T> ByMinAge<T> {
.into_iter()
}
}
impl<T> ByMinAge<Filtered<T>> {
pub fn iter_right(&self) -> impl Iterator<Item = &T> {
[
&self._1d.1,
&self._1w.1,
&self._1m.1,
&self._2m.1,
&self._3m.1,
&self._4m.1,
&self._5m.1,
&self._6m.1,
&self._1y.1,
&self._2y.1,
&self._3y.1,
&self._4y.1,
&self._5y.1,
&self._6y.1,
&self._7y.1,
&self._8y.1,
&self._10y.1,
&self._12y.1,
]
.into_iter()
}
}
impl<T> From<ByMinAge<T>> for ByMinAge<Filtered<T>> {
#[inline]
fn from(value: ByMinAge<T>) -> Self {
Self {
_1d: (Filter::GreaterOrEqual(1), value._1d).into(),
_1w: (Filter::GreaterOrEqual(7), value._1w).into(),
_1m: (Filter::GreaterOrEqual(30), value._1m).into(),
_2m: (Filter::GreaterOrEqual(2 * 30), value._2m).into(),
_3m: (Filter::GreaterOrEqual(3 * 30), value._3m).into(),
_4m: (Filter::GreaterOrEqual(4 * 30), value._4m).into(),
_5m: (Filter::GreaterOrEqual(5 * 30), value._5m).into(),
_6m: (Filter::GreaterOrEqual(6 * 30), value._6m).into(),
_1y: (Filter::GreaterOrEqual(365), value._1y).into(),
_2y: (Filter::GreaterOrEqual(2 * 365), value._2y).into(),
_3y: (Filter::GreaterOrEqual(3 * 365), value._3y).into(),
_4y: (Filter::GreaterOrEqual(4 * 365), value._4y).into(),
_5y: (Filter::GreaterOrEqual(5 * 365), value._5y).into(),
_6y: (Filter::GreaterOrEqual(6 * 365), value._6y).into(),
_7y: (Filter::GreaterOrEqual(7 * 365), value._7y).into(),
_8y: (Filter::GreaterOrEqual(8 * 365), value._8y).into(),
_10y: (Filter::GreaterOrEqual(10 * 365), value._10y).into(),
_12y: (Filter::GreaterOrEqual(12 * 365), value._12y).into(),
}
}
}

View File

@@ -4,7 +4,7 @@ use brk_traversable::Traversable;
use brk_types::OutputType;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use super::{Filter, Filtered};
use super::Filter;
#[derive(Default, Clone, Debug, Traversable)]
pub struct BySpendableType<T> {
@@ -22,6 +22,25 @@ pub struct BySpendableType<T> {
}
impl<T> BySpendableType<T> {
pub fn new<F>(mut create: F) -> Self
where
F: FnMut(Filter) -> T,
{
Self {
p2pk65: create(Filter::Type(OutputType::P2PK65)),
p2pk33: create(Filter::Type(OutputType::P2PK33)),
p2pkh: create(Filter::Type(OutputType::P2PKH)),
p2ms: create(Filter::Type(OutputType::P2MS)),
p2sh: create(Filter::Type(OutputType::P2SH)),
p2wpkh: create(Filter::Type(OutputType::P2WPKH)),
p2wsh: create(Filter::Type(OutputType::P2WSH)),
p2tr: create(Filter::Type(OutputType::P2TR)),
p2a: create(Filter::Type(OutputType::P2A)),
unknown: create(Filter::Type(OutputType::Unknown)),
empty: create(Filter::Type(OutputType::Empty)),
}
}
pub fn get_mut(&mut self, output_type: OutputType) -> &mut T {
match output_type {
OutputType::P2PK65 => &mut self.p2pk65,
@@ -39,6 +58,23 @@ impl<T> BySpendableType<T> {
}
}
pub fn iter(&self) -> impl Iterator<Item = &T> {
[
&self.p2pk65,
&self.p2pk33,
&self.p2pkh,
&self.p2ms,
&self.p2sh,
&self.p2wpkh,
&self.p2wsh,
&self.p2tr,
&self.p2a,
&self.unknown,
&self.empty,
]
.into_iter()
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
[
&mut self.p2pk65,
@@ -94,44 +130,6 @@ impl<T> BySpendableType<T> {
}
}
impl<T> BySpendableType<Filtered<T>> {
pub fn iter_right(&self) -> impl Iterator<Item = &T> {
[
&self.p2pk65.1,
&self.p2pk33.1,
&self.p2pkh.1,
&self.p2ms.1,
&self.p2sh.1,
&self.p2wpkh.1,
&self.p2wsh.1,
&self.p2tr.1,
&self.p2a.1,
&self.unknown.1,
&self.empty.1,
]
.into_iter()
}
}
impl<T> From<BySpendableType<T>> for BySpendableType<Filtered<T>> {
#[inline]
fn from(value: BySpendableType<T>) -> Self {
Self {
p2pk65: (Filter::Type(OutputType::P2PK65), value.p2pk65).into(),
p2pk33: (Filter::Type(OutputType::P2PK33), value.p2pk33).into(),
p2pkh: (Filter::Type(OutputType::P2PKH), value.p2pkh).into(),
p2ms: (Filter::Type(OutputType::P2MS), value.p2ms).into(),
p2sh: (Filter::Type(OutputType::P2SH), value.p2sh).into(),
p2wpkh: (Filter::Type(OutputType::P2WPKH), value.p2wpkh).into(),
p2wsh: (Filter::Type(OutputType::P2WSH), value.p2wsh).into(),
p2tr: (Filter::Type(OutputType::P2TR), value.p2tr).into(),
p2a: (Filter::Type(OutputType::P2A), value.p2a).into(),
unknown: (Filter::Type(OutputType::Unknown), value.unknown).into(),
empty: (Filter::Type(OutputType::Empty), value.empty).into(),
}
}
}
impl<T> Add for BySpendableType<T>
where
T: Add<Output = T>,

View File

@@ -1,8 +1,6 @@
use brk_traversable::Traversable;
use crate::Filtered;
use super::Filter;
use super::{Filter, Term};
#[derive(Default, Clone, Traversable)]
pub struct ByTerm<T> {
@@ -11,23 +9,21 @@ pub struct ByTerm<T> {
}
impl<T> ByTerm<T> {
pub fn new<F>(mut create: F) -> Self
where
F: FnMut(Filter) -> T,
{
Self {
short: create(Filter::Term(Term::Sth)),
long: create(Filter::Term(Term::Lth)),
}
}
pub fn iter(&self) -> impl Iterator<Item = &T> {
[&self.short, &self.long].into_iter()
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
[&mut self.short, &mut self.long].into_iter()
}
}
impl<T> ByTerm<Filtered<T>> {
pub fn iter_right(&self) -> impl Iterator<Item = &T> {
[&self.short.1, &self.long.1].into_iter()
}
}
impl<T> From<ByTerm<T>> for ByTerm<Filtered<T>> {
#[inline]
fn from(value: ByTerm<T>) -> Self {
Self {
short: (Filter::LowerThan(5 * 30), value.short).into(),
long: (Filter::GreaterOrEqual(5 * 30), value.long).into(),
}
}
}

View File

@@ -0,0 +1,17 @@
/// Context for cohort naming - determines whether a prefix is needed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CohortContext {
/// UTXO-based cohorts: uses "utxos_" prefix for Time/Amount filters
Utxo,
/// Address-based cohorts: uses "addrs_" prefix for Amount filters
Address,
}
impl CohortContext {
pub fn prefix(&self) -> &'static str {
match self {
CohortContext::Utxo => "utxos",
CohortContext::Address => "addrs",
}
}
}

View File

@@ -1,88 +1,109 @@
use std::ops::Range;
use brk_types::{HalvingEpoch, OutputType, Sats};
use brk_traversable::{Traversable, TreeNode};
use brk_types::{HalvingEpoch, OutputType};
use vecdb::AnyExportableVec;
use super::{AmountFilter, CohortContext, Term, TimeFilter};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Filter {
All,
LowerThan(usize),
Range(Range<usize>),
GreaterOrEqual(usize),
Term(Term),
Time(TimeFilter),
Amount(AmountFilter),
Epoch(HalvingEpoch),
Type(OutputType),
}
impl Filter {
pub fn contains(&self, value: usize) -> bool {
pub fn is_all(&self) -> bool {
matches!(self, Filter::All)
}
/// Returns true if this filter includes day 0 (only applicable to time-based filters)
pub fn includes_first_day(&self) -> bool {
match self {
Filter::Range(r) => r.contains(&value),
Filter::LowerThan(max) => *max > value,
Filter::GreaterOrEqual(min) => *min <= value,
Filter::All => true,
Filter::Epoch(_) | Filter::Type(_) => false,
Filter::Term(Term::Sth) => true,
Filter::Term(Term::Lth) => false,
Filter::Time(t) => t.includes_first_day(),
_ => false,
}
}
pub fn includes(&self, other: &Filter) -> bool {
pub fn to_name_suffix(&self) -> String {
match self {
Filter::All => String::new(),
Filter::Term(t) => t.to_name().to_string(),
Filter::Time(t) => t.to_name_suffix(),
Filter::Amount(a) => a.to_name_suffix(),
Filter::Epoch(e) => format!("epoch_{}", usize::from(*e)),
Filter::Type(t) => match t {
OutputType::P2MS => "p2ms_outputs".to_string(),
OutputType::Empty => "empty_outputs".to_string(),
OutputType::Unknown => "unknown_outputs".to_string(),
_ => format!("{:?}", t).to_lowercase(),
},
}
}
/// Returns the full name for this filter, including context-based prefix.
///
/// Prefix rules:
/// - No prefix: `All`, `Term`, `Epoch`, `Type`
/// - `utxos_` prefix: `Time` or `Amount` with `CohortContext::Utxo`
/// - `addrs_` prefix: `Amount` with `CohortContext::Address`
pub fn to_full_name(&self, context: CohortContext) -> String {
let suffix = self.to_name_suffix();
if suffix.is_empty() {
return suffix;
}
let needs_prefix = match self {
Filter::All | Filter::Term(_) | Filter::Epoch(_) | Filter::Type(_) => false,
Filter::Time(_) => matches!(context, CohortContext::Utxo),
Filter::Amount(_) => true,
};
if needs_prefix {
format!("{}_{}", context.prefix(), suffix)
} else {
suffix
}
}
/// Check if a time value (days) is contained by this filter
pub fn contains_time(&self, days: usize) -> bool {
match self {
Filter::All => true,
Filter::LowerThan(max) => match other {
Filter::LowerThan(max2) => max >= max2,
Filter::Range(range) => range.end <= *max,
Filter::All | Filter::GreaterOrEqual(_) | Filter::Epoch(_) | Filter::Type(_) => {
false
}
},
Filter::GreaterOrEqual(min) => match other {
Filter::Range(range) => range.start >= *min,
Filter::GreaterOrEqual(min2) => min <= min2,
Filter::All | Filter::LowerThan(_) | Filter::Epoch(_) | Filter::Type(_) => false,
},
Filter::Range(_) | Filter::Epoch(_) | Filter::Type(_) => false,
Filter::Term(Term::Sth) => days < Term::THRESHOLD_DAYS,
Filter::Term(Term::Lth) => days >= Term::THRESHOLD_DAYS,
Filter::Time(t) => t.contains(days),
_ => false,
}
}
/// Check if an amount value (sats) is contained by this filter
pub fn contains_amount(&self, sats: Sats) -> bool {
match self {
Filter::All => true,
Filter::Amount(a) => a.contains(sats),
_ => false,
}
}
/// Check if this filter includes another filter (for aggregation)
pub fn includes(&self, other: &Filter) -> bool {
match (self, other) {
(Filter::All, _) => true,
(Filter::Term(Term::Sth), Filter::Time(t)) => {
matches!(t, TimeFilter::LowerThan(d) if *d <= Term::THRESHOLD_DAYS)
|| matches!(t, TimeFilter::Range(r) if r.end <= Term::THRESHOLD_DAYS)
}
(Filter::Term(Term::Lth), Filter::Time(t)) => {
matches!(t, TimeFilter::GreaterOrEqual(d) if *d >= Term::THRESHOLD_DAYS)
|| matches!(t, TimeFilter::Range(r) if r.start >= Term::THRESHOLD_DAYS)
}
(Filter::Time(t1), Filter::Time(t2)) => t1.includes(t2),
(Filter::Amount(a1), Filter::Amount(a2)) => a1.includes(a2),
_ => false,
}
}
}
#[derive(Clone)]
pub struct Filtered<T>(pub Filter, pub T);
impl<T> Filtered<T> {
pub fn includes(&self, other: &Filter) -> bool {
self.0.includes(other)
}
pub fn filter(&self) -> &Filter {
&self.0
}
pub fn unwrap(self) -> T {
self.1
}
pub fn t(&self) -> &T {
&self.1
}
pub fn mut_t(&mut self) -> &mut T {
&mut self.1
}
}
impl<T> From<(Filter, T)> for Filtered<T> {
#[inline]
fn from(value: (Filter, T)) -> Self {
Self(value.0, value.1)
}
}
impl<T: Traversable> Traversable for Filtered<T> {
fn to_tree_node(&self) -> TreeNode {
self.1.to_tree_node()
}
fn iter_any_exportable(&self) -> impl Iterator<Item = &dyn AnyExportableVec> {
self.1.iter_any_exportable()
}
}

View File

@@ -0,0 +1,21 @@
use super::{CohortContext, Filter};
pub trait Filtered {
fn filter(&self) -> &Filter;
fn is_all(&self) -> bool {
self.filter().is_all()
}
fn includes_first_day(&self) -> bool {
self.filter().includes_first_day()
}
fn name_suffix(&self) -> String {
self.filter().to_name_suffix()
}
fn full_name(&self, context: CohortContext) -> String {
self.filter().to_full_name(context)
}
}

View File

@@ -1,6 +1,7 @@
#![doc = include_str!("../README.md")]
mod address;
mod amount_filter;
mod by_address_type;
mod by_age_range;
mod by_amount_range;
@@ -14,10 +15,16 @@ mod by_spendable_type;
mod by_term;
mod by_type;
mod by_unspendable_type;
mod cohort_context;
mod filter;
mod filtered;
mod state_level;
mod term;
mod time_filter;
mod utxo;
pub use address::*;
pub use amount_filter::*;
pub use by_address_type::*;
pub use by_age_range::*;
pub use by_amount_range::*;
@@ -31,5 +38,10 @@ pub use by_spendable_type::*;
pub use by_term::*;
pub use by_type::*;
pub use by_unspendable_type::*;
pub use cohort_context::*;
pub use filter::*;
pub use filtered::*;
pub use state_level::*;
pub use term::*;
pub use time_filter::*;
pub use utxo::*;

View File

@@ -0,0 +1,32 @@
/// Controls the level of state tracking for a cohort.
///
/// - `None`: No state tracking. Values are computed from stateful sub-cohorts.
/// - `PriceOnly`: Only tracks `price_to_amount` for percentile calculations.
/// Used by aggregate cohorts (all, sth, lth) that compute other values from sub-cohorts.
/// - `Full`: Full state tracking including supply, realized values, and `price_to_amount`.
/// Used by stateful cohorts like individual age ranges and epochs.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum StateLevel {
#[default]
None,
PriceOnly,
Full,
}
impl StateLevel {
pub fn is_none(&self) -> bool {
matches!(self, StateLevel::None)
}
pub fn is_price_only(&self) -> bool {
matches!(self, StateLevel::PriceOnly)
}
pub fn is_full(&self) -> bool {
matches!(self, StateLevel::Full)
}
pub fn has_price_to_amount(&self) -> bool {
matches!(self, StateLevel::PriceOnly | StateLevel::Full)
}
}

View File

@@ -0,0 +1,20 @@
/// Classification for short-term vs long-term holders.
/// The threshold is 150 days (approximately 5 months).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Term {
/// Short-Term Holder: < 150 days
Sth,
/// Long-Term Holder: >= 150 days
Lth,
}
impl Term {
pub const THRESHOLD_DAYS: usize = 150;
pub fn to_name(&self) -> &'static str {
match self {
Term::Sth => "sth",
Term::Lth => "lth",
}
}
}

View File

@@ -0,0 +1,114 @@
use std::ops::Range;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TimeFilter {
LowerThan(usize),
Range(Range<usize>),
GreaterOrEqual(usize),
}
impl TimeFilter {
pub fn contains(&self, days: usize) -> bool {
match self {
TimeFilter::LowerThan(max) => days < *max,
TimeFilter::Range(r) => r.contains(&days),
TimeFilter::GreaterOrEqual(min) => days >= *min,
}
}
pub fn includes(&self, other: &TimeFilter) -> bool {
match self {
TimeFilter::LowerThan(max) => match other {
TimeFilter::LowerThan(max2) => max >= max2,
TimeFilter::Range(range) => range.end <= *max,
TimeFilter::GreaterOrEqual(_) => false,
},
TimeFilter::GreaterOrEqual(min) => match other {
TimeFilter::Range(range) => range.start >= *min,
TimeFilter::GreaterOrEqual(min2) => min <= min2,
TimeFilter::LowerThan(_) => false,
},
TimeFilter::Range(_) => false,
}
}
/// Returns true if this filter includes day 0 (UTXOs less than 1 day old)
pub fn includes_first_day(&self) -> bool {
match self {
TimeFilter::LowerThan(_) => true,
TimeFilter::Range(r) => r.start == 0,
TimeFilter::GreaterOrEqual(_) => false,
}
}
pub fn to_name_suffix(&self) -> String {
match self {
// Special cases for common filters
TimeFilter::LowerThan(1) => "up_to_1d".to_string(),
TimeFilter::LowerThan(7) => "up_to_1w".to_string(),
TimeFilter::LowerThan(30) => "up_to_1m".to_string(),
TimeFilter::LowerThan(60) => "up_to_2m".to_string(),
TimeFilter::LowerThan(90) => "up_to_3m".to_string(),
TimeFilter::LowerThan(120) => "up_to_4m".to_string(),
TimeFilter::LowerThan(150) => "sth".to_string(),
TimeFilter::LowerThan(180) => "up_to_6m".to_string(),
TimeFilter::LowerThan(365) => "up_to_1y".to_string(),
TimeFilter::LowerThan(730) => "up_to_2y".to_string(),
TimeFilter::LowerThan(1095) => "up_to_3y".to_string(),
TimeFilter::LowerThan(1460) => "up_to_4y".to_string(),
TimeFilter::LowerThan(1825) => "up_to_5y".to_string(),
TimeFilter::LowerThan(2190) => "up_to_6y".to_string(),
TimeFilter::LowerThan(2555) => "up_to_7y".to_string(),
TimeFilter::LowerThan(2920) => "up_to_8y".to_string(),
TimeFilter::LowerThan(3650) => "up_to_10y".to_string(),
TimeFilter::LowerThan(4380) => "up_to_12y".to_string(),
TimeFilter::LowerThan(5475) => "up_to_15y".to_string(),
TimeFilter::GreaterOrEqual(1) => "at_least_1d".to_string(),
TimeFilter::GreaterOrEqual(7) => "at_least_1w".to_string(),
TimeFilter::GreaterOrEqual(30) => "at_least_1m".to_string(),
TimeFilter::GreaterOrEqual(60) => "at_least_2m".to_string(),
TimeFilter::GreaterOrEqual(90) => "at_least_3m".to_string(),
TimeFilter::GreaterOrEqual(120) => "at_least_4m".to_string(),
TimeFilter::GreaterOrEqual(150) => "lth".to_string(),
TimeFilter::GreaterOrEqual(180) => "at_least_6m".to_string(),
TimeFilter::GreaterOrEqual(365) => "at_least_1y".to_string(),
TimeFilter::GreaterOrEqual(730) => "at_least_2y".to_string(),
TimeFilter::GreaterOrEqual(1095) => "at_least_3y".to_string(),
TimeFilter::GreaterOrEqual(1460) => "at_least_4y".to_string(),
TimeFilter::GreaterOrEqual(1825) => "at_least_5y".to_string(),
TimeFilter::GreaterOrEqual(2190) => "at_least_6y".to_string(),
TimeFilter::GreaterOrEqual(2555) => "at_least_7y".to_string(),
TimeFilter::GreaterOrEqual(2920) => "at_least_8y".to_string(),
TimeFilter::GreaterOrEqual(3650) => "at_least_10y".to_string(),
TimeFilter::GreaterOrEqual(4380) => "at_least_12y".to_string(),
TimeFilter::GreaterOrEqual(5475) => "at_least_15y".to_string(),
// Range special cases
TimeFilter::Range(r) if *r == (0..1) => "up_to_1d".to_string(),
TimeFilter::Range(r) if *r == (1..7) => "1d_to_1w".to_string(),
TimeFilter::Range(r) if *r == (7..30) => "1w_to_1m".to_string(),
TimeFilter::Range(r) if *r == (30..60) => "1m_to_2m".to_string(),
TimeFilter::Range(r) if *r == (60..90) => "2m_to_3m".to_string(),
TimeFilter::Range(r) if *r == (90..120) => "3m_to_4m".to_string(),
TimeFilter::Range(r) if *r == (120..150) => "4m_to_5m".to_string(),
TimeFilter::Range(r) if *r == (150..180) => "5m_to_6m".to_string(),
TimeFilter::Range(r) if *r == (180..365) => "6m_to_1y".to_string(),
TimeFilter::Range(r) if *r == (365..730) => "1y_to_2y".to_string(),
TimeFilter::Range(r) if *r == (730..1095) => "2y_to_3y".to_string(),
TimeFilter::Range(r) if *r == (1095..1460) => "3y_to_4y".to_string(),
TimeFilter::Range(r) if *r == (1460..1825) => "4y_to_5y".to_string(),
TimeFilter::Range(r) if *r == (1825..2190) => "5y_to_6y".to_string(),
TimeFilter::Range(r) if *r == (2190..2555) => "6y_to_7y".to_string(),
TimeFilter::Range(r) if *r == (2555..2920) => "7y_to_8y".to_string(),
TimeFilter::Range(r) if *r == (2920..3650) => "8y_to_10y".to_string(),
TimeFilter::Range(r) if *r == (3650..4380) => "10y_to_12y".to_string(),
TimeFilter::Range(r) if *r == (4380..5475) => "12y_to_15y".to_string(),
// Fallback generic names
TimeFilter::LowerThan(d) => format!("up_to_{}d", d),
TimeFilter::GreaterOrEqual(d) => format!("at_least_{}d", d),
TimeFilter::Range(r) => format!("{}d_to_{}d", r.start, r.end),
}
}
}

View File

@@ -3,7 +3,7 @@ use rayon::prelude::*;
use crate::{
ByAgeRange, ByAmountRange, ByEpoch, ByGreatEqualAmount, ByLowerThanAmount, ByMaxAge, ByMinAge,
BySpendableType, ByTerm, Filter, Filtered,
BySpendableType, ByTerm, Filter,
};
#[derive(Default, Clone, Traversable)]
@@ -15,12 +15,44 @@ pub struct UTXOGroups<T> {
pub ge_amount: ByGreatEqualAmount<T>,
pub amount_range: ByAmountRange<T>,
pub term: ByTerm<T>,
pub _type: BySpendableType<T>,
pub type_: BySpendableType<T>,
pub max_age: ByMaxAge<T>,
pub lt_amount: ByLowerThanAmount<T>,
}
impl<T> UTXOGroups<T> {
pub fn new<F>(mut create: F) -> Self
where
F: FnMut(Filter) -> T,
{
Self {
all: create(Filter::All),
age_range: ByAgeRange::new(&mut create),
epoch: ByEpoch::new(&mut create),
min_age: ByMinAge::new(&mut create),
ge_amount: ByGreatEqualAmount::new(&mut create),
amount_range: ByAmountRange::new(&mut create),
term: ByTerm::new(&mut create),
type_: BySpendableType::new(&mut create),
max_age: ByMaxAge::new(&mut create),
lt_amount: ByLowerThanAmount::new(&mut create),
}
}
pub fn iter(&self) -> impl Iterator<Item = &T> {
[&self.all]
.into_iter()
.chain(self.term.iter())
.chain(self.max_age.iter())
.chain(self.min_age.iter())
.chain(self.ge_amount.iter())
.chain(self.age_range.iter())
.chain(self.epoch.iter())
.chain(self.amount_range.iter())
.chain(self.lt_amount.iter())
.chain(self.type_.iter())
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
[&mut self.all]
.into_iter()
@@ -32,7 +64,7 @@ impl<T> UTXOGroups<T> {
.chain(self.epoch.iter_mut())
.chain(self.amount_range.iter_mut())
.chain(self.lt_amount.iter_mut())
.chain(self._type.iter_mut())
.chain(self.type_.iter_mut())
}
pub fn iter_separate_mut(&mut self) -> impl Iterator<Item = &mut T> {
@@ -40,7 +72,7 @@ impl<T> UTXOGroups<T> {
.iter_mut()
.chain(self.epoch.iter_mut())
.chain(self.amount_range.iter_mut())
.chain(self._type.iter_mut())
.chain(self.type_.iter_mut())
}
pub fn par_iter_separate_mut(&mut self) -> impl ParallelIterator<Item = &mut T>
@@ -51,7 +83,7 @@ impl<T> UTXOGroups<T> {
.par_iter_mut()
.chain(self.epoch.par_iter_mut())
.chain(self.amount_range.par_iter_mut())
.chain(self._type.par_iter_mut())
.chain(self.type_.par_iter_mut())
}
pub fn iter_overlapping_mut(&mut self) -> impl Iterator<Item = &mut T> {
@@ -64,37 +96,3 @@ impl<T> UTXOGroups<T> {
.chain(self.ge_amount.iter_mut())
}
}
impl<T> UTXOGroups<Filtered<T>> {
pub fn iter_right(&self) -> impl Iterator<Item = &T> {
[&self.all.1]
.into_iter()
.chain(self.term.iter_right())
.chain(self.max_age.iter_right())
.chain(self.min_age.iter_right())
.chain(self.age_range.iter_right())
.chain(self.epoch.iter_right())
.chain(self.amount_range.iter_right())
.chain(self._type.iter_right())
.chain(self.lt_amount.iter_right())
.chain(self.ge_amount.iter_right())
}
}
impl<T> From<UTXOGroups<T>> for UTXOGroups<Filtered<T>> {
#[inline]
fn from(value: UTXOGroups<T>) -> Self {
Self {
all: (Filter::All, value.all).into(),
term: ByTerm::from(value.term),
max_age: ByMaxAge::from(value.max_age),
min_age: ByMinAge::from(value.min_age),
age_range: ByAgeRange::from(value.age_range),
epoch: ByEpoch::from(value.epoch),
amount_range: ByAmountRange::from(value.amount_range),
lt_amount: ByLowerThanAmount::from(value.lt_amount),
ge_amount: ByGreatEqualAmount::from(value.ge_amount),
_type: BySpendableType::from(value._type),
}
}
}