Skip to content

Commit 0e0007d

Browse files
committed
Add OwnedMatrix::remove_columns
1 parent 5e09574 commit 0e0007d

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/matrix.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,45 @@ where
686686
.drain((col_idx * self.rows)..(col_idx * self.rows + self.rows));
687687
self.cols -= 1;
688688
}
689+
690+
pub fn remove_columns(self, removing: &HashSet<usize>) -> Self
691+
where
692+
T: Copy + Send + Sync,
693+
{
694+
let cols = self.cols();
695+
if removing.iter().any(|i| *i >= cols) {
696+
panic!("cannot remove out of range column");
697+
}
698+
let cols = cols - removing.len();
699+
let rows = self.rows();
700+
let data = vec![MaybeUninit::<T>::uninit(); cols * rows];
701+
(0..self.cols())
702+
.filter(|x| !removing.contains(x))
703+
.collect::<Vec<_>>()
704+
.into_par_iter()
705+
.enumerate()
706+
.for_each(|(n, o)| {
707+
let s = unsafe {
708+
std::slice::from_raw_parts_mut(
709+
data.as_ptr().add(n * self.rows).cast::<T>().cast_mut(),
710+
self.rows,
711+
)
712+
};
713+
s.copy_from_slice(self.col(o))
714+
});
715+
OwnedMatrix {
716+
rows,
717+
cols,
718+
colnames: self.colnames.map(|c| {
719+
c.into_iter()
720+
.enumerate()
721+
.filter(|(i, _)| !removing.contains(i))
722+
.map(|(_, x)| x)
723+
.collect()
724+
}),
725+
data: unsafe { std::mem::transmute(data) },
726+
}
727+
}
689728
}
690729

691730
impl OwnedMatrix<f64> {

0 commit comments

Comments
 (0)