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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use num_cpus;
use rayon_core::scope;
use std::collections::HashMap;
use std::fmt::Debug;
use std::hash::{BuildHasherDefault, Hash};
use std::sync::Mutex;
use twox_hash::XxHash64;
#[allow(clippy::drop_copy)]
pub fn frequency_finder<T>(list: &[T]) -> HashMap<T, u64, BuildHasherDefault<XxHash64>>
where
T: Copy + Hash + Eq + Sync + Send + Debug,
{
let logical_cores = num_cpus::get();
let range: usize = list.len() / logical_cores;
let split_index: usize = logical_cores * range;
let remainder: usize = list.len() % logical_cores;
let map_mtx: Mutex<HashMap<T, u64, BuildHasherDefault<XxHash64>>> =
Mutex::new(Default::default());
let map_ref = &map_mtx;
scope(|s| {
for idx in 1..=logical_cores {
s.spawn(move |_| {
let mut map_guard = map_ref.lock().unwrap();
for item in list.iter().take(idx * range).skip((idx - 1) * range) {
match map_guard.get_mut(item) {
Some(val) => *val += 1,
None => drop(map_guard.insert(*item, 1)),
}
}
});
}
s.spawn(move |_| {
let mut map_guard = map_ref.lock().unwrap();
for item in list.iter().skip(split_index).take(remainder) {
match map_guard.get_mut(item) {
Some(val) => *val += 1,
None => drop(map_guard.insert(*item, 1)),
}
}
})
});
Mutex::into_inner(map_mtx).unwrap()
}