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
//! # lzd-rs
//!
//! This library provides a Rust implementation of LZ double-factor factorization, an efficient grammar-based compression algorithm, proposed in the paper:
//!
//! > K Goto, H Bannai, S Inenaga, and M Takeda. **LZD Factorization: Simple and Practical Online Grammar Compression with Variable-to-Fixed Encoding.** In *CPM*, 2015.
//!
//! ## Examples
//!
//! ### Factorization
//!
//! ```rust
//! use lzd::compressor::Compressor;
//!
//! fn main() {
//! // Input text
//! let text = "abaaabababaabbabab".as_bytes();
//!
//! // Factorization
//! let mut factors = Vec::new();
//! let defined_factors = Compressor::run(text, |id: usize| {
//! factors.push(id);
//! });
//!
//! // Output factors
//! println!("factors: {:?}", factors);
//!
//! // Statistics
//! println!("defined_factors: {:?}", defined_factors);
//! }
//! ```
//!
//! The output will be
//!
//! ```compile_fail
//! factors: [97, 98, 97, 97, 256, 256, 256, 257, 98, 98, 258]
//! defined_factors: 261
//! ```
//!
//! *NOTE:* In this implementation, all 256 single characters are predefined as factors, so the number of factors defined will become 261.
//!
//! ### Defactorization
//!
//! ```rust
//! use lzd::decompressor::Decompressor;
//!
//! fn main() {
//! // Input text
//! let factors = [97, 98, 97, 97, 256, 256, 256, 257, 98, 98, 258];
//!
//! // Defactorization
//! let mut text = String::new();
//! Decompressor::run(&factors, |c: u8| {
//! text.push(c as char);
//! });
//!
//! // Decoded text
//! println!("text: {:?}", text);
//! }
//! ```
//!
//! The output will be
//!
//! ```compile_fail
//! text: "abaaabababaabbabab"
//! ```
//!