diff options
Diffstat (limited to 'src/libcollections/vec_map.rs')
| -rw-r--r-- | src/libcollections/vec_map.rs | 136 |
1 files changed, 75 insertions, 61 deletions
diff --git a/src/libcollections/vec_map.rs b/src/libcollections/vec_map.rs index 063366c8d9c..172fd56ed39 100644 --- a/src/libcollections/vec_map.rs +++ b/src/libcollections/vec_map.rs @@ -107,7 +107,7 @@ impl<V> VecMap<V> { /// use std::collections::VecMap; /// let mut map: VecMap<&str> = VecMap::new(); /// ``` - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] pub fn new() -> VecMap<V> { VecMap { v: vec![] } } /// Creates an empty `VecMap` with space for at least `capacity` @@ -119,7 +119,7 @@ impl<V> VecMap<V> { /// use std::collections::VecMap; /// let mut map: VecMap<&str> = VecMap::with_capacity(10); /// ``` - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] pub fn with_capacity(capacity: uint) -> VecMap<V> { VecMap { v: Vec::with_capacity(capacity) } } @@ -135,7 +135,7 @@ impl<V> VecMap<V> { /// assert!(map.capacity() >= 10); /// ``` #[inline] - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] pub fn capacity(&self) -> uint { self.v.capacity() } @@ -154,7 +154,7 @@ impl<V> VecMap<V> { /// map.reserve_len(10); /// assert!(map.capacity() >= 10); /// ``` - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] pub fn reserve_len(&mut self, len: uint) { let cur_len = self.v.len(); if len >= cur_len { @@ -178,7 +178,7 @@ impl<V> VecMap<V> { /// map.reserve_len_exact(10); /// assert!(map.capacity() >= 10); /// ``` - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] pub fn reserve_len_exact(&mut self, len: uint) { let cur_len = self.v.len(); if len >= cur_len { @@ -188,7 +188,7 @@ impl<V> VecMap<V> { /// Returns an iterator visiting all keys in ascending order by the keys. /// The iterator's element type is `uint`. - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] pub fn keys<'r>(&'r self) -> Keys<'r, V> { fn first<A, B>((a, _): (A, B)) -> A { a } let first: fn((uint, &'r V)) -> uint = first; // coerce to fn pointer @@ -198,7 +198,7 @@ impl<V> VecMap<V> { /// Returns an iterator visiting all values in ascending order by the keys. /// The iterator's element type is `&'r V`. - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] pub fn values<'r>(&'r self) -> Values<'r, V> { fn second<A, B>((_, b): (A, B)) -> B { b } let second: fn((uint, &'r V)) -> &'r V = second; // coerce to fn pointer @@ -224,7 +224,7 @@ impl<V> VecMap<V> { /// println!("{}: {}", key, value); /// } /// ``` - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] pub fn iter<'r>(&'r self) -> Iter<'r, V> { Iter { front: 0, @@ -255,7 +255,7 @@ impl<V> VecMap<V> { /// assert_eq!(value, &"x"); /// } /// ``` - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] pub fn iter_mut<'r>(&'r mut self) -> IterMut<'r, V> { IterMut { front: 0, @@ -283,7 +283,7 @@ impl<V> VecMap<V> { /// /// assert_eq!(vec, vec![(1, "a"), (2, "b"), (3, "c")]); /// ``` - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] pub fn into_iter(&mut self) -> IntoIter<V> { fn filter<A>((i, v): (uint, Option<A>)) -> Option<(uint, A)> { v.map(|v| (i, v)) @@ -306,7 +306,7 @@ impl<V> VecMap<V> { /// a.insert(1, "a"); /// assert_eq!(a.len(), 1); /// ``` - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] pub fn len(&self) -> uint { self.v.iter().filter(|elt| elt.is_some()).count() } @@ -323,7 +323,7 @@ impl<V> VecMap<V> { /// a.insert(1, "a"); /// assert!(!a.is_empty()); /// ``` - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] pub fn is_empty(&self) -> bool { self.v.iter().all(|elt| elt.is_none()) } @@ -340,7 +340,7 @@ impl<V> VecMap<V> { /// a.clear(); /// assert!(a.is_empty()); /// ``` - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] pub fn clear(&mut self) { self.v.clear() } /// Deprecated: Renamed to `get`. @@ -361,7 +361,7 @@ impl<V> VecMap<V> { /// assert_eq!(map.get(&1), Some(&"a")); /// assert_eq!(map.get(&2), None); /// ``` - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] pub fn get(&self, key: &uint) -> Option<&V> { if *key < self.v.len() { match self.v[*key] { @@ -386,7 +386,7 @@ impl<V> VecMap<V> { /// assert_eq!(map.contains_key(&2), false); /// ``` #[inline] - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] pub fn contains_key(&self, key: &uint) -> bool { self.get(key).is_some() } @@ -412,7 +412,7 @@ impl<V> VecMap<V> { /// } /// assert_eq!(map[1], "b"); /// ``` - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] pub fn get_mut(&mut self, key: &uint) -> Option<&mut V> { if *key < self.v.len() { match *(&mut self.v[*key]) { @@ -446,11 +446,11 @@ impl<V> VecMap<V> { /// assert_eq!(map.insert(37, "c"), Some("b")); /// assert_eq!(map[37], "c"); /// ``` - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] 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); + self.v.extend(range(0, key - len + 1).map(|_| None)); } replace(&mut self.v[key], Some(value)) } @@ -474,7 +474,7 @@ impl<V> VecMap<V> { /// assert_eq!(map.remove(&1), Some("a")); /// assert_eq!(map.remove(&1), None); /// ``` - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] pub fn remove(&mut self, key: &uint) -> Option<V> { if *key >= self.v.len() { return None; @@ -484,50 +484,15 @@ impl<V> VecMap<V> { } impl<V:Clone> VecMap<V> { - /// 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 - /// - /// ``` - /// 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]); - /// ``` + /// Deprecated: Use the entry API when available; shouldn't matter anyway, access is cheap. + #[deprecated = "Use the entry API when available; shouldn't matter anyway, access is cheap"] + #[allow(deprecated)] pub fn update<F>(&mut self, key: uint, newval: V, ff: F) -> bool where F: FnOnce(V, V) -> V { self.update_with_key(key, newval, move |_k, v, v1| ff(v,v1)) } - /// 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 - /// - /// ``` - /// 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); - /// ``` + /// Deprecated: Use the entry API when available; shouldn't matter anyway, access is cheap. + #[deprecated = "Use the entry API when available; shouldn't matter anyway, access is cheap"] pub fn update_with_key<F>(&mut self, key: uint, val: V, ff: F) -> bool where F: FnOnce(uint, V, V) -> V { @@ -539,14 +504,17 @@ impl<V:Clone> VecMap<V> { } } +#[stable] impl<V: PartialEq> PartialEq for VecMap<V> { fn eq(&self, other: &VecMap<V>) -> bool { iter::order::eq(self.iter(), other.iter()) } } +#[stable] impl<V: Eq> Eq for VecMap<V> {} +#[stable] impl<V: PartialOrd> PartialOrd for VecMap<V> { #[inline] fn partial_cmp(&self, other: &VecMap<V>) -> Option<Ordering> { @@ -554,6 +522,7 @@ impl<V: PartialOrd> PartialOrd for VecMap<V> { } } +#[stable] impl<V: Ord> Ord for VecMap<V> { #[inline] fn cmp(&self, other: &VecMap<V>) -> Ordering { @@ -561,6 +530,7 @@ impl<V: Ord> Ord for VecMap<V> { } } +#[stable] impl<V: fmt::Show> fmt::Show for VecMap<V> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { try!(write!(f, "{{")); @@ -574,6 +544,7 @@ impl<V: fmt::Show> fmt::Show for VecMap<V> { } } +#[stable] impl<V> FromIterator<(uint, V)> for VecMap<V> { fn from_iter<Iter: Iterator<(uint, V)>>(iter: Iter) -> VecMap<V> { let mut map = VecMap::new(); @@ -582,6 +553,7 @@ impl<V> FromIterator<(uint, V)> for VecMap<V> { } } +#[stable] impl<V> Extend<(uint, V)> for VecMap<V> { fn extend<Iter: Iterator<(uint, V)>>(&mut self, mut iter: Iter) { for (k, v) in iter { @@ -590,6 +562,7 @@ impl<V> Extend<(uint, V)> for VecMap<V> { } } +#[stable] impl<V> Index<uint, V> for VecMap<V> { #[inline] fn index<'a>(&'a self, i: &uint) -> &'a V { @@ -597,6 +570,7 @@ impl<V> Index<uint, V> for VecMap<V> { } } +#[stable] impl<V> IndexMut<uint, V> for VecMap<V> { #[inline] fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut V { @@ -606,6 +580,7 @@ impl<V> IndexMut<uint, V> for VecMap<V> { macro_rules! iterator { (impl $name:ident -> $elem:ty, $($getter:ident),+) => { + #[stable] impl<'a, V> Iterator<$elem> for $name<'a, V> { #[inline] fn next(&mut self) -> Option<$elem> { @@ -638,6 +613,7 @@ macro_rules! iterator { macro_rules! double_ended_iterator { (impl $name:ident -> $elem:ty, $($getter:ident),+) => { + #[stable] impl<'a, V> DoubleEndedIterator<$elem> for $name<'a, V> { #[inline] fn next_back(&mut self) -> Option<$elem> { @@ -663,17 +639,30 @@ macro_rules! double_ended_iterator { } /// An iterator over the key-value pairs of a map. +#[stable] pub struct Iter<'a, V:'a> { front: uint, back: uint, iter: slice::Iter<'a, Option<V>> } +// FIXME(#19839) Remove in favor of `#[deriving(Clone)]` +impl<'a, V> Clone for Iter<'a, V> { + fn clone(&self) -> Iter<'a, V> { + Iter { + front: self.front, + back: self.back, + iter: self.iter.clone() + } + } +} + iterator! { impl Iter -> (uint, &'a V), as_ref } double_ended_iterator! { impl Iter -> (uint, &'a V), as_ref } /// An iterator over the key-value pairs of a map, with the /// values being mutable. +#[stable] pub struct IterMut<'a, V:'a> { front: uint, back: uint, @@ -684,16 +673,37 @@ iterator! { impl IterMut -> (uint, &'a mut V), as_mut } double_ended_iterator! { impl IterMut -> (uint, &'a mut V), as_mut } /// An iterator over the keys of a map. +#[stable] pub struct Keys<'a, V: 'a> { iter: Map<(uint, &'a V), uint, Iter<'a, V>, fn((uint, &'a V)) -> uint> } +// FIXME(#19839) Remove in favor of `#[deriving(Clone)]` +impl<'a, V> Clone for Keys<'a, V> { + fn clone(&self) -> Keys<'a, V> { + Keys { + iter: self.iter.clone() + } + } +} + /// An iterator over the values of a map. +#[stable] pub struct Values<'a, V: 'a> { iter: Map<(uint, &'a V), &'a V, Iter<'a, V>, fn((uint, &'a V)) -> &'a V> } +// FIXME(#19839) Remove in favor of `#[deriving(Clone)]` +impl<'a, V> Clone for Values<'a, V> { + fn clone(&self) -> Values<'a, V> { + Values { + iter: self.iter.clone() + } + } +} + /// A consuming iterator over the key-value pairs of a map. +#[stable] pub struct IntoIter<V> { iter: FilterMap< (uint, Option<V>), @@ -702,28 +712,32 @@ pub struct IntoIter<V> { fn((uint, Option<V>)) -> Option<(uint, V)>> } +#[stable] impl<'a, V> Iterator<uint> for Keys<'a, V> { fn next(&mut self) -> Option<uint> { self.iter.next() } fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() } } +#[stable] impl<'a, V> DoubleEndedIterator<uint> for Keys<'a, V> { fn next_back(&mut self) -> Option<uint> { self.iter.next_back() } } - +#[stable] impl<'a, V> Iterator<&'a V> for Values<'a, V> { fn next(&mut self) -> Option<(&'a V)> { self.iter.next() } fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() } } +#[stable] impl<'a, V> DoubleEndedIterator<&'a V> for Values<'a, V> { fn next_back(&mut self) -> Option<(&'a V)> { self.iter.next_back() } } - +#[stable] impl<V> Iterator<(uint, V)> for IntoIter<V> { fn next(&mut self) -> Option<(uint, V)> { self.iter.next() } fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() } } +#[stable] impl<V> DoubleEndedIterator<(uint, V)> for IntoIter<V> { fn next_back(&mut self) -> Option<(uint, V)> { self.iter.next_back() } } |
