use crate::encoding::CompressionLevel;
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Strategy {
Fast,
Dfast,
Greedy,
Lazy,
Lazy2,
Btlazy2,
Btopt,
Btultra,
Btultra2,
}
impl Strategy {
pub const fn ordinal(self) -> u32 {
match self {
Self::Fast => 1,
Self::Dfast => 2,
Self::Greedy => 3,
Self::Lazy => 4,
Self::Lazy2 => 5,
Self::Btlazy2 => 6,
Self::Btopt => 7,
Self::Btultra => 8,
Self::Btultra2 => 9,
}
}
pub const fn from_ordinal(ordinal: u32) -> Option<Self> {
Some(match ordinal {
1 => Self::Fast,
2 => Self::Dfast,
3 => Self::Greedy,
4 => Self::Lazy,
5 => Self::Lazy2,
6 => Self::Btlazy2,
7 => Self::Btopt,
8 => Self::Btultra,
9 => Self::Btultra2,
_ => return None,
})
}
pub(crate) const fn tag(self) -> crate::encoding::strategy::StrategyTag {
use crate::encoding::strategy::StrategyTag;
match self {
Self::Fast => StrategyTag::Fast,
Self::Dfast => StrategyTag::Dfast,
Self::Greedy => StrategyTag::Greedy,
Self::Lazy | Self::Lazy2 => StrategyTag::Lazy,
Self::Btlazy2 => StrategyTag::Btlazy2,
Self::Btopt => StrategyTag::BtOpt,
Self::Btultra => StrategyTag::BtUltra,
Self::Btultra2 => StrategyTag::BtUltra2,
}
}
pub(crate) const fn lazy_depth(self) -> u8 {
match self {
Self::Fast | Self::Dfast | Self::Greedy => 0,
Self::Lazy => 1,
_ => 2,
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum CParameter {
WindowLog,
HashLog,
ChainLog,
SearchLog,
MinMatch,
TargetLength,
Strategy,
EnableLongDistanceMatching,
LdmHashLog,
LdmMinMatch,
LdmBucketSizeLog,
LdmHashRateLog,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Bounds {
pub lower_bound: i64,
pub upper_bound: i64,
}
impl Bounds {
pub const fn contains(&self, value: i64) -> bool {
value >= self.lower_bound && value <= self.upper_bound
}
}
impl CParameter {
pub const fn bounds(self) -> Bounds {
let (lower_bound, upper_bound) = match self {
Self::WindowLog => (10, 30),
Self::HashLog => (6, 30),
Self::ChainLog => (6, 30),
Self::SearchLog => (1, 30),
Self::MinMatch => (3, 7),
Self::TargetLength => (0, 131_072),
Self::Strategy => (1, 9),
Self::EnableLongDistanceMatching => (0, 1),
Self::LdmHashLog => (6, 30),
Self::LdmMinMatch => (4, 4096),
Self::LdmBucketSizeLog => (1, 8),
Self::LdmHashRateLog => (0, 24),
};
Bounds {
lower_bound,
upper_bound,
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum ParameterError {
OutOfBounds {
parameter: CParameter,
value: i64,
bounds: Bounds,
},
}
impl core::fmt::Display for ParameterError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::OutOfBounds {
parameter,
value,
bounds,
} => write!(
f,
"compression parameter {parameter:?} = {value} out of bounds \
[{}, {}]",
bounds.lower_bound, bounds.upper_bound
),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for ParameterError {}
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub(crate) struct LdmOverride {
pub(crate) hash_log: Option<u32>,
pub(crate) min_match: Option<u32>,
pub(crate) bucket_size_log: Option<u32>,
pub(crate) hash_rate_log: Option<u32>,
}
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub(crate) struct ParamOverrides {
pub(crate) window_log: Option<u8>,
pub(crate) hash_log: Option<u32>,
pub(crate) chain_log: Option<u32>,
pub(crate) search_log: Option<u32>,
pub(crate) min_match: Option<u32>,
pub(crate) target_length: Option<u32>,
pub(crate) strategy: Option<Strategy>,
pub(crate) ldm: Option<LdmOverride>,
}
impl ParamOverrides {
pub(crate) fn is_empty(&self) -> bool {
self.window_log.is_none()
&& self.hash_log.is_none()
&& self.chain_log.is_none()
&& self.search_log.is_none()
&& self.min_match.is_none()
&& self.target_length.is_none()
&& self.strategy.is_none()
&& self.ldm.is_none()
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct CompressionParameters {
level: CompressionLevel,
overrides: ParamOverrides,
}
impl CompressionParameters {
pub fn builder(level: CompressionLevel) -> CompressionParametersBuilder {
CompressionParametersBuilder {
level,
window_log: None,
hash_log: None,
chain_log: None,
search_log: None,
min_match: None,
target_length: None,
strategy: None,
enable_ldm: false,
ldm: LdmOverride::default(),
}
}
pub fn level(&self) -> CompressionLevel {
self.level
}
pub fn long_distance_matching_enabled(&self) -> bool {
self.overrides.ldm.is_some()
}
pub(crate) fn overrides(&self) -> ParamOverrides {
self.overrides
}
}
#[derive(Copy, Clone, Debug)]
pub struct CompressionParametersBuilder {
level: CompressionLevel,
window_log: Option<u32>,
hash_log: Option<u32>,
chain_log: Option<u32>,
search_log: Option<u32>,
min_match: Option<u32>,
target_length: Option<u32>,
strategy: Option<Strategy>,
enable_ldm: bool,
ldm: LdmOverride,
}
impl CompressionParametersBuilder {
pub fn window_log(mut self, value: u32) -> Self {
self.window_log = Some(value);
self
}
pub fn hash_log(mut self, value: u32) -> Self {
self.hash_log = Some(value);
self
}
pub fn chain_log(mut self, value: u32) -> Self {
self.chain_log = Some(value);
self
}
pub fn search_log(mut self, value: u32) -> Self {
self.search_log = Some(value);
self
}
pub fn min_match(mut self, value: u32) -> Self {
self.min_match = Some(value);
self
}
pub fn target_length(mut self, value: u32) -> Self {
self.target_length = Some(value);
self
}
pub fn strategy(mut self, value: Strategy) -> Self {
self.strategy = Some(value);
self
}
pub fn enable_long_distance_matching(mut self, enable: bool) -> Self {
self.enable_ldm = enable;
self
}
pub fn ldm_hash_log(mut self, value: u32) -> Self {
self.enable_ldm = true;
self.ldm.hash_log = Some(value);
self
}
pub fn ldm_min_match(mut self, value: u32) -> Self {
self.enable_ldm = true;
self.ldm.min_match = Some(value);
self
}
pub fn ldm_bucket_size_log(mut self, value: u32) -> Self {
self.enable_ldm = true;
self.ldm.bucket_size_log = Some(value);
self
}
pub fn ldm_hash_rate_log(mut self, value: u32) -> Self {
self.enable_ldm = true;
self.ldm.hash_rate_log = Some(value);
self
}
pub fn build(self) -> Result<CompressionParameters, ParameterError> {
check(CParameter::WindowLog, self.window_log)?;
check(CParameter::HashLog, self.hash_log)?;
check(CParameter::ChainLog, self.chain_log)?;
check(CParameter::SearchLog, self.search_log)?;
check(CParameter::MinMatch, self.min_match)?;
check(CParameter::TargetLength, self.target_length)?;
if let Some(s) = self.strategy {
check(CParameter::Strategy, Some(s.ordinal()))?;
}
let ldm = if self.enable_ldm {
check(CParameter::LdmHashLog, self.ldm.hash_log)?;
check(CParameter::LdmMinMatch, self.ldm.min_match)?;
check(CParameter::LdmBucketSizeLog, self.ldm.bucket_size_log)?;
check(CParameter::LdmHashRateLog, self.ldm.hash_rate_log)?;
Some(self.ldm)
} else {
None
};
Ok(CompressionParameters {
level: self.level,
overrides: ParamOverrides {
window_log: self.window_log.map(|v| v as u8),
hash_log: self.hash_log,
chain_log: self.chain_log,
search_log: self.search_log,
min_match: self.min_match,
target_length: self.target_length,
strategy: self.strategy,
ldm,
},
})
}
}
fn check(parameter: CParameter, value: Option<u32>) -> Result<(), ParameterError> {
if let Some(value) = value {
let bounds = parameter.bounds();
let value = i64::from(value);
if !bounds.contains(value) {
return Err(ParameterError::OutOfBounds {
parameter,
value,
bounds,
});
}
}
Ok(())
}
#[cfg(test)]
mod tests;