2014-01-25 01:37:51 -06:00
|
|
|
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-03-02 13:41:31 -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.
|
|
|
|
|
2013-12-24 10:08:28 -06:00
|
|
|
//! Ordered containers with integer keys, implemented as radix tries (`TrieSet` and `TrieMap` types)
|
2013-03-02 13:41:31 -06:00
|
|
|
|
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::*;
|
|
|
|
|
|
|
|
use alloc::owned::Box;
|
2014-06-09 02:30:04 -05:00
|
|
|
use core::default::Default;
|
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::mem::zeroed;
|
|
|
|
use core::mem;
|
|
|
|
use core::uint;
|
|
|
|
|
2014-06-06 18:33:44 -05:00
|
|
|
use {Collection, Mutable, Map, MutableMap, Set, MutableSet};
|
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 slice::{Items, MutItems};
|
|
|
|
use slice;
|
2014-02-19 21:29:58 -06:00
|
|
|
|
2013-03-05 17:53:43 -06:00
|
|
|
// FIXME: #5244: need to manually update the TrieNode constructor
|
2013-03-22 16:00:15 -05:00
|
|
|
static SHIFT: uint = 4;
|
|
|
|
static SIZE: uint = 1 << SHIFT;
|
|
|
|
static MASK: uint = SIZE - 1;
|
2014-01-25 01:37:51 -06:00
|
|
|
static NUM_CHUNKS: uint = uint::BITS / SHIFT;
|
2013-03-02 13:41:31 -06:00
|
|
|
|
|
|
|
enum Child<T> {
|
2014-05-05 20:56:44 -05:00
|
|
|
Internal(Box<TrieNode<T>>),
|
2013-03-02 13:41:31 -06:00
|
|
|
External(uint, T),
|
|
|
|
Nothing
|
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
#[allow(missing_doc)]
|
2013-03-02 13:41:31 -06:00
|
|
|
pub struct TrieMap<T> {
|
2014-03-27 17:10:04 -05:00
|
|
|
root: TrieNode<T>,
|
|
|
|
length: uint
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
|
2014-05-19 13:32:09 -05:00
|
|
|
impl<T> Collection for TrieMap<T> {
|
2013-03-02 13:41:31 -06:00
|
|
|
/// Return the number of elements in the map
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-06-23 22:44:11 -05:00
|
|
|
fn len(&self) -> uint { self.length }
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
|
2013-03-05 17:53:43 -06:00
|
|
|
impl<T> Mutable for TrieMap<T> {
|
2013-03-02 13:41:31 -06:00
|
|
|
/// Clear the map, removing all values.
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-03-02 13:41:31 -06:00
|
|
|
fn clear(&mut self) {
|
|
|
|
self.root = TrieNode::new();
|
|
|
|
self.length = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-05 17:53:43 -06:00
|
|
|
impl<T> Map<uint, T> for TrieMap<T> {
|
2013-03-24 19:30:35 -05:00
|
|
|
/// Return a reference to the value corresponding to the key
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-04-10 15:11:35 -05:00
|
|
|
fn find<'a>(&'a self, key: &uint) -> Option<&'a T> {
|
|
|
|
let mut node: &'a TrieNode<T> = &self.root;
|
|
|
|
let mut idx = 0;
|
|
|
|
loop {
|
|
|
|
match node.children[chunk(*key, idx)] {
|
|
|
|
Internal(ref x) => node = &**x,
|
|
|
|
External(stored, ref value) => {
|
|
|
|
if stored == *key {
|
|
|
|
return Some(value)
|
|
|
|
} else {
|
|
|
|
return None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Nothing => return None
|
|
|
|
}
|
|
|
|
idx += 1;
|
|
|
|
}
|
|
|
|
}
|
2013-07-13 21:44:36 -05:00
|
|
|
}
|
2013-04-10 15:11:35 -05:00
|
|
|
|
2013-07-13 21:44:36 -05:00
|
|
|
impl<T> MutableMap<uint, T> for TrieMap<T> {
|
2013-03-24 19:40:17 -05:00
|
|
|
/// Return a mutable reference to the value corresponding to the key
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-04-10 15:11:35 -05:00
|
|
|
fn find_mut<'a>(&'a mut self, key: &uint) -> Option<&'a mut T> {
|
|
|
|
find_mut(&mut self.root.children[chunk(*key, 0)], *key, 1)
|
|
|
|
}
|
|
|
|
|
2013-05-04 08:54:58 -05:00
|
|
|
/// Insert a key-value pair from the map. If the key already had a value
|
|
|
|
/// present in the map, that value is returned. Otherwise None is returned.
|
|
|
|
fn swap(&mut self, key: uint, value: T) -> Option<T> {
|
|
|
|
let ret = insert(&mut self.root.count,
|
|
|
|
&mut self.root.children[chunk(key, 0)],
|
|
|
|
key, value, 1);
|
|
|
|
if ret.is_none() { self.length += 1 }
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes a key from the map, returning the value at the key if the key
|
|
|
|
/// was previously in the map.
|
|
|
|
fn pop(&mut self, key: &uint) -> Option<T> {
|
2013-03-02 13:41:31 -06:00
|
|
|
let ret = remove(&mut self.root.count,
|
|
|
|
&mut self.root.children[chunk(*key, 0)],
|
|
|
|
*key, 1);
|
2013-05-04 08:54:58 -05:00
|
|
|
if ret.is_some() { self.length -= 1 }
|
2013-03-02 13:41:31 -06:00
|
|
|
ret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 02:30:04 -05:00
|
|
|
impl<T> Default for TrieMap<T> {
|
|
|
|
#[inline]
|
|
|
|
fn default() -> TrieMap<T> { TrieMap::new() }
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl<T> TrieMap<T> {
|
2013-03-15 20:03:29 -05:00
|
|
|
/// Create an empty TrieMap
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn new() -> TrieMap<T> {
|
2013-03-02 13:41:31 -06:00
|
|
|
TrieMap{root: TrieNode::new(), length: 0}
|
|
|
|
}
|
|
|
|
|
2013-04-06 10:22:36 -05:00
|
|
|
/// Visit all key-value pairs in reverse order
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-11-18 23:15:42 -06:00
|
|
|
pub fn each_reverse<'a>(&'a self, f: |&uint, &'a T| -> bool) -> bool {
|
2013-05-02 17:33:18 -05:00
|
|
|
self.root.each_reverse(f)
|
|
|
|
}
|
|
|
|
|
2013-08-04 17:26:38 -05:00
|
|
|
/// Get an iterator over the key-value pairs in the map
|
2014-01-14 21:32:24 -06:00
|
|
|
pub fn iter<'a>(&'a self) -> Entries<'a, T> {
|
|
|
|
let mut iter = unsafe {Entries::new()};
|
2014-01-13 08:37:52 -06:00
|
|
|
iter.stack[0] = self.root.children.iter();
|
|
|
|
iter.length = 1;
|
|
|
|
iter.remaining_min = self.length;
|
|
|
|
iter.remaining_max = self.length;
|
|
|
|
|
|
|
|
iter
|
2013-08-04 17:26:38 -05:00
|
|
|
}
|
2013-08-07 17:37:13 -05:00
|
|
|
|
2014-01-06 07:14:37 -06:00
|
|
|
/// Get an iterator over the key-value pairs in the map, with the
|
|
|
|
/// ability to mutate the values.
|
2014-01-14 21:32:24 -06:00
|
|
|
pub fn mut_iter<'a>(&'a mut self) -> MutEntries<'a, T> {
|
|
|
|
let mut iter = unsafe {MutEntries::new()};
|
2014-01-13 08:37:52 -06:00
|
|
|
iter.stack[0] = self.root.children.mut_iter();
|
|
|
|
iter.length = 1;
|
|
|
|
iter.remaining_min = self.length;
|
|
|
|
iter.remaining_max = self.length;
|
|
|
|
|
|
|
|
iter
|
2014-01-06 07:14:37 -06:00
|
|
|
}
|
2014-01-06 08:00:19 -06:00
|
|
|
}
|
2014-01-06 07:14:37 -06:00
|
|
|
|
2014-01-06 08:00:19 -06:00
|
|
|
// FIXME #5846 we want to be able to choose between &x and &mut x
|
|
|
|
// (with many different `x`) below, so we need to optionally pass mut
|
|
|
|
// as a tt, but the only thing we can do with a `tt` is pass them to
|
|
|
|
// other macros, so this takes the `& <mutability> <operand>` token
|
2014-04-20 23:49:39 -05:00
|
|
|
// sequence and forces their evaluation as an expression. (see also
|
2014-01-06 08:00:19 -06:00
|
|
|
// `item!` below.)
|
|
|
|
macro_rules! addr { ($e:expr) => { $e } }
|
|
|
|
|
|
|
|
macro_rules! bound {
|
|
|
|
($iterator_name:ident,
|
|
|
|
// the current treemap
|
|
|
|
self = $this:expr,
|
|
|
|
// the key to look for
|
|
|
|
key = $key:expr,
|
|
|
|
// are we looking at the upper bound?
|
|
|
|
is_upper = $upper:expr,
|
|
|
|
|
|
|
|
// method names for slicing/iterating.
|
|
|
|
slice_from = $slice_from:ident,
|
|
|
|
iter = $iter:ident,
|
|
|
|
|
|
|
|
// see the comment on `addr!`, this is just an optional mut, but
|
|
|
|
// there's no 0-or-1 repeats yet.
|
|
|
|
mutability = $($mut_:tt)*) => {
|
|
|
|
{
|
|
|
|
// # For `mut`
|
|
|
|
// We need an unsafe pointer here because we are borrowing
|
|
|
|
// mutable references to the internals of each of these
|
|
|
|
// mutable nodes, while still using the outer node.
|
|
|
|
//
|
|
|
|
// However, we're allowed to flaunt rustc like this because we
|
|
|
|
// never actually modify the "shape" of the nodes. The only
|
|
|
|
// place that mutation is can actually occur is of the actual
|
|
|
|
// values of the TrieMap (as the return value of the
|
|
|
|
// iterator), i.e. we can never cause a deallocation of any
|
|
|
|
// TrieNodes so the raw pointer is always valid.
|
|
|
|
//
|
|
|
|
// # For non-`mut`
|
|
|
|
// We like sharing code so much that even a little unsafe won't
|
|
|
|
// stop us.
|
|
|
|
let this = $this;
|
|
|
|
let mut node = addr!(& $($mut_)* this.root as * $($mut_)* TrieNode<T>);
|
|
|
|
|
|
|
|
let key = $key;
|
|
|
|
|
2014-01-13 08:37:52 -06:00
|
|
|
let mut it = unsafe {$iterator_name::new()};
|
|
|
|
// everything else is zero'd, as we want.
|
|
|
|
it.remaining_max = this.length;
|
|
|
|
|
2014-01-06 08:00:19 -06:00
|
|
|
// this addr is necessary for the `Internal` pattern.
|
|
|
|
addr!(loop {
|
|
|
|
let children = unsafe {addr!(& $($mut_)* (*node).children)};
|
2014-01-13 08:37:52 -06:00
|
|
|
// it.length is the current depth in the iterator and the
|
|
|
|
// current depth through the `uint` key we've traversed.
|
|
|
|
let child_id = chunk(key, it.length);
|
2014-01-06 08:45:10 -06:00
|
|
|
let (slice_idx, ret) = match children[child_id] {
|
2014-01-06 08:00:19 -06:00
|
|
|
Internal(ref $($mut_)* n) => {
|
|
|
|
node = addr!(& $($mut_)* **n as * $($mut_)* TrieNode<T>);
|
2014-01-06 08:45:10 -06:00
|
|
|
(child_id + 1, false)
|
2014-01-06 08:00:19 -06:00
|
|
|
}
|
|
|
|
External(stored, _) => {
|
2014-01-06 08:45:10 -06:00
|
|
|
(if stored < key || ($upper && stored == key) {
|
|
|
|
child_id + 1
|
2014-01-06 08:00:19 -06:00
|
|
|
} else {
|
2014-01-06 08:45:10 -06:00
|
|
|
child_id
|
|
|
|
}, true)
|
2014-01-06 08:00:19 -06:00
|
|
|
}
|
|
|
|
Nothing => {
|
2014-01-06 08:45:10 -06:00
|
|
|
(child_id + 1, true)
|
2014-01-06 08:00:19 -06:00
|
|
|
}
|
2014-01-06 08:45:10 -06:00
|
|
|
};
|
2014-01-13 08:37:52 -06:00
|
|
|
// push to the stack.
|
|
|
|
it.stack[it.length] = children.$slice_from(slice_idx).$iter();
|
|
|
|
it.length += 1;
|
2014-01-06 08:45:10 -06:00
|
|
|
if ret { return it }
|
2014-01-06 08:00:19 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> TrieMap<T> {
|
2013-08-07 17:37:13 -05:00
|
|
|
// If `upper` is true then returns upper_bound else returns lower_bound.
|
|
|
|
#[inline]
|
2014-01-14 21:32:24 -06:00
|
|
|
fn bound<'a>(&'a self, key: uint, upper: bool) -> Entries<'a, T> {
|
|
|
|
bound!(Entries, self = self,
|
2014-01-06 08:00:19 -06:00
|
|
|
key = key, is_upper = upper,
|
|
|
|
slice_from = slice_from, iter = iter,
|
|
|
|
mutability = )
|
2013-08-07 17:37:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get an iterator pointing to the first key-value pair whose key is not less than `key`.
|
|
|
|
/// If all keys in the map are less than `key` an empty iterator is returned.
|
2014-01-14 21:32:24 -06:00
|
|
|
pub fn lower_bound<'a>(&'a self, key: uint) -> Entries<'a, T> {
|
2013-11-23 04:18:51 -06:00
|
|
|
self.bound(key, false)
|
2013-08-07 17:37:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get an iterator pointing to the first key-value pair whose key is greater than `key`.
|
|
|
|
/// If all keys in the map are not greater than `key` an empty iterator is returned.
|
2014-01-14 21:32:24 -06:00
|
|
|
pub fn upper_bound<'a>(&'a self, key: uint) -> Entries<'a, T> {
|
2013-11-23 04:18:51 -06:00
|
|
|
self.bound(key, true)
|
2013-08-07 17:37:13 -05:00
|
|
|
}
|
2014-01-06 07:14:37 -06:00
|
|
|
// If `upper` is true then returns upper_bound else returns lower_bound.
|
|
|
|
#[inline]
|
2014-01-14 21:32:24 -06:00
|
|
|
fn mut_bound<'a>(&'a mut self, key: uint, upper: bool) -> MutEntries<'a, T> {
|
|
|
|
bound!(MutEntries, self = self,
|
2014-01-06 08:00:19 -06:00
|
|
|
key = key, is_upper = upper,
|
|
|
|
slice_from = mut_slice_from, iter = mut_iter,
|
|
|
|
mutability = mut)
|
2014-01-06 07:14:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get an iterator pointing to the first key-value pair whose key is not less than `key`.
|
|
|
|
/// If all keys in the map are less than `key` an empty iterator is returned.
|
2014-01-14 21:32:24 -06:00
|
|
|
pub fn mut_lower_bound<'a>(&'a mut self, key: uint) -> MutEntries<'a, T> {
|
2014-01-06 07:14:37 -06:00
|
|
|
self.mut_bound(key, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get an iterator pointing to the first key-value pair whose key is greater than `key`.
|
|
|
|
/// If all keys in the map are not greater than `key` an empty iterator is returned.
|
2014-01-14 21:32:24 -06:00
|
|
|
pub fn mut_upper_bound<'a>(&'a mut self, key: uint) -> MutEntries<'a, T> {
|
2014-01-06 07:14:37 -06:00
|
|
|
self.mut_bound(key, true)
|
|
|
|
}
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
|
std: Move the iterator param on FromIterator and Extendable to the method.
If they are on the trait then it is extremely annoying to use them as
generic parameters to a function, e.g. with the iterator param on the trait
itself, if one was to pass an Extendable<int> to a function that filled it
either from a Range or a Map<VecIterator>, one needs to write something
like:
fn foo<E: Extendable<int, Range<int>> +
Extendable<int, Map<&'self int, int, VecIterator<int>>>
(e: &mut E, ...) { ... }
since using a generic, i.e. `foo<E: Extendable<int, I>, I: Iterator<int>>`
means that `foo` takes 2 type parameters, and the caller has to specify them
(which doesn't work anyway, as they'll mismatch with the iterators used in
`foo` itself).
This patch changes it to:
fn foo<E: Extendable<int>>(e: &mut E, ...) { ... }
2013-08-13 08:08:14 -05:00
|
|
|
impl<T> FromIterator<(uint, T)> for TrieMap<T> {
|
2014-03-30 23:45:55 -05:00
|
|
|
fn from_iter<Iter: Iterator<(uint, T)>>(iter: Iter) -> TrieMap<T> {
|
2013-07-14 11:20:48 -05:00
|
|
|
let mut map = TrieMap::new();
|
2013-07-29 19:17:17 -05:00
|
|
|
map.extend(iter);
|
|
|
|
map
|
|
|
|
}
|
|
|
|
}
|
2013-07-14 11:20:48 -05:00
|
|
|
|
std: Move the iterator param on FromIterator and Extendable to the method.
If they are on the trait then it is extremely annoying to use them as
generic parameters to a function, e.g. with the iterator param on the trait
itself, if one was to pass an Extendable<int> to a function that filled it
either from a Range or a Map<VecIterator>, one needs to write something
like:
fn foo<E: Extendable<int, Range<int>> +
Extendable<int, Map<&'self int, int, VecIterator<int>>>
(e: &mut E, ...) { ... }
since using a generic, i.e. `foo<E: Extendable<int, I>, I: Iterator<int>>`
means that `foo` takes 2 type parameters, and the caller has to specify them
(which doesn't work anyway, as they'll mismatch with the iterators used in
`foo` itself).
This patch changes it to:
fn foo<E: Extendable<int>>(e: &mut E, ...) { ... }
2013-08-13 08:08:14 -05:00
|
|
|
impl<T> Extendable<(uint, T)> for TrieMap<T> {
|
2014-03-20 08:12:56 -05:00
|
|
|
fn extend<Iter: Iterator<(uint, T)>>(&mut self, mut iter: Iter) {
|
|
|
|
for (k, v) in iter {
|
2013-07-29 19:17:17 -05:00
|
|
|
self.insert(k, v);
|
2013-07-14 11:20:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
#[allow(missing_doc)]
|
2013-03-02 13:41:31 -06:00
|
|
|
pub struct TrieSet {
|
2014-03-27 17:10:04 -05:00
|
|
|
map: TrieMap<()>
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
|
2014-05-19 13:32:09 -05:00
|
|
|
impl Collection for TrieSet {
|
2013-03-02 13:41:31 -06:00
|
|
|
/// Return the number of elements in the set
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-06-23 22:44:11 -05:00
|
|
|
fn len(&self) -> uint { self.map.len() }
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Mutable for TrieSet {
|
|
|
|
/// Clear the set, removing all values.
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-03-02 13:41:31 -06:00
|
|
|
fn clear(&mut self) { self.map.clear() }
|
|
|
|
}
|
|
|
|
|
2013-10-09 11:55:00 -05:00
|
|
|
impl Set<uint> for TrieSet {
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-10-09 11:55:00 -05:00
|
|
|
fn contains(&self, value: &uint) -> bool {
|
|
|
|
self.map.contains_key(value)
|
2013-03-15 20:03:29 -05:00
|
|
|
}
|
|
|
|
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-10-09 11:55:00 -05:00
|
|
|
fn is_disjoint(&self, other: &TrieSet) -> bool {
|
|
|
|
self.iter().all(|v| !other.contains(&v))
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-10-09 11:55:00 -05:00
|
|
|
fn is_subset(&self, other: &TrieSet) -> bool {
|
|
|
|
self.iter().all(|v| other.contains(&v))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn is_superset(&self, other: &TrieSet) -> bool {
|
|
|
|
other.is_subset(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MutableSet<uint> for TrieSet {
|
|
|
|
#[inline]
|
|
|
|
fn insert(&mut self, value: uint) -> bool {
|
2013-05-31 17:17:22 -05:00
|
|
|
self.map.insert(value, ())
|
|
|
|
}
|
2013-03-02 13:41:31 -06:00
|
|
|
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-10-09 11:55:00 -05:00
|
|
|
fn remove(&mut self, value: &uint) -> bool {
|
2013-05-31 17:17:22 -05:00
|
|
|
self.map.remove(value)
|
|
|
|
}
|
2013-10-09 11:55:00 -05:00
|
|
|
}
|
|
|
|
|
2014-06-09 02:30:04 -05:00
|
|
|
impl Default for TrieSet {
|
|
|
|
#[inline]
|
|
|
|
fn default() -> TrieSet { TrieSet::new() }
|
|
|
|
}
|
|
|
|
|
2013-10-09 11:55:00 -05:00
|
|
|
impl TrieSet {
|
|
|
|
/// Create an empty TrieSet
|
|
|
|
#[inline]
|
|
|
|
pub fn new() -> TrieSet {
|
|
|
|
TrieSet{map: TrieMap::new()}
|
|
|
|
}
|
2013-06-23 16:57:39 -05:00
|
|
|
|
|
|
|
/// Visit all values in reverse order
|
|
|
|
#[inline]
|
2013-11-18 23:15:42 -06:00
|
|
|
pub fn each_reverse(&self, f: |&uint| -> bool) -> bool {
|
2014-01-12 04:35:12 -06:00
|
|
|
self.map.each_reverse(|k, _| f(k))
|
2013-06-23 16:57:39 -05:00
|
|
|
}
|
2013-08-04 17:26:38 -05:00
|
|
|
|
|
|
|
/// Get an iterator over the values in the set
|
|
|
|
#[inline]
|
2014-01-14 21:32:24 -06:00
|
|
|
pub fn iter<'a>(&'a self) -> SetItems<'a> {
|
|
|
|
SetItems{iter: self.map.iter()}
|
2013-08-04 17:26:38 -05:00
|
|
|
}
|
2013-08-07 17:37:13 -05:00
|
|
|
|
|
|
|
/// Get an iterator pointing to the first value that is not less than `val`.
|
|
|
|
/// If all values in the set are less than `val` an empty iterator is returned.
|
2014-01-14 21:32:24 -06:00
|
|
|
pub fn lower_bound<'a>(&'a self, val: uint) -> SetItems<'a> {
|
|
|
|
SetItems{iter: self.map.lower_bound(val)}
|
2013-08-07 17:37:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get an iterator pointing to the first value that key is greater than `val`.
|
|
|
|
/// If all values in the set are not greater than `val` an empty iterator is returned.
|
2014-01-14 21:32:24 -06:00
|
|
|
pub fn upper_bound<'a>(&'a self, val: uint) -> SetItems<'a> {
|
|
|
|
SetItems{iter: self.map.upper_bound(val)}
|
2013-08-07 17:37:13 -05:00
|
|
|
}
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
|
std: Move the iterator param on FromIterator and Extendable to the method.
If they are on the trait then it is extremely annoying to use them as
generic parameters to a function, e.g. with the iterator param on the trait
itself, if one was to pass an Extendable<int> to a function that filled it
either from a Range or a Map<VecIterator>, one needs to write something
like:
fn foo<E: Extendable<int, Range<int>> +
Extendable<int, Map<&'self int, int, VecIterator<int>>>
(e: &mut E, ...) { ... }
since using a generic, i.e. `foo<E: Extendable<int, I>, I: Iterator<int>>`
means that `foo` takes 2 type parameters, and the caller has to specify them
(which doesn't work anyway, as they'll mismatch with the iterators used in
`foo` itself).
This patch changes it to:
fn foo<E: Extendable<int>>(e: &mut E, ...) { ... }
2013-08-13 08:08:14 -05:00
|
|
|
impl FromIterator<uint> for TrieSet {
|
2014-03-30 23:45:55 -05:00
|
|
|
fn from_iter<Iter: Iterator<uint>>(iter: Iter) -> TrieSet {
|
2013-07-14 11:20:48 -05:00
|
|
|
let mut set = TrieSet::new();
|
2013-07-29 19:17:17 -05:00
|
|
|
set.extend(iter);
|
|
|
|
set
|
|
|
|
}
|
|
|
|
}
|
2013-07-14 11:20:48 -05:00
|
|
|
|
std: Move the iterator param on FromIterator and Extendable to the method.
If they are on the trait then it is extremely annoying to use them as
generic parameters to a function, e.g. with the iterator param on the trait
itself, if one was to pass an Extendable<int> to a function that filled it
either from a Range or a Map<VecIterator>, one needs to write something
like:
fn foo<E: Extendable<int, Range<int>> +
Extendable<int, Map<&'self int, int, VecIterator<int>>>
(e: &mut E, ...) { ... }
since using a generic, i.e. `foo<E: Extendable<int, I>, I: Iterator<int>>`
means that `foo` takes 2 type parameters, and the caller has to specify them
(which doesn't work anyway, as they'll mismatch with the iterators used in
`foo` itself).
This patch changes it to:
fn foo<E: Extendable<int>>(e: &mut E, ...) { ... }
2013-08-13 08:08:14 -05:00
|
|
|
impl Extendable<uint> for TrieSet {
|
2014-03-20 08:12:56 -05:00
|
|
|
fn extend<Iter: Iterator<uint>>(&mut self, mut iter: Iter) {
|
|
|
|
for elem in iter {
|
2013-07-29 19:17:17 -05:00
|
|
|
self.insert(elem);
|
2013-07-14 11:20:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-02 13:41:31 -06:00
|
|
|
struct TrieNode<T> {
|
|
|
|
count: uint,
|
2013-03-22 20:52:04 -05:00
|
|
|
children: [Child<T>, ..SIZE]
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
|
2013-03-05 17:53:43 -06:00
|
|
|
impl<T> TrieNode<T> {
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn new() -> TrieNode<T> {
|
2013-07-10 16:43:25 -05:00
|
|
|
// FIXME: #5244: [Nothing, ..SIZE] should be possible without implicit
|
|
|
|
// copyability
|
2013-03-05 17:53:43 -06:00
|
|
|
TrieNode{count: 0,
|
|
|
|
children: [Nothing, Nothing, Nothing, Nothing,
|
|
|
|
Nothing, Nothing, Nothing, Nothing,
|
|
|
|
Nothing, Nothing, Nothing, Nothing,
|
|
|
|
Nothing, Nothing, Nothing, Nothing]}
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> TrieNode<T> {
|
2013-11-18 23:15:42 -06:00
|
|
|
fn each_reverse<'a>(&'a self, f: |&uint, &'a T| -> bool) -> bool {
|
Deprecate the rev_iter pattern in all places where a DoubleEndedIterator is provided (everywhere but treemap)
This commit deprecates rev_iter, mut_rev_iter, move_rev_iter everywhere (except treemap) and also
deprecates related functions like rsplit, rev_components, and rev_str_components. In every case,
these functions can be replaced with the non-reversed form followed by a call to .rev(). To make this
more concrete, a translation table for all functional changes necessary follows:
* container.rev_iter() -> container.iter().rev()
* container.mut_rev_iter() -> container.mut_iter().rev()
* container.move_rev_iter() -> container.move_iter().rev()
* sliceorstr.rsplit(sep) -> sliceorstr.split(sep).rev()
* path.rev_components() -> path.components().rev()
* path.rev_str_components() -> path.str_components().rev()
In terms of the type system, this change also deprecates any specialized reversed iterator types (except
in treemap), opting instead to use Rev directly if any type annotations are needed. However, since
methods directly returning reversed iterators are now discouraged, the need for such annotations should
be small. However, in those cases, the general pattern for conversion is to take whatever follows Rev in
the original reversed name and surround it with Rev<>:
* RevComponents<'a> -> Rev<Components<'a>>
* RevStrComponents<'a> -> Rev<StrComponents<'a>>
* RevItems<'a, T> -> Rev<Items<'a, T>>
* etc.
The reasoning behind this change is that it makes the standard API much simpler without reducing readability,
performance, or power. The presence of functions such as rev_iter adds more boilerplate code to libraries
(all of which simply call .iter().rev()), clutters up the documentation, and only helps code by saving two
characters. Additionally, the numerous type synonyms that were used to make the type signatures look nice
like RevItems add even more boilerplate and clutter up the docs even more. With this change, all that cruft
goes away.
[breaking-change]
2014-04-20 23:59:12 -05:00
|
|
|
for elt in self.children.iter().rev() {
|
2013-08-07 00:34:22 -05:00
|
|
|
match *elt {
|
|
|
|
Internal(ref x) => if !x.each_reverse(|i,t| f(i,t)) { return false },
|
|
|
|
External(k, ref v) => if !f(&k, v) { return false },
|
|
|
|
Nothing => ()
|
2013-04-10 15:11:35 -05:00
|
|
|
}
|
|
|
|
}
|
2013-08-07 00:34:22 -05:00
|
|
|
true
|
2013-04-10 15:11:35 -05:00
|
|
|
}
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// if this was done via a trait, the key could be generic
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn chunk(n: uint, idx: uint) -> uint {
|
2014-01-25 01:37:51 -06:00
|
|
|
let sh = uint::BITS - (SHIFT * (idx + 1));
|
2013-03-05 22:54:19 -06:00
|
|
|
(n >> sh) & MASK
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
|
2013-05-06 22:29:54 -05:00
|
|
|
fn find_mut<'r, T>(child: &'r mut Child<T>, key: uint, idx: uint) -> Option<&'r mut T> {
|
|
|
|
match *child {
|
2013-11-24 16:41:33 -06:00
|
|
|
External(stored, ref mut value) if stored == key => Some(value),
|
2013-11-28 14:22:53 -06:00
|
|
|
External(..) => None,
|
2013-05-06 22:29:54 -05:00
|
|
|
Internal(ref mut x) => find_mut(&mut x.children[chunk(key, idx)], key, idx + 1),
|
|
|
|
Nothing => None
|
|
|
|
}
|
2013-03-24 19:30:35 -05:00
|
|
|
}
|
|
|
|
|
2013-03-13 16:07:23 -05:00
|
|
|
fn insert<T>(count: &mut uint, child: &mut Child<T>, key: uint, value: T,
|
2013-05-04 08:54:58 -05:00
|
|
|
idx: uint) -> Option<T> {
|
2014-01-14 18:58:02 -06:00
|
|
|
// we branch twice to avoid having to do the `replace` when we
|
|
|
|
// don't need to; this is much faster, especially for keys that
|
|
|
|
// have long shared prefixes.
|
|
|
|
match *child {
|
|
|
|
Nothing => {
|
|
|
|
*count += 1;
|
|
|
|
*child = External(key, value);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Internal(ref mut x) => {
|
|
|
|
return insert(&mut x.count, &mut x.children[chunk(key, idx)], key, value, idx + 1);
|
|
|
|
}
|
|
|
|
External(stored_key, ref mut stored_value) if stored_key == key => {
|
|
|
|
// swap in the new value and return the old.
|
2014-01-31 14:35:36 -06:00
|
|
|
return Some(mem::replace(stored_value, value));
|
2014-01-14 18:58:02 -06:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// conflict, an external node with differing keys: we have to
|
|
|
|
// split the node, so we need the old value by value; hence we
|
|
|
|
// have to move out of `child`.
|
2014-01-31 14:35:36 -06:00
|
|
|
match mem::replace(child, Nothing) {
|
2014-01-14 18:58:02 -06:00
|
|
|
External(stored_key, stored_value) => {
|
2014-04-25 03:08:02 -05:00
|
|
|
let mut new = box TrieNode::new();
|
2014-01-14 18:58:02 -06:00
|
|
|
insert(&mut new.count,
|
|
|
|
&mut new.children[chunk(stored_key, idx)],
|
|
|
|
stored_key, stored_value, idx + 1);
|
|
|
|
let ret = insert(&mut new.count, &mut new.children[chunk(key, idx)],
|
|
|
|
key, value, idx + 1);
|
|
|
|
*child = Internal(new);
|
|
|
|
return ret;
|
|
|
|
}
|
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
|
|
|
_ => fail!("unreachable code"),
|
2014-01-14 18:58:02 -06:00
|
|
|
}
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn remove<T>(count: &mut uint, child: &mut Child<T>, key: uint,
|
2013-05-04 08:54:58 -05:00
|
|
|
idx: uint) -> Option<T> {
|
2013-03-02 13:41:31 -06:00
|
|
|
let (ret, this) = match *child {
|
2013-05-04 08:54:58 -05:00
|
|
|
External(stored, _) if stored == key => {
|
2014-01-31 14:35:36 -06:00
|
|
|
match mem::replace(child, Nothing) {
|
2013-05-04 08:54:58 -05:00
|
|
|
External(_, value) => (Some(value), true),
|
2013-10-21 15:08:31 -05:00
|
|
|
_ => fail!()
|
2013-05-04 08:54:58 -05:00
|
|
|
}
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
2013-11-28 14:22:53 -06:00
|
|
|
External(..) => (None, false),
|
2013-03-02 13:41:31 -06:00
|
|
|
Internal(ref mut x) => {
|
|
|
|
let ret = remove(&mut x.count, &mut x.children[chunk(key, idx)],
|
|
|
|
key, idx + 1);
|
|
|
|
(ret, x.count == 0)
|
|
|
|
}
|
2013-05-04 08:54:58 -05:00
|
|
|
Nothing => (None, false)
|
2013-03-02 13:41:31 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
if this {
|
|
|
|
*child = Nothing;
|
|
|
|
*count -= 1;
|
|
|
|
}
|
2013-05-04 08:54:58 -05:00
|
|
|
return ret;
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
|
2013-08-04 17:26:38 -05:00
|
|
|
/// Forward iterator over a map
|
2014-01-14 21:32:24 -06:00
|
|
|
pub struct Entries<'a, T> {
|
2014-03-27 17:10:04 -05:00
|
|
|
stack: [slice::Items<'a, Child<T>>, .. NUM_CHUNKS],
|
|
|
|
length: uint,
|
|
|
|
remaining_min: uint,
|
|
|
|
remaining_max: uint
|
2013-08-04 17:26:38 -05:00
|
|
|
}
|
|
|
|
|
2014-01-06 07:14:37 -06:00
|
|
|
/// Forward iterator over the key-value pairs of a map, with the
|
|
|
|
/// values being mutable.
|
2014-01-14 21:32:24 -06:00
|
|
|
pub struct MutEntries<'a, T> {
|
2014-03-27 17:10:04 -05:00
|
|
|
stack: [slice::MutItems<'a, Child<T>>, .. NUM_CHUNKS],
|
|
|
|
length: uint,
|
|
|
|
remaining_min: uint,
|
|
|
|
remaining_max: uint
|
2014-01-06 07:14:37 -06:00
|
|
|
}
|
|
|
|
|
2014-01-06 08:00:19 -06:00
|
|
|
// FIXME #5846: see `addr!` above.
|
|
|
|
macro_rules! item { ($i:item) => {$i}}
|
|
|
|
|
|
|
|
macro_rules! iterator_impl {
|
|
|
|
($name:ident,
|
|
|
|
iter = $iter:ident,
|
|
|
|
mutability = $($mut_:tt)*) => {
|
2014-01-13 08:37:52 -06:00
|
|
|
impl<'a, T> $name<'a, T> {
|
|
|
|
// Create new zero'd iterator. We have a thin gilding of safety by
|
|
|
|
// using init rather than uninit, so that the worst that can happen
|
|
|
|
// from failing to initialise correctly after calling these is a
|
|
|
|
// segfault.
|
|
|
|
#[cfg(target_word_size="32")]
|
|
|
|
unsafe fn new() -> $name<'a, T> {
|
|
|
|
$name {
|
|
|
|
remaining_min: 0,
|
|
|
|
remaining_max: 0,
|
|
|
|
length: 0,
|
|
|
|
// ick :( ... at least the compiler will tell us if we screwed up.
|
2014-05-17 02:56:00 -05:00
|
|
|
stack: [zeroed(), zeroed(), zeroed(), zeroed(), zeroed(),
|
|
|
|
zeroed(), zeroed(), zeroed()]
|
2014-01-13 08:37:52 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_word_size="64")]
|
|
|
|
unsafe fn new() -> $name<'a, T> {
|
|
|
|
$name {
|
|
|
|
remaining_min: 0,
|
|
|
|
remaining_max: 0,
|
|
|
|
length: 0,
|
2014-05-17 02:56:00 -05:00
|
|
|
stack: [zeroed(), zeroed(), zeroed(), zeroed(),
|
|
|
|
zeroed(), zeroed(), zeroed(), zeroed(),
|
|
|
|
zeroed(), zeroed(), zeroed(), zeroed(),
|
|
|
|
zeroed(), zeroed(), zeroed(), zeroed()]
|
2014-01-13 08:37:52 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-06 08:00:19 -06:00
|
|
|
item!(impl<'a, T> Iterator<(uint, &'a $($mut_)* T)> for $name<'a, T> {
|
2014-01-13 08:37:52 -06:00
|
|
|
// you might wonder why we're not even trying to act within the
|
|
|
|
// rules, and are just manipulating raw pointers like there's no
|
|
|
|
// such thing as invalid pointers and memory unsafety. The
|
|
|
|
// reason is performance, without doing this we can get the
|
|
|
|
// bench_iter_large microbenchmark down to about 30000 ns/iter
|
|
|
|
// (using .unsafe_ref to index self.stack directly, 38000
|
|
|
|
// ns/iter with [] checked indexing), but this smashes that down
|
|
|
|
// to 13500 ns/iter.
|
|
|
|
//
|
|
|
|
// Fortunately, it's still safe...
|
|
|
|
//
|
|
|
|
// We have an invariant that every Internal node
|
|
|
|
// corresponds to one push to self.stack, and one pop,
|
|
|
|
// nested appropriately. self.stack has enough storage
|
|
|
|
// to store the maximum depth of Internal nodes in the
|
|
|
|
// trie (8 on 32-bit platforms, 16 on 64-bit).
|
2014-01-06 08:00:19 -06:00
|
|
|
fn next(&mut self) -> Option<(uint, &'a $($mut_)* T)> {
|
2014-01-13 08:37:52 -06:00
|
|
|
let start_ptr = self.stack.as_mut_ptr();
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
// write_ptr is the next place to write to the stack.
|
|
|
|
// invariant: start_ptr <= write_ptr < end of the
|
|
|
|
// vector.
|
|
|
|
let mut write_ptr = start_ptr.offset(self.length as int);
|
|
|
|
while write_ptr != start_ptr {
|
|
|
|
// indexing back one is safe, since write_ptr >
|
|
|
|
// start_ptr now.
|
|
|
|
match (*write_ptr.offset(-1)).next() {
|
|
|
|
// exhausted this iterator (i.e. finished this
|
|
|
|
// Internal node), so pop from the stack.
|
|
|
|
//
|
|
|
|
// don't bother clearing the memory, because the
|
|
|
|
// next time we use it we'll've written to it
|
|
|
|
// first.
|
|
|
|
None => write_ptr = write_ptr.offset(-1),
|
|
|
|
Some(child) => {
|
|
|
|
addr!(match *child {
|
|
|
|
Internal(ref $($mut_)* node) => {
|
|
|
|
// going down a level, so push
|
|
|
|
// to the stack (this is the
|
|
|
|
// write referenced above)
|
|
|
|
*write_ptr = node.children.$iter();
|
|
|
|
write_ptr = write_ptr.offset(1);
|
|
|
|
}
|
|
|
|
External(key, ref $($mut_)* value) => {
|
|
|
|
self.remaining_max -= 1;
|
|
|
|
if self.remaining_min > 0 {
|
|
|
|
self.remaining_min -= 1;
|
|
|
|
}
|
|
|
|
// store the new length of the
|
|
|
|
// stack, based on our current
|
|
|
|
// position.
|
|
|
|
self.length = (write_ptr as uint
|
|
|
|
- start_ptr as uint) /
|
|
|
|
mem::size_of_val(&*write_ptr);
|
|
|
|
|
|
|
|
return Some((key, value));
|
2014-01-06 08:00:19 -06:00
|
|
|
}
|
2014-01-13 08:37:52 -06:00
|
|
|
Nothing => {}
|
|
|
|
})
|
|
|
|
}
|
2014-01-06 07:14:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-06 08:00:19 -06:00
|
|
|
return None;
|
2014-01-06 07:14:37 -06:00
|
|
|
}
|
|
|
|
|
2014-01-06 08:00:19 -06:00
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
|
|
|
(self.remaining_min, Some(self.remaining_max))
|
|
|
|
}
|
|
|
|
})
|
2014-01-06 07:14:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-14 21:32:24 -06:00
|
|
|
iterator_impl! { Entries, iter = iter, mutability = }
|
|
|
|
iterator_impl! { MutEntries, iter = mut_iter, mutability = mut }
|
2014-01-06 08:00:19 -06:00
|
|
|
|
2013-08-04 17:26:38 -05:00
|
|
|
/// Forward iterator over a set
|
2014-01-14 21:32:24 -06:00
|
|
|
pub struct SetItems<'a> {
|
2014-03-27 17:10:04 -05:00
|
|
|
iter: Entries<'a, ()>
|
2013-08-04 17:26:38 -05:00
|
|
|
}
|
|
|
|
|
2014-01-14 21:32:24 -06:00
|
|
|
impl<'a> Iterator<uint> for SetItems<'a> {
|
2013-08-04 17:26:38 -05:00
|
|
|
fn next(&mut self) -> Option<uint> {
|
2013-11-20 16:17:12 -06:00
|
|
|
self.iter.next().map(|(key, _)| key)
|
2013-08-04 17:26:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
|
|
|
self.iter.size_hint()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-02 13:41:31 -06:00
|
|
|
#[cfg(test)]
|
2013-07-14 11:20:48 -05:00
|
|
|
mod test_map {
|
2014-05-29 21:03:06 -05:00
|
|
|
use std::prelude::*;
|
2014-02-19 21:29:58 -06:00
|
|
|
use std::iter::range_step;
|
|
|
|
use std::uint;
|
|
|
|
|
2014-06-06 18:33:44 -05:00
|
|
|
use {MutableMap, Map};
|
2014-05-29 21:03:06 -05:00
|
|
|
use super::{TrieMap, TrieNode, Internal, External, Nothing};
|
|
|
|
|
2014-02-19 21:29:58 -06:00
|
|
|
fn check_integrity<T>(trie: &TrieNode<T>) {
|
|
|
|
assert!(trie.count != 0);
|
|
|
|
|
|
|
|
let mut sum = 0;
|
|
|
|
|
|
|
|
for x in trie.children.iter() {
|
|
|
|
match *x {
|
|
|
|
Nothing => (),
|
|
|
|
Internal(ref y) => {
|
|
|
|
check_integrity(&**y);
|
|
|
|
sum += 1
|
|
|
|
}
|
|
|
|
External(_, _) => { sum += 1 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(sum, trie.count);
|
|
|
|
}
|
2013-03-02 13:41:31 -06:00
|
|
|
|
2013-03-24 19:30:35 -05:00
|
|
|
#[test]
|
|
|
|
fn test_find_mut() {
|
|
|
|
let mut m = TrieMap::new();
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(m.insert(1, 12));
|
|
|
|
assert!(m.insert(2, 8));
|
|
|
|
assert!(m.insert(5, 14));
|
2013-03-24 19:30:35 -05:00
|
|
|
let new = 100;
|
|
|
|
match m.find_mut(&5) {
|
2013-10-21 15:08:31 -05:00
|
|
|
None => fail!(), Some(x) => *x = new
|
2013-03-24 19:30:35 -05:00
|
|
|
}
|
|
|
|
assert_eq!(m.find(&5), Some(&new));
|
|
|
|
}
|
|
|
|
|
2013-11-24 16:41:33 -06:00
|
|
|
#[test]
|
|
|
|
fn test_find_mut_missing() {
|
|
|
|
let mut m = TrieMap::new();
|
|
|
|
assert!(m.find_mut(&0).is_none());
|
|
|
|
assert!(m.insert(1, 12));
|
|
|
|
assert!(m.find_mut(&0).is_none());
|
|
|
|
assert!(m.insert(2, 8));
|
|
|
|
assert!(m.find_mut(&0).is_none());
|
|
|
|
}
|
|
|
|
|
2013-03-02 13:41:31 -06:00
|
|
|
#[test]
|
|
|
|
fn test_step() {
|
|
|
|
let mut trie = TrieMap::new();
|
2013-09-14 19:46:51 -05:00
|
|
|
let n = 300u;
|
2013-03-02 13:41:31 -06:00
|
|
|
|
2013-09-14 19:46:51 -05:00
|
|
|
for x in range_step(1u, n, 2) {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(trie.insert(x, x + 1));
|
|
|
|
assert!(trie.contains_key(&x));
|
2013-03-02 13:41:31 -06:00
|
|
|
check_integrity(&trie.root);
|
2013-09-14 19:46:51 -05:00
|
|
|
}
|
2013-03-02 13:41:31 -06:00
|
|
|
|
2013-09-14 19:46:51 -05:00
|
|
|
for x in range_step(0u, n, 2) {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(!trie.contains_key(&x));
|
|
|
|
assert!(trie.insert(x, x + 1));
|
2013-03-02 13:41:31 -06:00
|
|
|
check_integrity(&trie.root);
|
2013-09-14 19:46:51 -05:00
|
|
|
}
|
2013-03-02 13:41:31 -06:00
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for x in range(0u, n) {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(trie.contains_key(&x));
|
|
|
|
assert!(!trie.insert(x, x + 1));
|
2013-03-02 13:41:31 -06:00
|
|
|
check_integrity(&trie.root);
|
|
|
|
}
|
|
|
|
|
2013-09-14 19:46:51 -05:00
|
|
|
for x in range_step(1u, n, 2) {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(trie.remove(&x));
|
|
|
|
assert!(!trie.contains_key(&x));
|
2013-03-02 13:41:31 -06:00
|
|
|
check_integrity(&trie.root);
|
2013-09-14 19:46:51 -05:00
|
|
|
}
|
2013-03-02 13:41:31 -06:00
|
|
|
|
2013-09-14 19:46:51 -05:00
|
|
|
for x in range_step(0u, n, 2) {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(trie.contains_key(&x));
|
|
|
|
assert!(!trie.insert(x, x + 1));
|
2013-03-02 13:41:31 -06:00
|
|
|
check_integrity(&trie.root);
|
2013-09-14 19:46:51 -05:00
|
|
|
}
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
2013-03-05 08:35:45 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_each_reverse() {
|
|
|
|
let mut m = TrieMap::new();
|
|
|
|
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(m.insert(3, 6));
|
|
|
|
assert!(m.insert(0, 0));
|
|
|
|
assert!(m.insert(4, 8));
|
|
|
|
assert!(m.insert(2, 4));
|
|
|
|
assert!(m.insert(1, 2));
|
2013-03-05 08:35:45 -06:00
|
|
|
|
|
|
|
let mut n = 4;
|
2013-11-20 16:17:12 -06:00
|
|
|
m.each_reverse(|k, v| {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(*k, n);
|
|
|
|
assert_eq!(*v, n * 2);
|
2013-03-05 08:35:45 -06:00
|
|
|
n -= 1;
|
2013-07-31 14:07:44 -05:00
|
|
|
true
|
2013-11-20 16:17:12 -06:00
|
|
|
});
|
2013-03-05 08:35:45 -06:00
|
|
|
}
|
2013-03-05 09:10:22 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_each_reverse_break() {
|
|
|
|
let mut m = TrieMap::new();
|
|
|
|
|
2014-01-25 01:37:51 -06:00
|
|
|
for x in range(uint::MAX - 10000, uint::MAX).rev() {
|
2013-03-05 09:10:22 -06:00
|
|
|
m.insert(x, x / 2);
|
2013-08-07 00:34:22 -05:00
|
|
|
}
|
2013-03-05 09:10:22 -06:00
|
|
|
|
2014-01-25 01:37:51 -06:00
|
|
|
let mut n = uint::MAX - 1;
|
2013-11-20 16:17:12 -06:00
|
|
|
m.each_reverse(|k, v| {
|
2014-01-25 01:37:51 -06:00
|
|
|
if n == uint::MAX - 5000 { false } else {
|
|
|
|
assert!(n > uint::MAX - 5000);
|
2013-07-31 14:07:44 -05:00
|
|
|
|
|
|
|
assert_eq!(*k, n);
|
|
|
|
assert_eq!(*v, n / 2);
|
|
|
|
n -= 1;
|
|
|
|
true
|
|
|
|
}
|
2013-11-20 16:17:12 -06:00
|
|
|
});
|
2013-03-05 09:10:22 -06:00
|
|
|
}
|
2013-03-15 20:43:54 -05:00
|
|
|
|
2013-07-14 11:20:48 -05:00
|
|
|
#[test]
|
|
|
|
fn test_swap() {
|
|
|
|
let mut m = TrieMap::new();
|
|
|
|
assert_eq!(m.swap(1, 2), None);
|
|
|
|
assert_eq!(m.swap(1, 3), Some(2));
|
|
|
|
assert_eq!(m.swap(1, 4), Some(3));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pop() {
|
|
|
|
let mut m = TrieMap::new();
|
|
|
|
m.insert(1, 2);
|
|
|
|
assert_eq!(m.pop(&1), Some(2));
|
|
|
|
assert_eq!(m.pop(&1), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from_iter() {
|
2014-04-05 00:45:42 -05:00
|
|
|
let xs = vec![(1u, 1i), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
|
2013-07-14 12:18:50 -05:00
|
|
|
|
2013-08-09 22:09:47 -05:00
|
|
|
let map: TrieMap<int> = xs.iter().map(|&x| x).collect();
|
2013-07-14 11:20:48 -05:00
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for &(k, v) in xs.iter() {
|
2013-07-14 11:20:48 -05:00
|
|
|
assert_eq!(map.find(&k), Some(&v));
|
|
|
|
}
|
|
|
|
}
|
2013-08-04 17:26:38 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iteration() {
|
|
|
|
let empty_map : TrieMap<uint> = TrieMap::new();
|
|
|
|
assert_eq!(empty_map.iter().next(), None);
|
|
|
|
|
2014-01-25 01:37:51 -06:00
|
|
|
let first = uint::MAX - 10000;
|
|
|
|
let last = uint::MAX;
|
2013-08-04 17:26:38 -05:00
|
|
|
|
|
|
|
let mut map = TrieMap::new();
|
2014-01-23 13:41:57 -06:00
|
|
|
for x in range(first, last).rev() {
|
2013-08-04 17:26:38 -05:00
|
|
|
map.insert(x, x / 2);
|
2013-08-07 00:34:22 -05:00
|
|
|
}
|
2013-08-04 17:26:38 -05:00
|
|
|
|
|
|
|
let mut i = 0;
|
|
|
|
for (k, &v) in map.iter() {
|
|
|
|
assert_eq!(k, first + i);
|
|
|
|
assert_eq!(v, k / 2);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(i, last - first);
|
|
|
|
}
|
2013-08-07 17:37:13 -05:00
|
|
|
|
2014-01-06 07:14:37 -06:00
|
|
|
#[test]
|
|
|
|
fn test_mut_iter() {
|
|
|
|
let mut empty_map : TrieMap<uint> = TrieMap::new();
|
|
|
|
assert!(empty_map.mut_iter().next().is_none());
|
|
|
|
|
2014-01-25 01:37:51 -06:00
|
|
|
let first = uint::MAX - 10000;
|
|
|
|
let last = uint::MAX;
|
2014-01-06 07:14:37 -06:00
|
|
|
|
|
|
|
let mut map = TrieMap::new();
|
2014-01-23 13:41:57 -06:00
|
|
|
for x in range(first, last).rev() {
|
2014-01-06 07:14:37 -06:00
|
|
|
map.insert(x, x / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut i = 0;
|
|
|
|
for (k, v) in map.mut_iter() {
|
|
|
|
assert_eq!(k, first + i);
|
|
|
|
*v -= k / 2;
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(i, last - first);
|
|
|
|
|
|
|
|
assert!(map.iter().all(|(_, &v)| v == 0));
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:37:13 -05:00
|
|
|
#[test]
|
2013-11-23 04:18:51 -06:00
|
|
|
fn test_bound() {
|
2013-08-07 17:37:13 -05:00
|
|
|
let empty_map : TrieMap<uint> = TrieMap::new();
|
2013-11-23 04:18:51 -06:00
|
|
|
assert_eq!(empty_map.lower_bound(0).next(), None);
|
|
|
|
assert_eq!(empty_map.upper_bound(0).next(), None);
|
2013-08-07 17:37:13 -05:00
|
|
|
|
|
|
|
let last = 999u;
|
|
|
|
let step = 3u;
|
|
|
|
let value = 42u;
|
|
|
|
|
|
|
|
let mut map : TrieMap<uint> = TrieMap::new();
|
2013-09-14 19:46:51 -05:00
|
|
|
for x in range_step(0u, last, step) {
|
2013-08-07 17:37:13 -05:00
|
|
|
assert!(x % step == 0);
|
|
|
|
map.insert(x, value);
|
2013-09-14 19:46:51 -05:00
|
|
|
}
|
2013-08-07 17:37:13 -05:00
|
|
|
|
|
|
|
for i in range(0u, last - step) {
|
2013-11-23 04:18:51 -06:00
|
|
|
let mut lb = map.lower_bound(i);
|
|
|
|
let mut ub = map.upper_bound(i);
|
2013-08-07 17:37:13 -05:00
|
|
|
let next_key = i - i % step + step;
|
|
|
|
let next_pair = (next_key, &value);
|
2014-01-19 02:21:14 -06:00
|
|
|
if i % step == 0 {
|
2013-08-07 17:37:13 -05:00
|
|
|
assert_eq!(lb.next(), Some((i, &value)));
|
|
|
|
} else {
|
|
|
|
assert_eq!(lb.next(), Some(next_pair));
|
|
|
|
}
|
|
|
|
assert_eq!(ub.next(), Some(next_pair));
|
|
|
|
}
|
|
|
|
|
2013-11-23 04:18:51 -06:00
|
|
|
let mut lb = map.lower_bound(last - step);
|
2013-08-07 17:37:13 -05:00
|
|
|
assert_eq!(lb.next(), Some((last - step, &value)));
|
2013-11-23 04:18:51 -06:00
|
|
|
let mut ub = map.upper_bound(last - step);
|
2013-08-07 17:37:13 -05:00
|
|
|
assert_eq!(ub.next(), None);
|
|
|
|
|
|
|
|
for i in range(last - step + 1, last) {
|
2013-11-23 04:18:51 -06:00
|
|
|
let mut lb = map.lower_bound(i);
|
2013-08-07 17:37:13 -05:00
|
|
|
assert_eq!(lb.next(), None);
|
2013-11-23 04:18:51 -06:00
|
|
|
let mut ub = map.upper_bound(i);
|
2013-08-07 17:37:13 -05:00
|
|
|
assert_eq!(ub.next(), None);
|
|
|
|
}
|
|
|
|
}
|
2014-01-06 07:14:37 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mut_bound() {
|
|
|
|
let empty_map : TrieMap<uint> = TrieMap::new();
|
|
|
|
assert_eq!(empty_map.lower_bound(0).next(), None);
|
|
|
|
assert_eq!(empty_map.upper_bound(0).next(), None);
|
|
|
|
|
|
|
|
let mut m_lower = TrieMap::new();
|
|
|
|
let mut m_upper = TrieMap::new();
|
|
|
|
for i in range(0u, 100) {
|
|
|
|
m_lower.insert(2 * i, 4 * i);
|
|
|
|
m_upper.insert(2 * i, 4 * i);
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in range(0u, 199) {
|
|
|
|
let mut lb_it = m_lower.mut_lower_bound(i);
|
|
|
|
let (k, v) = lb_it.next().unwrap();
|
|
|
|
let lb = i + i % 2;
|
|
|
|
assert_eq!(lb, k);
|
|
|
|
*v -= k;
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in range(0u, 198) {
|
|
|
|
let mut ub_it = m_upper.mut_upper_bound(i);
|
|
|
|
let (k, v) = ub_it.next().unwrap();
|
|
|
|
let ub = i + 2 - i % 2;
|
|
|
|
assert_eq!(ub, k);
|
|
|
|
*v -= k;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(m_lower.mut_lower_bound(199).next().is_none());
|
|
|
|
assert!(m_upper.mut_upper_bound(198).next().is_none());
|
|
|
|
|
|
|
|
assert!(m_lower.iter().all(|(_, &x)| x == 0));
|
|
|
|
assert!(m_upper.iter().all(|(_, &x)| x == 0));
|
|
|
|
}
|
2013-07-14 11:20:48 -05:00
|
|
|
}
|
|
|
|
|
2014-01-06 08:42:55 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod bench_map {
|
2014-05-29 21:03:06 -05:00
|
|
|
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::{weak_rng, Rng};
|
2014-05-29 21:03:06 -05:00
|
|
|
use test::Bencher;
|
|
|
|
|
2014-06-06 18:33:44 -05:00
|
|
|
use MutableMap;
|
2014-05-29 21:03:06 -05:00
|
|
|
use super::TrieMap;
|
2014-01-06 08:42:55 -06:00
|
|
|
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
fn bench_iter_small(b: &mut Bencher) {
|
2014-01-06 08:42:55 -06:00
|
|
|
let mut m = TrieMap::<uint>::new();
|
|
|
|
let mut rng = weak_rng();
|
|
|
|
for _ in range(0, 20) {
|
|
|
|
m.insert(rng.gen(), rng.gen());
|
|
|
|
}
|
|
|
|
|
2014-03-31 20:16:35 -05:00
|
|
|
b.iter(|| for _ in m.iter() {})
|
2014-01-06 08:42:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
fn bench_iter_large(b: &mut Bencher) {
|
2014-01-06 08:42:55 -06:00
|
|
|
let mut m = TrieMap::<uint>::new();
|
|
|
|
let mut rng = weak_rng();
|
|
|
|
for _ in range(0, 1000) {
|
|
|
|
m.insert(rng.gen(), rng.gen());
|
|
|
|
}
|
|
|
|
|
2014-03-31 20:16:35 -05:00
|
|
|
b.iter(|| for _ in m.iter() {})
|
2014-01-06 08:42:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
fn bench_lower_bound(b: &mut Bencher) {
|
2014-01-06 08:42:55 -06:00
|
|
|
let mut m = TrieMap::<uint>::new();
|
|
|
|
let mut rng = weak_rng();
|
|
|
|
for _ in range(0, 1000) {
|
|
|
|
m.insert(rng.gen(), rng.gen());
|
|
|
|
}
|
|
|
|
|
2014-03-31 20:16:35 -05:00
|
|
|
b.iter(|| {
|
2014-01-06 08:42:55 -06:00
|
|
|
for _ in range(0, 10) {
|
|
|
|
m.lower_bound(rng.gen());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
fn bench_upper_bound(b: &mut Bencher) {
|
2014-01-06 08:42:55 -06:00
|
|
|
let mut m = TrieMap::<uint>::new();
|
|
|
|
let mut rng = weak_rng();
|
|
|
|
for _ in range(0, 1000) {
|
|
|
|
m.insert(rng.gen(), rng.gen());
|
|
|
|
}
|
|
|
|
|
2014-03-31 20:16:35 -05:00
|
|
|
b.iter(|| {
|
2014-01-06 08:42:55 -06:00
|
|
|
for _ in range(0, 10) {
|
|
|
|
m.upper_bound(rng.gen());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-01-14 18:32:53 -06:00
|
|
|
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
fn bench_insert_large(b: &mut Bencher) {
|
2014-01-14 18:32:53 -06:00
|
|
|
let mut m = TrieMap::<[uint, .. 10]>::new();
|
|
|
|
let mut rng = weak_rng();
|
|
|
|
|
2014-03-31 20:16:35 -05:00
|
|
|
b.iter(|| {
|
2014-01-14 18:32:53 -06:00
|
|
|
for _ in range(0, 1000) {
|
|
|
|
m.insert(rng.gen(), [1, .. 10]);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
fn bench_insert_large_low_bits(b: &mut Bencher) {
|
2014-01-14 18:32:53 -06:00
|
|
|
let mut m = TrieMap::<[uint, .. 10]>::new();
|
|
|
|
let mut rng = weak_rng();
|
|
|
|
|
2014-03-31 20:16:35 -05:00
|
|
|
b.iter(|| {
|
2014-01-14 18:32:53 -06:00
|
|
|
for _ in range(0, 1000) {
|
|
|
|
// only have the last few bits set.
|
|
|
|
m.insert(rng.gen::<uint>() & 0xff_ff, [1, .. 10]);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
fn bench_insert_small(b: &mut Bencher) {
|
2014-01-14 18:32:53 -06:00
|
|
|
let mut m = TrieMap::<()>::new();
|
|
|
|
let mut rng = weak_rng();
|
|
|
|
|
2014-03-31 20:16:35 -05:00
|
|
|
b.iter(|| {
|
2014-01-14 18:32:53 -06:00
|
|
|
for _ in range(0, 1000) {
|
|
|
|
m.insert(rng.gen(), ());
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
fn bench_insert_small_low_bits(b: &mut Bencher) {
|
2014-01-14 18:32:53 -06:00
|
|
|
let mut m = TrieMap::<()>::new();
|
|
|
|
let mut rng = weak_rng();
|
|
|
|
|
2014-03-31 20:16:35 -05:00
|
|
|
b.iter(|| {
|
2014-01-14 18:32:53 -06:00
|
|
|
for _ in range(0, 1000) {
|
|
|
|
// only have the last few bits set.
|
|
|
|
m.insert(rng.gen::<uint>() & 0xff_ff, ());
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2014-01-06 08:42:55 -06:00
|
|
|
}
|
|
|
|
|
2013-07-14 11:20:48 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test_set {
|
2014-05-29 21:03:06 -05:00
|
|
|
use std::prelude::*;
|
2014-02-19 21:29:58 -06:00
|
|
|
use std::uint;
|
2013-07-14 11:20:48 -05:00
|
|
|
|
2014-06-06 18:33:44 -05:00
|
|
|
use {MutableSet, Set};
|
2014-05-29 21:03:06 -05:00
|
|
|
use super::TrieSet;
|
|
|
|
|
2013-03-15 20:43:54 -05:00
|
|
|
#[test]
|
|
|
|
fn test_sane_chunk() {
|
|
|
|
let x = 1;
|
2014-01-25 01:37:51 -06:00
|
|
|
let y = 1 << (uint::BITS - 1);
|
2013-03-15 20:43:54 -05:00
|
|
|
|
|
|
|
let mut trie = TrieSet::new();
|
|
|
|
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(trie.insert(x));
|
|
|
|
assert!(trie.insert(y));
|
2013-03-15 20:43:54 -05:00
|
|
|
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(trie.len(), 2);
|
2013-03-15 20:43:54 -05:00
|
|
|
|
|
|
|
let expected = [x, y];
|
|
|
|
|
2014-01-06 07:17:38 -06:00
|
|
|
for (i, x) in trie.iter().enumerate() {
|
|
|
|
assert_eq!(expected[i], x);
|
|
|
|
}
|
2013-03-15 20:43:54 -05:00
|
|
|
}
|
2013-05-04 08:54:58 -05:00
|
|
|
|
|
|
|
#[test]
|
2013-07-14 11:20:48 -05:00
|
|
|
fn test_from_iter() {
|
2014-04-05 00:45:42 -05:00
|
|
|
let xs = vec![9u, 8, 7, 6, 5, 4, 3, 2, 1];
|
2013-05-04 08:54:58 -05:00
|
|
|
|
2013-08-09 22:09:47 -05:00
|
|
|
let set: TrieSet = xs.iter().map(|&x| x).collect();
|
2013-05-04 08:54:58 -05:00
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for x in xs.iter() {
|
2013-07-14 11:20:48 -05:00
|
|
|
assert!(set.contains(x));
|
|
|
|
}
|
2013-05-04 08:54:58 -05:00
|
|
|
}
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|