-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmap_with_dict.rs
32 lines (26 loc) · 1.21 KB
/
map_with_dict.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use entropy_map::MapWithDict;
use std::collections::HashMap;
fn main() {
// Initialize `MapWithDict` with default parameters from Rust's `HashMap`
let mut hash_map = HashMap::<u64, String>::new();
hash_map.insert(1, "Dog".to_string());
hash_map.insert(2, "Cat".to_string());
hash_map.insert(3, "Dog".to_string());
let map = MapWithDict::try_from(hash_map).expect("failed to create MapWithDict");
// Test keys that are present in the map
assert_eq!(map.get(&1).unwrap(), &"Dog".to_string());
assert_eq!(map.get(&2).unwrap(), &"Cat".to_string());
assert_eq!(map.get(&3).unwrap(), &"Dog".to_string());
// Test a key that is not present in the MPHF
assert_eq!(map.get(&4), None);
#[cfg(feature = "rkyv_derive")]
{
// Serialize map to rkyv and test again
let rkyv_bytes = rkyv::to_bytes::<_, 1024>(&map).unwrap();
let rkyv_map = rkyv::check_archived_root::<MapWithDict<u64, String>>(&rkyv_bytes).unwrap();
assert_eq!(rkyv_map.get(&1).unwrap(), &"Dog".to_string());
assert_eq!(rkyv_map.get(&2).unwrap(), &"Cat".to_string());
assert_eq!(rkyv_map.get(&3).unwrap(), &"Dog".to_string());
assert_eq!(rkyv_map.get(&4), None);
}
}