1 use super::seqnum_distance;
2 
3 #[derive(Debug, PartialEq)]
4 pub(crate) enum Comparison {
5     Void,
6     Before,
7     Inside,
8     After,
9 }
10 
11 pub(crate) struct Iterator<'a, T> {
12     data: &'a [Option<T>],
13     sample: SampleSequenceLocation,
14     i: u16,
15 }
16 
17 impl<'a, T> std::iter::Iterator for Iterator<'a, T> {
18     type Item = Option<&'a T>;
19 
next(&mut self) -> Option<Self::Item>20     fn next(&mut self) -> Option<Self::Item> {
21         if self.sample.compare(self.i) == Comparison::Inside {
22             let old_i = self.i as usize;
23             self.i = self.i.wrapping_add(1);
24             return Some(self.data[old_i].as_ref());
25         }
26 
27         None
28     }
29 }
30 
31 #[derive(Debug, Clone, Copy)]
32 pub(crate) struct SampleSequenceLocation {
33     /// head is the first packet in a sequence
34     pub(crate) head: u16,
35     /// tail is always set to one after the final sequence number,
36     /// so if `head == tail` then the sequence is empty
37     pub(crate) tail: u16,
38 }
39 
40 impl SampleSequenceLocation {
new() -> Self41     pub(crate) fn new() -> Self {
42         Self { head: 0, tail: 0 }
43     }
44 
empty(&self) -> bool45     pub(crate) fn empty(&self) -> bool {
46         self.head == self.tail
47     }
48 
has_data(&self) -> bool49     pub(crate) fn has_data(&self) -> bool {
50         self.head != self.tail
51     }
52 
count(&self) -> u1653     pub(crate) fn count(&self) -> u16 {
54         seqnum_distance(self.head, self.tail)
55     }
56 
compare(&self, pos: u16) -> Comparison57     pub(crate) fn compare(&self, pos: u16) -> Comparison {
58         if self.head == self.tail {
59             return Comparison::Void;
60         }
61         if self.head < self.tail {
62             if self.head <= pos && pos < self.tail {
63                 return Comparison::Inside;
64             }
65         } else if self.head <= pos || pos < self.tail {
66             return Comparison::Inside;
67         }
68         if self.head.wrapping_sub(pos) <= pos.wrapping_sub(self.tail) {
69             return Comparison::Before;
70         }
71         Comparison::After
72     }
73 
range<'a, T>(&self, data: &'a [Option<T>]) -> Iterator<'a, T>74     pub(crate) fn range<'a, T>(&self, data: &'a [Option<T>]) -> Iterator<'a, T> {
75         Iterator {
76             data,
77             sample: *self,
78             i: self.head,
79         }
80     }
81 }
82