summaryrefslogtreecommitdiff
path: root/src/poly/iter.rs
blob: 90cc4b5809a7c0da92131525c72a37c598658c42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use std::cmp;
use std::iter::{Take, Zip};

pub struct Iter {
    index: usize,
    data: Vec<f32>,
    degree: usize,
}

impl Iter {
    pub fn new(data: Vec<f32>, 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)
    }
}

impl Iterator for Iter {
    type Item = f32;

    fn next(&mut self) -> Option<f32> {
        self.index += 1;
        if self.index <= self.data.len() {
            Some(self.data[self.index - 1])
        } else {
            Some(0.0)
        }
    }
}