Skip to content

Commit

Permalink
auto merge of #8320 : mihneadb/rust/freq_count, r=cmr
Browse files Browse the repository at this point in the history
  • Loading branch information
bors committed Aug 7, 2013
2 parents 98ec79c + 17c12bb commit a85f9ac
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/libextra/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use sort;
use std::cmp;
use std::hashmap;
use std::io;
use std::num;

Expand Down Expand Up @@ -352,6 +353,16 @@ pub fn write_boxplot(w: @io::Writer, s: &Summary, width_hint: uint) {
w.write_str(histr);
}

/// Returns a HashMap with the number of occurences of every element in the
/// sequence that the iterator exposes.
pub fn freq_count<T: Iterator<U>, U: Eq+Hash>(mut iter: T) -> hashmap::HashMap<U, uint> {
let mut map = hashmap::HashMap::new::<U, uint>();
for elem in iter {
map.insert_or_update_with(elem, 1, |_, count| *count += 1);
}
map
}

// Test vectors generated from R, using the script src/etc/stat-test-vectors.r.

#[cfg(test)]
Expand Down

0 comments on commit a85f9ac

Please sign in to comment.