Address some of @cuviper's feedback
This commit is contained in:
parent
8d235759dd
commit
e486e6a981
|
@ -666,34 +666,33 @@ impl_integer_for_usize!(u64, test_integer_u64);
|
||||||
impl_integer_for_usize!(usize, test_integer_usize);
|
impl_integer_for_usize!(usize, test_integer_usize);
|
||||||
|
|
||||||
/// An iterator over binomial coefficients.
|
/// An iterator over binomial coefficients.
|
||||||
pub struct BinomialCoeff<T> {
|
pub struct IterBinomial<T> {
|
||||||
a: T,
|
a: T,
|
||||||
n: T,
|
n: T,
|
||||||
k: T,
|
k: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> BinomialCoeff<T>
|
impl<T> IterBinomial<T>
|
||||||
where T: Integer,
|
where T: Integer,
|
||||||
{
|
{
|
||||||
/// For a given n, iterate over all binomial coefficients ((k, n - k), binomial(n, k)).
|
/// For a given n, iterate over all binomial coefficients ((k, n - k), binomial(n, k)).
|
||||||
pub fn new(n: T) -> BinomialCoeff<T> {
|
pub fn new(n: T) -> IterBinomial<T> {
|
||||||
BinomialCoeff {
|
IterBinomial {
|
||||||
k: T::zero(), a: T::one(), n: n
|
k: T::zero(), a: T::one(), n: n
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Iterator for BinomialCoeff<T>
|
impl<T> Iterator for IterBinomial<T>
|
||||||
where T: Integer + Clone,
|
where T: Integer + Clone
|
||||||
for<'a> &'a T: std::cmp::PartialEq<&'a T>
|
|
||||||
{
|
{
|
||||||
type Item = ((T, T), T);
|
type Item = ((T, T), T);
|
||||||
|
|
||||||
fn next(&mut self) -> Option<((T, T), T)> {
|
fn next(&mut self) -> Option<((T, T), T)> {
|
||||||
if &self.k > &self.n {
|
if self.k > self.n {
|
||||||
return None;
|
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()
|
(self.a.clone() * (self.n.clone() - self.k.clone() + T::one())) / self.k.clone()
|
||||||
} else {
|
} else {
|
||||||
T::one()
|
T::one()
|
||||||
|
@ -768,7 +767,7 @@ fn test_binomial_coeff() {
|
||||||
macro_rules! check_simple {
|
macro_rules! check_simple {
|
||||||
($t:ty) => { {
|
($t:ty) => { {
|
||||||
let n: $t = 3;
|
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)];
|
let expected = vec![((0, 3), 1), ((1, 2), 3), ((2, 1), 3), ((3, 0), 1)];
|
||||||
assert_eq!(c, expected);
|
assert_eq!(c, expected);
|
||||||
} }
|
} }
|
||||||
|
@ -786,7 +785,7 @@ fn test_binomial_coeff() {
|
||||||
macro_rules! check_binomial {
|
macro_rules! check_binomial {
|
||||||
($t:ty, $n:expr) => { {
|
($t:ty, $n:expr) => { {
|
||||||
let n: $t = $n;
|
let n: $t = $n;
|
||||||
let c: Vec<_> = BinomialCoeff::new(n).collect();
|
let c: Vec<_> = IterBinomial::new(n).collect();
|
||||||
for &((k, _), b) in &c {
|
for &((k, _), b) in &c {
|
||||||
assert_eq!(b, binomial(n, k));
|
assert_eq!(b, binomial(n, k));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue