Skip to content

Commit bec5098

Browse files
authored
Chore: add set functions to mask mut (#5432)
Signed-off-by: Connor Tsui <[email protected]>
1 parent 557afef commit bec5098

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

vortex-mask/src/mask_mut.rs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,111 @@ impl MaskMut {
355355
_ => None,
356356
}
357357
}
358+
359+
/// Set the value at the given index to true.
360+
///
361+
/// # Panics
362+
///
363+
/// Panics if the index is out of bounds.
364+
pub fn set(&mut self, index: usize) {
365+
self.set_to(index, true);
366+
}
367+
368+
/// Set the value at the given index to false.
369+
///
370+
/// # Panics
371+
///
372+
/// Panics if the index is out of bounds.
373+
pub fn unset(&mut self, index: usize) {
374+
self.set_to(index, false);
375+
}
376+
377+
/// Set the value at the given index to the specified boolean value.
378+
///
379+
/// # Panics
380+
///
381+
/// Panics if the index is out of bounds.
382+
pub fn set_to(&mut self, index: usize, value: bool) {
383+
match &mut self.0 {
384+
Inner::Empty { .. } => {
385+
vortex_panic!("index out of bounds: the length is 0 but the index is {index}")
386+
}
387+
Inner::Constant {
388+
value: current_value,
389+
len,
390+
..
391+
} => {
392+
assert!(
393+
index < *len,
394+
"index out of bounds: the length is {} but the index is {index}",
395+
*len
396+
);
397+
398+
if *current_value != value {
399+
// Need to materialize the buffer since we're changing from constant.
400+
self.materialize().set_to(index, value);
401+
}
402+
// If the value is the same as the constant, no action needed.
403+
}
404+
Inner::Builder(bit_buffer) => {
405+
bit_buffer.set_to(index, value);
406+
}
407+
}
408+
}
409+
410+
/// Set the value at the given index to true without bounds checking.
411+
///
412+
/// # Safety
413+
///
414+
/// The caller must ensure that `index < self.len()`.
415+
pub unsafe fn set_unchecked(&mut self, index: usize) {
416+
unsafe { self.set_to_unchecked(index, true) }
417+
}
418+
419+
/// Set the value at the given index to false without bounds checking.
420+
///
421+
/// # Safety
422+
///
423+
/// The caller must ensure that `index < self.len()`.
424+
pub unsafe fn unset_unchecked(&mut self, index: usize) {
425+
unsafe { self.set_to_unchecked(index, false) }
426+
}
427+
428+
/// Set the value at the given index to the specified boolean value without bounds checking.
429+
///
430+
/// # Safety
431+
///
432+
/// The caller must ensure that `index < self.len()`.
433+
pub unsafe fn set_to_unchecked(&mut self, index: usize, value: bool) {
434+
unsafe {
435+
match &mut self.0 {
436+
Inner::Empty { .. } => {
437+
// In debug mode, we still want to catch this error.
438+
debug_assert!(false, "cannot set value in empty mask");
439+
}
440+
Inner::Constant {
441+
value: current_value,
442+
len,
443+
..
444+
} => {
445+
debug_assert!(
446+
index < *len,
447+
"index out of bounds: the length is {} but the index is {index}",
448+
*len
449+
);
450+
451+
if *current_value != value {
452+
// Need to materialize the buffer since we're changing from constant.
453+
self.materialize().set_to_unchecked(index, value);
454+
}
455+
// If the value is the same as the constant, no action needed.
456+
}
457+
Inner::Builder(bit_buffer) => {
458+
bit_buffer.set_to_unchecked(index, value);
459+
}
460+
}
461+
}
462+
}
358463
}
359464

360465
impl Mask {

0 commit comments

Comments
 (0)