2013-03-21 14:21:06 -05:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-10 19:32:48 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-04-28 13:40:04 -05:00
|
|
|
#![feature(std_misc, rand, duration, duration_span)]
|
2014-12-08 07:35:34 -06:00
|
|
|
|
2014-12-16 22:09:16 -06:00
|
|
|
use std::collections::{BTreeMap, HashMap, HashSet};
|
2015-02-16 08:04:02 -06:00
|
|
|
use std::env;
|
2015-04-10 15:39:51 -05:00
|
|
|
use std::__rand::{Rng, thread_rng};
|
2014-11-10 14:27:56 -06:00
|
|
|
use std::time::Duration;
|
2013-03-21 14:21:06 -05:00
|
|
|
|
2014-12-08 07:35:34 -06:00
|
|
|
fn timed<F>(label: &str, f: F) where F: FnMut() {
|
2015-07-06 01:20:00 -05:00
|
|
|
println!(" {}: {:?}", label, Duration::span(f));
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
|
|
|
|
2014-10-30 15:43:24 -05:00
|
|
|
trait MutableMap {
|
2015-02-16 08:04:02 -06:00
|
|
|
fn insert(&mut self, k: usize, v: usize);
|
|
|
|
fn remove(&mut self, k: &usize) -> bool;
|
|
|
|
fn find(&self, k: &usize) -> Option<&usize>;
|
2014-10-30 15:43:24 -05:00
|
|
|
}
|
|
|
|
|
2015-02-16 08:04:02 -06:00
|
|
|
impl MutableMap for BTreeMap<usize, usize> {
|
|
|
|
fn insert(&mut self, k: usize, v: usize) { self.insert(k, v); }
|
|
|
|
fn remove(&mut self, k: &usize) -> bool { self.remove(k).is_some() }
|
|
|
|
fn find(&self, k: &usize) -> Option<&usize> { self.get(k) }
|
2014-10-30 15:43:24 -05:00
|
|
|
}
|
2015-02-16 08:04:02 -06:00
|
|
|
impl MutableMap for HashMap<usize, usize> {
|
|
|
|
fn insert(&mut self, k: usize, v: usize) { self.insert(k, v); }
|
|
|
|
fn remove(&mut self, k: &usize) -> bool { self.remove(k).is_some() }
|
|
|
|
fn find(&self, k: &usize) -> Option<&usize> { self.get(k) }
|
2014-10-30 15:43:24 -05:00
|
|
|
}
|
|
|
|
|
2015-02-16 08:04:02 -06:00
|
|
|
fn ascending<M: MutableMap>(map: &mut M, n_keys: usize) {
|
2014-01-09 04:06:55 -06:00
|
|
|
println!(" Ascending integers:");
|
2012-08-21 17:55:17 -05:00
|
|
|
|
2013-11-21 21:20:48 -06:00
|
|
|
timed("insert", || {
|
2015-02-16 08:04:02 -06:00
|
|
|
for i in 0..n_keys {
|
2013-03-21 14:21:06 -05:00
|
|
|
map.insert(i, i + 1);
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
2013-11-21 21:20:48 -06:00
|
|
|
});
|
2012-08-21 17:55:17 -05:00
|
|
|
|
2013-11-21 21:20:48 -06:00
|
|
|
timed("search", || {
|
2015-02-16 08:04:02 -06:00
|
|
|
for i in 0..n_keys {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(map.find(&i).unwrap(), &(i + 1));
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
2013-11-21 21:20:48 -06:00
|
|
|
});
|
2012-08-21 17:55:17 -05:00
|
|
|
|
2013-11-21 21:20:48 -06:00
|
|
|
timed("remove", || {
|
2015-01-26 14:46:12 -06:00
|
|
|
for i in 0..n_keys {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(map.remove(&i));
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
2013-11-21 21:20:48 -06:00
|
|
|
});
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
|
|
|
|
2015-02-16 08:04:02 -06:00
|
|
|
fn descending<M: MutableMap>(map: &mut M, n_keys: usize) {
|
2014-01-09 04:06:55 -06:00
|
|
|
println!(" Descending integers:");
|
2012-08-21 17:55:17 -05:00
|
|
|
|
2013-11-21 21:20:48 -06:00
|
|
|
timed("insert", || {
|
2015-01-26 14:44:22 -06:00
|
|
|
for i in (0..n_keys).rev() {
|
2013-03-21 14:21:06 -05:00
|
|
|
map.insert(i, i + 1);
|
2013-08-07 00:34:22 -05:00
|
|
|
}
|
2013-11-21 21:20:48 -06:00
|
|
|
});
|
2012-08-21 17:55:17 -05:00
|
|
|
|
2013-11-21 21:20:48 -06:00
|
|
|
timed("search", || {
|
2015-01-26 14:44:22 -06:00
|
|
|
for i in (0..n_keys).rev() {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(map.find(&i).unwrap(), &(i + 1));
|
2013-08-07 00:34:22 -05:00
|
|
|
}
|
2013-11-21 21:20:48 -06:00
|
|
|
});
|
2012-08-21 17:55:17 -05:00
|
|
|
|
2013-11-21 21:20:48 -06:00
|
|
|
timed("remove", || {
|
2015-01-26 14:46:12 -06:00
|
|
|
for i in 0..n_keys {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(map.remove(&i));
|
2013-08-07 00:34:22 -05:00
|
|
|
}
|
2013-11-21 21:20:48 -06:00
|
|
|
});
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
|
|
|
|
2015-02-16 08:04:02 -06:00
|
|
|
fn vector<M: MutableMap>(map: &mut M, n_keys: usize, dist: &[usize]) {
|
2013-11-21 21:20:48 -06:00
|
|
|
timed("insert", || {
|
2015-02-16 08:04:02 -06:00
|
|
|
for i in 0..n_keys {
|
2013-03-21 14:21:06 -05:00
|
|
|
map.insert(dist[i], i + 1);
|
2013-01-23 18:51:49 -06:00
|
|
|
}
|
2013-11-21 21:20:48 -06:00
|
|
|
});
|
2013-01-23 18:51:49 -06:00
|
|
|
|
2013-11-21 21:20:48 -06:00
|
|
|
timed("search", || {
|
2015-02-16 08:04:02 -06:00
|
|
|
for i in 0..n_keys {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(map.find(&dist[i]).unwrap(), &(i + 1));
|
2013-01-23 18:51:49 -06:00
|
|
|
}
|
2013-11-21 21:20:48 -06:00
|
|
|
});
|
2013-01-23 18:51:49 -06:00
|
|
|
|
2013-11-21 21:20:48 -06:00
|
|
|
timed("remove", || {
|
2015-02-16 08:04:02 -06:00
|
|
|
for i in 0..n_keys {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(map.remove(&dist[i]));
|
2013-01-23 18:51:49 -06:00
|
|
|
}
|
2013-11-21 21:20:48 -06:00
|
|
|
});
|
2013-01-23 18:51:49 -06:00
|
|
|
}
|
|
|
|
|
2013-03-21 14:21:06 -05:00
|
|
|
fn main() {
|
2015-02-16 08:04:02 -06:00
|
|
|
let mut args = env::args();
|
2013-03-21 14:21:06 -05:00
|
|
|
let n_keys = {
|
|
|
|
if args.len() == 2 {
|
2015-02-16 08:04:02 -06:00
|
|
|
args.nth(1).unwrap().parse::<usize>().unwrap()
|
2013-03-21 14:21:06 -05:00
|
|
|
} else {
|
|
|
|
1000000
|
2013-01-23 18:51:49 -06:00
|
|
|
}
|
2013-03-21 14:21:06 -05:00
|
|
|
};
|
2013-01-23 18:51:49 -06:00
|
|
|
|
2014-04-17 17:59:07 -05:00
|
|
|
let mut rand = Vec::with_capacity(n_keys);
|
2013-01-23 18:51:49 -06:00
|
|
|
|
|
|
|
{
|
2014-08-04 07:19:02 -05:00
|
|
|
let seed: &[_] = &[1, 1, 1, 1, 1, 1, 1];
|
2015-04-10 15:39:51 -05:00
|
|
|
let mut rng = thread_rng();
|
2013-04-03 08:28:36 -05:00
|
|
|
let mut set = HashSet::new();
|
2013-03-21 14:21:06 -05:00
|
|
|
while set.len() != n_keys {
|
2013-09-21 07:06:50 -05:00
|
|
|
let next = rng.gen();
|
2013-03-21 14:21:06 -05:00
|
|
|
if set.insert(next) {
|
|
|
|
rand.push(next);
|
2013-01-23 18:51:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-25 00:16:43 -05:00
|
|
|
println!("{} keys", n_keys);
|
2013-01-23 18:51:49 -06:00
|
|
|
|
2014-01-09 04:06:55 -06:00
|
|
|
// FIXME: #9970
|
2014-12-16 22:09:16 -06:00
|
|
|
println!("{}", "\nBTreeMap:");
|
2013-01-23 18:51:49 -06:00
|
|
|
|
|
|
|
{
|
2015-02-16 08:04:02 -06:00
|
|
|
let mut map: BTreeMap<usize,usize> = BTreeMap::new();
|
2013-03-21 14:21:06 -05:00
|
|
|
ascending(&mut map, n_keys);
|
2013-01-23 18:51:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2015-02-16 08:04:02 -06:00
|
|
|
let mut map: BTreeMap<usize,usize> = BTreeMap::new();
|
2013-03-21 14:21:06 -05:00
|
|
|
descending(&mut map, n_keys);
|
2013-01-23 18:51:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2014-01-09 04:06:55 -06:00
|
|
|
println!(" Random integers:");
|
2015-02-16 08:04:02 -06:00
|
|
|
let mut map: BTreeMap<usize,usize> = BTreeMap::new();
|
2015-02-01 20:53:25 -06:00
|
|
|
vector(&mut map, n_keys, &rand);
|
2013-01-23 18:51:49 -06:00
|
|
|
}
|
|
|
|
|
2014-01-09 04:06:55 -06:00
|
|
|
// FIXME: #9970
|
|
|
|
println!("{}", "\nHashMap:");
|
2013-03-21 14:21:06 -05:00
|
|
|
|
2013-01-23 18:51:49 -06:00
|
|
|
{
|
2015-02-16 08:04:02 -06:00
|
|
|
let mut map: HashMap<usize,usize> = HashMap::new();
|
2013-03-21 14:21:06 -05:00
|
|
|
ascending(&mut map, n_keys);
|
2013-01-23 18:51:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2015-02-16 08:04:02 -06:00
|
|
|
let mut map: HashMap<usize,usize> = HashMap::new();
|
2013-03-21 14:21:06 -05:00
|
|
|
descending(&mut map, n_keys);
|
2013-01-23 18:51:49 -06:00
|
|
|
}
|
2012-08-21 17:55:17 -05:00
|
|
|
|
2013-03-21 14:21:06 -05:00
|
|
|
{
|
2014-01-09 04:06:55 -06:00
|
|
|
println!(" Random integers:");
|
2015-02-16 08:04:02 -06:00
|
|
|
let mut map: HashMap<usize,usize> = HashMap::new();
|
2015-02-01 20:53:25 -06:00
|
|
|
vector(&mut map, n_keys, &rand);
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
2012-12-28 21:57:18 -06:00
|
|
|
}
|