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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use bits;
use coding::trellis;
use consts;
pub type DibitCoder = DataCoder<trellis::DibitStates>;
pub type TribitCoder = DataCoder<trellis::TribitStates>;
pub struct DataCoder<S: trellis::States> {
fsm: trellis::TrellisFSM<S>,
buf: [bits::Dibit; consts::CODING_DIBITS],
pos: usize,
}
impl<S: trellis::States> DataCoder<S> {
fn for_fsm(fsm: trellis::TrellisFSM<S>) -> DataCoder<S> {
DataCoder {
fsm: fsm,
buf: [bits::Dibit::default(); consts::CODING_DIBITS],
pos: 0,
}
}
pub fn finish(mut self) -> [bits::Dibit; consts::CODING_DIBITS] {
let pair = self.fsm.finish();
self.append(pair);
assert!(self.pos == self.buf.len());
self.buf
}
fn feed_symbol(&mut self, symbol: S::Symbol) {
let pair = self.fsm.feed(symbol);
self.append(pair);
}
fn append(&mut self, (a, b): (bits::Dibit, bits::Dibit)) {
self.buf[self.pos] = a;
self.pos += 1;
self.buf[self.pos] = b;
self.pos += 1;
}
}
impl DibitCoder {
pub fn new() -> DibitCoder {
Self::for_fsm(trellis::DibitFSM::new())
}
pub fn feed_bytes<T: Iterator<Item = u8>>(mut self, bytes: T) -> Self {
for dibit in bits::Dibits::new(bytes) {
self.feed_symbol(dibit);
}
self
}
}
impl TribitCoder {
pub fn new() -> TribitCoder {
Self::for_fsm(trellis::TribitFSM::new())
}
pub fn feed_bytes<T: Iterator<Item = u8>>(mut self, bytes: T) -> Self {
for tribit in bits::Tribits::new(bytes) {
self.feed_symbol(tribit);
}
self
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_dibit_coder() {
let buf = DibitCoder::new().feed_bytes((0..12).map(|i| {
if i % 2 == 0 { 0x00 } else { 0xFF }
})).finish();
assert_eq!(buf[0].bits(), 0b00);
assert_eq!(buf[1].bits(), 0b10);
assert_eq!(buf[2].bits(), 0b00);
assert_eq!(buf[3].bits(), 0b10);
assert_eq!(buf[4].bits(), 0b00);
assert_eq!(buf[5].bits(), 0b10);
assert_eq!(buf[6].bits(), 0b00);
assert_eq!(buf[7].bits(), 0b10);
assert_eq!(buf[8].bits(), 0b11);
assert_eq!(buf[9].bits(), 0b11);
assert_eq!(buf[10].bits(), 0b10);
assert_eq!(buf[11].bits(), 0b00);
assert_eq!(buf[12].bits(), 0b10);
assert_eq!(buf[13].bits(), 0b00);
assert_eq!(buf[14].bits(), 0b10);
assert_eq!(buf[15].bits(), 0b00);
}
#[test]
fn test_tribit_coder() {
let buf = TribitCoder::new().feed_bytes((0..18).map(|i| {
if i % 2 == 0 { 0x00 } else { 0xFF }
})).finish();
assert_eq!(buf[0].bits(), 0b00);
assert_eq!(buf[1].bits(), 0b10);
assert_eq!(buf[2].bits(), 0b00);
assert_eq!(buf[3].bits(), 0b10);
assert_eq!(buf[4].bits(), 0b11);
assert_eq!(buf[5].bits(), 0b01);
assert_eq!(buf[6].bits(), 0b11);
assert_eq!(buf[7].bits(), 0b01);
assert_eq!(buf[8].bits(), 0b10);
assert_eq!(buf[9].bits(), 0b00);
assert_eq!(buf[10].bits(), 0b11);
assert_eq!(buf[11].bits(), 0b10);
assert_eq!(buf[12].bits(), 0b11);
assert_eq!(buf[13].bits(), 0b11);
}
}