diff options
author | Juan Manuel Tomás <jtomas1815@gmail.com> | 2021-01-11 19:44:07 -0300 |
---|---|---|
committer | Juan Manuel Tomás <jtomas1815@gmail.com> | 2021-01-11 19:44:07 -0300 |
commit | 1891436bf1d6b18f1dded15ae693f90f8a6ab975 (patch) | |
tree | 0042eb0075f0b684bd7b1ecc6a4bd4f54f5440ab /src | |
parent | 857b9974b7a8e89016357cc91ca4a37561ead7be (diff) | |
download | bezier-1891436bf1d6b18f1dded15ae693f90f8a6ab975.tar.gz bezier-1891436bf1d6b18f1dded15ae693f90f8a6ab975.zip |
Move zip implementation from poly to poly/iter
Zip seems more appropriate on an iterator.
Diffstat (limited to 'src')
-rw-r--r-- | src/poly.rs | 15 | ||||
-rw-r--r-- | src/poly/iter.rs | 16 |
2 files changed, 16 insertions, 15 deletions
diff --git a/src/poly.rs b/src/poly.rs index 9c95796..0e1cd9e 100644 --- a/src/poly.rs +++ b/src/poly.rs @@ -2,10 +2,8 @@ mod iter; use crate::number::Number; use iter::Iter; -use std::cmp; use std::cmp::Ordering; use std::ops::{Add, Sub, Mul, Rem}; -use std::iter::{Zip, Take}; #[derive(PartialEq, Debug)] pub struct Poly(pub Vec<Number>); @@ -20,14 +18,7 @@ impl Poly { } fn iter(&self) -> Iter { - Iter::new(self.0.clone()) - } - - fn zip(&self, other: &Self) -> Zip<Take<Iter>, Take<Iter>> { - let deg = cmp::max(self.degree(), other.degree()) + 1; - let a = self.iter().take(deg); - let b = other.iter().take(deg); - a.zip(b) + Iter::new(self.0.clone(), self.degree()) } pub fn eval(&self, n: Number) -> Number { @@ -52,7 +43,7 @@ impl Add for &Poly { type Output = Poly; fn add(self, other: Self) -> Poly { - Poly(self.zip(other).map(|(x, y)| {x + y}).collect()) + Poly(self.iter().zip(other.iter()).map(|(x, y)| {x + y}).collect()) } } @@ -60,7 +51,7 @@ impl Sub for &Poly { type Output = Poly; fn sub(self, other: Self) -> Poly { - Poly(self.zip(other).map(|(x, y)| {x - y}).collect()) + Poly(self.iter().zip(other.iter()).map(|(x, y)| {x - y}).collect()) } } diff --git a/src/poly/iter.rs b/src/poly/iter.rs index 26c5989..abc7462 100644 --- a/src/poly/iter.rs +++ b/src/poly/iter.rs @@ -1,13 +1,23 @@ use crate::number::Number; +use std::cmp; +use std::iter::{Zip, Take}; pub struct Iter { index: usize, - data: Vec<Number> + data: Vec<Number>, + degree: usize } impl Iter { - pub fn new(data: Vec<Number>) -> Iter { - Iter { index: 0, data } + pub fn new(data: Vec<Number>, degree: usize) -> Iter { + Iter { index: 0, data, degree } + } + + pub fn zip(self, other: Self) -> Zip<Take<Iter>, Take<Iter>> { + let deg = cmp::max(self.degree, other.degree) + 1; + let a = self.take(deg); + let b = other.take(deg); + a.zip(b) } } |