Skip to content

Commit a85f9ac

Browse files
committed
auto merge of #8320 : mihneadb/rust/freq_count, r=cmr
2 parents 98ec79c + 17c12bb commit a85f9ac

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/libextra/stats.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use sort;
1212
use std::cmp;
13+
use std::hashmap;
1314
use std::io;
1415
use std::num;
1516

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

356+
/// Returns a HashMap with the number of occurences of every element in the
357+
/// sequence that the iterator exposes.
358+
pub fn freq_count<T: Iterator<U>, U: Eq+Hash>(mut iter: T) -> hashmap::HashMap<U, uint> {
359+
let mut map = hashmap::HashMap::new::<U, uint>();
360+
for elem in iter {
361+
map.insert_or_update_with(elem, 1, |_, count| *count += 1);
362+
}
363+
map
364+
}
365+
355366
// Test vectors generated from R, using the script src/etc/stat-test-vectors.r.
356367

357368
#[cfg(test)]

0 commit comments

Comments
 (0)