Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change collect behaviour for maps #17111

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions src/libcollections/smallintmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,17 +405,26 @@ impl<V: fmt::Show> fmt::Show for SmallIntMap<V> {
}
}

impl<V> FromIterator<(uint, V)> for SmallIntMap<V> {
fn from_iter<Iter: Iterator<(uint, V)>>(iter: Iter) -> SmallIntMap<V> {
impl<V, U: Extendable<V>> FromIterator<(uint, V)> for SmallIntMap<U> {
fn from_iter<Iter: Iterator<(uint, V)>>(iter: Iter) -> SmallIntMap<U> {
let mut map = SmallIntMap::new();
map.extend(iter);
map
}
}

impl<V> Extendable<(uint, V)> for SmallIntMap<V> {
impl<V, U: Extendable<V>> Extendable<(uint, V)> for SmallIntMap<U> {
fn extend<Iter: Iterator<(uint, V)>>(&mut self, mut iter: Iter) {
for (k, v) in iter {
match self.find_mut(&k) {
Some(found) => {
found.extend(Some(v).move_iter());
continue
},
None => {} // Insert below
}

let v = FromIterator::from_iter(Some(v).move_iter());
self.insert(k, v);
}
}
Expand Down Expand Up @@ -856,12 +865,21 @@ mod test_map {

#[test]
fn test_from_iter() {
let xs: Vec<(uint, char)> = vec![(1u, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')];
let xs: Vec<(uint, char)> = vec![(1u, 'a'), (2, 'b'), (3, 'c'),
(4, 'd'), (5, 'e'), (3, 'f')];

let expected = vec![
(1u, vec!['a']),
(2u, vec!['b']),
(3u, vec!['c', 'f']),
(4u, vec!['d']),
(5u, vec!['e']),
];

let map: SmallIntMap<char> = xs.iter().map(|&x| x).collect();
let map: SmallIntMap<Vec<char>> = xs.iter().map(|&x| x).collect();

for &(k, v) in xs.iter() {
assert_eq!(map.find(&k), Some(&v));
for &(k, ref v) in expected.iter() {
assert_eq!(map.find(&k), Some(v))
}
}

Expand Down
36 changes: 26 additions & 10 deletions src/libcollections/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1617,18 +1617,27 @@ fn remove<K: Ord, V>(node: &mut Option<Box<TreeNode<K, V>>>,
};
}

impl<K: Ord, V> FromIterator<(K, V)> for TreeMap<K, V> {
fn from_iter<T: Iterator<(K, V)>>(iter: T) -> TreeMap<K, V> {
impl<K: Ord, V, U: Extendable<V>> FromIterator<(K, V)> for TreeMap<K, U> {
fn from_iter<T: Iterator<(K, V)>>(iter: T) -> TreeMap<K, U> {
let mut map = TreeMap::new();
map.extend(iter);
map
}
}

impl<K: Ord, V> Extendable<(K, V)> for TreeMap<K, V> {
impl<K: Ord, V, U: Extendable<V>> Extendable<(K, V)> for TreeMap<K, U> {
#[inline]
fn extend<T: Iterator<(K, V)>>(&mut self, mut iter: T) {
for (k, v) in iter {
match self.find_mut(&k) {
Some(found) => {
found.extend(Some(v).move_iter());
continue
}
None => {}
}

let v = FromIterator::from_iter(Some(v).move_iter());
self.insert(k, v);
}
}
Expand Down Expand Up @@ -2020,7 +2029,11 @@ mod test_treemap {
#[test]
fn test_keys() {
let vec = vec![(1i, 'a'), (2i, 'b'), (3i, 'c')];
let map = vec.move_iter().collect::<TreeMap<int, char>>();
let mut map = TreeMap::new();
for (k, v) in vec.move_iter() {
map.insert(k, v);
}

let keys = map.keys().map(|&k| k).collect::<Vec<int>>();
assert_eq!(keys.len(), 3);
assert!(keys.contains(&1));
Expand All @@ -2031,7 +2044,11 @@ mod test_treemap {
#[test]
fn test_values() {
let vec = vec![(1i, 'a'), (2i, 'b'), (3i, 'c')];
let map = vec.move_iter().collect::<TreeMap<int, char>>();
let mut map = TreeMap::new();
for (k, v) in vec.move_iter() {
map.insert(k, v);
}

let values = map.values().map(|&v| v).collect::<Vec<char>>();
assert_eq!(values.len(), 3);
assert!(values.contains(&'a'));
Expand Down Expand Up @@ -2152,13 +2169,12 @@ mod test_treemap {

#[test]
fn test_from_iter() {
let xs = [(1i, 1i), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
let xs = vec![(1, 'H'), (2, 'B'), (3, 'i'), (4, 'y'), (5, '!'), (6, 'e')];

let map: TreeMap<int, int> = xs.iter().map(|&x| x).collect();
let map = xs.iter().map(|&(k, v)| (k % 2, v)).collect::<TreeMap<int, String>>();

for &(k, v) in xs.iter() {
assert_eq!(map.find(&k), Some(&v));
}
assert_eq!(map.find(&1).map(|s| s.as_slice()), Some("Hi!"));
assert_eq!(map.find(&0).map(|s| s.as_slice()), Some("Bye"));
}

#[test]
Expand Down
71 changes: 54 additions & 17 deletions src/libcollections/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,10 @@ impl<T> TrieMap<T> {
///
/// ```
/// use std::collections::TrieMap;
/// let map: TrieMap<&str> = [(1, "a"), (2, "b"), (3, "c")].iter().map(|&x| x).collect();
/// let mut map = TrieMap::<&str>::new();
/// for &(k, v) in [(1, "a"), (2, "b"), (3, "c")].iter() {
/// map.insert(k, v);
/// }
///
/// let mut vec = Vec::new();
/// assert_eq!(true, map.each_reverse(|&key, &value| { vec.push((key, value)); true }));
Expand Down Expand Up @@ -246,7 +249,10 @@ impl<T> TrieMap<T> {
///
/// ```
/// use std::collections::TrieMap;
/// let map: TrieMap<&str> = [(3, "c"), (1, "a"), (2, "b")].iter().map(|&x| x).collect();
/// let mut map = TrieMap::<&str>::new();
/// for &(k, v) in [(3, "c"), (1, "a"), (2, "b")].iter() {
/// map.insert(k, v);
/// }
///
/// for (key, value) in map.iter() {
/// println!("{}: {}", key, value);
Expand All @@ -269,7 +275,10 @@ impl<T> TrieMap<T> {
///
/// ```
/// use std::collections::TrieMap;
/// let mut map: TrieMap<int> = [(1, 2), (2, 4), (3, 6)].iter().map(|&x| x).collect();
/// let mut map = TrieMap::<int>::new();
/// for &(k, v) in [(1, 2), (2, 4), (3, 6)].iter() {
/// map.insert(k, v);
/// }
///
/// for (key, value) in map.mut_iter() {
/// *value = -(key as int);
Expand Down Expand Up @@ -392,7 +401,10 @@ impl<T> TrieMap<T> {
///
/// ```
/// use std::collections::TrieMap;
/// let map: TrieMap<&str> = [(2, "a"), (4, "b"), (6, "c")].iter().map(|&x| x).collect();
/// let mut map = TrieMap::<&str>::new();
/// for &(k, v) in [(2, "a"), (4, "b"), (6, "c")].iter() {
/// map.insert(k, v);
/// }
///
/// assert_eq!(map.lower_bound(4).next(), Some((4, &"b")));
/// assert_eq!(map.lower_bound(5).next(), Some((6, &"c")));
Expand All @@ -409,7 +421,10 @@ impl<T> TrieMap<T> {
///
/// ```
/// use std::collections::TrieMap;
/// let map: TrieMap<&str> = [(2, "a"), (4, "b"), (6, "c")].iter().map(|&x| x).collect();
/// let mut map = TrieMap::<&str>::new();
/// for &(k, v) in [(2, "a"), (4, "b"), (6, "c")].iter() {
/// map.insert(k, v);
/// }
///
/// assert_eq!(map.upper_bound(4).next(), Some((6, &"c")));
/// assert_eq!(map.upper_bound(5).next(), Some((6, &"c")));
Expand All @@ -434,7 +449,10 @@ impl<T> TrieMap<T> {
///
/// ```
/// use std::collections::TrieMap;
/// let mut map: TrieMap<&str> = [(2, "a"), (4, "b"), (6, "c")].iter().map(|&x| x).collect();
/// let mut map = TrieMap::<&str>::new();
/// for &(k, v) in [(2, "a"), (4, "b"), (6, "c")].iter() {
/// map.insert(k, v);
/// }
///
/// assert_eq!(map.mut_lower_bound(4).next(), Some((4, &mut "b")));
/// assert_eq!(map.mut_lower_bound(5).next(), Some((6, &mut "c")));
Expand All @@ -459,7 +477,10 @@ impl<T> TrieMap<T> {
///
/// ```
/// use std::collections::TrieMap;
/// let mut map: TrieMap<&str> = [(2, "a"), (4, "b"), (6, "c")].iter().map(|&x| x).collect();
/// let mut map = TrieMap::<&str>::new();
/// for &(k, v) in [(2, "a"), (4, "b"), (6, "c")].iter() {
/// map.insert(k, v);
/// }
///
/// assert_eq!(map.mut_upper_bound(4).next(), Some((6, &mut "c")));
/// assert_eq!(map.mut_upper_bound(5).next(), Some((6, &mut "c")));
Expand All @@ -478,17 +499,26 @@ impl<T> TrieMap<T> {
}
}

impl<T> FromIterator<(uint, T)> for TrieMap<T> {
fn from_iter<Iter: Iterator<(uint, T)>>(iter: Iter) -> TrieMap<T> {
impl<T, U: Extendable<T>> FromIterator<(uint, T)> for TrieMap<U> {
fn from_iter<Iter: Iterator<(uint, T)>>(iter: Iter) -> TrieMap<U> {
let mut map = TrieMap::new();
map.extend(iter);
map
}
}

impl<T> Extendable<(uint, T)> for TrieMap<T> {
impl<T, U: Extendable<T>> Extendable<(uint, T)> for TrieMap<U> {
fn extend<Iter: Iterator<(uint, T)>>(&mut self, mut iter: Iter) {
for (k, v) in iter {
match self.find_mut(&k) {
Some(found) => {
found.extend(Some(v).move_iter());
continue
}
None => {}
}

let v = FromIterator::from_iter(Some(v).move_iter());
self.insert(k, v);
}
}
Expand Down Expand Up @@ -1160,19 +1190,22 @@ mod test_map {

#[test]
fn test_from_iter() {
let xs = vec![(1u, 1i), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
let xs = vec![(1, 'H'), (2, 'B'), (3, 'i'), (4, 'y'), (5, '!'), (6, 'e')];

let map: TrieMap<int> = xs.iter().map(|&x| x).collect();
let map = xs.iter().map(|&(k, v)| (k % 2, v)).collect::<TrieMap<String>>();

for &(k, v) in xs.iter() {
assert_eq!(map.find(&k), Some(&v));
}
assert_eq!(map.find(&1).map(|s| s.as_slice()), Some("Hi!"));
assert_eq!(map.find(&0).map(|s| s.as_slice()), Some("Bye"));
}

#[test]
fn test_keys() {
let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')];
let map = vec.move_iter().collect::<TrieMap<char>>();
let mut map = TrieMap::new();
for (k, v) in vec.move_iter() {
map.insert(k, v);
}

let keys = map.keys().collect::<Vec<uint>>();
assert_eq!(keys.len(), 3);
assert!(keys.contains(&1));
Expand All @@ -1183,7 +1216,11 @@ mod test_map {
#[test]
fn test_values() {
let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')];
let map = vec.move_iter().collect::<TrieMap<char>>();
let mut map = TrieMap::new();
for (k, v) in vec.move_iter() {
map.insert(k, v);
}

let values = map.values().map(|&v| v).collect::<Vec<char>>();
assert_eq!(values.len(), 3);
assert!(values.contains(&'a'));
Expand Down
13 changes: 6 additions & 7 deletions src/librustc/lint/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,13 @@ impl LintStore {
match self.by_name.find_equiv(&lint_name.as_slice()) {
Some(&lint_id) => self.set_level(lint_id, (level, CommandLine)),
None => {
match self.lint_groups.iter().map(|(&x, pair)| (x, pair.ref0().clone()))
.collect::<HashMap<&'static str, Vec<LintId>>>()
.find_equiv(&lint_name.as_slice()) {
match self.lint_groups.iter()
.find(|&(&x, _)| x == lint_name.as_slice())
.map(|(_, pair)| pair.ref0().clone()) {
Some(v) => {
v.iter()
.map(|lint_id: &LintId|
self.set_level(*lint_id, (level, CommandLine)))
.collect::<Vec<()>>();
for &lint_id in v.iter() {
self.set_level(lint_id, (level, CommandLine))
}
}
None => sess.err(format!("unknown {} flag: {}",
level.as_str(), lint_name).as_slice()),
Expand Down
8 changes: 5 additions & 3 deletions src/librustc/metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,17 @@ fn resolve_crate_deps(e: &mut Env,
debug!("resolving deps of external crate");
// The map from crate numbers in the crate we're resolving to local crate
// numbers
decoder::get_crate_deps(cdata).iter().map(|dep| {
let mut map = HashMap::new();
for dep in decoder::get_crate_deps(cdata).iter() {
debug!("resolving dep crate {} hash: `{}`", dep.name, dep.hash);
let (local_cnum, _, _) = resolve_crate(e, root,
dep.name.as_slice(),
dep.name.as_slice(),
Some(&dep.hash),
span);
(dep.cnum, local_cnum)
}).collect()
map.insert(dep.cnum, local_cnum);
}
map
}

pub struct PluginMetadataReader<'a> {
Expand Down
27 changes: 17 additions & 10 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,25 +295,32 @@ pub fn run(mut krate: clean::Crate, external_html: &ExternalHtml, dst: Path) ->
let analysis = ::analysiskey.get();
let public_items = analysis.as_ref().map(|a| a.public_items.clone());
let public_items = public_items.unwrap_or(NodeSet::new());
let paths: HashMap<ast::DefId, (Vec<String>, ItemType)> =
analysis.as_ref().map(|a| {
let paths = a.external_paths.borrow_mut().take().unwrap();
paths.move_iter().map(|(k, (v, t))| {
(k, (v, match t {
let mut paths = HashMap::<ast::DefId, (Vec<String>, ItemType)>::new();

for a in analysis.as_ref().iter() {
let ext_paths = a.external_paths.borrow_mut().take().unwrap();

for (k, (v, t)) in ext_paths.move_iter() {
paths.insert(k, (v, match t {
clean::TypeStruct => item_type::Struct,
clean::TypeEnum => item_type::Enum,
clean::TypeFunction => item_type::Function,
clean::TypeTrait => item_type::Trait,
clean::TypeModule => item_type::Module,
clean::TypeStatic => item_type::Static,
clean::TypeVariant => item_type::Variant,
}))
}).collect()
}).unwrap_or(HashMap::new());
}));
}
}

let mut external_paths = HashMap::new();
for (&k, v) in paths.iter() {
external_paths.insert(k, v.ref0().clone());
}

let mut cache = Cache {
impls: HashMap::new(),
external_paths: paths.iter().map(|(&k, v)| (k, v.ref0().clone()))
.collect(),
external_paths: external_paths,
paths: paths,
implementors: HashMap::new(),
stack: Vec::new(),
Expand Down
Loading