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::str::from_str;
17 use std::time::Duration;
20 fn timed<F>(label: &str, f: F) where F: FnMut() {
21 println!(" {}: {}", label, Duration::span(f));
25 fn insert(&mut self, k: uint, v: uint);
26 fn remove(&mut self, k: &uint) -> bool;
27 fn find(&self, k: &uint) -> Option<&uint>;
30 impl MutableMap for BTreeMap<uint, uint> {
31 fn insert(&mut self, k: uint, v: uint) { self.insert(k, v); }
32 fn remove(&mut self, k: &uint) -> bool { self.remove(k).is_some() }
33 fn find(&self, k: &uint) -> Option<&uint> { self.get(k) }
35 impl MutableMap for HashMap<uint, uint> {
36 fn insert(&mut self, k: uint, v: uint) { self.insert(k, v); }
37 fn remove(&mut self, k: &uint) -> bool { self.remove(k).is_some() }
38 fn find(&self, k: &uint) -> Option<&uint> { self.get(k) }
41 fn ascending<M: MutableMap>(map: &mut M, n_keys: uint) {
42 println!(" Ascending integers:");
45 for i in range(0u, n_keys) {
51 for i in range(0u, n_keys) {
52 assert_eq!(map.find(&i).unwrap(), &(i + 1));
57 for i in range(0, n_keys) {
58 assert!(map.remove(&i));
63 fn descending<M: MutableMap>(map: &mut M, n_keys: uint) {
64 println!(" Descending integers:");
67 for i in range(0, n_keys).rev() {
73 for i in range(0, n_keys).rev() {
74 assert_eq!(map.find(&i).unwrap(), &(i + 1));
79 for i in range(0, n_keys) {
80 assert!(map.remove(&i));
85 fn vector<M: MutableMap>(map: &mut M, n_keys: uint, dist: &[uint]) {
87 for i in range(0u, n_keys) {
88 map.insert(dist[i], i + 1);
93 for i in range(0u, n_keys) {
94 assert_eq!(map.find(&dist[i]).unwrap(), &(i + 1));
99 for i in range(0u, n_keys) {
100 assert!(map.remove(&dist[i]));
106 let args = os::args();
107 let args = args.as_slice();
110 from_str::<uint>(args[1].as_slice()).unwrap()
116 let mut rand = Vec::with_capacity(n_keys);
119 let seed: &[_] = &[1, 1, 1, 1, 1, 1, 1];
120 let mut rng: IsaacRng = SeedableRng::from_seed(seed);
121 let mut set = HashSet::new();
122 while set.len() != n_keys {
123 let next = rng.gen();
124 if set.insert(next) {
130 println!("{} keys", n_keys);
133 println!("{}", "\nBTreeMap:");
136 let mut map: BTreeMap<uint,uint> = BTreeMap::new();
137 ascending(&mut map, n_keys);
141 let mut map: BTreeMap<uint,uint> = BTreeMap::new();
142 descending(&mut map, n_keys);
146 println!(" Random integers:");
147 let mut map: BTreeMap<uint,uint> = BTreeMap::new();
148 vector(&mut map, n_keys, rand.as_slice());
152 println!("{}", "\nHashMap:");
155 let mut map: HashMap<uint,uint> = HashMap::new();
156 ascending(&mut map, n_keys);
160 let mut map: HashMap<uint,uint> = HashMap::new();
161 descending(&mut map, n_keys);
165 println!(" Random integers:");
166 let mut map: HashMap<uint,uint> = HashMap::new();
167 vector(&mut map, n_keys, rand.as_slice());