]> git.lizzy.rs Git - rust.git/commitdiff
Split mutable methods out of Set and Map
authorSteven Fackler <sfackler@gmail.com>
Sun, 14 Jul 2013 02:44:36 +0000 (19:44 -0700)
committerSteven Fackler <sfackler@gmail.com>
Sun, 14 Jul 2013 02:44:36 +0000 (19:44 -0700)
Fixes most of #4989. I didn't add Persistent{Set,Map} since the only
persistent data structure is fun_treemap and its functionality is
currently too limited to build a trait out of.

src/libextra/bitv.rs
src/libextra/smallintmap.rs
src/libextra/treemap.rs
src/libstd/container.rs
src/libstd/gc.rs
src/libstd/hashmap.rs
src/libstd/prelude.rs
src/libstd/task/spawn.rs
src/libstd/to_str.rs
src/libstd/trie.rs
src/test/run-pass/class-impl-very-parameterized-trait.rs

index 7a0db7a1de7fdb51b2d9946a36fdc59bbf50b817..49cedac399e0ce0b81367b72102259f846047ca4 100644 (file)
@@ -718,38 +718,6 @@ fn contains(&self, value: &uint) -> bool {
         *value < self.bitv.storage.len() * uint::bits && self.bitv.get(*value)
     }
 
-    fn insert(&mut self, value: uint) -> bool {
-        if self.contains(&value) {
-            return false;
-        }
-        let nbits = self.capacity();
-        if value >= nbits {
-            let newsize = num::max(value, nbits * 2) / uint::bits + 1;
-            assert!(newsize > self.bitv.storage.len());
-            self.bitv.storage.grow(newsize, &0);
-        }
-        self.size += 1;
-        self.bitv.set(value, true);
-        return true;
-    }
-
-    fn remove(&mut self, value: &uint) -> bool {
-        if !self.contains(value) {
-            return false;
-        }
-        self.size -= 1;
-        self.bitv.set(*value, false);
-
-        // Attempt to truncate our storage
-        let mut i = self.bitv.storage.len();
-        while i > 1 && self.bitv.storage[i - 1] == 0 {
-            i -= 1;
-        }
-        self.bitv.storage.truncate(i);
-
-        return true;
-    }
-
     fn is_disjoint(&self, other: &BitvSet) -> bool {
         for self.intersection(other) |_| {
             return false;
@@ -816,6 +784,40 @@ fn union(&self, other: &BitvSet, f: &fn(&uint) -> bool) -> bool {
     }
 }
 
+impl MutableSet<uint> for BitvSet {
+    fn insert(&mut self, value: uint) -> bool {
+        if self.contains(&value) {
+            return false;
+        }
+        let nbits = self.capacity();
+        if value >= nbits {
+            let newsize = num::max(value, nbits * 2) / uint::bits + 1;
+            assert!(newsize > self.bitv.storage.len());
+            self.bitv.storage.grow(newsize, &0);
+        }
+        self.size += 1;
+        self.bitv.set(value, true);
+        return true;
+    }
+
+    fn remove(&mut self, value: &uint) -> bool {
+        if !self.contains(value) {
+            return false;
+        }
+        self.size -= 1;
+        self.bitv.set(*value, false);
+
+        // Attempt to truncate our storage
+        let mut i = self.bitv.storage.len();
+        while i > 1 && self.bitv.storage[i - 1] == 0 {
+            i -= 1;
+        }
+        self.bitv.storage.truncate(i);
+
+        return true;
+    }
+}
+
 impl BitvSet {
     /// Visits each of the words that the two bit vectors (self and other)
     /// both have in common. The three yielded arguments are (bit location,
index d79a8cc426a6ebe675932371d0b42d8e152b4b23..27e9f8cd60f3307ed480a23d81fc4001c064e525 100644 (file)
@@ -17,8 +17,7 @@
 
 
 use std::cmp;
-use std::container::{Container, Mutable, Map, Set};
-use std::iterator::*;
+use std::iterator::{Iterator,IteratorUtil,ZipIterator,Counter,EnumerateIterator,FilterMapIterator};
 use std::uint;
 use std::util::replace;
 use std::vec::{VecIterator,VecMutIterator,VecRevIterator,VecMutRevIterator};
@@ -68,7 +67,9 @@ fn find<'a>(&'a self, key: &uint) -> Option<&'a V> {
             None
         }
     }
+}
 
+impl<V> MutableMap<uint, V> for SmallIntMap<V> {
     /// Return a mutable reference to the value corresponding to the key
     fn find_mut<'a>(&'a mut self, key: &uint) -> Option<&'a mut V> {
         if *key < self.v.len() {
@@ -349,14 +350,6 @@ impl Set<uint> for SmallIntSet {
     /// Return true if the set contains a value
     fn contains(&self, value: &uint) -> bool { self.map.contains_key(value) }
 
-    /// Add a value to the set. Return true if the value was not already
-    /// present in the set.
-    fn insert(&mut self, value: uint) -> bool { self.map.insert(value, ()) }
-
-    /// Remove a value from the set. Return true if the value was
-    /// present in the set.
-    fn remove(&mut self, value: &uint) -> bool { self.map.remove(value) }
-
     /// Return true if the set has no elements in common with `other`.
     /// This is equivalent to checking for an empty uintersection.
     fn is_disjoint(&self, other: &SmallIntSet) -> bool {
@@ -412,6 +405,16 @@ fn union(&self, other: &SmallIntSet, f: &fn(&uint) -> bool) -> bool {
     }
 }
 
+impl MutableSet<uint> for SmallIntSet {
+    /// Add a value to the set. Return true if the value was not already
+    /// present in the set.
+    fn insert(&mut self, value: uint) -> bool { self.map.insert(value, ()) }
+
+    /// Remove a value from the set. Return true if the value was
+    /// present in the set.
+    fn remove(&mut self, value: &uint) -> bool { self.map.remove(value) }
+}
+
 impl SmallIntSet {
     /// Create an empty SmallIntSet
     pub fn new() -> SmallIntSet { SmallIntSet{map: SmallIntMap::new()} }
index 8fac055d26f683f9aa07af851039caa77b681ccc..05a941b4925d006a92d7612d860f730e5e9b673a 100644 (file)
@@ -124,7 +124,9 @@ fn find<'a>(&'a self, key: &K) -> Option<&'a V> {
             }
         }
     }
+}
 
+impl<K: TotalOrd, V> MutableMap<K, V> for TreeMap<K, V> {
     /// Return a mutable reference to the value corresponding to the key
     #[inline]
     fn find_mut<'a>(&'a mut self, key: &K) -> Option<&'a mut V> {
@@ -293,16 +295,6 @@ fn contains(&self, value: &T) -> bool {
         self.map.contains_key(value)
     }
 
-    /// Add a value to the set. Return true if the value was not already
-    /// present in the set.
-    #[inline]
-    fn insert(&mut self, value: T) -> bool { self.map.insert(value, ()) }
-
-    /// Remove a value from the set. Return true if the value was
-    /// present in the set.
-    #[inline]
-    fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
-
     /// Return true if the set has no elements in common with `other`.
     /// This is equivalent to checking for an empty intersection.
     fn is_disjoint(&self, other: &TreeSet<T>) -> bool {
@@ -475,6 +467,18 @@ fn union(&self, other: &TreeSet<T>, f: &fn(&T) -> bool) -> bool {
     }
 }
 
+impl<T: TotalOrd> MutableSet<T> for TreeSet<T> {
+    /// Add a value to the set. Return true if the value was not already
+    /// present in the set.
+    #[inline]
+    fn insert(&mut self, value: T) -> bool { self.map.insert(value, ()) }
+
+    /// Remove a value from the set. Return true if the value was
+    /// present in the set.
+    #[inline]
+    fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
+}
+
 impl<T: TotalOrd> TreeSet<T> {
     /// Create an empty TreeSet
     #[inline]
index d6f4c26715a4f6e18c656ab322d6c596c57e7d24..4bad28ca3383081bf04f3f140168074268a138f6 100644 (file)
@@ -30,16 +30,16 @@ pub trait Mutable: Container {
 
 /// A map is a key-value store where values may be looked up by their keys. This
 /// trait provides basic operations to operate on these stores.
-pub trait Map<K, V>: Mutable {
+pub trait Map<K, V>: Container {
     /// Return true if the map contains a value for the specified key
     fn contains_key(&self, key: &K) -> bool;
 
     /// Return a reference to the value corresponding to the key
     fn find<'a>(&'a self, key: &K) -> Option<&'a V>;
+}
 
-    /// Return a mutable reference to the value corresponding to the key
-    fn find_mut<'a>(&'a mut self, key: &K) -> Option<&'a mut V>;
-
+/// This trait provides basic operations to modify the contents of a map.
+pub trait MutableMap<K, V>: Map<K, V> + Mutable {
     /// 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.
@@ -56,23 +56,18 @@ pub trait Map<K, V>: Mutable {
     /// Removes a key from the map, returning the value at the key if the key
     /// was previously in the map.
     fn pop(&mut self, k: &K) -> Option<V>;
+
+    /// Return a mutable reference to the value corresponding to the key
+    fn find_mut<'a>(&'a mut self, key: &K) -> Option<&'a mut V>;
 }
 
 /// A set is a group of objects which are each distinct from one another. This
-/// trait represents actions which can be performed on sets to manipulate and
-/// iterate over them.
-pub trait Set<T>: Mutable {
+/// trait represents actions which can be performed on sets to iterate over
+/// them.
+pub trait Set<T>: Container {
     /// Return true if the set contains a value
     fn contains(&self, value: &T) -> bool;
 
-    /// Add a value to the set. Return true if the value was not already
-    /// present in the set.
-    fn insert(&mut self, value: T) -> bool;
-
-    /// Remove a value from the set. Return true if the value was
-    /// present in the set.
-    fn remove(&mut self, value: &T) -> bool;
-
     /// Return true if the set has no elements in common with `other`.
     /// This is equivalent to checking for an empty intersection.
     fn is_disjoint(&self, other: &Self) -> bool;
@@ -95,3 +90,15 @@ pub trait Set<T>: Mutable {
     /// Visit the values representing the union
     fn union(&self, other: &Self, f: &fn(&T) -> bool) -> bool;
 }
+
+/// This trait represents actions which can be performed on sets to mutate
+/// them.
+pub trait MutableSet<T>: Set<T> + Mutable {
+    /// Add a value to the set. Return true if the value was not already
+    /// present in the set.
+    fn insert(&mut self, value: T) -> bool;
+
+    /// Remove a value from the set. Return true if the value was
+    /// present in the set.
+    fn remove(&mut self, value: &T) -> bool;
+}
index f92561edcb0863f66c3587b02abb9e8d3254775d..9d0588fdcc1b0f4434a5e686704af053659dbbee 100644 (file)
@@ -39,7 +39,7 @@
 */
 
 use cast;
-use container::{Map, Set};
+use container::{Set, MutableSet};
 use io;
 use libc::{uintptr_t};
 use option::{None, Option, Some};
index 6c93cd0dc86828c98ffbf84f05d12a5892789486..e95c63e891201c825656cfcb97da7e33cd13d777 100644 (file)
@@ -15,7 +15,7 @@
 
 #[mutable_doc];
 
-use container::{Container, Mutable, Map, Set};
+use container::{Container, Mutable, Map, MutableMap, Set, MutableSet};
 use cmp::{Eq, Equiv};
 use hash::Hash;
 use iterator::{Iterator, IteratorUtil};
@@ -316,7 +316,9 @@ fn find<'a>(&'a self, k: &K) -> Option<&'a V> {
             TableFull | FoundHole(_) => None,
         }
     }
+}
 
+impl<K:Hash + Eq,V> MutableMap<K, V> for HashMap<K, V> {
     /// Return a mutable reference to the value corresponding to the key
     fn find_mut<'a>(&'a mut self, k: &K) -> Option<&'a mut V> {
         let idx = match self.bucket_for_key(k) {
@@ -640,14 +642,6 @@ impl<T:Hash + Eq> Set<T> for HashSet<T> {
     /// Return true if the set contains a value
     fn contains(&self, value: &T) -> bool { self.map.contains_key(value) }
 
-    /// Add a value to the set. Return true if the value was not already
-    /// present in the set.
-    fn insert(&mut self, value: T) -> bool { self.map.insert(value, ()) }
-
-    /// Remove a value from the set. Return true if the value was
-    /// present in the set.
-    fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
-
     /// Return true if the set has no elements in common with `other`.
     /// This is equivalent to checking for an empty intersection.
     fn is_disjoint(&self, other: &HashSet<T>) -> bool {
@@ -688,6 +682,16 @@ fn union(&self, other: &HashSet<T>, f: &fn(&T) -> bool) -> bool {
     }
 }
 
+impl<T:Hash + Eq> MutableSet<T> for HashSet<T> {
+    /// Add a value to the set. Return true if the value was not already
+    /// present in the set.
+    fn insert(&mut self, value: T) -> bool { self.map.insert(value, ()) }
+
+    /// Remove a value from the set. Return true if the value was
+    /// present in the set.
+    fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
+}
+
 impl<T:Hash + Eq> HashSet<T> {
     /// Create an empty HashSet
     pub fn new() -> HashSet<T> {
index ea49144b7716c17d39423b9f6c6b0dc26e314e2e..8be34896bef6d4f797fc8b677cceb66806b51f5e 100644 (file)
@@ -45,7 +45,7 @@
 pub use clone::{Clone, DeepClone};
 pub use cmp::{Eq, ApproxEq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater, Equiv};
 pub use char::Char;
-pub use container::{Container, Mutable, Map, Set};
+pub use container::{Container, Mutable, Map, MutableMap, Set, MutableSet};
 pub use hash::Hash;
 pub use iter::Times;
 pub use iterator::{Iterator, IteratorUtil, DoubleEndedIterator, DoubleEndedIteratorUtil};
index f45d470a9f67a18c5fcdf364d867dfc4a26ae884..27cb1c2c1000a328959194383291f3886211a518 100644 (file)
@@ -77,7 +77,7 @@
 use cast::transmute;
 use cast;
 use cell::Cell;
-use container::Map;
+use container::MutableMap;
 use comm::{Chan, GenericChan};
 use hashmap::HashSet;
 use task::local_data_priv::{local_get, local_set, OldHandle};
index 77701acd33e2eec8d88ced57ae28556e9def5aa5..0c82df7e43a5ed023026052c630f6fbd4b8d36a2 100644 (file)
@@ -182,7 +182,7 @@ fn to_str(&self) -> ~str {
 mod tests {
     use hashmap::HashMap;
     use hashmap::HashSet;
-    use container::{Set, Map};
+    use container::{Set, Map, MutableSet, MutableMap};
     #[test]
     fn test_simple_types() {
         assert_eq!(1i.to_str(), ~"1");
index 8ce02d59ab15c6fab53db596d02086147da8da9e..c4bfda7e2bef0fa13fd4ec1dc1e9537de5f56908 100644 (file)
@@ -78,7 +78,9 @@ fn find<'a>(&'a self, key: &uint) -> Option<&'a T> {
             idx += 1;
         }
     }
+}
 
+impl<T> MutableMap<uint, T> for TrieMap<T> {
     /// Return a mutable reference to the value corresponding to the key
     #[inline]
     fn find_mut<'a>(&'a mut self, key: &uint) -> Option<&'a mut T> {
index 2805fec6fce2cda2a6afe8cec5bce4bed8c37e5c..a1ea1d6e3e2af9807a00b3c864ab8d97c8b0af9d 100644 (file)
@@ -11,7 +11,6 @@
 // xfail-fast
 
 use std::cmp;
-use std::container::{Container, Mutable, Map};
 use std::int;
 use std::uint;
 
@@ -63,11 +62,6 @@ fn clear(&mut self) {}
 impl<T> Map<int, T> for cat<T> {
     fn contains_key(&self, k: &int) -> bool { *k <= self.meows }
 
-    fn insert(&mut self, k: int, _: T) -> bool {
-        self.meows += k;
-        true
-    }
-
     fn find<'a>(&'a self, k: &int) -> Option<&'a T> {
         if *k <= self.meows {
             Some(&self.name)
@@ -75,6 +69,13 @@ fn find<'a>(&'a self, k: &int) -> Option<&'a T> {
             None
         }
     }
+}
+
+impl<T> MutableMap<int, T> for cat<T> {
+    fn insert(&mut self, k: int, _: T) -> bool {
+        self.meows += k;
+        true
+    }
 
     fn find_mut<'a>(&'a mut self, _k: &int) -> Option<&'a mut T> { fail!() }