// Copyright 2013 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. extern mod std; use core::io; use std::time; use std::treemap::TreeMap; use core::hashmap::linear::{LinearMap, LinearSet}; use core::trie::TrieMap; fn timed(label: &str, f: &fn()) { let start = time::precise_time_s(); f(); let end = time::precise_time_s(); io::println(fmt!(" %s: %f", label, end - start)); } fn ascending>(map: &mut M, n_keys: uint) { io::println(" Ascending integers:"); do timed("insert") { for uint::range(0, n_keys) |i| { map.insert(i, i + 1); } } do timed("search") { for uint::range(0, n_keys) |i| { fail_unless!(map.find(&i).unwrap() == &(i + 1)); } } do timed("remove") { for uint::range(0, n_keys) |i| { fail_unless!(map.remove(&i)); } } } fn descending>(map: &mut M, n_keys: uint) { io::println(" Descending integers:"); do timed("insert") { for uint::range(0, n_keys) |i| { map.insert(i, i + 1); } } do timed("search") { for uint::range(0, n_keys) |i| { fail_unless!(map.find(&i).unwrap() == &(i + 1)); } } do timed("remove") { for uint::range(0, n_keys) |i| { fail_unless!(map.remove(&i)); } } } fn vector>(map: &mut M, n_keys: uint, dist: &[uint]) { do timed("insert") { for uint::range(0, n_keys) |i| { map.insert(dist[i], i + 1); } } do timed("search") { for uint::range(0, n_keys) |i| { fail_unless!(map.find(&dist[i]).unwrap() == &(i + 1)); } } do timed("remove") { for uint::range(0, n_keys) |i| { fail_unless!(map.remove(&dist[i])); } } } fn main() { let args = os::args(); let n_keys = { if args.len() == 2 { uint::from_str(args[1]).get() } else { 1000000 } }; let mut rand = vec::with_capacity(n_keys); { let rng = core::rand::seeded_rng([1, 1, 1, 1, 1, 1, 1]); let mut set = LinearSet::new(); while set.len() != n_keys { let next = rng.next() as uint; if set.insert(next) { rand.push(next); } } } io::println(fmt!("%? keys", n_keys)); io::println("\nTreeMap:"); { let mut map = TreeMap::new::(); ascending(&mut map, n_keys); } { let mut map = TreeMap::new::(); descending(&mut map, n_keys); } { io::println(" Random integers:"); let mut map = TreeMap::new::(); vector(&mut map, n_keys, rand); } io::println("\nLinearMap:"); { let mut map = LinearMap::new::(); ascending(&mut map, n_keys); } { let mut map = LinearMap::new::(); descending(&mut map, n_keys); } { io::println(" Random integers:"); let mut map = LinearMap::new::(); vector(&mut map, n_keys, rand); } io::println("\nTrieMap:"); { let mut map = TrieMap::new::(); ascending(&mut map, n_keys); } { let mut map = TrieMap::new::(); descending(&mut map, n_keys); } { io::println(" Random integers:"); let mut map = TrieMap::new::(); vector(&mut map, n_keys, rand); } }