diff options
| author | Steven Fackler <sfackler@gmail.com> | 2013-07-13 19:44:36 -0700 |
|---|---|---|
| committer | Steven Fackler <sfackler@gmail.com> | 2013-07-13 19:44:36 -0700 |
| commit | 6b37b5bab74a87c4f05274e6a49d9e28a4c0f4e1 (patch) | |
| tree | 1a92b4e674126ff3bf2a9e6204a25a2ae30071b4 /src/libstd | |
| parent | 8d0feb58e7f5ae67546db9c3cd7fdf4ab792d839 (diff) | |
| download | rust-6b37b5bab74a87c4f05274e6a49d9e28a4c0f4e1.tar.gz rust-6b37b5bab74a87c4f05274e6a49d9e28a4c0f4e1.zip | |
Split mutable methods out of Set and Map
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.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/container.rs | 37 | ||||
| -rw-r--r-- | src/libstd/gc.rs | 2 | ||||
| -rw-r--r-- | src/libstd/hashmap.rs | 22 | ||||
| -rw-r--r-- | src/libstd/prelude.rs | 2 | ||||
| -rw-r--r-- | src/libstd/task/spawn.rs | 2 | ||||
| -rw-r--r-- | src/libstd/to_str.rs | 2 | ||||
| -rw-r--r-- | src/libstd/trie.rs | 2 |
7 files changed, 41 insertions, 28 deletions
diff --git a/src/libstd/container.rs b/src/libstd/container.rs index d6f4c26715a..4bad28ca338 100644 --- a/src/libstd/container.rs +++ b/src/libstd/container.rs @@ -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; +} diff --git a/src/libstd/gc.rs b/src/libstd/gc.rs index f92561edcb0..9d0588fdcc1 100644 --- a/src/libstd/gc.rs +++ b/src/libstd/gc.rs @@ -39,7 +39,7 @@ with destructors. */ use cast; -use container::{Map, Set}; +use container::{Set, MutableSet}; use io; use libc::{uintptr_t}; use option::{None, Option, Some}; diff --git a/src/libstd/hashmap.rs b/src/libstd/hashmap.rs index 6c93cd0dc86..e95c63e8912 100644 --- a/src/libstd/hashmap.rs +++ b/src/libstd/hashmap.rs @@ -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 @@ impl<K:Hash + Eq,V> Map<K, V> for HashMap<K, 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 @@ impl<T:Hash + Eq> Set<T> for HashSet<T> { } } +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> { diff --git a/src/libstd/prelude.rs b/src/libstd/prelude.rs index ea49144b771..8be34896bef 100644 --- a/src/libstd/prelude.rs +++ b/src/libstd/prelude.rs @@ -45,7 +45,7 @@ pub use io::{print, println}; 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}; diff --git a/src/libstd/task/spawn.rs b/src/libstd/task/spawn.rs index f45d470a9f6..27cb1c2c100 100644 --- a/src/libstd/task/spawn.rs +++ b/src/libstd/task/spawn.rs @@ -77,7 +77,7 @@ use prelude::*; 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}; diff --git a/src/libstd/to_str.rs b/src/libstd/to_str.rs index 77701acd33e..0c82df7e43a 100644 --- a/src/libstd/to_str.rs +++ b/src/libstd/to_str.rs @@ -182,7 +182,7 @@ impl<A:ToStr> ToStr for @[A] { 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"); diff --git a/src/libstd/trie.rs b/src/libstd/trie.rs index 8ce02d59ab1..c4bfda7e2be 100644 --- a/src/libstd/trie.rs +++ b/src/libstd/trie.rs @@ -78,7 +78,9 @@ impl<T> Map<uint, T> for TrieMap<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> { |
