Skip to content

Commit

Permalink
impl_frequencies_by_array_for u8 and u16
Browse files Browse the repository at this point in the history
  • Loading branch information
beling committed Feb 18, 2024
1 parent a41b57e commit af3ad93
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions minimum_redundancy/src/frequencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,9 @@ impl<Value: Ord, W: Weight> Frequencies for BTreeMap<Value, W> {
#[inline(always)] fn without_occurrences() -> Self { Default::default() }
}

impl<W: Weight> Frequencies for [W; 1<<u8::BITS] {
type Value = u8;
macro_rules! impl_frequencies_by_array_for {($Value:ty) => {
impl<W: Weight> Frequencies for [W; 1 << <$Value>::BITS] {
type Value = $Value;
type Weight = W;

#[inline(always)] fn occurrences_of(&mut self, value: &Self::Value) -> Self::Weight {
Expand All @@ -216,12 +217,16 @@ impl<W: Weight> Frequencies for [W; 1<<u8::BITS] {
}

#[inline(always)] fn frequencies(&self) -> impl Iterator<Item=(Self::Value, Self::Weight)> where Self::Value: Clone {
self.iter().enumerate().filter_map(|(v, o)| (*o > Self::Weight::of(0)).then(|| (v as u8, *o)))
self.iter().enumerate().filter_map(|(v, o)| (*o > Self::Weight::of(0)).then(|| (v as $Value, *o)))
}

#[inline(always)] fn occurrences(&self) -> impl Iterator<Item = Self::Weight> {
self.iter().filter_map(|occ| (*occ > Self::Weight::of(0)).then_some(*occ))
}

#[inline(always)] fn without_occurrences() -> Self { [Self::Weight::of(0); 1<<u8::BITS] }
}
#[inline(always)] fn without_occurrences() -> Self { [Self::Weight::of(0); 1<< <$Value>::BITS] }
}
}}

impl_frequencies_by_array_for!(u8);
impl_frequencies_by_array_for!(u16);

0 comments on commit af3ad93

Please sign in to comment.