rust/src/libcollections/deque.rs

124 lines
3.6 KiB
Rust
Raw Normal View History

// 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 <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.
//! Container traits for collections
std: Recreate a `collections` module As with the previous commit with `librand`, this commit shuffles around some `collections` code. The new state of the world is similar to that of librand: * The libcollections crate now only depends on libcore and liballoc. * The standard library has a new module, `std::collections`. All functionality of libcollections is reexported through this module. I would like to stress that this change is purely cosmetic. There are very few alterations to these primitives. There are a number of notable points about the new organization: * std::{str, slice, string, vec} all moved to libcollections. There is no reason that these primitives shouldn't be necessarily usable in a freestanding context that has allocation. These are all reexported in their usual places in the standard library. * The `hashmap`, and transitively the `lru_cache`, modules no longer reside in `libcollections`, but rather in libstd. The reason for this is because the `HashMap::new` contructor requires access to the OSRng for initially seeding the hash map. Beyond this requirement, there is no reason that the hashmap could not move to libcollections. I do, however, have a plan to move the hash map to the collections module. The `HashMap::new` function could be altered to require that the `H` hasher parameter ascribe to the `Default` trait, allowing the entire `hashmap` module to live in libcollections. The key idea would be that the default hasher would be different in libstd. Something along the lines of: // src/libstd/collections/mod.rs pub type HashMap<K, V, H = RandomizedSipHasher> = core_collections::HashMap<K, V, H>; This is not possible today because you cannot invoke static methods through type aliases. If we modified the compiler, however, to allow invocation of static methods through type aliases, then this type definition would essentially be switching the default hasher from `SipHasher` in libcollections to a libstd-defined `RandomizedSipHasher` type. This type's `Default` implementation would randomly seed the `SipHasher` instance, and otherwise perform the same as `SipHasher`. This future state doesn't seem incredibly far off, but until that time comes, the hashmap module will live in libstd to not compromise on functionality. * In preparation for the hashmap moving to libcollections, the `hash` module has moved from libstd to libcollections. A previously snapshotted commit enables a distinct `Writer` trait to live in the `hash` module which `Hash` implementations are now parameterized over. Due to using a custom trait, the `SipHasher` implementation has lost its specialized methods for writing integers. These can be re-added backwards-compatibly in the future via default methods if necessary, but the FNV hashing should satisfy much of the need for speedier hashing. A list of breaking changes: * HashMap::{get, get_mut} no longer fails with the key formatted into the error message with `{:?}`, instead, a generic message is printed. With backtraces, it should still be not-too-hard to track down errors. * The HashMap, HashSet, and LruCache types are now available through std::collections instead of the collections crate. * Manual implementations of hash should be parameterized over `hash::Writer` instead of just `Writer`. [breaking-change]
2014-05-29 20:50:12 -05:00
use core::prelude::*;
/// A double-ended sequence that allows querying, insertion and deletion at both ends.
pub trait Deque<T> : Mutable {
/// Provide a reference to the front element, or None if the sequence is empty
fn front<'a>(&'a self) -> Option<&'a T>;
/// Provide a mutable reference to the front element, or None if the sequence is empty
fn front_mut<'a>(&'a mut self) -> Option<&'a mut T>;
/// Provide a reference to the back element, or None if the sequence is empty
fn back<'a>(&'a self) -> Option<&'a T>;
/// Provide a mutable reference to the back element, or None if the sequence is empty
fn back_mut<'a>(&'a mut self) -> Option<&'a mut T>;
/// Insert an element first in the sequence
fn push_front(&mut self, elt: T);
/// Insert an element last in the sequence
fn push_back(&mut self, elt: T);
/// Remove the last element and return it, or None if the sequence is empty
fn pop_back(&mut self) -> Option<T>;
/// Remove the first element and return it, or None if the sequence is empty
fn pop_front(&mut self) -> Option<T>;
}
2013-07-19 16:07:00 -05:00
#[cfg(test)]
pub mod bench {
use std::prelude::*;
std: Recreate a `rand` module This commit shuffles around some of the `rand` code, along with some reorganization. The new state of the world is as follows: * The librand crate now only depends on libcore. This interface is experimental. * The standard library has a new module, `std::rand`. This interface will eventually become stable. Unfortunately, this entailed more of a breaking change than just shuffling some names around. The following breaking changes were made to the rand library: * Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which will return an infinite stream of random values. Previous behavior can be regained with `rng.gen_iter().take(n).collect()` * Rng::gen_ascii_str() was removed. This has been replaced with Rng::gen_ascii_chars() which will return an infinite stream of random ascii characters. Similarly to gen_iter(), previous behavior can be emulated with `rng.gen_ascii_chars().take(n).collect()` * {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all relied on being able to use an OSRng for seeding, but this is no longer available in librand (where these types are defined). To retain the same functionality, these types now implement the `Rand` trait so they can be generated with a random seed from another random number generator. This allows the stdlib to use an OSRng to create seeded instances of these RNGs. * Rand implementations for `Box<T>` and `@T` were removed. These seemed to be pretty rare in the codebase, and it allows for librand to not depend on liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not supported. If this is undesirable, librand can depend on liballoc and regain these implementations. * The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`, but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice structure now has a lifetime associated with it. * The `sample` method on `Rng` has been moved to a top-level function in the `rand` module due to its dependence on `Vec`. cc #13851 [breaking-change]
2014-05-25 03:39:37 -05:00
use std::rand;
use std::rand::Rng;
use test::Bencher;
2013-07-19 16:07:00 -05:00
pub fn insert_rand_n<M:MutableMap<uint,uint>>(n: uint,
map: &mut M,
b: &mut Bencher) {
2013-07-19 16:07:00 -05:00
// setup
let mut rng = rand::weak_rng();
2013-07-19 16:07:00 -05:00
map.clear();
for _ in range(0, n) {
2013-07-19 16:07:00 -05:00
map.insert(rng.gen::<uint>() % n, 1);
}
// measure
b.iter(|| {
2013-07-19 16:07:00 -05:00
let k = rng.gen::<uint>() % n;
map.insert(k, 1);
map.remove(&k);
})
2013-07-19 16:07:00 -05:00
}
pub fn insert_seq_n<M:MutableMap<uint,uint>>(n: uint,
map: &mut M,
b: &mut Bencher) {
2013-07-19 16:07:00 -05:00
// setup
map.clear();
for i in range(0u, n) {
2013-07-19 16:07:00 -05:00
map.insert(i*2, 1);
}
// measure
let mut i = 1;
b.iter(|| {
2013-07-19 16:07:00 -05:00
map.insert(i, 1);
map.remove(&i);
i = (i + 2) % n;
})
2013-07-19 16:07:00 -05:00
}
pub fn find_rand_n<M:MutableMap<uint,uint>>(n: uint,
map: &mut M,
b: &mut Bencher) {
2013-07-19 16:07:00 -05:00
// setup
let mut rng = rand::weak_rng();
let mut keys = Vec::from_fn(n, |_| rng.gen::<uint>() % n);
2013-07-19 16:07:00 -05:00
for k in keys.iter() {
2013-07-19 16:07:00 -05:00
map.insert(*k, 1);
}
rng.shuffle(keys.as_mut_slice());
2013-07-19 16:07:00 -05:00
// measure
let mut i = 0;
b.iter(|| {
map.find(keys.get(i));
2013-07-19 16:07:00 -05:00
i = (i + 1) % n;
})
2013-07-19 16:07:00 -05:00
}
pub fn find_seq_n<M:MutableMap<uint,uint>>(n: uint,
map: &mut M,
b: &mut Bencher) {
2013-07-19 16:07:00 -05:00
// setup
for i in range(0u, n) {
2013-07-19 16:07:00 -05:00
map.insert(i, 1);
}
// measure
let mut i = 0;
b.iter(|| {
let x = map.find(&i);
2013-07-19 16:07:00 -05:00
i = (i + 1) % n;
x
})
2013-07-19 16:07:00 -05:00
}
}