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.
|
|
|
|
|
2012-09-11 19:46:20 -05:00
|
|
|
extern mod std;
|
2012-08-21 17:55:17 -05:00
|
|
|
|
2013-03-21 14:21:06 -05:00
|
|
|
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));
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
|
|
|
|
2013-03-21 14:21:06 -05:00
|
|
|
fn ascending<M: Map<uint, uint>>(map: &mut M, n_keys: uint) {
|
|
|
|
io::println(" Ascending integers:");
|
2012-08-21 17:55:17 -05:00
|
|
|
|
2013-03-21 14:21:06 -05:00
|
|
|
do timed("insert") {
|
|
|
|
for uint::range(0, n_keys) |i| {
|
|
|
|
map.insert(i, i + 1);
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 14:21:06 -05:00
|
|
|
do timed("search") {
|
|
|
|
for uint::range(0, n_keys) |i| {
|
|
|
|
fail_unless!(map.find(&i).unwrap() == &(i + 1));
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 14:21:06 -05:00
|
|
|
do timed("remove") {
|
|
|
|
for uint::range(0, n_keys) |i| {
|
|
|
|
fail_unless!(map.remove(&i));
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 14:21:06 -05:00
|
|
|
fn descending<M: Map<uint, uint>>(map: &mut M, n_keys: uint) {
|
|
|
|
io::println(" Descending integers:");
|
2012-08-21 17:55:17 -05:00
|
|
|
|
2013-03-21 14:21:06 -05:00
|
|
|
do timed("insert") {
|
|
|
|
for uint::range(0, n_keys) |i| {
|
|
|
|
map.insert(i, i + 1);
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 14:21:06 -05:00
|
|
|
do timed("search") {
|
|
|
|
for uint::range(0, n_keys) |i| {
|
|
|
|
fail_unless!(map.find(&i).unwrap() == &(i + 1));
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 14:21:06 -05:00
|
|
|
do timed("remove") {
|
|
|
|
for uint::range(0, n_keys) |i| {
|
|
|
|
fail_unless!(map.remove(&i));
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 14:21:06 -05:00
|
|
|
fn vector<M: Map<uint, uint>>(map: &mut M, n_keys: uint, dist: &[uint]) {
|
2013-01-23 18:51:49 -06:00
|
|
|
|
2013-03-21 14:21:06 -05:00
|
|
|
do timed("insert") {
|
|
|
|
for uint::range(0, n_keys) |i| {
|
|
|
|
map.insert(dist[i], i + 1);
|
2013-01-23 18:51:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 14:21:06 -05:00
|
|
|
do timed("search") {
|
|
|
|
for uint::range(0, n_keys) |i| {
|
|
|
|
fail_unless!(map.find(&dist[i]).unwrap() == &(i + 1));
|
2013-01-23 18:51:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 14:21:06 -05:00
|
|
|
do timed("remove") {
|
|
|
|
for uint::range(0, n_keys) |i| {
|
|
|
|
fail_unless!(map.remove(&dist[i]));
|
2013-01-23 18:51:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 14:21:06 -05:00
|
|
|
fn main() {
|
|
|
|
let args = os::args();
|
|
|
|
let n_keys = {
|
|
|
|
if args.len() == 2 {
|
|
|
|
uint::from_str(args[1]).get()
|
|
|
|
} 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
|
|
|
|
2013-03-21 14:21:06 -05:00
|
|
|
let mut rand = vec::with_capacity(n_keys);
|
2013-01-23 18:51:49 -06:00
|
|
|
|
|
|
|
{
|
2013-03-21 14:21:06 -05:00
|
|
|
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);
|
2013-01-23 18:51:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 14:21:06 -05:00
|
|
|
io::println(fmt!("%? keys", n_keys));
|
2013-01-23 18:51:49 -06:00
|
|
|
|
2013-03-21 14:21:06 -05:00
|
|
|
io::println("\nTreeMap:");
|
2013-01-23 18:51:49 -06:00
|
|
|
|
|
|
|
{
|
2013-03-21 14:21:06 -05:00
|
|
|
let mut map = TreeMap::new::<uint, uint>();
|
|
|
|
ascending(&mut map, n_keys);
|
2013-01-23 18:51:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-03-21 14:21:06 -05:00
|
|
|
let mut map = TreeMap::new::<uint, uint>();
|
|
|
|
descending(&mut map, n_keys);
|
2013-01-23 18:51:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-03-21 14:21:06 -05:00
|
|
|
io::println(" Random integers:");
|
|
|
|
let mut map = TreeMap::new::<uint, uint>();
|
|
|
|
vector(&mut map, n_keys, rand);
|
2013-01-23 18:51:49 -06:00
|
|
|
}
|
|
|
|
|
2013-03-21 14:21:06 -05:00
|
|
|
io::println("\nLinearMap:");
|
|
|
|
|
2013-01-23 18:51:49 -06:00
|
|
|
{
|
2013-03-21 14:21:06 -05:00
|
|
|
let mut map = LinearMap::new::<uint, uint>();
|
|
|
|
ascending(&mut map, n_keys);
|
2013-01-23 18:51:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-03-21 14:21:06 -05:00
|
|
|
let mut map = LinearMap::new::<uint, uint>();
|
|
|
|
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
|
|
|
{
|
|
|
|
io::println(" Random integers:");
|
|
|
|
let mut map = LinearMap::new::<uint, uint>();
|
|
|
|
vector(&mut map, n_keys, rand);
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
|
|
|
|
2013-03-21 14:21:06 -05:00
|
|
|
io::println("\nTrieMap:");
|
2012-08-21 17:55:17 -05:00
|
|
|
|
|
|
|
{
|
2013-03-21 14:21:06 -05:00
|
|
|
let mut map = TrieMap::new::<uint>();
|
|
|
|
ascending(&mut map, n_keys);
|
2013-01-23 18:51:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-03-21 14:21:06 -05:00
|
|
|
let mut map = TrieMap::new::<uint>();
|
|
|
|
descending(&mut map, n_keys);
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-03-21 14:21:06 -05:00
|
|
|
io::println(" Random integers:");
|
|
|
|
let mut map = TrieMap::new::<uint>();
|
|
|
|
vector(&mut map, n_keys, rand);
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
2012-12-28 21:57:18 -06:00
|
|
|
}
|