From e486e6a9819997c6c6bb6f2b6d243b411e9c6ee8 Mon Sep 17 00:00:00 2001 From: Vinzent Steinberg Date: Mon, 20 Mar 2017 10:14:01 +0100 Subject: [PATCH] Address some of @cuviper's feedback --- integer/src/lib.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/integer/src/lib.rs b/integer/src/lib.rs index 95484dc..5a895a2 100644 --- a/integer/src/lib.rs +++ b/integer/src/lib.rs @@ -666,34 +666,33 @@ impl_integer_for_usize!(u64, test_integer_u64); impl_integer_for_usize!(usize, test_integer_usize); /// An iterator over binomial coefficients. -pub struct BinomialCoeff { +pub struct IterBinomial { a: T, n: T, k: T, } -impl BinomialCoeff +impl IterBinomial where T: Integer, { /// For a given n, iterate over all binomial coefficients ((k, n - k), binomial(n, k)). - pub fn new(n: T) -> BinomialCoeff { - BinomialCoeff { + pub fn new(n: T) -> IterBinomial { + IterBinomial { k: T::zero(), a: T::one(), n: n } } } -impl Iterator for BinomialCoeff - where T: Integer + Clone, - for<'a> &'a T: std::cmp::PartialEq<&'a T> +impl Iterator for IterBinomial + where T: Integer + Clone { type Item = ((T, T), T); fn next(&mut self) -> Option<((T, T), T)> { - if &self.k > &self.n { + if self.k > self.n { return None; } - self.a = if &self.k != &T::zero() { + self.a = if !self.k.is_zero() { (self.a.clone() * (self.n.clone() - self.k.clone() + T::one())) / self.k.clone() } else { T::one() @@ -768,7 +767,7 @@ fn test_binomial_coeff() { macro_rules! check_simple { ($t:ty) => { { let n: $t = 3; - let c: Vec<_> = BinomialCoeff::new(n).collect(); + let c: Vec<_> = IterBinomial::new(n).collect(); let expected = vec![((0, 3), 1), ((1, 2), 3), ((2, 1), 3), ((3, 0), 1)]; assert_eq!(c, expected); } } @@ -786,7 +785,7 @@ fn test_binomial_coeff() { macro_rules! check_binomial { ($t:ty, $n:expr) => { { let n: $t = $n; - let c: Vec<_> = BinomialCoeff::new(n).collect(); + let c: Vec<_> = IterBinomial::new(n).collect(); for &((k, _), b) in &c { assert_eq!(b, binomial(n, k)); }