Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 61 additions & 6 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,30 +327,40 @@ impl ByteMask {
None
}

/// turns on bits in inclusive range `start..=end`
/// Turns on the bits in `range`
///
/// An empty range, `a..=b` with `a > b`, changes nothing, as with [`ByteMask::from_range`].
///
/// Only the range's bounds are read (so the method can be `const`). A range that has been
/// iterated to its end is empty, but its bounds read `end..=end`, so pass ranges that haven't
/// been iterated.
#[inline(always)]
pub const fn set_inclusive_bit_range(&mut self, range: RangeInclusive<u8>) {
let b = BlockRange::new(*range.start(), *range.end());
let Some(b) = BlockRange::of(&range) else { return };

self.0[0] |= b.u64_block(0);
self.0[1] |= b.u64_block(1);
self.0[2] |= b.u64_block(2);
self.0[3] |= b.u64_block(3);
}
/// turns off bits in inclusive range `start..=end`
/// Turns off the bits in `range`
///
/// An empty range changes nothing, as in [`set_inclusive_bit_range`](Self::set_inclusive_bit_range).
#[inline(always)]
pub const fn clear_inclusive_bit_range(&mut self, range: RangeInclusive<u8>) {
let b = BlockRange::new(*range.start(), *range.end());
let Some(b) = BlockRange::of(&range) else { return };

self.0[0] &= !b.u64_block(0);
self.0[1] &= !b.u64_block(1);
self.0[2] &= !b.u64_block(2);
self.0[3] &= !b.u64_block(3);
}
/// flips all bits in inclusive range `start..=end`
/// Flips the bits in `range`
///
/// An empty range changes nothing, as in [`set_inclusive_bit_range`](Self::set_inclusive_bit_range).
#[inline(always)]
pub const fn toggle_inclusive_bit_range(&mut self, range: RangeInclusive<u8>) {
let b = BlockRange::new(*range.start(), *range.end());
let Some(b) = BlockRange::of(&range) else { return };

self.0[0] ^= b.u64_block(0);
self.0[1] ^= b.u64_block(1);
Expand All @@ -368,6 +378,19 @@ struct BlockRange {
bit_end : u8,
}
impl BlockRange {
/// The blocks of `range`, or `None` if it is empty
///
/// `RangeInclusive::is_empty` isn't `const`, so emptiness is read from the bounds: `a..=b`
/// with `a > b`. A range iterated to its end can't be told from `end..=end` this way.
#[inline(always)]
const fn of(range: &RangeInclusive<u8>) -> Option<Self> {
let (start, end) = (*range.start(), *range.end());
if start > end {
None
} else {
Some(Self::new(start, end))
}
}
#[inline(always)]
const fn new(start : u8, end : u8) -> Self {
core::debug_assert!(start<=end);
Expand Down Expand Up @@ -1214,6 +1237,38 @@ mod tests {
assert_eq!(left.psubtract(&left), AlgebraicResult::None);
}

/// An empty `a..=b` (`a > b`) names no bits; in debug builds it used to trip `BlockRange`'s
/// `debug_assert`
#[test]
fn byte_mask_inclusive_range_empty() {
let base = ByteMask::from_iter([4u8, 5, 9, 200]);
for range in [5u8..=4, 255u8..=0, 1u8..=0] {
assert_eq!(ByteMask::from(range.clone()), ByteMask::EMPTY, "{range:?}");
let mut m = base;
m.set_inclusive_bit_range(range.clone());
assert_eq!(m, base, "set {range:?}");
m.clear_inclusive_bit_range(range.clone());
assert_eq!(m, base, "clear {range:?}");
m.toggle_inclusive_bit_range(range.clone());
assert_eq!(m, base, "toggle {range:?}");
}
//A range partly iterated still covers what is left of it
let mut partial = 5u8..=9;
partial.next();
let mut m = ByteMask::EMPTY;
m.set_inclusive_bit_range(partial.clone());
assert_eq!(m, ByteMask::from(partial));
//Still usable in a const context
const M: ByteMask = {
let mut m = ByteMask::EMPTY;
m.set_inclusive_bit_range(10..=20);
m.clear_inclusive_bit_range(15..=14);
m.toggle_inclusive_bit_range(20..=21);
m
};
assert_eq!(M, ByteMask::from_range(10..20) | ByteMask::from(21));
}

#[test]fn byte_mask_inclusive_range_test() {

fn test_single_range(b : &mut ByteMask, start : u8, end : u8) {
Expand Down