use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct Dictionary {
str_to_id: HashMap<String, u32>,
id_to_str: Vec<String>,
}
impl Dictionary {
pub fn new() -> Self {
Self {
str_to_id: HashMap::new(),
id_to_str: Vec::new(),
}
}
pub fn len(&self) -> usize {
self.id_to_str.len()
}
#[allow(dead_code)]
pub fn is_empty(&self) -> bool {
self.id_to_str.is_empty()
}
pub fn get_or_insert(&mut self, s: &str) -> u32 {
if let Some(&id) = self.str_to_id.get(s) {
id
} else {
let id = self.id_to_str.len() as u32;
self.str_to_id.insert(s.to_string(), id);
self.id_to_str.push(s.to_string());
id
}
}
pub fn clear(&mut self) {
self.str_to_id.clear();
self.id_to_str.clear();
}
pub fn get_name(&self, id: u32) -> Option<&str> {
self.id_to_str.get(id as usize).map(|s| s.as_str())
}
pub fn iter(&self) -> impl Iterator<Item = (&str, u32)> + '_ {
self.id_to_str
.iter()
.enumerate()
.map(|(id, s)| (s.as_str(), id as u32))
}
}
impl Default for Dictionary {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_dictionary_basic() {
let mut dict = Dictionary::new();
assert_eq!(dict.len(), 0);
let id1 = dict.get_or_insert("hello");
assert_eq!(id1, 0);
assert_eq!(dict.len(), 1);
let id2 = dict.get_or_insert("world");
assert_eq!(id2, 1);
assert_eq!(dict.len(), 2);
let id3 = dict.get_or_insert("hello");
assert_eq!(id3, id1);
assert_eq!(dict.len(), 2);
}
#[test]
fn test_dictionary_clear() {
let mut dict = Dictionary::new();
dict.get_or_insert("hello");
dict.get_or_insert("world");
assert_eq!(dict.len(), 2);
dict.clear();
assert_eq!(dict.len(), 0);
}
#[test]
fn test_dictionary_iter() {
let mut dict = Dictionary::new();
dict.get_or_insert("hello");
dict.get_or_insert("world");
dict.get_or_insert("rust");
let items: Vec<_> = dict.iter().collect();
assert_eq!(items.len(), 3);
assert_eq!(items[0], ("hello", 0));
assert_eq!(items[1], ("world", 1));
assert_eq!(items[2], ("rust", 2));
}
}