Skip to content

Commit

Permalink
Add an iterator over owned data held by the sampling
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris00 committed Apr 2, 2024
1 parent 3b71386 commit 891fcc7
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,15 @@ impl<D> Sampling<D> {
}
}

/// Consumes the sampling and return an iterator on the curve
/// points and (owned) associated data.
pub fn into_iter_data(self) -> SamplingIntoIterData<D> {
SamplingIntoIterData {
path: self.path.into_iter(),
guess_len: self.guess_len.get(),
}
}

/// Iterator on the x-coordinates of the sampling.
/// See [`Self::iter`] for more information.
#[inline]
Expand Down Expand Up @@ -406,8 +415,37 @@ impl<'a, D> Iterator for SamplingIterMut<'a, D> {
}
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
(0, Some(self.guess_len))
}
}

/// Iterator returning the curve points and owned data.
pub struct SamplingIntoIterData<D> {
path: list::IntoIter<Point<D>>,
guess_len: usize,
}

impl<D> Iterator for SamplingIntoIterData<D> {
type Item = ([f64; 2], D);

fn next(&mut self) -> Option<Self::Item> {
match self.path.next() {
None => None,
Some(p) => {
self.guess_len -= 1;
Some((p.xy, p.data))
}
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
(0, Some(self.guess_len))
}
}


/// Intersection of a segment with the bounding box.
#[derive(Debug)]
enum Intersection<D> {
Expand Down Expand Up @@ -1817,6 +1855,18 @@ mod tests {
}
}

#[test]
fn into_iter_data() {
let s = Sampling::uniform(|x| (x, x as i32), 0., 4.).n(3)
.init(&[1.]).init_pt([(3., (0., -1))]).build();
let expected = vec![
([0.,0.], 0), ([1.,1.], 1), ([2.,2.], 2), ([3., 0.], -1),
([4.,4.], 4)];
for (i, d) in s.into_iter_data().enumerate() {
assert_eq!(d, expected[i]);
}
}

/// In order the judge the quality of the sampling, we save it
/// with the internal cost data.
fn write_with_point_costs<D>(
Expand Down

0 comments on commit 891fcc7

Please sign in to comment.