cshannon 0.2.1

An implementation of compression algorithms leading up to Huffman's encoding
Documentation
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Copyright 2020 Prathmesh Prabhu
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::common::{pack_u64, unpack_u64};
use super::letter::{Letter, Peephole as lPeephole};
use anyhow::{anyhow, Result};

/// Alphabet is an ordered list of unique Letters.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Alphabet(Vec<Letter>);

impl Alphabet {
    /// Create a new Alphabet with the given [`Letter`]s.
    ///
    /// The order of letters is significant.
    /// [`Self::pack()`] & [`Self::unpack()`] conserve the order.
    pub fn new(letters: Vec<Letter>) -> Result<Self> {
        let a = Alphabet(letters);
        a.validate()?;
        Ok(a)
    }

    /// Number of letters in the alphpabet.
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Check whether this alphabet is empty.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Return a reference to the ordered [`Letter`]s in this alphabet.
    pub fn letters(&self) -> &Vec<Letter> {
        &self.0
    }
}

impl Alphabet {
    fn validate(&self) -> Result<()> {
        for l in self.0.iter() {
            l.validate()?;
        }
        self.tree()?;
        Ok(())
    }
}

/// An alphabet may be generated from an iterator over letters.
///
/// This operation clones each provided letter.
impl<'a> std::iter::FromIterator<&'a Letter> for Alphabet {
    fn from_iter<I: IntoIterator<Item = &'a Letter>>(i: I) -> Self {
        let mut a = Alphabet(Vec::new());
        for l in i {
            a.0.push(l.clone());
        }
        a
    }
}

impl Alphabet {
    /// Serialize the alphabet to a [`Write`er](std::io::Write).
    ///
    /// Can be deserialized back to an alphabet with [`Self::unpack()`].
    pub fn pack<W: std::io::Write>(self, mut w: W) -> Result<()> {
        let letter_count = self.0.len();
        w.write_all(&pack_u64(letter_count as u64))?;
        for l in self.0.into_iter() {
            l.pack(&mut w)?;
        }
        Ok(())
    }

    /// Deserialize a data generated with [`Self::pack()`] from a
    /// [`Read`er](std::io::Read).
    pub fn unpack<R: std::io::Read>(mut r: R) -> Result<Self> {
        let letter_count = unpack_u64(&mut r)?;
        let mut letters = Vec::new();
        for _ in 0..letter_count {
            let l = Letter::unpack(&mut r)?;
            letters.push(l);
        }
        Ok(Alphabet(letters))
    }
}

/// Provides deeper access for sibling modules than the public API.
pub trait Peephole {
    fn tree<'a>(&'a self) -> Result<Node<'a>>;
}

impl Peephole for Alphabet {
    fn tree<'a>(&'a self) -> Result<Node<'a>> {
        let mut root = Node::Internal {
            zero: None,
            one: None,
        };
        if self.0.is_empty() {
            // Attempt to parse _any_ text will fail, since the Alphabet is
            // empty.
            return Ok(root);
        }
        for l in self.0.iter() {
            let (tip, offset) = Alphabet::follow_branch(&mut root, l, 0)?;
            let tail = Alphabet::tail(l, offset + 1);
            match tip {
                Node::Internal { zero, one } => {
                    if l.at(offset)? {
                        *one = Some(Box::new(tail));
                    } else {
                        *zero = Some(Box::new(tail));
                    }
                }
                Node::Leaf { .. } => panic!("tip can not be a Leaf"),
            }
        }
        Ok(root)
    }
}

impl Alphabet {
    fn follow_branch<'a, 'b>(
        tree: &'b mut Node<'a>,
        l: &'a Letter,
        offset: usize,
    ) -> Result<(&'b mut Node<'a>, usize)> {
        match l.at(offset) {
            Ok(false) => {
                match tree {
                    Node::Internal {
                        zero: Some(zero), ..
                    } => {
                        return Alphabet::follow_branch(zero, l, offset + 1);
                    }
                    // This error message actually needs a slice l[0:offset]
                    Node::Leaf { .. } => return Err(anyhow!("Duplicate prefix {}", l)),
                    _ => return Ok((tree, offset)),
                }
            }
            Ok(true) => {
                match tree {
                    Node::Internal { one: Some(one), .. } => {
                        return Alphabet::follow_branch(one, l, offset + 1);
                    }
                    // This error message actually needs a slice l[0:offset]
                    Node::Leaf { .. } => return Err(anyhow!("Duplicate prefix {}", l)),
                    _ => return Ok((tree, offset)),
                }
            }
            Err(_) => Err(anyhow!("Duplicate prefix {}", l)),
        }
    }

    fn tail<'a>(l: &'a Letter, offset: usize) -> Node<'a> {
        match l.at(offset) {
            Ok(false) => Node::Internal {
                zero: Some(Box::new(Alphabet::tail(l, offset + 1))),
                one: None,
            },
            Ok(true) => Node::Internal {
                zero: None,
                one: Some(Box::new(Alphabet::tail(l, offset + 1))),
            },
            Err(_) => Node::Leaf { letter: l },
        }
    }
}

#[derive(Debug, PartialEq)]
pub enum Node<'a> {
    Internal {
        zero: Option<Box<Node<'a>>>,
        one: Option<Box<Node<'a>>>,
    },
    Leaf {
        letter: &'a Letter,
    },
}

impl Node<'_> {
    // These methods are used only by unit tests.
    #![allow(dead_code)]

    fn new0<'a>(zero: Node<'a>) -> Node<'a> {
        Node::Internal {
            zero: Some(Box::new(zero)),
            one: None,
        }
    }

    pub fn new1<'a>(one: Node<'a>) -> Node<'a> {
        Node::Internal {
            zero: None,
            one: Some(Box::new(one)),
        }
    }

    pub fn newl<'a>(l: &'a Letter) -> Node<'a> {
        Node::Leaf { letter: l }
    }

    pub fn new<'a>(zero: Node<'a>, one: Node<'a>) -> Node<'a> {
        Node::Internal {
            zero: Some(Box::new(zero)),
            one: Some(Box::new(one)),
        }
    }
}

#[cfg(test)]
mod pack_tests {
    use super::*;
    use std::io::Cursor;

    #[test]
    fn roundtrip_trivial() {
        let a = Alphabet::new(vec![]).unwrap();
        let mut packed = Vec::<u8>::new();
        assert!(a.pack(&mut packed).is_ok());
        let got = Alphabet::unpack(Cursor::new(packed)).unwrap();
        assert_eq!(got.0.len(), 0);
    }

    #[test]
    fn roundtrip_single_letter() {
        let v = vec![Letter::from_bytes(&[0b10000001])];
        let a = Alphabet::new(v.clone()).unwrap();
        let mut packed = Vec::<u8>::new();
        assert!(a.pack(&mut packed).is_ok());
        let got = Alphabet::unpack(Cursor::new(packed)).unwrap();
        assert_eq!(got.0, v);
    }

    #[test]
    fn roundtrip_byte_letters() {
        let v = vec![
            Letter::from_bytes(&[0b10000001]),
            Letter::from_bytes(&[0b10000000]),
            Letter::from_bytes(&[0b00000111]),
        ];
        let a = Alphabet::new(v.clone()).unwrap();
        let mut packed = Vec::<u8>::new();
        assert!(a.pack(&mut packed).is_ok());
        let got = Alphabet::unpack(Cursor::new(packed)).unwrap();
        assert_eq!(got.0, v);
    }

    #[test]
    fn roundtrip_large_letters() {
        let v = vec![
            Letter::from_bytes(&[0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99]),
            Letter::from_bytes(&[0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9]),
        ];
        let a = Alphabet::new(v.clone()).unwrap();
        let mut packed = Vec::<u8>::new();
        assert!(a.pack(&mut packed).is_ok());
        let got = Alphabet::unpack(Cursor::new(packed)).unwrap();
        assert_eq!(got.0, v);
    }

    #[test]
    fn roundtrip_many_letters() {
        let v = vec![
            Letter::from_bytes(&[0x11]),
            Letter::from_bytes(&[0x12]),
            Letter::from_bytes(&[0x13]),
            Letter::from_bytes(&[0x14]),
            Letter::from_bytes(&[0x15]),
            Letter::from_bytes(&[0x16]),
            Letter::from_bytes(&[0x17]),
            Letter::from_bytes(&[0x18]),
            Letter::from_bytes(&[0x19]),
        ];
        let a = Alphabet::new(v.clone()).unwrap();
        let mut packed = Vec::<u8>::new();
        assert!(a.pack(&mut packed).is_ok());
        let got = Alphabet::unpack(Cursor::new(packed)).unwrap();
        assert_eq!(got.0, v);
    }

    #[test]
    fn roundtrip_different_lengths() {
        let v = vec![
            Letter::from_bytes(&[0x01]),
            Letter::from_bytes(&[0xa1, 0xa2]),
            Letter::from_bytes(&[0xb1, 0xb2, 0xb3]),
            Letter::from_bytes(&[0xc1, 0xc2]),
            Letter::from_bytes(&[0xd1]),
        ];
        let a = Alphabet::new(v.clone()).unwrap();
        let mut packed = Vec::<u8>::new();
        assert!(a.pack(&mut packed).is_ok());
        let got = Alphabet::unpack(Cursor::new(packed)).unwrap();
        assert_eq!(got.0, v);
    }

    #[test]
    fn roundtrip_unaligned_lengths() {
        let v = vec![
            Letter::new(&[0b1110_0000], 3),
            Letter::new(&[0b1000_0000, 0x11], 11),
        ];
        let a = Alphabet::new(v.clone()).unwrap();
        let mut packed = Vec::<u8>::new();
        assert!(a.pack(&mut packed).is_ok());
        let got = Alphabet::unpack(Cursor::new(packed)).unwrap();
        assert_eq!(got.0, v);
    }
}

#[cfg(test)]
mod tree_tests {
    use super::*;

    #[test]
    fn empty() {
        let a = Alphabet::new(vec![]).unwrap();
        assert_eq!(
            a.tree().unwrap(),
            Node::Internal {
                zero: None,
                one: None
            }
        );
    }

    #[test]
    fn leaf1() {
        let l = Letter::new(&[0b1000_0000], 1);
        let a = Alphabet::new(vec![l.clone()]).unwrap();
        assert_eq!(a.tree().unwrap(), Node::new1(Node::newl(&l)));
    }

    #[test]
    fn leaf0() {
        let l = Letter::new(&[0b1000_0000], 2);
        let a = Alphabet::new(vec![l.clone()]).unwrap();
        assert_eq!(a.tree().unwrap(), Node::new1(Node::new0(Node::newl(&l))),);
    }

    #[test]
    fn multi_byte_letter() {
        let l = Letter::new(&[0b1000_0000, 0b1100_0000], 10);
        let a = Alphabet::new(vec![l.clone()]).unwrap();
        assert_eq!(
            a.tree().unwrap(),
            Node::new1(Node::new0(Node::new0(Node::new0(Node::new0(Node::new0(
                Node::new0(Node::new0(Node::new1(Node::new1(Node::newl(&l)))))
            )))))),
        );
    }

    #[test]
    fn unshared_letters() {
        let l0 = Letter::new(&[0b0100_0000], 2);
        let l1 = Letter::new(&[0b1000_0000], 1);
        let a = Alphabet::new(vec![l0.clone(), l1.clone()]).unwrap();
        assert_eq!(
            a.tree().unwrap(),
            Node::new(Node::new1(Node::newl(&l0)), Node::newl(&l1)),
        )
    }

    #[test]
    fn shared_letters() {
        let l0 = Letter::new(&[0b0100_0000], 3);
        let l1 = Letter::new(&[0b0110_0000], 4);
        let a = Alphabet::new(vec![l0.clone(), l1.clone()]).unwrap();
        assert_eq!(
            a.tree().unwrap(),
            Node::new0(Node::new1(Node::new(
                Node::newl(&l0),
                Node::new0(Node::newl(&l1)),
            ))),
        );
    }

    #[test]
    fn multi_byte_shared() {
        let l0 = Letter::new(&[0b1000_0000, 0b1100_0000], 10);
        let l1 = Letter::new(&[0b1000_0000, 0b0000_0000], 10);
        let l2 = Letter::new(&[0b1010_0000], 3);
        let l3 = Letter::new(&[0b0010_0000], 3);
        let a = Alphabet::new(vec![l0.clone(), l1.clone(), l2.clone(), l3.clone()]).unwrap();
        assert_eq!(
            a.tree().unwrap(),
            Node::new(
                Node::new0(Node::new1(Node::newl(&l3))),
                Node::new0(Node::new(
                    Node::new0(Node::new0(Node::new0(Node::new0(Node::new0(Node::new(
                        Node::new0(Node::newl(&l1)),
                        Node::new1(Node::newl(&l0)),
                    )))))),
                    Node::newl(&l2),
                )),
            ),
        );
    }
}