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    pub search_log: u32,
22    pub min_match: u32,
23    pub target_length: u32,
24    pub search_strength: u32,
25}
26
27/// Returns the compression parameters for a given level, or `None` if out of range.
28pub fn level_params(level: i32) -> Option<LevelParams> {
29    Some(match level {
30        -7 => LevelParams {
31            strategy: Strategy::Fast,
32            window_log: 14,
33            hash_log: 14,
34            search_log: 0,
35            min_match: 4,
36            target_length: 8,
37            search_strength: 7,
38        },
39        -6 => LevelParams {
40            strategy: Strategy::Fast,
41            window_log: 14,
42            hash_log: 14,
43            search_log: 0,
44            min_match: 4,
45            target_length: 7,
46            search_strength: 7,
47        },
48        -5 => LevelParams {
49            strategy: Strategy::Fast,
50            window_log: 14,
51            hash_log: 14,
52            search_log: 0,
53            min_match: 4,
54            target_length: 6,
55            search_strength: 7,
56        },
57        -4 => LevelParams {
58            strategy: Strategy::Fast,
59            window_log: 14,
60            hash_log: 14,
61            search_log: 0,
62            min_match: 4,
63            target_length: 5,
64            search_strength: 7,
65        },
66        -3 => LevelParams {
67            strategy: Strategy::Fast,
68            window_log: 14,
69            hash_log: 14,
70            search_log: 0,
71            min_match: 4,
72            target_length: 4,
73            search_strength: 7,
74        },
75        -2 => LevelParams {
76            strategy: Strategy::Fast,
77            window_log: 14,
78            hash_log: 14,
79            search_log: 0,
80            min_match: 4,
81            target_length: 3,
82            search_strength: 7,
83        },
84        -1 => LevelParams {
85            strategy: Strategy::Fast,
86            window_log: 14,
87            hash_log: 14,
88            search_log: 0,
89            min_match: 4,
90            target_length: 2,
91            search_strength: 7,
92        },
93        1 => LevelParams {
94            strategy: Strategy::Fast,
95            window_log: 19,
96            hash_log: 14,
97            search_log: 0,
98            min_match: 7,
99            target_length: 1,
100            search_strength: 8,
101        },
102        2 => LevelParams {
103            strategy: Strategy::Fast,
104            window_log: 20,
105            hash_log: 16,
106            search_log: 0,
107            min_match: 6,
108            target_length: 1,
109            search_strength: 8,
110        },
111        3 => LevelParams {
112            strategy: Strategy::DFast,
113            window_log: 21,
114            hash_log: 17,
115            search_log: 1,
116            min_match: 5,
117            target_length: 1,
118            search_strength: 4,
119        },
120        4 => LevelParams {
121            strategy: Strategy::DFast,
122            window_log: 21,
123            hash_log: 18,
124            search_log: 1,
125            min_match: 5,
126            target_length: 1,
127            search_strength: 4,
128        },
129        _ => return None,
130    })
131}