diff options
| author | bors <bors@rust-lang.org> | 2013-08-07 15:02:19 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-08-07 15:02:19 -0700 |
| commit | a85f9acbfce3d47cf05ce4dd33a06907df261d49 (patch) | |
| tree | 4e871f5dc3b9dc160f06f668abb58fca6847e1fb | |
| parent | 98ec79c9576052d9fededd3b72b47d387c1c455d (diff) | |
| parent | 17c12bbd1b2fdfc958f12527538acb787cc2d89a (diff) | |
| download | rust-a85f9acbfce3d47cf05ce4dd33a06907df261d49.tar.gz rust-a85f9acbfce3d47cf05ce4dd33a06907df261d49.zip | |
auto merge of #8320 : mihneadb/rust/freq_count, r=cmr
| -rw-r--r-- | src/libextra/stats.rs | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/src/libextra/stats.rs b/src/libextra/stats.rs index 68d5af43688..9238034cba3 100644 --- a/src/libextra/stats.rs +++ b/src/libextra/stats.rs @@ -10,6 +10,7 @@ use sort; use std::cmp; +use std::hashmap; use std::io; use std::num; @@ -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)] |
