Skip to main content

lib_compression/
pattern_dict.rs

1//! Global pattern dictionary
2
3use crate::patterns::Pattern;
4use serde::{Deserialize, Serialize};
5
6/// Dictionary entry.
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct DictionaryEntry {
9    pub pattern: Pattern,
10    pub local_frequency: u64,
11    pub network_frequency: u64,
12}
13
14/// Global pattern dictionary.
15pub struct PatternDictionary {
16    entries: Vec<DictionaryEntry>,
17}
18
19impl PatternDictionary {
20    pub fn new() -> Self {
21        Self { entries: Vec::new() }
22    }
23
24    pub fn add(&mut self, entry: DictionaryEntry) {
25        self.entries.push(entry);
26    }
27
28    pub fn get(&self, id: &[u8; 32]) -> Option<&DictionaryEntry> {
29        self.entries.iter().find(|e| e.pattern.id == *id)
30    }
31}
32
33impl Default for PatternDictionary {
34    fn default() -> Self {
35        Self::new()
36    }
37}
38
39/// Global pattern dictionary (lazy static).
40lazy_static::lazy_static! {
41    pub static ref GLOBAL_PATTERN_DICT: std::sync::RwLock<PatternDictionary> =
42        std::sync::RwLock::new(PatternDictionary::new());
43}