From 0517aa474b93068132869e03f1a43fc653c2d393 Mon Sep 17 00:00:00 2001 From: Michael Neumann Date: Mon, 19 Oct 2015 23:31:40 +0200 Subject: [PATCH] Get rid of nalgebra. Simplify traits --- Cargo.toml | 1 - src/lib.rs | 38 +++++++++++++++----------------------- 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b375811..a0d7c22 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,5 +4,4 @@ version = "0.0.1" authors = ["Michael Neumann "] [dependencies] -nalgebra = "*" bit-vec = "*" diff --git a/src/lib.rs b/src/lib.rs index bfd70e0..4b7299c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,15 +11,12 @@ // TODO: -// * Implement SquareMatrix. Get rid of nalgebra. // * Reuse path Vec in step5 // * Cleanup -extern crate nalgebra as na; extern crate bit_vec; -use na::{DMat, BaseNum}; -use std::ops::{Neg, Sub}; +use std::ops::{Add, Sub}; use std::num::Zero; use std::cmp; use square_matrix::SquareMatrix; @@ -28,12 +25,17 @@ use coverage::Coverage; mod square_matrix; mod coverage; +pub trait WeightNum: Ord + Eq + Copy + Sub + Add + Zero {} + +impl WeightNum for T +where T: Ord + Eq + Copy + Sub + Add + Zero { } + #[derive(Debug)] -pub struct WeightMatrix { +pub struct WeightMatrix { c: SquareMatrix } -impl WeightMatrix where T: BaseNum + Ord + Eq + Sub + Copy { +impl WeightMatrix { pub fn from_row_vec(n: usize, data: Vec) -> WeightMatrix { WeightMatrix{c: SquareMatrix::from_row_vec(n, data)} } @@ -229,8 +231,7 @@ impl MarkMatrix { /// For each row of the matrix, find the smallest element and /// subtract it from every element in its row. Go to Step 2. -fn step1(c: &mut WeightMatrix) -> Step -where T: BaseNum + Ord { +fn step1(c: &mut WeightMatrix) -> Step { let n = c.n(); for row in 0..n { @@ -245,8 +246,7 @@ where T: BaseNum + Ord { /// Find a zero (Z) in the resulting matrix. If there is no starred /// zero in its row or column, star Z. Repeat for each element in the /// matrix. Go to Step 3. -fn step2(c: &WeightMatrix, marks: &mut MarkMatrix, cov: &mut Coverage) -> Step -where T: BaseNum + Ord + Neg + Eq { +fn step2(c: &WeightMatrix, marks: &mut MarkMatrix, cov: &mut Coverage) -> Step { let n = c.n(); assert!(marks.n() == n); @@ -272,8 +272,7 @@ where T: BaseNum + Ord + Neg + Eq { /// Cover each column containing a starred zero. If K columns are /// covered, the starred zeros describe a complete set of unique /// assignments. In this case, Go to DONE, otherwise, Go to Step 4. -fn step3(c: &WeightMatrix, marks: &MarkMatrix, cov: &mut Coverage) -> Step -where T: BaseNum + Ord + Neg + Eq { +fn step3(c: &WeightMatrix, marks: &MarkMatrix, cov: &mut Coverage) -> Step { let n = c.n(); assert!(marks.n() == n); @@ -302,9 +301,7 @@ where T: BaseNum + Ord + Neg + Eq { /// cover this row and uncover the column containing the starred /// zero. Continue in this manner until there are no uncovered zeros /// left. Save the smallest uncovered value and Go to Step 6. -fn step4(c: &WeightMatrix, marks: &mut MarkMatrix, cov: &mut Coverage) -> Step -where T: BaseNum + Ord + Neg + Eq { - +fn step4(c: &WeightMatrix, marks: &mut MarkMatrix, cov: &mut Coverage) -> Step { let n = c.n(); assert!(marks.n() == n); @@ -341,9 +338,7 @@ where T: BaseNum + Ord + Neg + Eq { /// that has no starred zero in its column. Unstar each starred zero /// of the series, star each primed zero of the series, erase all /// primes and uncover every line in the matrix. Return to Step 3 -fn step5(c: &WeightMatrix, marks: &mut MarkMatrix, cov: &mut Coverage, z0: (usize, usize)) -> Step -where T: BaseNum + Ord + Neg + Eq { - +fn step5(c: &WeightMatrix, marks: &mut MarkMatrix, cov: &mut Coverage, z0: (usize, usize)) -> Step { let n = c.n(); assert!(marks.n() == n); @@ -388,9 +383,7 @@ where T: BaseNum + Ord + Neg + Eq { /// row, and subtract it from every element of each uncovered column. /// Return to Step 4 without altering any stars, primes, or covered /// lines. -fn step6(c: &mut WeightMatrix, cov: &Coverage) -> Step -where T: BaseNum + Ord + Neg + Eq + Copy + Neg { - +fn step6(c: &mut WeightMatrix, cov: &Coverage) -> Step { let n = c.n(); assert!(cov.n() == n); @@ -409,8 +402,7 @@ where T: BaseNum + Ord + Neg + Eq + Copy + Neg { return Step::Step4(None); } -pub fn solve_assignment(weights: &mut WeightMatrix) -> Vec<(usize,usize)> -where T: BaseNum + Ord + Neg + Eq + Copy { +pub fn solve_assignment(weights: &mut WeightMatrix) -> Vec<(usize,usize)> { let n = weights.n(); let mut marks = MarkMatrix::new(n);