atlas-archive-core 1.1.0

High-performance compression library with adaptive context modeling (Loom) and .nyx archives
Documentation
//! Move-to-Front (MTF) transform for Atlas preprocessing.
//! Effectively clusters frequent symbols to low indices after a BWT.

use crate::alloc::vec::Vec;

pub fn mtf_transform(data: &[u8]) -> Vec<u8> {
    let mut alphabet: Vec<u8> = (0..=255).collect();
    let mut out = Vec::with_capacity(data.len());

    for &b in data {
        let pos = alphabet.iter().position(|&x| x == b).unwrap();
        out.push(pos as u8);

        // Move to front
        let val = alphabet.remove(pos);
        alphabet.insert(0, val);
    }
    out
}

pub fn mtf_inverse(data: &[u8]) -> Vec<u8> {
    let mut alphabet: Vec<u8> = (0..=255).collect();
    let mut out = Vec::with_capacity(data.len());

    for &pos in data {
        let b = alphabet[pos as usize];
        out.push(b);

        // Move to front
        let val = alphabet.remove(pos as usize);
        alphabet.insert(0, val);
    }
    out
}