2013-03-21 15:21:06 -04:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-10 17:32:48 -08: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.
|
|
|
|
|
2013-05-20 17:07:24 -07:00
|
|
|
extern mod extra;
|
2012-08-21 15:55:17 -07:00
|
|
|
|
2013-05-20 17:07:24 -07:00
|
|
|
use extra::time;
|
|
|
|
use extra::treemap::TreeMap;
|
|
|
|
use std::hashmap::{HashMap, HashSet};
|
2013-05-24 19:35:29 -07:00
|
|
|
use std::io;
|
|
|
|
use std::os;
|
2013-05-20 17:07:24 -07:00
|
|
|
use std::rand::Rng;
|
2013-05-24 19:35:29 -07:00
|
|
|
use std::trie::TrieMap;
|
|
|
|
use std::uint;
|
|
|
|
use std::vec;
|
2013-03-21 15:21:06 -04:00
|
|
|
|
|
|
|
fn timed(label: &str, f: &fn()) {
|
|
|
|
let start = time::precise_time_s();
|
|
|
|
f();
|
|
|
|
let end = time::precise_time_s();
|
2013-09-24 22:16:43 -07:00
|
|
|
println!(" {}: {}", label, end - start);
|
2012-08-21 15:55:17 -07:00
|
|
|
}
|
|
|
|
|
2013-07-13 20:30:05 -07:00
|
|
|
fn ascending<M: MutableMap<uint, uint>>(map: &mut M, n_keys: uint) {
|
2013-03-21 15:21:06 -04:00
|
|
|
io::println(" Ascending integers:");
|
2012-08-21 15:55:17 -07:00
|
|
|
|
2013-03-21 15:21:06 -04:00
|
|
|
do timed("insert") {
|
2013-08-03 12:45:23 -04:00
|
|
|
for i in range(0u, n_keys) {
|
2013-03-21 15:21:06 -04:00
|
|
|
map.insert(i, i + 1);
|
2012-08-21 15:55:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 15:21:06 -04:00
|
|
|
do timed("search") {
|
2013-08-03 12:45:23 -04:00
|
|
|
for i in range(0u, n_keys) {
|
2013-05-18 22:02:45 -04:00
|
|
|
assert_eq!(map.find(&i).unwrap(), &(i + 1));
|
2012-08-21 15:55:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 15:21:06 -04:00
|
|
|
do timed("remove") {
|
2013-08-03 12:45:23 -04:00
|
|
|
for i in range(0, n_keys) {
|
2013-03-28 18:39:09 -07:00
|
|
|
assert!(map.remove(&i));
|
2012-08-21 15:55:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-13 20:30:05 -07:00
|
|
|
fn descending<M: MutableMap<uint, uint>>(map: &mut M, n_keys: uint) {
|
2013-03-21 15:21:06 -04:00
|
|
|
io::println(" Descending integers:");
|
2012-08-21 15:55:17 -07:00
|
|
|
|
2013-03-21 15:21:06 -04:00
|
|
|
do timed("insert") {
|
2013-08-06 22:34:22 -07:00
|
|
|
for i in range(0, n_keys).invert() {
|
2013-03-21 15:21:06 -04:00
|
|
|
map.insert(i, i + 1);
|
2013-08-06 22:34:22 -07:00
|
|
|
}
|
2012-08-21 15:55:17 -07:00
|
|
|
}
|
|
|
|
|
2013-03-21 15:21:06 -04:00
|
|
|
do timed("search") {
|
2013-08-06 22:34:22 -07:00
|
|
|
for i in range(0, n_keys).invert() {
|
2013-05-18 22:02:45 -04:00
|
|
|
assert_eq!(map.find(&i).unwrap(), &(i + 1));
|
2013-08-06 22:34:22 -07:00
|
|
|
}
|
2012-08-21 15:55:17 -07:00
|
|
|
}
|
|
|
|
|
2013-03-21 15:21:06 -04:00
|
|
|
do timed("remove") {
|
2013-08-06 22:34:22 -07:00
|
|
|
for i in range(0, n_keys) {
|
2013-03-28 18:39:09 -07:00
|
|
|
assert!(map.remove(&i));
|
2013-08-06 22:34:22 -07:00
|
|
|
}
|
2012-08-21 15:55:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-13 20:30:05 -07:00
|
|
|
fn vector<M: MutableMap<uint, uint>>(map: &mut M, n_keys: uint, dist: &[uint]) {
|
2013-01-23 19:51:49 -05:00
|
|
|
|
2013-03-21 15:21:06 -04:00
|
|
|
do timed("insert") {
|
2013-08-03 12:45:23 -04:00
|
|
|
for i in range(0u, n_keys) {
|
2013-03-21 15:21:06 -04:00
|
|
|
map.insert(dist[i], i + 1);
|
2013-01-23 19:51:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 15:21:06 -04:00
|
|
|
do timed("search") {
|
2013-08-03 12:45:23 -04:00
|
|
|
for i in range(0u, n_keys) {
|
2013-05-18 22:02:45 -04:00
|
|
|
assert_eq!(map.find(&dist[i]).unwrap(), &(i + 1));
|
2013-01-23 19:51:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 15:21:06 -04:00
|
|
|
do timed("remove") {
|
2013-08-03 12:45:23 -04:00
|
|
|
for i in range(0u, n_keys) {
|
2013-03-28 18:39:09 -07:00
|
|
|
assert!(map.remove(&dist[i]));
|
2013-01-23 19:51:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-01 15:33:21 -07:00
|
|
|
#[fixed_stack_segment]
|
2013-03-21 15:21:06 -04:00
|
|
|
fn main() {
|
|
|
|
let args = os::args();
|
|
|
|
let n_keys = {
|
|
|
|
if args.len() == 2 {
|
2013-09-15 07:27:32 +02:00
|
|
|
from_str::<uint>(args[1]).unwrap()
|
2013-03-21 15:21:06 -04:00
|
|
|
} else {
|
|
|
|
1000000
|
2013-01-23 19:51:49 -05:00
|
|
|
}
|
2013-03-21 15:21:06 -04:00
|
|
|
};
|
2013-01-23 19:51:49 -05:00
|
|
|
|
2013-03-21 15:21:06 -04:00
|
|
|
let mut rand = vec::with_capacity(n_keys);
|
2013-01-23 19:51:49 -05:00
|
|
|
|
|
|
|
{
|
2013-05-20 17:07:24 -07:00
|
|
|
let mut rng = std::rand::IsaacRng::new_seeded([1, 1, 1, 1, 1, 1, 1]);
|
2013-04-03 09:28:36 -04:00
|
|
|
let mut set = HashSet::new();
|
2013-03-21 15:21:06 -04:00
|
|
|
while set.len() != n_keys {
|
2013-09-21 22:06:50 +10:00
|
|
|
let next = rng.gen();
|
2013-03-21 15:21:06 -04:00
|
|
|
if set.insert(next) {
|
|
|
|
rand.push(next);
|
2013-01-23 19:51:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-24 22:16:43 -07:00
|
|
|
println!("{} keys", n_keys);
|
2013-01-23 19:51:49 -05:00
|
|
|
|
2013-03-21 15:21:06 -04:00
|
|
|
io::println("\nTreeMap:");
|
2013-01-23 19:51:49 -05:00
|
|
|
|
|
|
|
{
|
2013-08-27 18:45:13 -07:00
|
|
|
let mut map: TreeMap<uint,uint> = TreeMap::new();
|
2013-03-21 15:21:06 -04:00
|
|
|
ascending(&mut map, n_keys);
|
2013-01-23 19:51:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-08-27 18:45:13 -07:00
|
|
|
let mut map: TreeMap<uint,uint> = TreeMap::new();
|
2013-03-21 15:21:06 -04:00
|
|
|
descending(&mut map, n_keys);
|
2013-01-23 19:51:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-03-21 15:21:06 -04:00
|
|
|
io::println(" Random integers:");
|
2013-08-27 18:45:13 -07:00
|
|
|
let mut map: TreeMap<uint,uint> = TreeMap::new();
|
2013-03-21 15:21:06 -04:00
|
|
|
vector(&mut map, n_keys, rand);
|
2013-01-23 19:51:49 -05:00
|
|
|
}
|
|
|
|
|
2013-04-03 09:28:36 -04:00
|
|
|
io::println("\nHashMap:");
|
2013-03-21 15:21:06 -04:00
|
|
|
|
2013-01-23 19:51:49 -05:00
|
|
|
{
|
2013-08-27 18:45:13 -07:00
|
|
|
let mut map: HashMap<uint,uint> = HashMap::new();
|
2013-03-21 15:21:06 -04:00
|
|
|
ascending(&mut map, n_keys);
|
2013-01-23 19:51:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-08-27 18:45:13 -07:00
|
|
|
let mut map: HashMap<uint,uint> = HashMap::new();
|
2013-03-21 15:21:06 -04:00
|
|
|
descending(&mut map, n_keys);
|
2013-01-23 19:51:49 -05:00
|
|
|
}
|
2012-08-21 15:55:17 -07:00
|
|
|
|
2013-03-21 15:21:06 -04:00
|
|
|
{
|
|
|
|
io::println(" Random integers:");
|
2013-08-27 18:45:13 -07:00
|
|
|
let mut map: HashMap<uint,uint> = HashMap::new();
|
2013-03-21 15:21:06 -04:00
|
|
|
vector(&mut map, n_keys, rand);
|
2012-08-21 15:55:17 -07:00
|
|
|
}
|
|
|
|
|
2013-03-21 15:21:06 -04:00
|
|
|
io::println("\nTrieMap:");
|
2012-08-21 15:55:17 -07:00
|
|
|
|
|
|
|
{
|
2013-08-27 18:45:13 -07:00
|
|
|
let mut map: TrieMap<uint> = TrieMap::new();
|
2013-03-21 15:21:06 -04:00
|
|
|
ascending(&mut map, n_keys);
|
2013-01-23 19:51:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-08-27 18:45:13 -07:00
|
|
|
let mut map: TrieMap<uint> = TrieMap::new();
|
2013-03-21 15:21:06 -04:00
|
|
|
descending(&mut map, n_keys);
|
2012-08-21 15:55:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-03-21 15:21:06 -04:00
|
|
|
io::println(" Random integers:");
|
2013-08-27 18:45:13 -07:00
|
|
|
let mut map: TrieMap<uint> = TrieMap::new();
|
2013-03-21 15:21:06 -04:00
|
|
|
vector(&mut map, n_keys, rand);
|
2012-08-21 15:55:17 -07:00
|
|
|
}
|
2012-12-28 19:57:18 -08:00
|
|
|
}
|