lib-compression 0.1.0

Universal lossless compression via network-wide deduplication
Documentation
//! Global pattern dictionary

use crate::patterns::Pattern;
use serde::{Deserialize, Serialize};

/// Dictionary entry.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DictionaryEntry {
    pub pattern: Pattern,
    pub local_frequency: u64,
    pub network_frequency: u64,
}

/// Global pattern dictionary.
pub struct PatternDictionary {
    entries: Vec<DictionaryEntry>,
}

impl PatternDictionary {
    pub fn new() -> Self {
        Self { entries: Vec::new() }
    }

    pub fn add(&mut self, entry: DictionaryEntry) {
        self.entries.push(entry);
    }

    pub fn get(&self, id: &[u8; 32]) -> Option<&DictionaryEntry> {
        self.entries.iter().find(|e| e.pattern.id == *id)
    }
}

impl Default for PatternDictionary {
    fn default() -> Self {
        Self::new()
    }
}

/// Global pattern dictionary (lazy static).
lazy_static::lazy_static! {
    pub static ref GLOBAL_PATTERN_DICT: std::sync::RwLock<PatternDictionary> =
        std::sync::RwLock::new(PatternDictionary::new());
}