|
|
@@ -69,6 +69,20 @@ impl Hufftree {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+pub fn get_char_frequencies(text: String) -> HashMap<char, i32> {
|
|
|
+ let mut map = HashMap::new();
|
|
|
+
|
|
|
+ for character in text.chars() {
|
|
|
+ if map.contains_key(&character) {
|
|
|
+ map.entry(character).and_modify(|e| *e += 1);
|
|
|
+ } else {
|
|
|
+ map.insert(character, 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ map
|
|
|
+}
|
|
|
+
|
|
|
#[cfg(test)]
|
|
|
mod test {
|
|
|
use super::*;
|
|
|
@@ -104,4 +118,15 @@ mod test {
|
|
|
assert_eq!(b_code.to_string(), "01");
|
|
|
assert_eq!(c_code.to_string(), "00");
|
|
|
}
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn get_charcter_freq() {
|
|
|
+ let text = String::from("aaaaaabb cc");
|
|
|
+ let freq = get_char_frequencies(text);
|
|
|
+
|
|
|
+ assert_eq!(freq.get(&'a').unwrap(), &6);
|
|
|
+ assert_eq!(freq.get(&'b').unwrap(), &2);
|
|
|
+ assert_eq!(freq.get(&'c').unwrap(), &2);
|
|
|
+ assert_eq!(freq.get(&' ').unwrap(), &1);
|
|
|
+ }
|
|
|
}
|