Struct collections::vec_map::VecMap [] [src]

pub struct VecMap<V> {
    // some fields omitted
}
Unstable

A map optimized for small integer keys.

Examples

#![feature(collections)] extern crate collections; fn main() { use std::collections::VecMap; let mut months = VecMap::new(); months.insert(1, "Jan"); months.insert(2, "Feb"); months.insert(3, "Mar"); if !months.contains_key(&12) { println!("The end is near!"); } assert_eq!(months.get(&1), Some(&"Jan")); if let Some(value) = months.get_mut(&3) { *value = "Venus"; } assert_eq!(months.get(&3), Some(&"Venus")); // Print out all months for (key, value) in months.iter() { println!("month {} is {}", key, value); } months.clear(); assert!(months.is_empty()); }
use std::collections::VecMap;

let mut months = VecMap::new();
months.insert(1, "Jan");
months.insert(2, "Feb");
months.insert(3, "Mar");

if !months.contains_key(&12) {
    println!("The end is near!");
}

assert_eq!(months.get(&1), Some(&"Jan"));

if let Some(value) = months.get_mut(&3) {
    *value = "Venus";
}

assert_eq!(months.get(&3), Some(&"Venus"));

// Print out all months
for (key, value) in months.iter() {
    println!("month {} is {}", key, value);
}

months.clear();
assert!(months.is_empty());

Methods

impl<V> VecMap<V>

fn new() -> VecMap<V>

Creates an empty VecMap.

Examples

#![feature(collections)] extern crate collections; fn main() { use std::collections::VecMap; let mut map: VecMap<&str> = VecMap::new(); }
use std::collections::VecMap;
let mut map: VecMap<&str> = VecMap::new();

fn with_capacity(capacity: usize) -> VecMap<V>

Creates an empty VecMap with space for at least capacity elements before resizing.

Examples

#![feature(collections)] extern crate collections; fn main() { use std::collections::VecMap; let mut map: VecMap<&str> = VecMap::with_capacity(10); }
use std::collections::VecMap;
let mut map: VecMap<&str> = VecMap::with_capacity(10);

fn capacity(&self) -> usize

Returns the number of elements the VecMap can hold without reallocating.

Examples

#![feature(collections)] extern crate collections; fn main() { use std::collections::VecMap; let map: VecMap<String> = VecMap::with_capacity(10); assert!(map.capacity() >= 10); }
use std::collections::VecMap;
let map: VecMap<String> = VecMap::with_capacity(10);
assert!(map.capacity() >= 10);

fn reserve_len(&mut self, len: usize)

Reserves capacity for the given VecMap to contain len distinct keys. In the case of VecMap this means reallocations will not occur as long as all inserted keys are less than len.

The collection may reserve more space to avoid frequent reallocations.

Examples

#![feature(collections)] extern crate collections; fn main() { use std::collections::VecMap; let mut map: VecMap<&str> = VecMap::new(); map.reserve_len(10); assert!(map.capacity() >= 10); }
use std::collections::VecMap;
let mut map: VecMap<&str> = VecMap::new();
map.reserve_len(10);
assert!(map.capacity() >= 10);

fn reserve_len_exact(&mut self, len: usize)

Reserves the minimum capacity for the given VecMap to contain len distinct keys. In the case of VecMap this means reallocations will not occur as long as all inserted keys are less than len.

Note that the allocator may give the collection more space than it requests. Therefore capacity cannot be relied upon to be precisely minimal. Prefer reserve_len if future insertions are expected.

Examples

#![feature(collections)] extern crate collections; fn main() { use std::collections::VecMap; let mut map: VecMap<&str> = VecMap::new(); map.reserve_len_exact(10); assert!(map.capacity() >= 10); }
use std::collections::VecMap;
let mut map: VecMap<&str> = VecMap::new();
map.reserve_len_exact(10);
assert!(map.capacity() >= 10);

fn keys<'r>(&'r self) -> Keys<'r, V>

Returns an iterator visiting all keys in ascending order of the keys. The iterator's element type is usize.

fn values<'r>(&'r self) -> Values<'r, V>

Returns an iterator visiting all values in ascending order of the keys. The iterator's element type is &'r V.

fn iter<'r>(&'r self) -> Iter<'r, V>

Returns an iterator visiting all key-value pairs in ascending order of the keys. The iterator's element type is (usize, &'r V).

Examples

#![feature(collections)] extern crate collections; fn main() { use std::collections::VecMap; let mut map = VecMap::new(); 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); } }
use std::collections::VecMap;

let mut map = VecMap::new();
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);
}

fn iter_mut<'r>(&'r mut self) -> IterMut<'r, V>

Returns an iterator visiting all key-value pairs in ascending order of the keys, with mutable references to the values. The iterator's element type is (usize, &'r mut V).

Examples

#![feature(collections)] extern crate collections; fn main() { use std::collections::VecMap; let mut map = VecMap::new(); map.insert(1, "a"); map.insert(2, "b"); map.insert(3, "c"); for (key, value) in map.iter_mut() { *value = "x"; } for (key, value) in map.iter() { assert_eq!(value, &"x"); } }
use std::collections::VecMap;

let mut map = VecMap::new();
map.insert(1, "a");
map.insert(2, "b");
map.insert(3, "c");

for (key, value) in map.iter_mut() {
    *value = "x";
}

for (key, value) in map.iter() {
    assert_eq!(value, &"x");
}

fn append(&mut self, other: &mut Self)

Unstable

: recently added as part of collections reform 2

Moves all elements from other into the map while overwriting existing keys.

Examples

#![feature(collections)] extern crate collections; fn main() { use std::collections::VecMap; let mut a = VecMap::new(); a.insert(1, "a"); a.insert(2, "b"); let mut b = VecMap::new(); b.insert(3, "c"); b.insert(4, "d"); a.append(&mut b); assert_eq!(a.len(), 4); assert_eq!(b.len(), 0); assert_eq!(a[1], "a"); assert_eq!(a[2], "b"); assert_eq!(a[3], "c"); assert_eq!(a[4], "d"); }
use std::collections::VecMap;

let mut a = VecMap::new();
a.insert(1, "a");
a.insert(2, "b");

let mut b = VecMap::new();
b.insert(3, "c");
b.insert(4, "d");

a.append(&mut b);

assert_eq!(a.len(), 4);
assert_eq!(b.len(), 0);
assert_eq!(a[1], "a");
assert_eq!(a[2], "b");
assert_eq!(a[3], "c");
assert_eq!(a[4], "d");

fn split_off(&mut self, at: usize) -> Self

Unstable

: recently added as part of collections reform 2

Splits the collection into two at the given key.

Returns a newly allocated Self. self contains elements [0, at), and the returned Self contains elements [at, max_key).

Note that the capacity of self does not change.

Examples

#![feature(collections)] extern crate collections; fn main() { use std::collections::VecMap; let mut a = VecMap::new(); a.insert(1, "a"); a.insert(2, "b"); a.insert(3, "c"); a.insert(4, "d"); let b = a.split_off(3); assert_eq!(a[1], "a"); assert_eq!(a[2], "b"); assert_eq!(b[3], "c"); assert_eq!(b[4], "d"); }
use std::collections::VecMap;

let mut a = VecMap::new();
a.insert(1, "a");
a.insert(2, "b");
a.insert(3, "c");
a.insert(4, "d");

let b = a.split_off(3);

assert_eq!(a[1], "a");
assert_eq!(a[2], "b");

assert_eq!(b[3], "c");
assert_eq!(b[4], "d");

fn drain<'a>(&'a mut self) -> Drain<'a, V>

Unstable

: matches collection reform specification, waiting for dust to settle

Returns an iterator visiting all key-value pairs in ascending order of the keys, emptying (but not consuming) the original VecMap. The iterator's element type is (usize, &'r V). Keeps the allocated memory for reuse.

Examples

#![feature(collections)] extern crate collections; fn main() { use std::collections::VecMap; let mut map = VecMap::new(); map.insert(1, "a"); map.insert(3, "c"); map.insert(2, "b"); let vec: Vec<(usize, &str)> = map.drain().collect(); assert_eq!(vec, [(1, "a"), (2, "b"), (3, "c")]); }
use std::collections::VecMap;

let mut map = VecMap::new();
map.insert(1, "a");
map.insert(3, "c");
map.insert(2, "b");

let vec: Vec<(usize, &str)> = map.drain().collect();

assert_eq!(vec, [(1, "a"), (2, "b"), (3, "c")]);

fn len(&self) -> usize

Returns the number of elements in the map.

Examples

#![feature(collections)] extern crate collections; fn main() { use std::collections::VecMap; let mut a = VecMap::new(); assert_eq!(a.len(), 0); a.insert(1, "a"); assert_eq!(a.len(), 1); }
use std::collections::VecMap;

let mut a = VecMap::new();
assert_eq!(a.len(), 0);
a.insert(1, "a");
assert_eq!(a.len(), 1);

fn is_empty(&self) -> bool

Returns true if the map contains no elements.

Examples

#![feature(collections)] extern crate collections; fn main() { use std::collections::VecMap; let mut a = VecMap::new(); assert!(a.is_empty()); a.insert(1, "a"); assert!(!a.is_empty()); }
use std::collections::VecMap;

let mut a = VecMap::new();
assert!(a.is_empty());
a.insert(1, "a");
assert!(!a.is_empty());

fn clear(&mut self)

Clears the map, removing all key-value pairs.

Examples

#![feature(collections)] extern crate collections; fn main() { use std::collections::VecMap; let mut a = VecMap::new(); a.insert(1, "a"); a.clear(); assert!(a.is_empty()); }
use std::collections::VecMap;

let mut a = VecMap::new();
a.insert(1, "a");
a.clear();
assert!(a.is_empty());

fn get(&self, key: &usize) -> Option<&V>

Returns a reference to the value corresponding to the key.

Examples

#![feature(collections)] extern crate collections; fn main() { use std::collections::VecMap; let mut map = VecMap::new(); map.insert(1, "a"); assert_eq!(map.get(&1), Some(&"a")); assert_eq!(map.get(&2), None); }
use std::collections::VecMap;

let mut map = VecMap::new();
map.insert(1, "a");
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), None);

fn contains_key(&self, key: &usize) -> bool

Returns true if the map contains a value for the specified key.

Examples

#![feature(collections)] extern crate collections; fn main() { use std::collections::VecMap; let mut map = VecMap::new(); map.insert(1, "a"); assert_eq!(map.contains_key(&1), true); assert_eq!(map.contains_key(&2), false); }
use std::collections::VecMap;

let mut map = VecMap::new();
map.insert(1, "a");
assert_eq!(map.contains_key(&1), true);
assert_eq!(map.contains_key(&2), false);

fn get_mut(&mut self, key: &usize) -> Option<&mut V>

Returns a mutable reference to the value corresponding to the key.

Examples

#![feature(collections)] extern crate collections; fn main() { use std::collections::VecMap; let mut map = VecMap::new(); map.insert(1, "a"); if let Some(x) = map.get_mut(&1) { *x = "b"; } assert_eq!(map[1], "b"); }
use std::collections::VecMap;

let mut map = VecMap::new();
map.insert(1, "a");
if let Some(x) = map.get_mut(&1) {
    *x = "b";
}
assert_eq!(map[1], "b");

fn insert(&mut self, key: usize, value: V) -> Option<V>

Inserts a key-value pair into the map. If the key already had a value present in the map, that value is returned. Otherwise, None is returned.

Examples

#![feature(collections)] extern crate collections; fn main() { use std::collections::VecMap; let mut map = VecMap::new(); assert_eq!(map.insert(37, "a"), None); assert_eq!(map.is_empty(), false); map.insert(37, "b"); assert_eq!(map.insert(37, "c"), Some("b")); assert_eq!(map[37], "c"); }
use std::collections::VecMap;

let mut map = VecMap::new();
assert_eq!(map.insert(37, "a"), None);
assert_eq!(map.is_empty(), false);

map.insert(37, "b");
assert_eq!(map.insert(37, "c"), Some("b"));
assert_eq!(map[37], "c");

fn remove(&mut self, key: &usize) -> Option<V>

Removes a key from the map, returning the value at the key if the key was previously in the map.

Examples

#![feature(collections)] extern crate collections; fn main() { use std::collections::VecMap; let mut map = VecMap::new(); map.insert(1, "a"); assert_eq!(map.remove(&1), Some("a")); assert_eq!(map.remove(&1), None); }
use std::collections::VecMap;

let mut map = VecMap::new();
map.insert(1, "a");
assert_eq!(map.remove(&1), Some("a"));
assert_eq!(map.remove(&1), None);

fn entry(&mut self, key: usize) -> Entry<V>

Gets the given key's corresponding entry in the map for in-place manipulation.

Examples

#![feature(collections)] extern crate collections; fn main() { use std::collections::VecMap; let mut count: VecMap<u32> = VecMap::new(); // count the number of occurrences of numbers in the vec for x in vec![1, 2, 1, 2, 3, 4, 1, 2, 4] { *count.entry(x).or_insert(0) += 1; } assert_eq!(count[1], 3); }
use std::collections::VecMap;

let mut count: VecMap<u32> = VecMap::new();

// count the number of occurrences of numbers in the vec
for x in vec![1, 2, 1, 2, 3, 4, 1, 2, 4] {
    *count.entry(x).or_insert(0) += 1;
}

assert_eq!(count[1], 3);

Trait Implementations

impl<V> Default for VecMap<V>

fn default() -> VecMap<V>

impl<V: Clone> Clone for VecMap<V>

fn clone(&self) -> VecMap<V>

fn clone_from(&mut self, source: &VecMap<V>)

impl<V: Hash> Hash for VecMap<V>

fn hash<H: Hasher>(&self, state: &mut H)

fn hash_slice<H>(data: &[Self], state: &mut H) where H: Hasher

impl<V: PartialEq> PartialEq for VecMap<V>

fn eq(&self, other: &VecMap<V>) -> bool

fn ne(&self, other: &Rhs) -> bool

impl<V: Eq> Eq for VecMap<V>

impl<V: PartialOrd> PartialOrd for VecMap<V>

fn partial_cmp(&self, other: &VecMap<V>) -> Option<Ordering>

fn lt(&self, other: &Rhs) -> bool

fn le(&self, other: &Rhs) -> bool

fn gt(&self, other: &Rhs) -> bool

fn ge(&self, other: &Rhs) -> bool

impl<V: Ord> Ord for VecMap<V>

fn cmp(&self, other: &VecMap<V>) -> Ordering

impl<V: Debug> Debug for VecMap<V>

fn fmt(&self, f: &mut Formatter) -> Result

impl<V> FromIterator<(usize, V)> for VecMap<V>

fn from_iter<I: IntoIterator<Item=(usize, V)>>(iter: I) -> VecMap<V>

impl<T> IntoIterator for VecMap<T>

type Item = (usize, T)

type IntoIter = IntoIter<T>

fn into_iter(self) -> IntoIter<T>

impl<'a, T> IntoIterator for &'a VecMap<T>

type Item = (usize, &'a T)

type IntoIter = Iter<'a, T>

fn into_iter(self) -> Iter<'a, T>

impl<'a, T> IntoIterator for &'a mut VecMap<T>

type Item = (usize, &'a mut T)

type IntoIter = IterMut<'a, T>

fn into_iter(self) -> IterMut<'a, T>

impl<V> Extend<(usize, V)> for VecMap<V>

fn extend<I: IntoIterator<Item=(usize, V)>>(&mut self, iter: I)

impl<V> Index<usize> for VecMap<V>

type Output = V

fn index<'a>(&'a self, i: usize) -> &'a V

impl<'a, V> Index<&'a usize> for VecMap<V>

type Output = V

fn index(&self, i: &usize) -> &V

impl<V> IndexMut<usize> for VecMap<V>

fn index_mut(&mut self, i: usize) -> &mut V

impl<'a, V> IndexMut<&'a usize> for VecMap<V>

fn index_mut(&mut self, i: &usize) -> &mut V