use crate::prelude::{Shape, Unit};
pub fn triangle_mask<S: Shape, E: Unit>(data: &mut [E], shape: &S, upper: bool, offset: isize) {
let [num_rows, num_cols] = [
(S::NUM_DIMS > 1)
.then(|| shape.concrete()[S::NUM_DIMS - 2])
.unwrap_or(1),
(S::NUM_DIMS > 0)
.then(|| shape.concrete()[S::NUM_DIMS - 1])
.unwrap_or(1),
];
let mat_size = num_rows * num_cols;
let (mut mat2d, mut rest) = data.split_at_mut(mat_size);
if upper {
for r in (-offset).max(0) as usize..num_rows {
for c in 0..((r as isize + offset).max(0) as usize).min(num_cols) {
mat2d[r * num_cols + c] = E::default();
}
}
} else {
for r in 0..num_rows {
for c in (r as isize + offset + 1).max(0) as usize..num_cols {
mat2d[r * num_cols + c] = E::default();
}
}
}
while !rest.is_empty() {
rest[..mat_size].copy_from_slice(mat2d);
(mat2d, rest) = rest.split_at_mut(mat_size);
}
}