Simplify integer bits iterator.

This commit is contained in:
Stephen Chung 2022-10-29 10:27:39 +08:00
parent 6de493c8c2
commit 6ce1dae110

View File

@ -122,7 +122,7 @@ impl<T: Debug + Copy + PartialOrd> FusedIterator for StepRange<T> {}
// Bit-field iterator with step // Bit-field iterator with step
#[derive(Debug, Clone, Hash, Eq, PartialEq)] #[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct BitRange(INT, INT, usize); pub struct BitRange(INT, usize);
impl BitRange { impl BitRange {
pub fn new(value: INT, from: INT, len: INT) -> RhaiResultOf<Self> { pub fn new(value: INT, from: INT, len: INT) -> RhaiResultOf<Self> {
@ -138,7 +138,7 @@ impl BitRange {
len as usize len as usize
}; };
Ok(Self(value, 1 << from, len)) Ok(Self(value >> from, len))
} }
} }
@ -146,19 +146,19 @@ impl Iterator for BitRange {
type Item = bool; type Item = bool;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
if self.2 == 0 { if self.1 == 0 {
None None
} else { } else {
let r = (self.0 & self.1) != 0; let r = (self.0 & 0x0001) != 0;
self.1 <<= 1; self.0 >>= 1;
self.2 -= 1; self.1 -= 1;
Some(r) Some(r)
} }
} }
#[inline(always)] #[inline(always)]
fn size_hint(&self) -> (usize, Option<usize>) { fn size_hint(&self) -> (usize, Option<usize>) {
(self.2, Some(self.2)) (self.1, Some(self.1))
} }
} }
@ -167,7 +167,7 @@ impl FusedIterator for BitRange {}
impl ExactSizeIterator for BitRange { impl ExactSizeIterator for BitRange {
#[inline(always)] #[inline(always)]
fn len(&self) -> usize { fn len(&self) -> usize {
self.2 self.1
} }
} }