mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-05 19:58:32 +00:00
auto merge of #8320 : mihneadb/rust/freq_count, r=cmr
This commit is contained in:
commit
a85f9acbfc
@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
use sort;
|
use sort;
|
||||||
use std::cmp;
|
use std::cmp;
|
||||||
|
use std::hashmap;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::num;
|
use std::num;
|
||||||
|
|
||||||
@ -352,6 +353,16 @@ pub fn write_boxplot(w: @io::Writer, s: &Summary, width_hint: uint) {
|
|||||||
w.write_str(histr);
|
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.
|
// Test vectors generated from R, using the script src/etc/stat-test-vectors.r.
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
Loading…
Reference in New Issue
Block a user