use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Pattern {
pub chars: Vec<String>,
pub weights: Vec<i32>,
pub length: usize,
}
impl Pattern {
pub fn new(chars: Vec<String>, weights: Vec<i32>) -> Self {
let length = chars.len();
Pattern {
chars,
weights,
length,
}
}
}
#[derive(Debug, Default, Clone)]
pub struct PatternsCollection {
patterns: HashMap<String, Vec<i32>>,
max_pattern_length: usize,
}
impl PatternsCollection {
pub fn new() -> Self {
Self::default()
}
pub fn add(&mut self, pattern: Pattern) {
let key = pattern.chars.concat();
if pattern.length > self.max_pattern_length {
self.max_pattern_length = pattern.length;
}
self.patterns.insert(key, pattern.weights);
}
pub fn get_weights(&self, subword: &str) -> Option<&[i32]> {
self.patterns.get(subword).map(|v| v.as_slice())
}
pub fn count(&self) -> usize {
self.patterns.len()
}
pub fn max_length(&self) -> usize {
self.max_pattern_length
}
pub fn is_empty(&self) -> bool {
self.patterns.is_empty()
}
}