Use associated type
This commit is contained in:
parent
482bf47cd0
commit
f47fb4eb32
39
src/lib.rs
39
src/lib.rs
@ -38,14 +38,15 @@ pub trait WeightNum: PartialOrd + Copy + Sub<Output=Self> + Add<Output=Self> + Z
|
|||||||
impl<T> WeightNum for T
|
impl<T> WeightNum for T
|
||||||
where T: PartialOrd + Copy + Sub<Output=T> + Add<Output=T> + Zero { }
|
where T: PartialOrd + Copy + Sub<Output=T> + Add<Output=T> + Zero { }
|
||||||
|
|
||||||
pub trait Weights<T: WeightNum> {
|
pub trait Weights {
|
||||||
|
type T: WeightNum;
|
||||||
fn n(&self) -> usize;
|
fn n(&self) -> usize;
|
||||||
fn element_at(&self, pos: (usize, usize)) -> T;
|
fn element_at(&self, pos: (usize, usize)) -> Self::T;
|
||||||
fn is_element_zero(&self, pos: (usize, usize)) -> bool;
|
fn is_element_zero(&self, pos: (usize, usize)) -> bool;
|
||||||
|
|
||||||
fn sub_min_of_each_row(&mut self);
|
fn sub_min_of_each_row(&mut self);
|
||||||
fn add_row(&mut self, row: usize, val: T);
|
fn add_row(&mut self, row: usize, val: Self::T);
|
||||||
fn sub_col(&mut self, col: usize, val: T);
|
fn sub_col(&mut self, col: usize, val: Self::T);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
@ -61,9 +62,8 @@ enum Step {
|
|||||||
|
|
||||||
/// For each row of the matrix, find the smallest element and
|
/// For each row of the matrix, find the smallest element and
|
||||||
/// subtract it from every element in its row. Go to Step 2.
|
/// subtract it from every element in its row. Go to Step 2.
|
||||||
fn step1<T, W>(c: &mut W) -> Step
|
fn step1<W>(c: &mut W) -> Step
|
||||||
where T: WeightNum,
|
where W: Weights
|
||||||
W: Weights<T>
|
|
||||||
{
|
{
|
||||||
c.sub_min_of_each_row();
|
c.sub_min_of_each_row();
|
||||||
return Step::Step2;
|
return Step::Step2;
|
||||||
@ -72,9 +72,8 @@ fn step1<T, W>(c: &mut W) -> Step
|
|||||||
/// Find a zero (Z) in the resulting matrix. If there is no starred
|
/// 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
|
/// zero in its row or column, star Z. Repeat for each element in the
|
||||||
/// matrix. Go to Step 3.
|
/// matrix. Go to Step 3.
|
||||||
fn step2<T, W>(c: &W, marks: &mut MarkMatrix, cov: &mut Coverage) -> Step
|
fn step2<W>(c: &W, marks: &mut MarkMatrix, cov: &mut Coverage) -> Step
|
||||||
where T: WeightNum,
|
where W: Weights
|
||||||
W: Weights<T>
|
|
||||||
{
|
{
|
||||||
|
|
||||||
let n = c.n();
|
let n = c.n();
|
||||||
@ -100,9 +99,8 @@ fn step2<T, W>(c: &W, marks: &mut MarkMatrix, cov: &mut Coverage) -> Step
|
|||||||
/// Cover each column containing a starred zero. If K columns are
|
/// Cover each column containing a starred zero. If K columns are
|
||||||
/// covered, the starred zeros describe a complete set of unique
|
/// covered, the starred zeros describe a complete set of unique
|
||||||
/// assignments. In this case, Go to DONE, otherwise, Go to Step 4.
|
/// assignments. In this case, Go to DONE, otherwise, Go to Step 4.
|
||||||
fn step3<T, W>(c: &W, marks: &MarkMatrix, cov: &mut Coverage) -> Step
|
fn step3<W>(c: &W, marks: &MarkMatrix, cov: &mut Coverage) -> Step
|
||||||
where T: WeightNum,
|
where W: Weights
|
||||||
W: Weights<T>
|
|
||||||
{
|
{
|
||||||
|
|
||||||
let n = c.n();
|
let n = c.n();
|
||||||
@ -129,9 +127,8 @@ fn step3<T, W>(c: &W, marks: &MarkMatrix, cov: &mut Coverage) -> Step
|
|||||||
/// cover this row and uncover the column containing the starred
|
/// cover this row and uncover the column containing the starred
|
||||||
/// zero. Continue in this manner until there are no uncovered zeros
|
/// zero. Continue in this manner until there are no uncovered zeros
|
||||||
/// left. Save the smallest uncovered value and Go to Step 6.
|
/// left. Save the smallest uncovered value and Go to Step 6.
|
||||||
fn step4<T, W>(c: &W, marks: &mut MarkMatrix, cov: &mut Coverage) -> Step
|
fn step4<W>(c: &W, marks: &mut MarkMatrix, cov: &mut Coverage) -> Step
|
||||||
where T: WeightNum,
|
where W: Weights
|
||||||
W: Weights<T>
|
|
||||||
{
|
{
|
||||||
|
|
||||||
let n = c.n();
|
let n = c.n();
|
||||||
@ -216,9 +213,8 @@ fn step5(marks: &mut MarkMatrix, cov: &mut Coverage, z0: (usize, usize)) -> Step
|
|||||||
/// row, and subtract it from every element of each uncovered column.
|
/// row, and subtract it from every element of each uncovered column.
|
||||||
/// Return to Step 4 without altering any stars, primes, or covered
|
/// Return to Step 4 without altering any stars, primes, or covered
|
||||||
/// lines.
|
/// lines.
|
||||||
fn step6<T, W>(c: &mut W, cov: &Coverage) -> Step
|
fn step6<W>(c: &mut W, cov: &Coverage) -> Step
|
||||||
where T: WeightNum,
|
where W: Weights
|
||||||
W: Weights<T>
|
|
||||||
{
|
{
|
||||||
|
|
||||||
let n = c.n();
|
let n = c.n();
|
||||||
@ -255,9 +251,8 @@ fn step6<T, W>(c: &mut W, cov: &Coverage) -> Step
|
|||||||
return Step::Step4(None);
|
return Step::Step4(None);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn solve_assignment<T, W>(weights: &mut W) -> Vec<(usize, usize)>
|
pub fn solve_assignment<W>(weights: &mut W) -> Vec<(usize, usize)>
|
||||||
where T: WeightNum,
|
where W: Weights
|
||||||
W: Weights<T>
|
|
||||||
{
|
{
|
||||||
let n = weights.n();
|
let n = weights.n();
|
||||||
|
|
||||||
|
@ -7,7 +7,8 @@ pub struct WeightMatrix<T: WeightNum> {
|
|||||||
c: SquareMatrix<T>,
|
c: SquareMatrix<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: WeightNum> Weights<T> for WeightMatrix<T> {
|
impl<T: WeightNum> Weights for WeightMatrix<T> {
|
||||||
|
type T = T;
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn n(&self) -> usize {
|
fn n(&self) -> usize {
|
||||||
self.c.n()
|
self.c.n()
|
||||||
|
Loading…
Reference in New Issue
Block a user