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;
19 fn timed<F>(label: &str, f: F) where F: FnMut() {
20 println!(" {}: {}", label, Duration::span(f));
24 fn insert(&mut self, k: uint, v: uint);
25 fn remove(&mut self, k: &uint) -> bool;
26 fn find(&self, k: &uint) -> Option<&uint>;
29 impl MutableMap for BTreeMap<uint, uint> {
30 fn insert(&mut self, k: uint, v: uint) { self.insert(k, v); }
31 fn remove(&mut self, k: &uint) -> bool { self.remove(k).is_some() }
32 fn find(&self, k: &uint) -> Option<&uint> { self.get(k) }
34 impl MutableMap for HashMap<uint, uint> {
35 fn insert(&mut self, k: uint, v: uint) { self.insert(k, v); }
36 fn remove(&mut self, k: &uint) -> bool { self.remove(k).is_some() }
37 fn find(&self, k: &uint) -> Option<&uint> { self.get(k) }
40 fn ascending<M: MutableMap>(map: &mut M, n_keys: uint) {
41 println!(" Ascending integers:");
44 for i in range(0u, n_keys) {
50 for i in range(0u, n_keys) {
51 assert_eq!(map.find(&i).unwrap(), &(i + 1));
56 for i in range(0, n_keys) {
57 assert!(map.remove(&i));
62 fn descending<M: MutableMap>(map: &mut M, n_keys: uint) {
63 println!(" Descending integers:");
66 for i in range(0, n_keys).rev() {
72 for i in range(0, n_keys).rev() {
73 assert_eq!(map.find(&i).unwrap(), &(i + 1));
78 for i in range(0, n_keys) {
79 assert!(map.remove(&i));
84 fn vector<M: MutableMap>(map: &mut M, n_keys: uint, dist: &[uint]) {
86 for i in range(0u, n_keys) {
87 map.insert(dist[i], i + 1);
92 for i in range(0u, n_keys) {
93 assert_eq!(map.find(&dist[i]).unwrap(), &(i + 1));
98 for i in range(0u, n_keys) {
99 assert!(map.remove(&dist[i]));
105 let args = os::args();
106 let args = args.as_slice();
109 args[1].parse::<uint>().unwrap()
115 let mut rand = Vec::with_capacity(n_keys);
118 let seed: &[_] = &[1, 1, 1, 1, 1, 1, 1];
119 let mut rng: IsaacRng = SeedableRng::from_seed(seed);
120 let mut set = HashSet::new();
121 while set.len() != n_keys {
122 let next = rng.gen();
123 if set.insert(next) {
129 println!("{} keys", n_keys);
132 println!("{}", "\nBTreeMap:");
135 let mut map: BTreeMap<uint,uint> = BTreeMap::new();
136 ascending(&mut map, n_keys);
140 let mut map: BTreeMap<uint,uint> = BTreeMap::new();
141 descending(&mut map, n_keys);
145 println!(" Random integers:");
146 let mut map: BTreeMap<uint,uint> = BTreeMap::new();
147 vector(&mut map, n_keys, rand.as_slice());
151 println!("{}", "\nHashMap:");
154 let mut map: HashMap<uint,uint> = HashMap::new();
155 ascending(&mut map, n_keys);
159 let mut map: HashMap<uint,uint> = HashMap::new();
160 descending(&mut map, n_keys);
164 println!(" Random integers:");
165 let mut map: HashMap<uint,uint> = HashMap::new();
166 vector(&mut map, n_keys, rand.as_slice());