Skip to content

Commit a07f8e3

Browse files
author
maxtremblay
committed
Implement display trait
1 parent a0b4867 commit a07f8e3

File tree

5 files changed

+32
-18
lines changed

5 files changed

+32
-18
lines changed

src/matrix/concat.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ fn concat_rows_horizontally(
2828
rows: EitherOrBoth<SparseBinSlice, SparseBinSlice>,
2929
) -> Vec<usize> {
3030
match rows {
31-
EitherOrBoth::Both(left_row, right_row) => left_row.concat(&right_row).take_inner_vec(),
32-
EitherOrBoth::Left(row) => row.to_owned().take_inner_vec(),
31+
EitherOrBoth::Both(left_row, right_row) => left_row.concat(&right_row).to_positions_vec(),
32+
EitherOrBoth::Left(row) => row.to_owned().to_positions_vec(),
3333
EitherOrBoth::Right(row) => pad_right_row(pad, row.as_slice()),
3434
}
3535
}
@@ -46,7 +46,7 @@ pub(super) fn concat_vertically(
4646
let rows = top_matrix
4747
.rows()
4848
.chain(bottom_matrix.rows())
49-
.map(|row| row.to_owned().take_inner_vec())
49+
.map(|row| row.to_owned().to_positions_vec())
5050
.collect();
5151
SparseBinMat::new(top_matrix.number_of_columns(), rows)
5252
}

src/matrix/gauss_jordan.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl GaussJordan {
8282
self.number_of_columns,
8383
&self.rows[row_index],
8484
))
85-
.take_inner_vec();
85+
.to_positions_vec();
8686
if self.rows[row_index].is_empty() {
8787
self.rows.swap_remove(row_index);
8888
continue;

src/matrix/mod.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -700,18 +700,14 @@ fn dimension_to_string(dimension: (usize, usize)) -> String {
700700
format!("({} x {})", dimension.0, dimension.1)
701701
}
702702

703-
//impl std::fmt::Display for SparseBinMat {
704-
//fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
705-
//for row in self.rows() {
706-
//write!(f, "[ ")?;
707-
//for column in row.iter() {
708-
//write!(f, "{} ", column)?;
709-
//}
710-
//write!(f, "]")?;
711-
//}
712-
//Ok(())
713-
//}
714-
//}
703+
impl std::fmt::Display for SparseBinMat {
704+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
705+
for row in self.rows() {
706+
writeln!(f, "{}", row)?;
707+
}
708+
Ok(())
709+
}
710+
}
715711

716712
#[cfg(test)]
717713
mod test {

src/matrix/nullspace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn reduce_to_normal_form(matrix: &SparseBinMat) -> SparseBinMat {
3636
while r.weight() > 1 && r.as_slice()[1] < matrix.number_of_rows() {
3737
r = &r + &matrix.row(r.as_slice()[1]).unwrap();
3838
}
39-
r.take_inner_vec()
39+
r.to_positions_vec()
4040
})
4141
.collect();
4242
SparseBinMat::new(matrix.number_of_columns(), rows)

src/vector/mod.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::BinaryNumber;
22
use is_sorted::IsSorted;
33
use std::collections::HashMap;
4+
use std::fmt;
45
use std::ops::{Add, Deref};
56

67
mod bitwise_operations;
@@ -94,7 +95,18 @@ impl SparseBinVec {
9495
}
9596
}
9697

97-
pub(crate) fn take_inner_vec(self) -> Vec<usize> {
98+
/// Converts the sparse binary vector to a `Vec` of
99+
/// the non trivial positions.
100+
///
101+
/// # Example
102+
///
103+
/// ```
104+
/// # use sparse_bin_mat::SparseBinVec;
105+
/// let vector = SparseBinVec::new(3, vec![0, 2]);
106+
///
107+
/// assert_eq!(vector.to_positions_vec(), vec![0, 2]);
108+
/// ```
109+
pub fn to_positions_vec(self) -> Vec<usize> {
98110
self.positions
99111
}
100112
}
@@ -439,6 +451,12 @@ where
439451
}
440452
}
441453

454+
impl<T: Deref<Target = [usize]>> fmt::Display for SparseBinVecBase<T> {
455+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
456+
write!(f, "{:?}", self.positions.deref())
457+
}
458+
}
459+
442460
#[cfg(test)]
443461
mod test {
444462
use super::*;

0 commit comments

Comments
 (0)