Skip to content

Instantly share code, notes, and snippets.

@jamestwebber
Created May 14, 2021 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamestwebber/e90419acd991e27313486d227c6dc22c to your computer and use it in GitHub Desktop.
Save jamestwebber/e90419acd991e27313486d227c6dc22c to your computer and use it in GitHub Desktop.
Implementation of ParExtend for a Counter...maybe
use std::collections::LinkedList;
use std::hash::Hash;
use counter::Counter;
use rayon::prelude::*;
#[derive(Default)]
pub struct ParCounter<T: Hash + Eq, N = usize>(Counter<T, N>);
impl<T: Hash + Eq + Clone, N: Clone + Ord> ParCounter<T, N> {
pub fn len(&self) -> usize {
self.0.len()
}
pub fn most_common(&self) -> Vec<(T, N)> {
self.0.most_common()
}
}
impl<T: Hash + Eq + Send> ParallelExtend<T> for ParCounter<T> {
fn par_extend<I>(&mut self, par_iter: I)
where
I: IntoParallelIterator<Item = T>,
{
let list = par_iter
.into_par_iter()
.fold(Vec::new, vec_push)
.map(as_list)
.reduce(LinkedList::new, list_append);
self.0.reserve(list.iter().map(Vec::len).sum());
for vec in list {
self.0.update(vec);
}
}
}
fn vec_push<T>(mut vec: Vec<T>, elem: T) -> Vec<T> {
vec.push(elem);
vec
}
fn as_list<T>(item: T) -> LinkedList<T> {
let mut list = LinkedList::new();
list.push_back(item);
list
}
fn list_append<T>(mut list1: LinkedList<T>, mut list2: LinkedList<T>) -> LinkedList<T> {
list1.append(&mut list2);
list1
}
@jamestwebber
Copy link
Author

Note: I made this public so that someone could help me debug it. It's not a good idea. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment