Skip to content

Commit

Permalink
Add methods 'from_fmoc_ranges' and 'filter_freq' to store [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
fxpineau committed Mar 31, 2023
1 parent e4825fd commit 8b6e378
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/qty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,10 @@ impl<T: Idx> Frequency<T> {
/// # Panics
/// * if `freq` not in `[5.048709793414476e-29, 5.846006549323611e+48[`.
pub fn freq2hash(freq: f64) -> T {
const FREQ_MIN: f64 = 5.048_709_793_414_476e-29; // f64::from_bits(929_u64 << 52);
const FREQ_MAX: f64 = 5.846_006_549_323_611e48; // f64::from_bits((1184_u64 << 52) | F64_MANTISSA_BIT_MASK);
const FREQ_MIN: f64 = 5.048_709_793_414_476e-29; // f64::from_bits( 929_u64 << 52);
const FREQ_MAX: f64 = 5.846_006_549_323_611e48; // f64::from_bits((1184_u64 << 52) | F64_MANTISSA_BIT_MASK);
assert!(FREQ_MIN <= freq, "Wrong frequency in Hz. Expected: >= {}. Actual: {}", FREQ_MIN, freq);
assert!(freq < FREQ_MAX, "Wrong frequency in Hz. Expected: < {}. Actual: {}", FREQ_MAX, freq);
assert!(freq <= FREQ_MAX, "Wrong frequency in Hz. Expected: < {}. Actual: {}", FREQ_MAX, freq);
// f64: 1 sign bit + 11 exponent bits + 52 fraction bits
// value = (-1)^sign * 2^(exponent - 1023) * (1 + fraction/2^53)
// * assert bit sign == 0
Expand Down
27 changes: 27 additions & 0 deletions src/storage/u64idx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,19 @@ impl U64MocStore {

// * F-MOC CREATION //

pub fn from_fmoc_ranges<T: Idx, I>(
&self,
depth: u8,
ranges_it: I,
) -> Result<usize, String>
where
I: Iterator<Item=Range<T>>
{
let it = ranges_it.map(|range| T::to_u64_idx(range.start)..T::to_u64_idx(range.end));
let moc: RangeMOC<u64, Frequency::<u64>> = RangeMOC::from_maxdepth_ranges(depth, it, None);
store::add(moc)
}

/// Create an store a new F-MOC from the given list of frequencies (Hz).
///
/// # Input
Expand Down Expand Up @@ -1841,6 +1854,20 @@ impl U64MocStore {
}


pub fn filter_freq<T, F, R>(&self, moc_index: usize, hz_it: T, fn_bool: F) -> Result<Vec<R>, String>
where
T: Iterator<Item=f64>,
F: Fn(bool) -> R
{
let filter = move |moc: &InternalMoc| match moc {
InternalMoc::Frequency(moc) => Ok(
hz_it.map(|hz| fn_bool(moc.contains_val(&Frequency::<u64>::freq2hash(hz)))).collect::<Vec<R>>()
),
_ => Err(String::from("Can't filter time on a MOC different from a T-MOC")),
};
store::exec_on_one_readonly_moc(moc_index, filter)
}

/// Returns an array (of boolean or u8 or ...) telling if the pairs of coordinates
/// in the input slice are in (true=1) or out of (false=0) the S-MOC.
/// # Args
Expand Down

0 comments on commit 8b6e378

Please sign in to comment.