Skip to main content

zrip_encode/
strategy.rs

1#![forbid(unsafe_code)]
2
3/// Match-finding strategy.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum Strategy {
6    /// Single hash table (levels -7 through 2).
7    Fast,
8    /// Short + long hash tables (levels 3-4).
9    DFast,
10}
11
12/// Compression parameters for a specific level.
13///
14/// Obtain via [`level_params`] or construct directly for custom tuning.
15/// Pass to [`compress_with_params`](crate::compress_with_params).
16#[derive(Debug, Clone, Copy)]
17pub struct LevelParams {
18    pub strategy: Strategy,
19    pub window_log: u32,
20    pub hash_log: u32,
21    /// DFast short table log. Same as hashLog for Fast strategy.
22    pub chain_log: u32,
23    pub search_log: u32,
24    pub min_match: u32,
25    pub target_length: u32,
26    pub search_strength: u32,
27    pub force_raw_literals: bool,
28}
29
30/// Default compression level used when level 0 is requested.
31pub const DEFAULT_LEVEL: i32 = 1;
32
33/// Returns the compression parameters for a given level, or `None` if out of range.
34///
35/// Level 0 is treated as "library default" and maps to level 1.
36pub fn level_params(level: i32) -> Option<LevelParams> {
37    Some(match level {
38        0 => return level_params(DEFAULT_LEVEL),
39        -7 => LevelParams {
40            strategy: Strategy::Fast,
41            window_log: 19,
42            hash_log: 13,
43            chain_log: 13,
44            search_log: 0,
45            min_match: 5,
46            target_length: 8,
47            search_strength: 7,
48            force_raw_literals: true,
49        },
50        -6 => LevelParams {
51            strategy: Strategy::Fast,
52            window_log: 19,
53            hash_log: 13,
54            chain_log: 13,
55            search_log: 0,
56            min_match: 5,
57            target_length: 7,
58            search_strength: 7,
59            force_raw_literals: false,
60        },
61        -5 => LevelParams {
62            strategy: Strategy::Fast,
63            window_log: 19,
64            hash_log: 13,
65            chain_log: 13,
66            search_log: 0,
67            min_match: 5,
68            target_length: 6,
69            search_strength: 7,
70            force_raw_literals: false,
71        },
72        -4 => LevelParams {
73            strategy: Strategy::Fast,
74            window_log: 19,
75            hash_log: 13,
76            chain_log: 13,
77            search_log: 0,
78            min_match: 5,
79            target_length: 5,
80            search_strength: 7,
81            force_raw_literals: false,
82        },
83        -3 => LevelParams {
84            strategy: Strategy::Fast,
85            window_log: 19,
86            hash_log: 13,
87            chain_log: 13,
88            search_log: 0,
89            min_match: 5,
90            target_length: 4,
91            search_strength: 7,
92            force_raw_literals: false,
93        },
94        -2 => LevelParams {
95            strategy: Strategy::Fast,
96            window_log: 19,
97            hash_log: 13,
98            chain_log: 13,
99            search_log: 0,
100            min_match: 5,
101            target_length: 3,
102            search_strength: 7,
103            force_raw_literals: false,
104        },
105        -1 => LevelParams {
106            strategy: Strategy::Fast,
107            window_log: 19,
108            hash_log: 13,
109            chain_log: 13,
110            search_log: 0,
111            min_match: 5,
112            target_length: 2,
113            search_strength: 7,
114            force_raw_literals: false,
115        },
116        1 => LevelParams {
117            strategy: Strategy::Fast,
118            window_log: 19,
119            hash_log: 14,
120            chain_log: 14,
121            search_log: 0,
122            min_match: 4,
123            target_length: 1,
124            search_strength: 8,
125            force_raw_literals: false,
126        },
127        2 => LevelParams {
128            strategy: Strategy::Fast,
129            window_log: 20,
130            hash_log: 16,
131            chain_log: 16,
132            search_log: 0,
133            min_match: 4,
134            target_length: 1,
135            search_strength: 8,
136            force_raw_literals: false,
137        },
138        3 => LevelParams {
139            strategy: Strategy::DFast,
140            window_log: 21,
141            hash_log: 17,
142            chain_log: 17,
143            search_log: 1,
144            min_match: 5,
145            target_length: 1,
146            search_strength: 4,
147            force_raw_literals: false,
148        },
149        4 => LevelParams {
150            strategy: Strategy::DFast,
151            window_log: 21,
152            hash_log: 18,
153            chain_log: 18,
154            search_log: 1,
155            min_match: 5,
156            target_length: 1,
157            search_strength: 4,
158            force_raw_literals: false,
159        },
160        _ => return None,
161    })
162}