Struct std::collections::VecMapExperimental [-]  [+] [src]

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

A map optimized for small integer keys.

Examples

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")); match months.get_mut(&3) { Some(value) => *value = "Venus", None => (), } 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"));

match months.get_mut(&3) {
    Some(value) => *value = "Venus",
    None => (),
}

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

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: uint) -> VecMap<V>

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

Examples

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) -> uint

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

Examples

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: uint)

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

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: uint)

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

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 self) -> Keys<'r, V>

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

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

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

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

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

Examples

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 mut self) -> IterMut<'r, V>

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).

Examples

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 into_iter(&mut self) -> IntoIter<V>

Returns an iterator visiting all key-value pairs in ascending order by the keys, emptying (but not consuming) the original VecMap. The iterator's element type is (uint, &'r V).

Examples

fn main() { use std::collections::VecMap; let mut map = VecMap::new(); map.insert(1, "a"); map.insert(3, "c"); map.insert(2, "b"); // Not possible with .iter() let vec: Vec<(uint, &str)> = map.into_iter().collect(); assert_eq!(vec, 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");

// Not possible with .iter()
let vec: Vec<(uint, &str)> = map.into_iter().collect();

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

fn len(&self) -> uint

Return the number of elements in the map.

Examples

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

Return true if the map contains no elements.

Examples

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

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 find(&self, key: &uint) -> Option<&V>

Deprecated: Renamed to get.

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

Returns a reference to the value corresponding to the key.

Examples

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: &uint) -> bool

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

Examples

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 find_mut(&mut self, key: &uint) -> Option<&mut V>

Deprecated: Renamed to get_mut.

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

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

Examples

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

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

fn swap(&mut self, key: uint, value: V) -> Option<V>

Deprecated: Renamed to insert.

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

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.

Examples

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 pop(&mut self, key: &uint) -> Option<V>

Deprecated: Renamed to remove.

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

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

Examples

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);

impl<V: Clone> VecMap<V>

fn update<F>(&mut self, key: uint, newval: V, ff: F) -> bool

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. Returns true if the key did not already exist in the map.

Examples

fn main() { use std::collections::VecMap; let mut map = VecMap::new(); // Key does not exist, will do a simple insert assert!(map.update(1, vec![1i, 2], |mut old, new| { old.extend(new.into_iter()); old })); assert_eq!(map[1], vec![1i, 2]); // Key exists, update the value assert!(!map.update(1, vec![3i, 4], |mut old, new| { old.extend(new.into_iter()); old })); assert_eq!(map[1], vec![1i, 2, 3, 4]); }
use std::collections::VecMap;

let mut map = VecMap::new();

// Key does not exist, will do a simple insert
assert!(map.update(1, vec![1i, 2], |mut old, new| { old.extend(new.into_iter()); old }));
assert_eq!(map[1], vec![1i, 2]);

// Key exists, update the value
assert!(!map.update(1, vec![3i, 4], |mut old, new| { old.extend(new.into_iter()); old }));
assert_eq!(map[1], vec![1i, 2, 3, 4]);

fn update_with_key<F>(&mut self, key: uint, val: V, ff: F) -> bool

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.

Examples

fn main() { use std::collections::VecMap; let mut map = VecMap::new(); // Key does not exist, will do a simple insert assert!(map.update_with_key(7, 10, |key, old, new| (old + new) % key)); assert_eq!(map[7], 10); // Key exists, update the value assert!(!map.update_with_key(7, 20, |key, old, new| (old + new) % key)); assert_eq!(map[7], 2); }
use std::collections::VecMap;

let mut map = VecMap::new();

// Key does not exist, will do a simple insert
assert!(map.update_with_key(7, 10, |key, old, new| (old + new) % key));
assert_eq!(map[7], 10);

// Key exists, update the value
assert!(!map.update_with_key(7, 20, |key, old, new| (old + new) % key));
assert_eq!(map[7], 2);

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>)

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

impl<S: Writer, V: Hash<S>> Hash<S> for VecMap<V>

fn hash(&self, state: &mut S)

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

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

fn ne(&self, &VecMap<V>) -> bool

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

fn assert_receiver_is_total_eq(&self)

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

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

fn lt(&self, &VecMap<V>) -> bool

fn le(&self, &VecMap<V>) -> bool

fn gt(&self, &VecMap<V>) -> bool

fn ge(&self, &VecMap<V>) -> bool

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

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

impl<V: Show> Show for VecMap<V>

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

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

fn from_iter<Iter: Iterator<(uint, V)>>(iter: Iter) -> VecMap<V>

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

fn extend<Iter: Iterator<(uint, V)>>(&mut self, iter: Iter)

impl<V> Index<uint, V> for VecMap<V>

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

impl<V> IndexMut<uint, V> for VecMap<V>

fn index_mut(&'a mut self, i: &uint) -> &'a mut V