Skip to content

Instantly share code, notes, and snippets.

@emk
Created September 27, 2014 12:08
Show Gist options
  • Save emk/b070ed7bee7a9c221047 to your computer and use it in GitHub Desktop.
Save emk/b070ed7bee7a9c221047 to your computer and use it in GitHub Desktop.
Need something where both Clone and Copy work
pub struct Emitter<R,K: Hash + Eq + Clone,V: Copy,MR: MapReduce<R,K,V>> {
results: HashMap<K,V>
}
impl<R,K: Hash + Eq + Clone,V: Copy,MR: MapReduce<R,K,V>> Emitter<R,K,V,MR> {
fn new() -> Emitter<R,K,V,MR> {
Emitter{results: HashMap::with_capacity(25000)}
}
#[inline]
fn emit(&mut self, mr: &mut MR, key: K, value: V) {
match self.results.entry(key) {
Vacant(entry) => { entry.set(value); },
Occupied(mut entry) => {
let old = *entry.get();
entry.set(mr.reduce(old, value));
}
}
}
fn merge(&mut self, mr: &mut MR, other: &Emitter<R,K,V,MR>) {
for (key,value) in other.results.iter() {
self.emit(mr, key.clone(), *value);
}
}
}
pub trait MapReduce<R,K,V> {
fn map(&mut self, record: &R, emitter: &mut Emitter<R,K,V,Self>);
fn reduce(&mut self, val1: V, val2: V) -> V;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment