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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use super::seqnum_distance;
#[derive(Debug, PartialEq)]
pub(crate) enum Comparison {
Void,
Before,
Inside,
After,
}
pub(crate) struct Iterator<'a, T> {
data: &'a [Option<T>],
sample: SampleSequenceLocation,
i: u16,
}
impl<'a, T> std::iter::Iterator for Iterator<'a, T> {
type Item = Option<&'a T>;
fn next(&mut self) -> Option<Self::Item> {
if self.sample.compare(self.i) == Comparison::Inside {
let old_i = self.i as usize;
self.i = self.i.wrapping_add(1);
return Some(self.data[old_i].as_ref());
}
None
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct SampleSequenceLocation {
pub(crate) head: u16,
pub(crate) tail: u16,
}
impl SampleSequenceLocation {
pub(crate) fn new() -> Self {
Self { head: 0, tail: 0 }
}
pub(crate) fn empty(&self) -> bool {
self.head == self.tail
}
pub(crate) fn has_data(&self) -> bool {
self.head != self.tail
}
pub(crate) fn count(&self) -> u16 {
seqnum_distance(self.head, self.tail)
}
pub(crate) fn compare(&self, pos: u16) -> Comparison {
if self.head == self.tail {
return Comparison::Void;
}
if self.head < self.tail {
if self.head <= pos && pos < self.tail {
return Comparison::Inside;
}
} else if self.head <= pos || pos < self.tail {
return Comparison::Inside;
}
if self.head.wrapping_sub(pos) <= pos.wrapping_sub(self.tail) {
return Comparison::Before;
}
Comparison::After
}
pub(crate) fn range<'s, 'a, T>(&'s self, data: &'a [Option<T>]) -> Iterator<'a, T> {
Iterator {
data,
sample: *self,
i: self.head,
}
}
}