2013-01-31 17:56:12 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* A simple map based on a vector for small integer keys. Space requirements
|
|
|
|
* are O(highest integer key).
|
|
|
|
*/
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
#[allow(missing_doc)];
|
|
|
|
|
2013-07-27 16:41:20 -05:00
|
|
|
use std::iterator::{Iterator, IteratorUtil, Enumerate, FilterMap, Invert};
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::util::replace;
|
2013-07-28 15:37:35 -05:00
|
|
|
use std::vec::{VecIterator, VecMutIterator};
|
|
|
|
use std::vec;
|
2013-01-31 17:56:12 -06:00
|
|
|
|
2013-05-28 22:11:41 -05:00
|
|
|
#[allow(missing_doc)]
|
2013-01-31 18:07:08 -06:00
|
|
|
pub struct SmallIntMap<T> {
|
|
|
|
priv v: ~[Option<T>],
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl<V> Container for SmallIntMap<V> {
|
2013-01-31 17:56:12 -06:00
|
|
|
/// Return the number of elements in the map
|
2013-06-15 01:20:06 -05:00
|
|
|
fn len(&self) -> uint {
|
2013-01-31 18:07:08 -06:00
|
|
|
let mut sz = 0;
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0u, self.v.len()) {
|
2013-03-16 13:11:31 -05:00
|
|
|
match self.v[i] {
|
|
|
|
Some(_) => sz += 1,
|
|
|
|
None => {}
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sz
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl<V> Mutable for SmallIntMap<V> {
|
2013-01-31 18:07:08 -06:00
|
|
|
/// Clear the map, removing all key-value pairs.
|
|
|
|
fn clear(&mut self) { self.v.clear() }
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl<V> Map<uint, V> for SmallIntMap<V> {
|
2013-03-24 19:35:23 -05:00
|
|
|
/// Return a reference to the value corresponding to the key
|
2013-04-10 15:14:06 -05:00
|
|
|
fn find<'a>(&'a self, key: &uint) -> Option<&'a V> {
|
|
|
|
if *key < self.v.len() {
|
|
|
|
match self.v[*key] {
|
|
|
|
Some(ref value) => Some(value),
|
|
|
|
None => None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2013-07-13 21:44:36 -05:00
|
|
|
}
|
2013-04-10 15:14:06 -05:00
|
|
|
|
2013-07-13 21:44:36 -05:00
|
|
|
impl<V> MutableMap<uint, V> for SmallIntMap<V> {
|
2013-03-24 19:40:17 -05:00
|
|
|
/// Return a mutable reference to the value corresponding to the key
|
2013-04-10 15:14:06 -05:00
|
|
|
fn find_mut<'a>(&'a mut self, key: &uint) -> Option<&'a mut V> {
|
|
|
|
if *key < self.v.len() {
|
|
|
|
match self.v[*key] {
|
|
|
|
Some(ref mut value) => Some(value),
|
|
|
|
None => None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-31 18:07:08 -06:00
|
|
|
/// Insert a key-value pair into the map. An existing value for a
|
|
|
|
/// key is replaced by the new value. Return true if the key did
|
|
|
|
/// not already exist in the map.
|
|
|
|
fn insert(&mut self, key: uint, value: V) -> bool {
|
|
|
|
let exists = self.contains_key(&key);
|
|
|
|
let len = self.v.len();
|
|
|
|
if len <= key {
|
2013-06-28 23:02:20 -05:00
|
|
|
self.v.grow_fn(key - len + 1, |_| None);
|
2013-01-31 18:07:08 -06:00
|
|
|
}
|
|
|
|
self.v[key] = Some(value);
|
|
|
|
!exists
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
|
|
|
|
2013-01-31 18:07:08 -06:00
|
|
|
/// Remove a key-value pair from the map. Return true if the key
|
|
|
|
/// was present in the map, otherwise false.
|
|
|
|
fn remove(&mut self, key: &uint) -> bool {
|
2013-05-04 08:54:58 -05:00
|
|
|
self.pop(key).is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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: V) -> Option<V> {
|
|
|
|
match self.find_mut(&key) {
|
|
|
|
Some(loc) => { return Some(replace(loc, value)); }
|
|
|
|
None => ()
|
|
|
|
}
|
|
|
|
self.insert(key, value);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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<V> {
|
2013-01-31 18:07:08 -06:00
|
|
|
if *key >= self.v.len() {
|
2013-05-04 08:54:58 -05:00
|
|
|
return None;
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
2013-07-17 16:41:50 -05:00
|
|
|
self.v[*key].take()
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
2013-01-31 18:07:08 -06:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl<V> SmallIntMap<V> {
|
2013-01-31 18:07:08 -06:00
|
|
|
/// Create an empty SmallIntMap
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn new() -> SmallIntMap<V> { SmallIntMap{v: ~[]} }
|
2013-01-31 18:07:08 -06:00
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn get<'a>(&'a self, key: &uint) -> &'a V {
|
2013-04-10 15:14:06 -05:00
|
|
|
self.find(key).expect("key not present")
|
|
|
|
}
|
2013-07-04 18:18:44 -05:00
|
|
|
|
|
|
|
/// An iterator visiting all key-value pairs in ascending order by the keys.
|
|
|
|
/// Iterator element type is (uint, &'r V)
|
|
|
|
pub fn iter<'r>(&'r self) -> SmallIntMapIterator<'r, V> {
|
|
|
|
SmallIntMapIterator {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An iterator visiting all key-value pairs in ascending order by the keys,
|
|
|
|
/// with mutable references to the values
|
|
|
|
/// Iterator element type is (uint, &'r mut V)
|
|
|
|
pub fn mut_iter<'r>(&'r mut self) -> SmallIntMapMutIterator<'r, V> {
|
|
|
|
SmallIntMapMutIterator {
|
2013-07-19 19:29:54 -05:00
|
|
|
front: 0,
|
|
|
|
back: self.v.len(),
|
|
|
|
iter: self.v.mut_iter()
|
2013-07-04 18:18:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An iterator visiting all key-value pairs in descending order by the keys.
|
|
|
|
/// Iterator element type is (uint, &'r V)
|
|
|
|
pub fn rev_iter<'r>(&'r self) -> SmallIntMapRevIterator<'r, V> {
|
2013-07-19 19:29:54 -05:00
|
|
|
self.iter().invert()
|
2013-07-04 18:18:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// An iterator visiting all key-value pairs in descending order by the keys,
|
|
|
|
/// with mutable references to the values
|
|
|
|
/// Iterator element type is (uint, &'r mut V)
|
|
|
|
pub fn mut_rev_iter<'r>(&'r mut self) -> SmallIntMapMutRevIterator <'r, V> {
|
2013-07-19 19:29:54 -05:00
|
|
|
self.mut_iter().invert()
|
2013-07-04 18:18:44 -05:00
|
|
|
}
|
2013-07-08 23:16:23 -05:00
|
|
|
|
|
|
|
/// Empties the hash map, moving all values into the specified closure
|
|
|
|
pub fn consume(&mut self)
|
2013-07-27 16:41:20 -05:00
|
|
|
-> FilterMap<(uint, Option<V>), (uint, V),
|
|
|
|
Enumerate<vec::ConsumeIterator<Option<V>>>>
|
2013-07-08 23:16:23 -05:00
|
|
|
{
|
|
|
|
let values = replace(&mut self.v, ~[]);
|
|
|
|
values.consume_iter().enumerate().filter_map(|(i, v)| {
|
2013-08-04 16:59:36 -05:00
|
|
|
v.map_move(|v| (i, v))
|
2013-07-08 23:16:23 -05:00
|
|
|
})
|
|
|
|
}
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
|
|
|
|
2013-07-02 14:47:32 -05:00
|
|
|
impl<V:Clone> SmallIntMap<V> {
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn update_with_key(&mut self, key: uint, val: V,
|
|
|
|
ff: &fn(uint, V, V) -> V) -> bool {
|
2013-02-09 00:21:45 -06:00
|
|
|
let new_val = match self.find(&key) {
|
|
|
|
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
|
|
|
};
|
|
|
|
self.insert(key, new_val)
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
2013-01-31 18:07:08 -06:00
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn update(&mut self, key: uint, newval: V, ff: &fn(V, V) -> V)
|
|
|
|
-> bool {
|
2013-01-31 18:07:08 -06:00
|
|
|
self.update_with_key(key, newval, |_k, v, v1| ff(v,v1))
|
|
|
|
}
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
|
|
|
|
2013-07-04 18:18:44 -05:00
|
|
|
|
|
|
|
macro_rules! iterator {
|
2013-07-19 19:29:54 -05:00
|
|
|
(impl $name:ident -> $elem:ty, $getter:ident) => {
|
|
|
|
impl<'self, T> Iterator<$elem> for $name<'self, 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;
|
|
|
|
return Some((index, elem. $getter ()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ()
|
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 {
|
|
|
|
(impl $name:ident -> $elem:ty, $getter:ident) => {
|
|
|
|
impl<'self, T> DoubleEndedIterator<$elem> for $name<'self, 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;
|
|
|
|
return Some((self.back, elem. $getter ()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ()
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SmallIntMapIterator<'self, T> {
|
2013-07-19 19:29:54 -05:00
|
|
|
priv front: uint,
|
|
|
|
priv back: uint,
|
|
|
|
priv iter: VecIterator<'self, Option<T>>
|
2013-07-04 18:18:44 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 19:29:54 -05:00
|
|
|
iterator!(impl SmallIntMapIterator -> (uint, &'self T), get_ref)
|
|
|
|
double_ended_iterator!(impl SmallIntMapIterator -> (uint, &'self T), get_ref)
|
2013-07-27 16:41:20 -05:00
|
|
|
pub type SmallIntMapRevIterator<'self, T> = Invert<SmallIntMapIterator<'self, T>>;
|
2013-07-04 18:18:44 -05:00
|
|
|
|
|
|
|
pub struct SmallIntMapMutIterator<'self, T> {
|
2013-07-19 19:29:54 -05:00
|
|
|
priv front: uint,
|
|
|
|
priv back: uint,
|
|
|
|
priv iter: VecMutIterator<'self, Option<T>>
|
2013-07-04 18:18:44 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 19:29:54 -05:00
|
|
|
iterator!(impl SmallIntMapMutIterator -> (uint, &'self mut T), get_mut_ref)
|
|
|
|
double_ended_iterator!(impl SmallIntMapMutIterator -> (uint, &'self mut T), get_mut_ref)
|
2013-07-27 16:41:20 -05:00
|
|
|
pub type SmallIntMapMutRevIterator<'self, T> = Invert<SmallIntMapMutIterator<'self, T>>;
|
2013-07-04 18:18:44 -05:00
|
|
|
|
2013-01-31 17:56:12 -06:00
|
|
|
#[cfg(test)]
|
2013-07-19 19:29:54 -05:00
|
|
|
mod test_map {
|
2013-05-21 19:24:31 -05:00
|
|
|
|
2013-01-31 18:07:08 -06:00
|
|
|
use super::SmallIntMap;
|
2013-03-24 19:35:23 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_find_mut() {
|
|
|
|
let mut m = SmallIntMap::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:35:23 -05:00
|
|
|
let new = 100;
|
|
|
|
match m.find_mut(&5) {
|
|
|
|
None => fail!(), Some(x) => *x = new
|
|
|
|
}
|
|
|
|
assert_eq!(m.find(&5), Some(&new));
|
|
|
|
}
|
2013-01-31 17:56:12 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_len() {
|
2013-01-31 18:07:08 -06:00
|
|
|
let mut map = SmallIntMap::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());
|
|
|
|
assert!(map.insert(5, 20));
|
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());
|
|
|
|
assert!(map.insert(11, 12));
|
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());
|
|
|
|
assert!(map.insert(14, 22));
|
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() {
|
2013-01-31 18:07:08 -06:00
|
|
|
let mut map = SmallIntMap::new();
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(map.insert(5, 20));
|
|
|
|
assert!(map.insert(11, 12));
|
|
|
|
assert!(map.insert(14, 22));
|
2013-01-31 17:56:12 -06:00
|
|
|
map.clear();
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(map.is_empty());
|
|
|
|
assert!(map.find(&5).is_none());
|
|
|
|
assert!(map.find(&11).is_none());
|
|
|
|
assert!(map.find(&14).is_none());
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_insert_with_key() {
|
2013-01-31 18:07:08 -06:00
|
|
|
let mut map = SmallIntMap::new();
|
2013-01-31 17:56:12 -06:00
|
|
|
|
|
|
|
// given a new key, initialize it with this new count, given
|
|
|
|
// given an existing key, add more to its count
|
|
|
|
fn addMoreToCount(_k: uint, v0: uint, v1: uint) -> uint {
|
|
|
|
v0 + v1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn addMoreToCount_simple(v0: uint, v1: uint) -> uint {
|
|
|
|
v0 + v1
|
|
|
|
}
|
|
|
|
|
|
|
|
// count integers
|
|
|
|
map.update(3, 1, addMoreToCount_simple);
|
|
|
|
map.update_with_key(9, 1, addMoreToCount);
|
|
|
|
map.update(3, 7, addMoreToCount_simple);
|
|
|
|
map.update_with_key(5, 3, addMoreToCount);
|
|
|
|
map.update_with_key(3, 2, addMoreToCount);
|
|
|
|
|
|
|
|
// check the total counts
|
2013-08-03 18:59:24 -05:00
|
|
|
assert_eq!(map.find(&3).unwrap(), &10);
|
|
|
|
assert_eq!(map.find(&5).unwrap(), &3);
|
|
|
|
assert_eq!(map.find(&9).unwrap(), &1);
|
2013-01-31 17:56:12 -06:00
|
|
|
|
|
|
|
// sadly, no sevens were counted
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(map.find(&7).is_none());
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
2013-05-04 08:54:58 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_swap() {
|
|
|
|
let mut m = SmallIntMap::new();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(m.swap(1, 2), None);
|
|
|
|
assert_eq!(m.swap(1, 3), Some(2));
|
|
|
|
assert_eq!(m.swap(1, 4), Some(3));
|
2013-05-04 08:54:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pop() {
|
|
|
|
let mut m = SmallIntMap::new();
|
|
|
|
m.insert(1, 2);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(m.pop(&1), Some(2));
|
|
|
|
assert_eq!(m.pop(&1), None);
|
2013-05-04 08:54:58 -05:00
|
|
|
}
|
2013-07-04 18:18:44 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator() {
|
2013-07-19 19:29:54 -05:00
|
|
|
let mut m = SmallIntMap::new();
|
2013-07-04 18:18:44 -05:00
|
|
|
|
2013-07-19 19:29:54 -05:00
|
|
|
assert!(m.insert(0, 1));
|
|
|
|
assert!(m.insert(1, 2));
|
|
|
|
assert!(m.insert(3, 5));
|
|
|
|
assert!(m.insert(6, 10));
|
|
|
|
assert!(m.insert(10, 11));
|
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() {
|
|
|
|
let mut m = SmallIntMap::new();
|
|
|
|
|
|
|
|
assert!(m.insert(0, 1));
|
|
|
|
assert!(m.insert(1, 2));
|
|
|
|
assert!(m.insert(3, 5));
|
|
|
|
assert!(m.insert(6, 10));
|
|
|
|
assert!(m.insert(10, 11));
|
|
|
|
|
|
|
|
assert_eq!(m.iter().size_hint(), (0, Some(11)));
|
|
|
|
assert_eq!(m.rev_iter().size_hint(), (0, Some(11)));
|
|
|
|
assert_eq!(m.mut_iter().size_hint(), (0, Some(11)));
|
|
|
|
assert_eq!(m.mut_rev_iter().size_hint(), (0, Some(11)));
|
|
|
|
}
|
|
|
|
|
2013-07-04 18:18:44 -05:00
|
|
|
#[test]
|
|
|
|
fn test_mut_iterator() {
|
2013-07-19 19:29:54 -05:00
|
|
|
let mut m = SmallIntMap::new();
|
2013-07-04 18:18:44 -05:00
|
|
|
|
2013-07-19 19:29:54 -05:00
|
|
|
assert!(m.insert(0, 1));
|
|
|
|
assert!(m.insert(1, 2));
|
|
|
|
assert!(m.insert(3, 5));
|
|
|
|
assert!(m.insert(6, 10));
|
|
|
|
assert!(m.insert(10, 11));
|
2013-07-04 18:18:44 -05:00
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for (k, v) in m.mut_iter() {
|
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() {
|
2013-07-19 19:29:54 -05:00
|
|
|
let mut m = SmallIntMap::new();
|
2013-07-04 18:18:44 -05:00
|
|
|
|
2013-07-19 19:29:54 -05:00
|
|
|
assert!(m.insert(0, 1));
|
|
|
|
assert!(m.insert(1, 2));
|
|
|
|
assert!(m.insert(3, 5));
|
|
|
|
assert!(m.insert(6, 10));
|
|
|
|
assert!(m.insert(10, 11));
|
2013-07-04 18:18:44 -05:00
|
|
|
|
2013-07-19 19:29:54 -05:00
|
|
|
let mut it = m.rev_iter();
|
|
|
|
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() {
|
2013-07-19 19:29:54 -05:00
|
|
|
let mut m = SmallIntMap::new();
|
2013-07-04 18:18:44 -05:00
|
|
|
|
2013-07-19 19:29:54 -05:00
|
|
|
assert!(m.insert(0, 1));
|
|
|
|
assert!(m.insert(1, 2));
|
|
|
|
assert!(m.insert(3, 5));
|
|
|
|
assert!(m.insert(6, 10));
|
|
|
|
assert!(m.insert(10, 11));
|
2013-07-04 18:18:44 -05:00
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for (k, v) in m.mut_rev_iter() {
|
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]
|
|
|
|
fn test_consume() {
|
|
|
|
let mut m = SmallIntMap::new();
|
|
|
|
m.insert(1, ~2);
|
|
|
|
let mut called = false;
|
2013-08-03 11:45:23 -05:00
|
|
|
for (k, v) in m.consume() {
|
2013-07-08 23:16:23 -05:00
|
|
|
assert!(!called);
|
|
|
|
called = true;
|
|
|
|
assert_eq!(k, 1);
|
|
|
|
assert_eq!(v, ~2);
|
|
|
|
}
|
|
|
|
assert!(called);
|
|
|
|
m.insert(2, ~1);
|
|
|
|
}
|
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 {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use test::BenchHarness;
|
|
|
|
use container::bench::*;
|
|
|
|
|
|
|
|
// Find seq
|
|
|
|
#[bench]
|
|
|
|
pub fn insert_rand_100(bh: &mut BenchHarness) {
|
|
|
|
let mut m : SmallIntMap<uint> = SmallIntMap::new();
|
|
|
|
insert_rand_n(100, &mut m, bh);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
pub fn insert_rand_10_000(bh: &mut BenchHarness) {
|
|
|
|
let mut m : SmallIntMap<uint> = SmallIntMap::new();
|
|
|
|
insert_rand_n(10_000, &mut m, bh);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert seq
|
|
|
|
#[bench]
|
|
|
|
pub fn insert_seq_100(bh: &mut BenchHarness) {
|
|
|
|
let mut m : SmallIntMap<uint> = SmallIntMap::new();
|
|
|
|
insert_seq_n(100, &mut m, bh);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
pub fn insert_seq_10_000(bh: &mut BenchHarness) {
|
|
|
|
let mut m : SmallIntMap<uint> = SmallIntMap::new();
|
|
|
|
insert_seq_n(10_000, &mut m, bh);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find rand
|
|
|
|
#[bench]
|
|
|
|
pub fn find_rand_100(bh: &mut BenchHarness) {
|
|
|
|
let mut m : SmallIntMap<uint> = SmallIntMap::new();
|
|
|
|
find_rand_n(100, &mut m, bh);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
pub fn find_rand_10_000(bh: &mut BenchHarness) {
|
|
|
|
let mut m : SmallIntMap<uint> = SmallIntMap::new();
|
|
|
|
find_rand_n(10_000, &mut m, bh);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find seq
|
|
|
|
#[bench]
|
|
|
|
pub fn find_seq_100(bh: &mut BenchHarness) {
|
|
|
|
let mut m : SmallIntMap<uint> = SmallIntMap::new();
|
|
|
|
find_seq_n(100, &mut m, bh);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
pub fn find_seq_10_000(bh: &mut BenchHarness) {
|
|
|
|
let mut m : SmallIntMap<uint> = SmallIntMap::new();
|
|
|
|
find_seq_n(10_000, &mut m, bh);
|
|
|
|
}
|
|
|
|
}
|