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
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-01-31 17:56:12 -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.
|
|
|
|
|
2014-07-24 08:22:24 -05:00
|
|
|
//! A simple map based on a vector for small integer keys. Space requirements
|
|
|
|
//! are O(highest integer key).
|
2013-01-31 17:56:12 -06:00
|
|
|
|
2014-10-27 17:37:07 -05:00
|
|
|
#![allow(missing_docs)]
|
2013-05-31 17:17:22 -05: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::*;
|
|
|
|
|
2014-06-09 02:30:04 -05:00
|
|
|
use core::default::Default;
|
2014-06-07 11:17:58 -05:00
|
|
|
use core::fmt;
|
2014-07-24 23:48:05 -05:00
|
|
|
use core::iter;
|
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::iter::{Enumerate, FilterMap};
|
|
|
|
use core::mem::replace;
|
|
|
|
|
|
|
|
use {vec, slice};
|
|
|
|
use vec::Vec;
|
2014-07-26 11:04:41 -05:00
|
|
|
use hash;
|
|
|
|
use hash::Hash;
|
2013-01-31 17:56:12 -06:00
|
|
|
|
2014-11-06 11:24:47 -06:00
|
|
|
// FIXME(conventions): capacity management???
|
|
|
|
|
2014-07-24 08:46:55 -05:00
|
|
|
/// A map optimized for small integer keys.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2014-10-30 20:25:08 -05:00
|
|
|
/// use std::collections::VecMap;
|
2014-07-24 08:46:55 -05:00
|
|
|
///
|
2014-10-30 20:25:08 -05:00
|
|
|
/// let mut months = VecMap::new();
|
2014-07-24 08:46:55 -05:00
|
|
|
/// months.insert(1, "Jan");
|
|
|
|
/// months.insert(2, "Feb");
|
|
|
|
/// months.insert(3, "Mar");
|
|
|
|
///
|
|
|
|
/// if !months.contains_key(&12) {
|
|
|
|
/// println!("The end is near!");
|
|
|
|
/// }
|
|
|
|
///
|
2014-11-06 11:24:47 -06:00
|
|
|
/// assert_eq!(months.get(&1), Some(&"Jan"));
|
2014-07-24 08:46:55 -05:00
|
|
|
///
|
2014-11-06 11:24:47 -06:00
|
|
|
/// match months.get_mut(&3) {
|
2014-07-24 08:46:55 -05:00
|
|
|
/// Some(value) => *value = "Venus",
|
|
|
|
/// None => (),
|
|
|
|
/// }
|
|
|
|
///
|
2014-11-06 11:24:47 -06:00
|
|
|
/// assert_eq!(months.get(&3), Some(&"Venus"));
|
2014-07-24 08:46:55 -05:00
|
|
|
///
|
|
|
|
/// // Print out all months
|
|
|
|
/// for (key, value) in months.iter() {
|
|
|
|
/// println!("month {} is {}", key, value);
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// months.clear();
|
|
|
|
/// assert!(months.is_empty());
|
|
|
|
/// ```
|
2014-07-26 11:04:41 -05:00
|
|
|
#[deriving(PartialEq, Eq)]
|
2014-10-30 20:25:08 -05:00
|
|
|
pub struct VecMap<T> {
|
2014-04-05 00:45:42 -05:00
|
|
|
v: Vec<Option<T>>,
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
|
|
|
|
2014-10-30 20:25:08 -05:00
|
|
|
impl<V> Default for VecMap<V> {
|
2014-06-09 02:30:04 -05:00
|
|
|
#[inline]
|
2014-10-30 20:25:08 -05:00
|
|
|
fn default() -> VecMap<V> { VecMap::new() }
|
2014-06-09 02:30:04 -05:00
|
|
|
}
|
|
|
|
|
2014-10-30 20:25:08 -05:00
|
|
|
impl<V:Clone> Clone for VecMap<V> {
|
2014-07-24 16:50:21 -05:00
|
|
|
#[inline]
|
2014-10-30 20:25:08 -05:00
|
|
|
fn clone(&self) -> VecMap<V> {
|
|
|
|
VecMap { v: self.v.clone() }
|
2014-07-24 16:50:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-10-30 20:25:08 -05:00
|
|
|
fn clone_from(&mut self, source: &VecMap<V>) {
|
2014-11-06 11:24:47 -06:00
|
|
|
self.v.clone_from(&source.v);
|
2014-07-24 16:50:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-30 20:25:08 -05:00
|
|
|
impl <S: hash::Writer, T: Hash<S>> Hash<S> for VecMap<T> {
|
2014-07-26 11:04:41 -05:00
|
|
|
fn hash(&self, state: &mut S) {
|
|
|
|
self.v.hash(state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-30 20:25:08 -05:00
|
|
|
impl<V> VecMap<V> {
|
|
|
|
/// Creates an empty `VecMap`.
|
2014-07-24 08:46:55 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2014-10-30 20:25:08 -05:00
|
|
|
/// use std::collections::VecMap;
|
|
|
|
/// let mut map: VecMap<&str> = VecMap::new();
|
2014-07-24 08:46:55 -05:00
|
|
|
/// ```
|
2014-11-06 11:24:47 -06:00
|
|
|
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
2014-10-30 20:25:08 -05:00
|
|
|
pub fn new() -> VecMap<V> { VecMap{v: vec!()} }
|
2013-01-31 18:07:08 -06:00
|
|
|
|
2014-10-30 20:25:08 -05:00
|
|
|
/// Creates an empty `VecMap` with space for at least `capacity`
|
2014-08-04 05:48:39 -05:00
|
|
|
/// elements before resizing.
|
2014-07-24 08:46:55 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2014-10-30 20:25:08 -05:00
|
|
|
/// use std::collections::VecMap;
|
|
|
|
/// let mut map: VecMap<&str> = VecMap::with_capacity(10);
|
2014-07-24 08:46:55 -05:00
|
|
|
/// ```
|
2014-11-06 11:24:47 -06:00
|
|
|
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
2014-10-30 20:25:08 -05:00
|
|
|
pub fn with_capacity(capacity: uint) -> VecMap<V> {
|
|
|
|
VecMap { v: Vec::with_capacity(capacity) }
|
2014-04-03 22:47:53 -05:00
|
|
|
}
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Returns an iterator visiting all keys in ascending order by the keys.
|
|
|
|
/// The iterator's element type is `uint`.
|
2014-11-06 11:24:47 -06:00
|
|
|
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
2014-07-24 23:48:05 -05:00
|
|
|
pub fn keys<'r>(&'r self) -> Keys<'r, V> {
|
|
|
|
self.iter().map(|(k, _v)| k)
|
|
|
|
}
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Returns an iterator visiting all values in ascending order by the keys.
|
|
|
|
/// The iterator's element type is `&'r V`.
|
2014-11-06 11:24:47 -06:00
|
|
|
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
2014-07-24 23:48:05 -05:00
|
|
|
pub fn values<'r>(&'r self) -> Values<'r, V> {
|
|
|
|
self.iter().map(|(_k, v)| v)
|
|
|
|
}
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Returns an iterator visiting all key-value pairs in ascending order by the keys.
|
|
|
|
/// The iterator's element type is `(uint, &'r V)`.
|
2014-07-24 08:46:55 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2014-10-30 20:25:08 -05:00
|
|
|
/// use std::collections::VecMap;
|
2014-07-24 08:46:55 -05:00
|
|
|
///
|
2014-10-30 20:25:08 -05:00
|
|
|
/// let mut map = VecMap::new();
|
2014-07-24 08:46:55 -05:00
|
|
|
/// map.insert(1, "a");
|
|
|
|
/// map.insert(3, "c");
|
|
|
|
/// map.insert(2, "b");
|
|
|
|
///
|
|
|
|
/// // Print `1: a` then `2: b` then `3: c`
|
|
|
|
/// for (key, value) in map.iter() {
|
|
|
|
/// println!("{}: {}", key, value);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2014-11-06 11:24:47 -06:00
|
|
|
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
2014-01-14 21:32:24 -06:00
|
|
|
pub fn iter<'r>(&'r self) -> Entries<'r, V> {
|
|
|
|
Entries {
|
2013-07-19 19:29:54 -05:00
|
|
|
front: 0,
|
|
|
|
back: self.v.len(),
|
|
|
|
iter: self.v.iter()
|
2013-07-04 18:18:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Returns an iterator visiting all key-value pairs in ascending order by the keys,
|
|
|
|
/// with mutable references to the values.
|
|
|
|
/// The iterator's element type is `(uint, &'r mut V)`.
|
2014-07-24 08:46:55 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2014-10-30 20:25:08 -05:00
|
|
|
/// use std::collections::VecMap;
|
2014-07-24 08:46:55 -05:00
|
|
|
///
|
2014-10-30 20:25:08 -05:00
|
|
|
/// let mut map = VecMap::new();
|
2014-07-24 08:46:55 -05:00
|
|
|
/// map.insert(1, "a");
|
|
|
|
/// map.insert(2, "b");
|
|
|
|
/// map.insert(3, "c");
|
|
|
|
///
|
2014-09-14 22:27:36 -05:00
|
|
|
/// for (key, value) in map.iter_mut() {
|
2014-07-24 08:46:55 -05:00
|
|
|
/// *value = "x";
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// for (key, value) in map.iter() {
|
|
|
|
/// assert_eq!(value, &"x");
|
|
|
|
/// }
|
|
|
|
/// ```
|
2014-11-06 11:24:47 -06:00
|
|
|
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
2014-09-14 17:57:55 -05:00
|
|
|
pub fn iter_mut<'r>(&'r mut self) -> MutEntries<'r, V> {
|
2014-01-14 21:32:24 -06:00
|
|
|
MutEntries {
|
2013-07-19 19:29:54 -05:00
|
|
|
front: 0,
|
|
|
|
back: self.v.len(),
|
2014-09-14 22:27:36 -05:00
|
|
|
iter: self.v.iter_mut()
|
2013-07-04 18:18:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Returns an iterator visiting all key-value pairs in ascending order by
|
2014-10-30 20:25:08 -05:00
|
|
|
/// the keys, emptying (but not consuming) the original `VecMap`.
|
2014-08-04 05:48:39 -05:00
|
|
|
/// The iterator's element type is `(uint, &'r V)`.
|
2014-07-24 08:46:55 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2014-10-30 20:25:08 -05:00
|
|
|
/// use std::collections::VecMap;
|
2014-07-24 08:46:55 -05:00
|
|
|
///
|
2014-10-30 20:25:08 -05:00
|
|
|
/// let mut map = VecMap::new();
|
2014-07-24 08:46:55 -05:00
|
|
|
/// map.insert(1, "a");
|
|
|
|
/// map.insert(3, "c");
|
|
|
|
/// map.insert(2, "b");
|
|
|
|
///
|
|
|
|
/// // Not possible with .iter()
|
2014-09-14 22:27:36 -05:00
|
|
|
/// let vec: Vec<(uint, &str)> = map.into_iter().collect();
|
2014-07-24 08:46:55 -05:00
|
|
|
///
|
|
|
|
/// assert_eq!(vec, vec![(1, "a"), (2, "b"), (3, "c")]);
|
|
|
|
/// ```
|
2014-11-06 11:24:47 -06:00
|
|
|
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
2014-09-14 17:57:55 -05:00
|
|
|
pub fn into_iter(&mut self)
|
2013-07-27 16:41:20 -05:00
|
|
|
-> FilterMap<(uint, Option<V>), (uint, V),
|
2014-04-05 00:45:42 -05:00
|
|
|
Enumerate<vec::MoveItems<Option<V>>>>
|
2013-07-08 23:16:23 -05:00
|
|
|
{
|
2014-04-05 00:45:42 -05:00
|
|
|
let values = replace(&mut self.v, vec!());
|
2014-09-14 22:27:36 -05:00
|
|
|
values.into_iter().enumerate().filter_map(|(i, v)| {
|
2013-09-20 01:08:47 -05:00
|
|
|
v.map(|v| (i, v))
|
2013-07-08 23:16:23 -05:00
|
|
|
})
|
|
|
|
}
|
2014-10-30 15:43:24 -05:00
|
|
|
|
|
|
|
/// Return the number of elements in the map.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2014-10-30 20:25:08 -05:00
|
|
|
/// use std::collections::VecMap;
|
2014-10-30 15:43:24 -05:00
|
|
|
///
|
2014-10-30 20:25:08 -05:00
|
|
|
/// let mut a = VecMap::new();
|
2014-10-30 15:43:24 -05:00
|
|
|
/// assert_eq!(a.len(), 0);
|
|
|
|
/// a.insert(1, "a");
|
|
|
|
/// assert_eq!(a.len(), 1);
|
|
|
|
/// ```
|
2014-11-06 11:24:47 -06:00
|
|
|
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
2014-10-30 15:43:24 -05:00
|
|
|
pub fn len(&self) -> uint {
|
|
|
|
self.v.iter().filter(|elt| elt.is_some()).count()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return true if the map contains no elements.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2014-10-30 20:25:08 -05:00
|
|
|
/// use std::collections::VecMap;
|
2014-10-30 15:43:24 -05:00
|
|
|
///
|
2014-10-30 20:25:08 -05:00
|
|
|
/// let mut a = VecMap::new();
|
2014-10-30 15:43:24 -05:00
|
|
|
/// assert!(a.is_empty());
|
|
|
|
/// a.insert(1, "a");
|
|
|
|
/// assert!(!a.is_empty());
|
|
|
|
/// ```
|
2014-11-06 11:24:47 -06:00
|
|
|
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
2014-10-30 15:43:24 -05:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.v.iter().all(|elt| elt.is_none())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clears the map, removing all key-value pairs.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2014-10-30 20:25:08 -05:00
|
|
|
/// use std::collections::VecMap;
|
2014-10-30 15:43:24 -05:00
|
|
|
///
|
2014-10-30 20:25:08 -05:00
|
|
|
/// let mut a = VecMap::new();
|
2014-10-30 15:43:24 -05:00
|
|
|
/// a.insert(1, "a");
|
|
|
|
/// a.clear();
|
|
|
|
/// assert!(a.is_empty());
|
|
|
|
/// ```
|
2014-11-06 11:24:47 -06:00
|
|
|
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
2014-10-30 15:43:24 -05:00
|
|
|
pub fn clear(&mut self) { self.v.clear() }
|
|
|
|
|
2014-11-06 11:24:47 -06:00
|
|
|
/// Deprecated: Renamed to `get`.
|
|
|
|
#[deprecated = "Renamed to `get`"]
|
|
|
|
pub fn find(&self, key: &uint) -> Option<&V> {
|
|
|
|
self.get(key)
|
|
|
|
}
|
|
|
|
|
2014-10-30 15:43:24 -05:00
|
|
|
/// Returns a reference to the value corresponding to the key.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2014-10-30 20:25:08 -05:00
|
|
|
/// use std::collections::VecMap;
|
2014-10-30 15:43:24 -05:00
|
|
|
///
|
2014-10-30 20:25:08 -05:00
|
|
|
/// let mut map = VecMap::new();
|
2014-10-30 15:43:24 -05:00
|
|
|
/// map.insert(1, "a");
|
2014-11-06 11:24:47 -06:00
|
|
|
/// assert_eq!(map.get(&1), Some(&"a"));
|
|
|
|
/// assert_eq!(map.get(&2), None);
|
2014-10-30 15:43:24 -05:00
|
|
|
/// ```
|
2014-11-06 11:24:47 -06:00
|
|
|
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
|
|
|
pub fn get(&self, key: &uint) -> Option<&V> {
|
2014-10-30 15:43:24 -05:00
|
|
|
if *key < self.v.len() {
|
|
|
|
match self.v[*key] {
|
|
|
|
Some(ref value) => Some(value),
|
|
|
|
None => None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if the map contains a value for the specified key.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2014-10-30 20:25:08 -05:00
|
|
|
/// use std::collections::VecMap;
|
2014-10-30 15:43:24 -05:00
|
|
|
///
|
2014-10-30 20:25:08 -05:00
|
|
|
/// let mut map = VecMap::new();
|
2014-10-30 15:43:24 -05:00
|
|
|
/// map.insert(1, "a");
|
|
|
|
/// assert_eq!(map.contains_key(&1), true);
|
|
|
|
/// assert_eq!(map.contains_key(&2), false);
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
2014-11-06 11:24:47 -06:00
|
|
|
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
2014-10-30 15:43:24 -05:00
|
|
|
pub fn contains_key(&self, key: &uint) -> bool {
|
2014-11-06 11:24:47 -06:00
|
|
|
self.get(key).is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Deprecated: Renamed to `get_mut`.
|
|
|
|
#[deprecated = "Renamed to `get_mut`"]
|
|
|
|
pub fn find_mut(&mut self, key: &uint) -> Option<&mut V> {
|
|
|
|
self.get_mut(key)
|
2014-10-30 15:43:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a mutable reference to the value corresponding to the key.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2014-10-30 20:25:08 -05:00
|
|
|
/// use std::collections::VecMap;
|
2014-10-30 15:43:24 -05:00
|
|
|
///
|
2014-10-30 20:25:08 -05:00
|
|
|
/// let mut map = VecMap::new();
|
2014-10-30 15:43:24 -05:00
|
|
|
/// map.insert(1, "a");
|
2014-11-06 11:24:47 -06:00
|
|
|
/// match map.get_mut(&1) {
|
2014-10-30 15:43:24 -05:00
|
|
|
/// Some(x) => *x = "b",
|
|
|
|
/// None => (),
|
|
|
|
/// }
|
|
|
|
/// assert_eq!(map[1], "b");
|
|
|
|
/// ```
|
2014-11-06 11:24:47 -06:00
|
|
|
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
|
|
|
pub fn get_mut(&mut self, key: &uint) -> Option<&mut V> {
|
2014-10-30 15:43:24 -05:00
|
|
|
if *key < self.v.len() {
|
|
|
|
match *(&mut self.v[*key]) {
|
|
|
|
Some(ref mut value) => Some(value),
|
|
|
|
None => None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-06 11:24:47 -06:00
|
|
|
/// Deprecated: Renamed to `insert`.
|
|
|
|
#[deprecated = "Renamed to `insert`"]
|
|
|
|
pub fn swap(&mut self, key: uint, value: V) -> Option<V> {
|
|
|
|
self.insert(key, value)
|
2014-10-30 15:43:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Inserts 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.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2014-10-30 20:25:08 -05:00
|
|
|
/// use std::collections::VecMap;
|
2014-10-30 15:43:24 -05:00
|
|
|
///
|
2014-10-30 20:25:08 -05:00
|
|
|
/// let mut map = VecMap::new();
|
2014-11-06 11:24:47 -06:00
|
|
|
/// assert_eq!(map.insert(37, "a"), None);
|
2014-10-30 15:43:24 -05:00
|
|
|
/// assert_eq!(map.is_empty(), false);
|
|
|
|
///
|
|
|
|
/// map.insert(37, "b");
|
2014-11-06 11:24:47 -06:00
|
|
|
/// assert_eq!(map.insert(37, "c"), Some("b"));
|
2014-10-30 15:43:24 -05:00
|
|
|
/// assert_eq!(map[37], "c");
|
|
|
|
/// ```
|
2014-11-06 11:24:47 -06:00
|
|
|
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
|
|
|
pub fn insert(&mut self, key: uint, value: V) -> Option<V> {
|
|
|
|
let len = self.v.len();
|
|
|
|
if len <= key {
|
|
|
|
self.v.grow_fn(key - len + 1, |_| None);
|
2014-10-30 15:43:24 -05:00
|
|
|
}
|
2014-11-06 11:24:47 -06:00
|
|
|
replace(&mut self.v[key], Some(value))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Deprecated: Renamed to `remove`.
|
|
|
|
#[deprecated = "Renamed to `remove`"]
|
|
|
|
pub fn pop(&mut self, key: &uint) -> Option<V> {
|
|
|
|
self.remove(key)
|
2014-10-30 15:43:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes a key from the map, returning the value at the key if the key
|
|
|
|
/// was previously in the map.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2014-10-30 20:25:08 -05:00
|
|
|
/// use std::collections::VecMap;
|
2014-10-30 15:43:24 -05:00
|
|
|
///
|
2014-10-30 20:25:08 -05:00
|
|
|
/// let mut map = VecMap::new();
|
2014-10-30 15:43:24 -05:00
|
|
|
/// map.insert(1, "a");
|
2014-11-06 11:24:47 -06:00
|
|
|
/// assert_eq!(map.remove(&1), Some("a"));
|
|
|
|
/// assert_eq!(map.remove(&1), None);
|
2014-10-30 15:43:24 -05:00
|
|
|
/// ```
|
2014-11-06 11:24:47 -06:00
|
|
|
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
|
|
|
pub fn remove(&mut self, key: &uint) -> Option<V> {
|
2014-10-30 15:43:24 -05:00
|
|
|
if *key >= self.v.len() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
self.v[*key].take()
|
|
|
|
}
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
|
|
|
|
2014-10-30 20:25:08 -05:00
|
|
|
impl<V:Clone> VecMap<V> {
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Updates a value in the map. If the key already exists in the map,
|
|
|
|
/// modifies the value with `ff` taking `oldval, newval`.
|
|
|
|
/// Otherwise, sets the value to `newval`.
|
2014-09-02 00:35:58 -05:00
|
|
|
/// Returns `true` if the key did not already exist in the map.
|
2014-07-24 09:28:34 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2014-10-30 20:25:08 -05:00
|
|
|
/// use std::collections::VecMap;
|
2014-07-24 09:28:34 -05:00
|
|
|
///
|
2014-10-30 20:25:08 -05:00
|
|
|
/// let mut map = VecMap::new();
|
2014-07-24 09:28:34 -05:00
|
|
|
///
|
|
|
|
/// // Key does not exist, will do a simple insert
|
2014-09-22 12:31:31 -05:00
|
|
|
/// assert!(map.update(1, vec![1i, 2], |mut old, new| { old.extend(new.into_iter()); old }));
|
2014-08-02 02:01:33 -05:00
|
|
|
/// assert_eq!(map[1], vec![1i, 2]);
|
2014-07-24 09:28:34 -05:00
|
|
|
///
|
|
|
|
/// // Key exists, update the value
|
2014-09-22 12:31:31 -05:00
|
|
|
/// assert!(!map.update(1, vec![3i, 4], |mut old, new| { old.extend(new.into_iter()); old }));
|
2014-08-02 02:01:33 -05:00
|
|
|
/// assert_eq!(map[1], vec![1i, 2, 3, 4]);
|
2014-07-24 09:28:34 -05:00
|
|
|
/// ```
|
|
|
|
pub fn update(&mut self, key: uint, newval: V, ff: |V, V| -> V) -> bool {
|
|
|
|
self.update_with_key(key, newval, |_k, v, v1| ff(v,v1))
|
|
|
|
}
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Updates a value in the map. If the key already exists in the map,
|
|
|
|
/// modifies the value with `ff` taking `key, oldval, newval`.
|
|
|
|
/// Otherwise, sets the value to `newval`.
|
|
|
|
/// Returns `true` if the key did not already exist in the map.
|
2014-07-24 09:28:34 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2014-10-30 20:25:08 -05:00
|
|
|
/// use std::collections::VecMap;
|
2014-07-24 09:28:34 -05:00
|
|
|
///
|
2014-10-30 20:25:08 -05:00
|
|
|
/// let mut map = VecMap::new();
|
2014-07-24 09:28:34 -05:00
|
|
|
///
|
|
|
|
/// // Key does not exist, will do a simple insert
|
|
|
|
/// assert!(map.update_with_key(7, 10, |key, old, new| (old + new) % key));
|
2014-08-02 02:01:33 -05:00
|
|
|
/// assert_eq!(map[7], 10);
|
2014-07-24 09:28:34 -05:00
|
|
|
///
|
|
|
|
/// // Key exists, update the value
|
|
|
|
/// assert!(!map.update_with_key(7, 20, |key, old, new| (old + new) % key));
|
2014-08-02 02:01:33 -05:00
|
|
|
/// assert_eq!(map[7], 2);
|
2014-07-24 09:28:34 -05:00
|
|
|
/// ```
|
2013-11-18 23:54:13 -06:00
|
|
|
pub fn update_with_key(&mut self,
|
|
|
|
key: uint,
|
|
|
|
val: V,
|
|
|
|
ff: |uint, V, V| -> V)
|
|
|
|
-> bool {
|
2014-11-06 11:24:47 -06:00
|
|
|
let new_val = match self.get(&key) {
|
2013-02-09 00:21:45 -06:00
|
|
|
None => val,
|
2013-07-02 14:47:32 -05:00
|
|
|
Some(orig) => ff(key, (*orig).clone(), val)
|
2013-02-09 00:21:45 -06:00
|
|
|
};
|
2014-11-06 11:24:47 -06:00
|
|
|
self.insert(key, new_val).is_none()
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-30 20:25:08 -05:00
|
|
|
impl<V: PartialOrd> PartialOrd for VecMap<V> {
|
2014-07-27 23:00:29 -05:00
|
|
|
#[inline]
|
2014-10-30 20:25:08 -05:00
|
|
|
fn partial_cmp(&self, other: &VecMap<V>) -> Option<Ordering> {
|
2014-07-27 23:00:29 -05:00
|
|
|
iter::order::partial_cmp(self.iter(), other.iter())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-30 20:25:08 -05:00
|
|
|
impl<V: Ord> Ord for VecMap<V> {
|
2014-07-28 01:53:44 -05:00
|
|
|
#[inline]
|
2014-10-30 20:25:08 -05:00
|
|
|
fn cmp(&self, other: &VecMap<V>) -> Ordering {
|
2014-07-28 01:53:44 -05:00
|
|
|
iter::order::cmp(self.iter(), other.iter())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-30 20:25:08 -05:00
|
|
|
impl<V: fmt::Show> fmt::Show for VecMap<V> {
|
2014-05-28 11:24:28 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
try!(write!(f, "{{"));
|
|
|
|
|
|
|
|
for (i, (k, v)) in self.iter().enumerate() {
|
|
|
|
if i != 0 { try!(write!(f, ", ")); }
|
|
|
|
try!(write!(f, "{}: {}", k, *v));
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "}}")
|
|
|
|
}
|
2014-06-07 11:17:58 -05:00
|
|
|
}
|
2013-07-04 18:18:44 -05:00
|
|
|
|
2014-10-30 20:25:08 -05:00
|
|
|
impl<V> FromIterator<(uint, V)> for VecMap<V> {
|
|
|
|
fn from_iter<Iter: Iterator<(uint, V)>>(iter: Iter) -> VecMap<V> {
|
|
|
|
let mut map = VecMap::new();
|
2014-07-24 16:50:21 -05:00
|
|
|
map.extend(iter);
|
|
|
|
map
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-07 18:39:39 -06:00
|
|
|
impl<V> Extend<(uint, V)> for VecMap<V> {
|
2014-07-24 16:50:21 -05:00
|
|
|
fn extend<Iter: Iterator<(uint, V)>>(&mut self, mut iter: Iter) {
|
|
|
|
for (k, v) in iter {
|
|
|
|
self.insert(k, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-30 20:25:08 -05:00
|
|
|
impl<V> Index<uint, V> for VecMap<V> {
|
2014-08-02 02:01:33 -05:00
|
|
|
#[inline]
|
|
|
|
fn index<'a>(&'a self, i: &uint) -> &'a V {
|
2014-11-06 11:24:47 -06:00
|
|
|
self.get(i).expect("key not present")
|
2014-08-02 02:01:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-30 20:25:08 -05:00
|
|
|
impl<V> IndexMut<uint, V> for VecMap<V> {
|
2014-08-02 02:01:33 -05:00
|
|
|
#[inline]
|
|
|
|
fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut V {
|
2014-11-06 11:24:47 -06:00
|
|
|
self.get_mut(i).expect("key not present")
|
2014-08-02 02:01:33 -05:00
|
|
|
}
|
2014-10-23 10:42:21 -05:00
|
|
|
}
|
2014-08-02 02:01:33 -05:00
|
|
|
|
2013-07-04 18:18:44 -05:00
|
|
|
macro_rules! iterator {
|
2014-09-22 12:30:06 -05:00
|
|
|
(impl $name:ident -> $elem:ty, $($getter:ident),+) => {
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a, T> Iterator<$elem> for $name<'a, T> {
|
2013-07-04 18:18:44 -05:00
|
|
|
#[inline]
|
2013-07-19 19:29:54 -05:00
|
|
|
fn next(&mut self) -> Option<$elem> {
|
|
|
|
while self.front < self.back {
|
|
|
|
match self.iter.next() {
|
|
|
|
Some(elem) => {
|
|
|
|
if elem.is_some() {
|
|
|
|
let index = self.front;
|
|
|
|
self.front += 1;
|
2014-09-22 12:30:06 -05:00
|
|
|
return Some((index, elem $(. $getter ())+));
|
2013-07-19 19:29:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ()
|
2013-07-04 18:18:44 -05:00
|
|
|
}
|
2013-07-19 19:29:54 -05:00
|
|
|
self.front += 1;
|
2013-07-04 18:18:44 -05:00
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2013-07-19 19:29:54 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
|
|
|
(0, Some(self.back - self.front))
|
|
|
|
}
|
2013-07-04 18:18:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 19:29:54 -05:00
|
|
|
macro_rules! double_ended_iterator {
|
2014-09-22 12:30:06 -05:00
|
|
|
(impl $name:ident -> $elem:ty, $($getter:ident),+) => {
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a, T> DoubleEndedIterator<$elem> for $name<'a, T> {
|
2013-07-04 18:18:44 -05:00
|
|
|
#[inline]
|
2013-07-19 19:29:54 -05:00
|
|
|
fn next_back(&mut self) -> Option<$elem> {
|
|
|
|
while self.front < self.back {
|
|
|
|
match self.iter.next_back() {
|
|
|
|
Some(elem) => {
|
|
|
|
if elem.is_some() {
|
|
|
|
self.back -= 1;
|
2014-09-22 12:30:06 -05:00
|
|
|
return Some((self.back, elem$(. $getter ())+));
|
2013-07-19 19:29:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ()
|
2013-07-04 18:18:44 -05:00
|
|
|
}
|
2013-07-19 19:29:54 -05:00
|
|
|
self.back -= 1;
|
2013-07-04 18:18:44 -05:00
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-27 20:46:52 -05:00
|
|
|
/// Forward iterator over a map.
|
|
|
|
pub struct Entries<'a, T:'a> {
|
|
|
|
front: uint,
|
|
|
|
back: uint,
|
|
|
|
iter: slice::Items<'a, Option<T>>
|
|
|
|
}
|
|
|
|
|
2014-09-22 12:30:06 -05:00
|
|
|
iterator!(impl Entries -> (uint, &'a T), as_ref, unwrap)
|
|
|
|
double_ended_iterator!(impl Entries -> (uint, &'a T), as_ref, unwrap)
|
2013-07-04 18:18:44 -05:00
|
|
|
|
2014-07-24 08:46:55 -05:00
|
|
|
/// Forward iterator over the key-value pairs of a map, with the
|
|
|
|
/// values being mutable.
|
2014-08-27 20:46:52 -05:00
|
|
|
pub struct MutEntries<'a, T:'a> {
|
2014-03-27 17:10:04 -05:00
|
|
|
front: uint,
|
|
|
|
back: uint,
|
|
|
|
iter: slice::MutItems<'a, Option<T>>
|
2013-07-04 18:18:44 -05:00
|
|
|
}
|
|
|
|
|
2014-09-22 12:30:06 -05:00
|
|
|
iterator!(impl MutEntries -> (uint, &'a mut T), as_mut, unwrap)
|
|
|
|
double_ended_iterator!(impl MutEntries -> (uint, &'a mut T), as_mut, unwrap)
|
2013-07-04 18:18:44 -05:00
|
|
|
|
2014-07-24 23:48:05 -05:00
|
|
|
/// Forward iterator over the keys of a map
|
|
|
|
pub type Keys<'a, T> =
|
|
|
|
iter::Map<'static, (uint, &'a T), uint, Entries<'a, T>>;
|
|
|
|
|
|
|
|
/// Forward iterator over the values of a map
|
|
|
|
pub type Values<'a, T> =
|
|
|
|
iter::Map<'static, (uint, &'a T), &'a T, Entries<'a, T>>;
|
|
|
|
|
2013-01-31 17:56:12 -06:00
|
|
|
#[cfg(test)]
|
2013-07-19 19:29:54 -05:00
|
|
|
mod test_map {
|
2014-05-29 21:03:06 -05:00
|
|
|
use std::prelude::*;
|
2014-07-25 20:35:58 -05:00
|
|
|
use vec::Vec;
|
2014-07-26 11:04:41 -05:00
|
|
|
use hash;
|
2013-05-21 19:24:31 -05:00
|
|
|
|
2014-10-30 20:25:08 -05:00
|
|
|
use super::VecMap;
|
2013-03-24 19:35:23 -05:00
|
|
|
|
|
|
|
#[test]
|
2014-11-06 11:24:47 -06:00
|
|
|
fn test_get_mut() {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut m = VecMap::new();
|
2014-11-06 11:24:47 -06:00
|
|
|
assert!(m.insert(1, 12i).is_none());
|
|
|
|
assert!(m.insert(2, 8).is_none());
|
|
|
|
assert!(m.insert(5, 14).is_none());
|
2013-03-24 19:35:23 -05:00
|
|
|
let new = 100;
|
2014-11-06 11:24:47 -06:00
|
|
|
match m.get_mut(&5) {
|
2014-10-09 14:17:22 -05:00
|
|
|
None => panic!(), Some(x) => *x = new
|
2013-03-24 19:35:23 -05:00
|
|
|
}
|
2014-11-06 11:24:47 -06:00
|
|
|
assert_eq!(m.get(&5), Some(&new));
|
2013-03-24 19:35:23 -05:00
|
|
|
}
|
2013-01-31 17:56:12 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_len() {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut map = VecMap::new();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(map.len(), 0);
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(map.is_empty());
|
2014-11-06 11:24:47 -06:00
|
|
|
assert!(map.insert(5, 20i).is_none());
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(map.len(), 1);
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(!map.is_empty());
|
2014-11-06 11:24:47 -06:00
|
|
|
assert!(map.insert(11, 12).is_none());
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(map.len(), 2);
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(!map.is_empty());
|
2014-11-06 11:24:47 -06:00
|
|
|
assert!(map.insert(14, 22).is_none());
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(map.len(), 3);
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(!map.is_empty());
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_clear() {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut map = VecMap::new();
|
2014-11-06 11:24:47 -06:00
|
|
|
assert!(map.insert(5, 20i).is_none());
|
|
|
|
assert!(map.insert(11, 12).is_none());
|
|
|
|
assert!(map.insert(14, 22).is_none());
|
2013-01-31 17:56:12 -06:00
|
|
|
map.clear();
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(map.is_empty());
|
2014-11-06 11:24:47 -06:00
|
|
|
assert!(map.get(&5).is_none());
|
|
|
|
assert!(map.get(&11).is_none());
|
|
|
|
assert!(map.get(&14).is_none());
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_insert_with_key() {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut map = VecMap::new();
|
2013-01-31 17:56:12 -06:00
|
|
|
|
2014-03-06 01:35:12 -06:00
|
|
|
// given a new key, initialize it with this new count,
|
2013-01-31 17:56:12 -06:00
|
|
|
// given an existing key, add more to its count
|
2014-05-25 19:12:43 -05:00
|
|
|
fn add_more_to_count(_k: uint, v0: uint, v1: uint) -> uint {
|
2013-01-31 17:56:12 -06:00
|
|
|
v0 + v1
|
|
|
|
}
|
|
|
|
|
2014-05-25 19:12:43 -05:00
|
|
|
fn add_more_to_count_simple(v0: uint, v1: uint) -> uint {
|
2013-01-31 17:56:12 -06:00
|
|
|
v0 + v1
|
|
|
|
}
|
|
|
|
|
|
|
|
// count integers
|
2014-05-25 19:12:43 -05:00
|
|
|
map.update(3, 1, add_more_to_count_simple);
|
|
|
|
map.update_with_key(9, 1, add_more_to_count);
|
|
|
|
map.update(3, 7, add_more_to_count_simple);
|
|
|
|
map.update_with_key(5, 3, add_more_to_count);
|
|
|
|
map.update_with_key(3, 2, add_more_to_count);
|
2013-01-31 17:56:12 -06:00
|
|
|
|
|
|
|
// check the total counts
|
2014-11-06 11:24:47 -06:00
|
|
|
assert_eq!(map.get(&3).unwrap(), &10);
|
|
|
|
assert_eq!(map.get(&5).unwrap(), &3);
|
|
|
|
assert_eq!(map.get(&9).unwrap(), &1);
|
2013-01-31 17:56:12 -06:00
|
|
|
|
|
|
|
// sadly, no sevens were counted
|
2014-11-06 11:24:47 -06:00
|
|
|
assert!(map.get(&7).is_none());
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
2013-05-04 08:54:58 -05:00
|
|
|
|
|
|
|
#[test]
|
2014-11-06 11:24:47 -06:00
|
|
|
fn test_insert() {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut m = VecMap::new();
|
2014-11-06 11:24:47 -06:00
|
|
|
assert_eq!(m.insert(1, 2i), None);
|
|
|
|
assert_eq!(m.insert(1, 3i), Some(2));
|
|
|
|
assert_eq!(m.insert(1, 4i), Some(3));
|
2013-05-04 08:54:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2014-11-06 11:24:47 -06:00
|
|
|
fn test_remove() {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut m = VecMap::new();
|
2014-04-21 16:58:52 -05:00
|
|
|
m.insert(1, 2i);
|
2014-11-06 11:24:47 -06:00
|
|
|
assert_eq!(m.remove(&1), Some(2));
|
|
|
|
assert_eq!(m.remove(&1), None);
|
2013-05-04 08:54:58 -05:00
|
|
|
}
|
2013-07-04 18:18:44 -05:00
|
|
|
|
2014-07-24 23:48:05 -05:00
|
|
|
#[test]
|
|
|
|
fn test_keys() {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut map = VecMap::new();
|
2014-07-24 23:48:05 -05:00
|
|
|
map.insert(1, 'a');
|
|
|
|
map.insert(2, 'b');
|
|
|
|
map.insert(3, 'c');
|
|
|
|
let keys = map.keys().collect::<Vec<uint>>();
|
|
|
|
assert_eq!(keys.len(), 3);
|
|
|
|
assert!(keys.contains(&1));
|
|
|
|
assert!(keys.contains(&2));
|
|
|
|
assert!(keys.contains(&3));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_values() {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut map = VecMap::new();
|
2014-07-24 23:48:05 -05:00
|
|
|
map.insert(1, 'a');
|
|
|
|
map.insert(2, 'b');
|
|
|
|
map.insert(3, 'c');
|
|
|
|
let values = map.values().map(|&v| v).collect::<Vec<char>>();
|
|
|
|
assert_eq!(values.len(), 3);
|
|
|
|
assert!(values.contains(&'a'));
|
|
|
|
assert!(values.contains(&'b'));
|
|
|
|
assert!(values.contains(&'c'));
|
|
|
|
}
|
|
|
|
|
2013-07-04 18:18:44 -05:00
|
|
|
#[test]
|
|
|
|
fn test_iterator() {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut m = VecMap::new();
|
2013-07-04 18:18:44 -05:00
|
|
|
|
2014-11-06 11:24:47 -06:00
|
|
|
assert!(m.insert(0, 1i).is_none());
|
|
|
|
assert!(m.insert(1, 2).is_none());
|
|
|
|
assert!(m.insert(3, 5).is_none());
|
|
|
|
assert!(m.insert(6, 10).is_none());
|
|
|
|
assert!(m.insert(10, 11).is_none());
|
2013-07-04 18:18:44 -05:00
|
|
|
|
2013-07-19 19:29:54 -05:00
|
|
|
let mut it = m.iter();
|
|
|
|
assert_eq!(it.size_hint(), (0, Some(11)));
|
2013-07-04 18:18:44 -05:00
|
|
|
assert_eq!(it.next().unwrap(), (0, &1));
|
2013-07-19 19:29:54 -05:00
|
|
|
assert_eq!(it.size_hint(), (0, Some(10)));
|
2013-07-04 18:18:44 -05:00
|
|
|
assert_eq!(it.next().unwrap(), (1, &2));
|
2013-07-19 19:29:54 -05:00
|
|
|
assert_eq!(it.size_hint(), (0, Some(9)));
|
|
|
|
assert_eq!(it.next().unwrap(), (3, &5));
|
|
|
|
assert_eq!(it.size_hint(), (0, Some(7)));
|
|
|
|
assert_eq!(it.next().unwrap(), (6, &10));
|
|
|
|
assert_eq!(it.size_hint(), (0, Some(4)));
|
|
|
|
assert_eq!(it.next().unwrap(), (10, &11));
|
|
|
|
assert_eq!(it.size_hint(), (0, Some(0)));
|
2013-07-04 18:18:44 -05:00
|
|
|
assert!(it.next().is_none());
|
|
|
|
}
|
|
|
|
|
2013-07-19 19:29:54 -05:00
|
|
|
#[test]
|
|
|
|
fn test_iterator_size_hints() {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut m = VecMap::new();
|
2013-07-19 19:29:54 -05:00
|
|
|
|
2014-11-06 11:24:47 -06:00
|
|
|
assert!(m.insert(0, 1i).is_none());
|
|
|
|
assert!(m.insert(1, 2).is_none());
|
|
|
|
assert!(m.insert(3, 5).is_none());
|
|
|
|
assert!(m.insert(6, 10).is_none());
|
|
|
|
assert!(m.insert(10, 11).is_none());
|
2013-07-19 19:29:54 -05:00
|
|
|
|
|
|
|
assert_eq!(m.iter().size_hint(), (0, Some(11)));
|
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
|
|
|
assert_eq!(m.iter().rev().size_hint(), (0, Some(11)));
|
2014-09-14 22:27:36 -05:00
|
|
|
assert_eq!(m.iter_mut().size_hint(), (0, Some(11)));
|
|
|
|
assert_eq!(m.iter_mut().rev().size_hint(), (0, Some(11)));
|
2013-07-19 19:29:54 -05:00
|
|
|
}
|
|
|
|
|
2013-07-04 18:18:44 -05:00
|
|
|
#[test]
|
|
|
|
fn test_mut_iterator() {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut m = VecMap::new();
|
2013-07-04 18:18:44 -05:00
|
|
|
|
2014-11-06 11:24:47 -06:00
|
|
|
assert!(m.insert(0, 1i).is_none());
|
|
|
|
assert!(m.insert(1, 2).is_none());
|
|
|
|
assert!(m.insert(3, 5).is_none());
|
|
|
|
assert!(m.insert(6, 10).is_none());
|
|
|
|
assert!(m.insert(10, 11).is_none());
|
2013-07-04 18:18:44 -05:00
|
|
|
|
2014-09-14 22:27:36 -05:00
|
|
|
for (k, v) in m.iter_mut() {
|
2013-07-19 19:29:54 -05:00
|
|
|
*v += k as int;
|
2013-07-04 18:18:44 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 19:29:54 -05:00
|
|
|
let mut it = m.iter();
|
|
|
|
assert_eq!(it.next().unwrap(), (0, &1));
|
|
|
|
assert_eq!(it.next().unwrap(), (1, &3));
|
|
|
|
assert_eq!(it.next().unwrap(), (3, &8));
|
|
|
|
assert_eq!(it.next().unwrap(), (6, &16));
|
|
|
|
assert_eq!(it.next().unwrap(), (10, &21));
|
|
|
|
assert!(it.next().is_none());
|
2013-07-04 18:18:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rev_iterator() {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut m = VecMap::new();
|
2013-07-04 18:18:44 -05:00
|
|
|
|
2014-11-06 11:24:47 -06:00
|
|
|
assert!(m.insert(0, 1i).is_none());
|
|
|
|
assert!(m.insert(1, 2).is_none());
|
|
|
|
assert!(m.insert(3, 5).is_none());
|
|
|
|
assert!(m.insert(6, 10).is_none());
|
|
|
|
assert!(m.insert(10, 11).is_none());
|
2013-07-04 18:18:44 -05:00
|
|
|
|
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
|
|
|
let mut it = m.iter().rev();
|
2013-07-19 19:29:54 -05:00
|
|
|
assert_eq!(it.next().unwrap(), (10, &11));
|
|
|
|
assert_eq!(it.next().unwrap(), (6, &10));
|
|
|
|
assert_eq!(it.next().unwrap(), (3, &5));
|
|
|
|
assert_eq!(it.next().unwrap(), (1, &2));
|
|
|
|
assert_eq!(it.next().unwrap(), (0, &1));
|
|
|
|
assert!(it.next().is_none());
|
2013-07-04 18:18:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mut_rev_iterator() {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut m = VecMap::new();
|
2013-07-04 18:18:44 -05:00
|
|
|
|
2014-11-06 11:24:47 -06:00
|
|
|
assert!(m.insert(0, 1i).is_none());
|
|
|
|
assert!(m.insert(1, 2).is_none());
|
|
|
|
assert!(m.insert(3, 5).is_none());
|
|
|
|
assert!(m.insert(6, 10).is_none());
|
|
|
|
assert!(m.insert(10, 11).is_none());
|
2013-07-04 18:18:44 -05:00
|
|
|
|
2014-09-14 22:27:36 -05:00
|
|
|
for (k, v) in m.iter_mut().rev() {
|
2013-07-19 19:29:54 -05:00
|
|
|
*v += k as int;
|
2013-07-04 18:18:44 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 19:29:54 -05:00
|
|
|
let mut it = m.iter();
|
|
|
|
assert_eq!(it.next().unwrap(), (0, &1));
|
|
|
|
assert_eq!(it.next().unwrap(), (1, &3));
|
|
|
|
assert_eq!(it.next().unwrap(), (3, &8));
|
|
|
|
assert_eq!(it.next().unwrap(), (6, &16));
|
|
|
|
assert_eq!(it.next().unwrap(), (10, &21));
|
|
|
|
assert!(it.next().is_none());
|
2013-07-04 18:18:44 -05:00
|
|
|
}
|
2013-07-08 23:16:23 -05:00
|
|
|
|
|
|
|
#[test]
|
2013-08-07 21:21:36 -05:00
|
|
|
fn test_move_iter() {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut m = VecMap::new();
|
2014-04-21 16:58:52 -05:00
|
|
|
m.insert(1, box 2i);
|
2013-07-08 23:16:23 -05:00
|
|
|
let mut called = false;
|
2014-09-14 22:27:36 -05:00
|
|
|
for (k, v) in m.into_iter() {
|
2013-07-08 23:16:23 -05:00
|
|
|
assert!(!called);
|
|
|
|
called = true;
|
|
|
|
assert_eq!(k, 1);
|
2014-04-21 16:58:52 -05:00
|
|
|
assert_eq!(v, box 2i);
|
2013-07-08 23:16:23 -05:00
|
|
|
}
|
|
|
|
assert!(called);
|
2014-04-21 16:58:52 -05:00
|
|
|
m.insert(2, box 1i);
|
2013-07-08 23:16:23 -05:00
|
|
|
}
|
2014-06-07 11:17:58 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_show() {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut map = VecMap::new();
|
|
|
|
let empty = VecMap::<int>::new();
|
2014-06-07 11:17:58 -05:00
|
|
|
|
2014-04-21 16:58:52 -05:00
|
|
|
map.insert(1, 2i);
|
|
|
|
map.insert(3, 4i);
|
2014-06-07 11:17:58 -05:00
|
|
|
|
2014-06-21 05:39:03 -05:00
|
|
|
let map_str = map.to_string();
|
2014-06-07 11:17:58 -05:00
|
|
|
let map_str = map_str.as_slice();
|
|
|
|
assert!(map_str == "{1: 2, 3: 4}" || map_str == "{3: 4, 1: 2}");
|
|
|
|
assert_eq!(format!("{}", empty), "{}".to_string());
|
|
|
|
}
|
2014-07-24 16:50:21 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_clone() {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut a = VecMap::new();
|
2014-07-24 16:50:21 -05:00
|
|
|
|
2014-07-25 20:35:58 -05:00
|
|
|
a.insert(1, 'x');
|
|
|
|
a.insert(4, 'y');
|
|
|
|
a.insert(6, 'z');
|
2014-07-24 16:50:21 -05:00
|
|
|
|
|
|
|
assert!(a.clone() == a);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_eq() {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut a = VecMap::new();
|
|
|
|
let mut b = VecMap::new();
|
2014-07-24 16:50:21 -05:00
|
|
|
|
|
|
|
assert!(a == b);
|
2014-11-06 11:24:47 -06:00
|
|
|
assert!(a.insert(0, 5i).is_none());
|
2014-07-24 16:50:21 -05:00
|
|
|
assert!(a != b);
|
2014-11-06 11:24:47 -06:00
|
|
|
assert!(b.insert(0, 4i).is_none());
|
2014-07-24 16:50:21 -05:00
|
|
|
assert!(a != b);
|
2014-11-06 11:24:47 -06:00
|
|
|
assert!(a.insert(5, 19).is_none());
|
2014-07-24 16:50:21 -05:00
|
|
|
assert!(a != b);
|
2014-11-06 11:24:47 -06:00
|
|
|
assert!(!b.insert(0, 5).is_none());
|
2014-07-24 16:50:21 -05:00
|
|
|
assert!(a != b);
|
2014-11-06 11:24:47 -06:00
|
|
|
assert!(b.insert(5, 19).is_none());
|
2014-07-24 16:50:21 -05:00
|
|
|
assert!(a == b);
|
|
|
|
}
|
|
|
|
|
2014-07-27 23:00:29 -05:00
|
|
|
#[test]
|
|
|
|
fn test_lt() {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut a = VecMap::new();
|
|
|
|
let mut b = VecMap::new();
|
2014-07-27 23:00:29 -05:00
|
|
|
|
|
|
|
assert!(!(a < b) && !(b < a));
|
2014-11-06 11:24:47 -06:00
|
|
|
assert!(b.insert(2u, 5i).is_none());
|
2014-07-27 23:00:29 -05:00
|
|
|
assert!(a < b);
|
2014-11-06 11:24:47 -06:00
|
|
|
assert!(a.insert(2, 7).is_none());
|
2014-07-27 23:00:29 -05:00
|
|
|
assert!(!(a < b) && b < a);
|
2014-11-06 11:24:47 -06:00
|
|
|
assert!(b.insert(1, 0).is_none());
|
2014-07-27 23:00:29 -05:00
|
|
|
assert!(b < a);
|
2014-11-06 11:24:47 -06:00
|
|
|
assert!(a.insert(0, 6).is_none());
|
2014-07-27 23:00:29 -05:00
|
|
|
assert!(a < b);
|
2014-11-06 11:24:47 -06:00
|
|
|
assert!(a.insert(6, 2).is_none());
|
2014-07-27 23:00:29 -05:00
|
|
|
assert!(a < b && !(b < a));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ord() {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut a = VecMap::new();
|
|
|
|
let mut b = VecMap::new();
|
2014-07-27 23:00:29 -05:00
|
|
|
|
|
|
|
assert!(a <= b && a >= b);
|
2014-11-06 11:24:47 -06:00
|
|
|
assert!(a.insert(1u, 1i).is_none());
|
2014-07-27 23:00:29 -05:00
|
|
|
assert!(a > b && a >= b);
|
|
|
|
assert!(b < a && b <= a);
|
2014-11-06 11:24:47 -06:00
|
|
|
assert!(b.insert(2, 2).is_none());
|
2014-07-27 23:00:29 -05:00
|
|
|
assert!(b > a && b >= a);
|
|
|
|
assert!(a < b && a <= b);
|
|
|
|
}
|
|
|
|
|
2014-07-24 16:50:21 -05:00
|
|
|
#[test]
|
|
|
|
fn test_hash() {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut x = VecMap::new();
|
|
|
|
let mut y = VecMap::new();
|
2014-07-24 16:50:21 -05:00
|
|
|
|
2014-07-26 11:04:41 -05:00
|
|
|
assert!(hash::hash(&x) == hash::hash(&y));
|
|
|
|
x.insert(1, 'a');
|
|
|
|
x.insert(2, 'b');
|
|
|
|
x.insert(3, 'c');
|
2014-07-24 16:50:21 -05:00
|
|
|
|
2014-07-26 11:04:41 -05:00
|
|
|
y.insert(3, 'c');
|
|
|
|
y.insert(2, 'b');
|
|
|
|
y.insert(1, 'a');
|
2014-07-24 16:50:21 -05:00
|
|
|
|
2014-07-26 11:04:41 -05:00
|
|
|
assert!(hash::hash(&x) == hash::hash(&y));
|
2014-07-24 16:50:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from_iter() {
|
2014-07-25 20:35:58 -05:00
|
|
|
let xs: Vec<(uint, char)> = vec![(1u, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')];
|
2014-07-24 16:50:21 -05:00
|
|
|
|
2014-10-30 20:25:08 -05:00
|
|
|
let map: VecMap<char> = xs.iter().map(|&x| x).collect();
|
2014-07-24 16:50:21 -05:00
|
|
|
|
|
|
|
for &(k, v) in xs.iter() {
|
2014-11-06 11:24:47 -06:00
|
|
|
assert_eq!(map.get(&k), Some(&v));
|
2014-07-24 16:50:21 -05:00
|
|
|
}
|
|
|
|
}
|
2014-08-02 02:01:33 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_index() {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut map: VecMap<int> = VecMap::new();
|
2014-08-02 02:01:33 -05:00
|
|
|
|
|
|
|
map.insert(1, 2);
|
|
|
|
map.insert(2, 1);
|
|
|
|
map.insert(3, 4);
|
|
|
|
|
|
|
|
assert_eq!(map[3], 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
fn test_index_nonexistent() {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut map: VecMap<int> = VecMap::new();
|
2014-08-02 02:01:33 -05:00
|
|
|
|
|
|
|
map.insert(1, 2);
|
|
|
|
map.insert(2, 1);
|
|
|
|
map.insert(3, 4);
|
|
|
|
|
|
|
|
map[4];
|
|
|
|
}
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
2013-05-22 07:01:21 -05:00
|
|
|
|
2013-07-19 16:07:00 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod bench {
|
2014-02-13 19:49:11 -06:00
|
|
|
extern crate test;
|
2014-03-31 20:16:35 -05:00
|
|
|
use self::test::Bencher;
|
2014-10-30 20:25:08 -05:00
|
|
|
use super::VecMap;
|
2014-10-30 15:43:24 -05:00
|
|
|
use bench::{insert_rand_n, insert_seq_n, find_rand_n, find_seq_n};
|
2013-07-19 16:07:00 -05:00
|
|
|
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
pub fn insert_rand_100(b: &mut Bencher) {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut m : VecMap<uint> = VecMap::new();
|
2014-10-30 15:43:24 -05:00
|
|
|
insert_rand_n(100, &mut m, b,
|
|
|
|
|m, i| { m.insert(i, 1); },
|
|
|
|
|m, i| { m.remove(&i); });
|
2013-07-19 16:07:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
pub fn insert_rand_10_000(b: &mut Bencher) {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut m : VecMap<uint> = VecMap::new();
|
2014-10-30 15:43:24 -05:00
|
|
|
insert_rand_n(10_000, &mut m, b,
|
|
|
|
|m, i| { m.insert(i, 1); },
|
|
|
|
|m, i| { m.remove(&i); });
|
2013-07-19 16:07:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert seq
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
pub fn insert_seq_100(b: &mut Bencher) {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut m : VecMap<uint> = VecMap::new();
|
2014-10-30 15:43:24 -05:00
|
|
|
insert_seq_n(100, &mut m, b,
|
|
|
|
|m, i| { m.insert(i, 1); },
|
|
|
|
|m, i| { m.remove(&i); });
|
2013-07-19 16:07:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
pub fn insert_seq_10_000(b: &mut Bencher) {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut m : VecMap<uint> = VecMap::new();
|
2014-10-30 15:43:24 -05:00
|
|
|
insert_seq_n(10_000, &mut m, b,
|
|
|
|
|m, i| { m.insert(i, 1); },
|
|
|
|
|m, i| { m.remove(&i); });
|
2013-07-19 16:07:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find rand
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
pub fn find_rand_100(b: &mut Bencher) {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut m : VecMap<uint> = VecMap::new();
|
2014-10-30 15:43:24 -05:00
|
|
|
find_rand_n(100, &mut m, b,
|
|
|
|
|m, i| { m.insert(i, 1); },
|
2014-11-06 11:24:47 -06:00
|
|
|
|m, i| { m.get(&i); });
|
2013-07-19 16:07:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
pub fn find_rand_10_000(b: &mut Bencher) {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut m : VecMap<uint> = VecMap::new();
|
2014-10-30 15:43:24 -05:00
|
|
|
find_rand_n(10_000, &mut m, b,
|
|
|
|
|m, i| { m.insert(i, 1); },
|
2014-11-06 11:24:47 -06:00
|
|
|
|m, i| { m.get(&i); });
|
2013-07-19 16:07:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find seq
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
pub fn find_seq_100(b: &mut Bencher) {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut m : VecMap<uint> = VecMap::new();
|
2014-10-30 15:43:24 -05:00
|
|
|
find_seq_n(100, &mut m, b,
|
|
|
|
|m, i| { m.insert(i, 1); },
|
2014-11-06 11:24:47 -06:00
|
|
|
|m, i| { m.get(&i); });
|
2013-07-19 16:07:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
pub fn find_seq_10_000(b: &mut Bencher) {
|
2014-10-30 20:25:08 -05:00
|
|
|
let mut m : VecMap<uint> = VecMap::new();
|
2014-10-30 15:43:24 -05:00
|
|
|
find_seq_n(10_000, &mut m, b,
|
|
|
|
|m, i| { m.insert(i, 1); },
|
2014-11-06 11:24:47 -06:00
|
|
|
|m, i| { m.get(&i); });
|
2013-07-19 16:07:00 -05:00
|
|
|
}
|
|
|
|
}
|