response_time_analysis/arrival/
dmin.rs1use std::iter;
2
3use super::ArrivalBound;
4use crate::time::Duration;
5
6struct DeltaMinIterator<'a, AB: ArrivalBound> {
7 ab: &'a AB,
8 steps: Box<dyn Iterator<Item = Duration> + 'a>,
9 step_count: usize,
10 next_step: Option<Duration>,
11 next_count: usize,
12}
13
14impl<'a, AB: ArrivalBound> DeltaMinIterator<'a, AB> {
15 fn new(ab: &'a AB) -> Self {
16 DeltaMinIterator {
17 ab,
18 steps: ab.steps_iter(),
19 next_step: None,
20 next_count: 2,
21 step_count: 0,
22 }
23 }
24
25 fn advance(&mut self) {
26 while self.step_count < self.next_count {
27 self.next_step = self.steps.next();
28 if let Some(delta) = self.next_step {
29 self.step_count = self.ab.number_arrivals(delta);
30 } else {
31 break;
32 }
33 }
34 }
35}
36
37impl<'a, AB: ArrivalBound> Iterator for DeltaMinIterator<'a, AB> {
38 type Item = (usize, Duration);
39
40 fn next(&mut self) -> Option<Self::Item> {
41 self.advance();
42 if let Some(delta) = self.next_step {
43 let dmin = Some((self.next_count, delta - Duration::from(1)));
44 self.next_count += 1;
45 dmin
46 } else {
47 None
48 }
49 }
50}
51
52pub fn nonzero_delta_min_iter(
53 ab: &impl ArrivalBound,
54) -> impl Iterator<Item = (usize, Duration)> + '_ {
55 DeltaMinIterator::new(ab)
57}
58
59pub fn delta_min_iter(ab: &impl ArrivalBound) -> impl Iterator<Item = (usize, Duration)> + '_ {
60 iter::once((0, Duration::from(0)))
62 .chain(iter::once((1, Duration::from(0))))
63 .chain(DeltaMinIterator::new(ab))
64}