1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![feature(unboxed_closures)]
13 use std::collections::{BTreeMap, HashMap, HashSet};
15 use std::rand::{Rng, IsaacRng, SeedableRng};
16 use std::time::Duration;
18 fn timed<F>(label: &str, f: F) where F: FnMut() {
19 println!(" {}: {}", label, Duration::span(f));
23 fn insert(&mut self, k: uint, v: uint);
24 fn remove(&mut self, k: &uint) -> bool;
25 fn find(&self, k: &uint) -> Option<&uint>;
28 impl MutableMap for BTreeMap<uint, uint> {
29 fn insert(&mut self, k: uint, v: uint) { self.insert(k, v); }
30 fn remove(&mut self, k: &uint) -> bool { self.remove(k).is_some() }
31 fn find(&self, k: &uint) -> Option<&uint> { self.get(k) }
33 impl MutableMap for HashMap<uint, uint> {
34 fn insert(&mut self, k: uint, v: uint) { self.insert(k, v); }
35 fn remove(&mut self, k: &uint) -> bool { self.remove(k).is_some() }
36 fn find(&self, k: &uint) -> Option<&uint> { self.get(k) }
39 fn ascending<M: MutableMap>(map: &mut M, n_keys: uint) {
40 println!(" Ascending integers:");
50 assert_eq!(map.find(&i).unwrap(), &(i + 1));
56 assert!(map.remove(&i));
61 fn descending<M: MutableMap>(map: &mut M, n_keys: uint) {
62 println!(" Descending integers:");
65 for i in (0..n_keys).rev() {
71 for i in (0..n_keys).rev() {
72 assert_eq!(map.find(&i).unwrap(), &(i + 1));
78 assert!(map.remove(&i));
83 fn vector<M: MutableMap>(map: &mut M, n_keys: uint, dist: &[uint]) {
86 map.insert(dist[i], i + 1);
92 assert_eq!(map.find(&dist[i]).unwrap(), &(i + 1));
98 assert!(map.remove(&dist[i]));
104 let args = os::args();
105 let args = args.as_slice();
108 args[1].parse::<uint>().unwrap()
114 let mut rand = Vec::with_capacity(n_keys);
117 let seed: &[_] = &[1, 1, 1, 1, 1, 1, 1];
118 let mut rng: IsaacRng = SeedableRng::from_seed(seed);
119 let mut set = HashSet::new();
120 while set.len() != n_keys {
121 let next = rng.gen();
122 if set.insert(next) {
128 println!("{} keys", n_keys);
131 println!("{}", "\nBTreeMap:");
134 let mut map: BTreeMap<uint,uint> = BTreeMap::new();
135 ascending(&mut map, n_keys);
139 let mut map: BTreeMap<uint,uint> = BTreeMap::new();
140 descending(&mut map, n_keys);
144 println!(" Random integers:");
145 let mut map: BTreeMap<uint,uint> = BTreeMap::new();
146 vector(&mut map, n_keys, rand.as_slice());
150 println!("{}", "\nHashMap:");
153 let mut map: HashMap<uint,uint> = HashMap::new();
154 ascending(&mut map, n_keys);
158 let mut map: HashMap<uint,uint> = HashMap::new();
159 descending(&mut map, n_keys);
163 println!(" Random integers:");
164 let mut map: HashMap<uint,uint> = HashMap::new();
165 vector(&mut map, n_keys, rand.as_slice());