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
//! rust-compression
//!
//! # Licensing
//! This Source Code is subject to the terms of the Mozilla Public License
//! version 2.0 (the "License"). You can obtain a copy of the License at
//! <http://mozilla.org/MPL/2.0/>.
//!
//! # Examples
//!
//! ```rust
//! use compression::prelude::*;
//! use std::cmp::Ordering;
//!
//! fn main() {
//!     pub fn comparison(lhs: LzssCode, rhs: LzssCode) -> Ordering {
//!         match (lhs, rhs) {
//!             (
//!                 LzssCode::Reference {
//!                     len: llen,
//!                     pos: lpos,
//!                 },
//!                 LzssCode::Reference {
//!                     len: rlen,
//!                     pos: rpos,
//!                 },
//!             ) => ((llen << 3) + rpos).cmp(&((rlen << 3) + lpos)).reverse(),
//!             (LzssCode::Symbol(_), LzssCode::Symbol(_)) => Ordering::Equal,
//!             (_, LzssCode::Symbol(_)) => Ordering::Greater,
//!             (LzssCode::Symbol(_), _) => Ordering::Less,
//!         }
//!     }
//!     # #[cfg(feature = "lzss")]
//!     let compressed = b"aabbaabbaabbaabb\n"
//!         .into_iter()
//!         .cloned()
//!         .encode(&mut LzssEncoder::new(comparison, 0x1_0000, 256, 3, 3), Action::Finish)
//!         .collect::<Result<Vec<_>, _>>()
//!         .unwrap();
//!
//!     # #[cfg(feature = "lzss")]
//!     let decompressed = compressed
//!         .iter()
//!         .cloned()
//!         .decode(&mut LzssDecoder::new(0x1_0000))
//!         .collect::<Result<Vec<_>, _>>()
//!         .unwrap();
//! }
//! ```
#![cfg(feature = "lzss")]

pub(crate) mod decoder;
pub(crate) mod encoder;
mod slidedict;

use crate::core::cmp::Ordering;

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum LzssCode {
    Symbol(u8),
    Reference { len: usize, pos: usize },
}

impl Default for LzssCode {
    fn default() -> Self {
        LzssCode::Symbol(0)
    }
}

#[derive(Clone, Debug)]
pub(crate) struct MatchInfo {
    pub(crate) len: usize,
    pub(crate) pos: u16,
}

fn compare_match_info<F: Fn(LzssCode, LzssCode) -> Ordering>(
    comp: F,
    arg1: &MatchInfo,
    arg2: &MatchInfo,
) -> Ordering {
    comp(
        LzssCode::Reference {
            len: arg1.len,
            pos: arg1.pos as usize,
        },
        LzssCode::Reference {
            len: arg2.len,
            pos: arg2.pos as usize,
        },
    )
}

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

    pub(crate) fn comparison(lhs: LzssCode, rhs: LzssCode) -> Ordering {
        match (lhs, rhs) {
            (
                LzssCode::Reference {
                    len: llen,
                    pos: lpos,
                },
                LzssCode::Reference {
                    len: rlen,
                    pos: rpos,
                },
            ) => ((llen << 3) + rpos).cmp(&((rlen << 3) + lpos)).reverse(),
            (LzssCode::Symbol(_), LzssCode::Symbol(_)) => Ordering::Equal,
            (_, LzssCode::Symbol(_)) => Ordering::Greater,
            (LzssCode::Symbol(_), _) => Ordering::Less,
        }
    }
}