Skip to content

hashmap: Remove .consume() methods #7833

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

Closed
wants to merge 1 commit 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
3 changes: 1 addition & 2 deletions src/libextra/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1086,9 +1086,8 @@ impl serialize::Decoder for Decoder {
debug!("read_map()");
let len = match self.stack.pop() {
Object(obj) => {
let mut obj = obj;
let len = obj.len();
do obj.consume |key, value| {
for obj.consume().advance |(key, value)| {
self.stack.push(value);
self.stack.push(String(key));
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ Available lint options:
"));

let lint_dict = lint::get_lint_dict();
let mut lint_dict = lint_dict.consume_iter()
let mut lint_dict = lint_dict.consume()
.transform(|(k, v)| (v, k))
.collect::<~[(lint::LintSpec, &'static str)]>();
lint_dict.qsort();
Expand Down
10 changes: 7 additions & 3 deletions src/librusti/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// except according to those terms.

use std::cast;
use std::util;
use std::hashmap::HashMap;
use std::local_data;

Expand Down Expand Up @@ -165,7 +166,8 @@ impl Program {
None => {}
}

do self.newvars.consume |name, var| {
let newvars = util::replace(&mut self.newvars, HashMap::new());
for newvars.consume().advance |(name, var)| {
self.local_vars.insert(name, var);
}

Expand Down Expand Up @@ -230,7 +232,8 @@ impl Program {
/// it updates this cache with the new values of each local variable.
pub fn consume_cache(&mut self) {
let map = local_data::pop(tls_key).expect("tls is empty");
do map.consume |name, value| {
let cons_map = util::replace(map, HashMap::new());
for cons_map.consume().advance |(name, value)| {
match self.local_vars.find_mut(&name) {
Some(v) => { v.data = (*value).clone(); }
None => { fail!("unknown variable %s", name) }
Expand Down Expand Up @@ -341,7 +344,8 @@ impl Program {
}

// I'm not an @ pointer, so this has to be done outside.
do newvars.consume |k, v| {
let cons_newvars = util::replace(newvars, HashMap::new());
for cons_newvars.consume().advance |(k, v)| {
self.newvars.insert(k, v);
}

Expand Down
84 changes: 20 additions & 64 deletions src/libstd/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,31 +438,6 @@ impl<K: Hash + Eq, V> HashMap<K, V> {
self.mangle(k, v, |_k,a| a, |k,v,_a| f(k,v))
}

/// Calls a function on each element of a hash map, destroying the hash
/// map in the process.
pub fn consume(&mut self, f: &fn(K, V)) {
let buckets = replace(&mut self.buckets,
vec::from_fn(INITIAL_CAPACITY, |_| None));
self.size = 0;

for buckets.consume_iter().advance |bucket| {
match bucket {
None => {},
Some(Bucket{key, value, _}) => {
f(key, value)
}
}
}
}

/// Creates a consuming iterator, that is, one that moves each key-value
/// pair out of the map in arbitrary order. The map cannot be used after
/// calling this.
pub fn consume_iter(self) -> HashMapConsumeIterator<K, V> {
// `consume_rev_iter` is more efficient than `consume_iter` for vectors
HashMapConsumeIterator {iter: self.buckets.consume_rev_iter()}
}

/// Retrieves a value for the given key, failing if the key is not
/// present.
pub fn get<'a>(&'a self, k: &K) -> &'a V {
Expand Down Expand Up @@ -522,6 +497,15 @@ impl<K: Hash + Eq, V> HashMap<K, V> {
pub fn mut_iter<'a>(&'a mut self) -> HashMapMutIterator<'a, K, V> {
HashMapMutIterator { iter: self.buckets.mut_iter() }
}

/// Creates a consuming iterator, that is, one that moves each key-value
/// pair out of the map in arbitrary order. The map cannot be used after
/// calling this.
pub fn consume(self) -> HashMapConsumeIterator<K, V> {
// `consume_rev_iter` is more efficient than `consume_iter` for vectors
HashMapConsumeIterator {iter: self.buckets.consume_rev_iter()}
}

}

impl<K: Hash + Eq, V: Clone> HashMap<K, V> {
Expand Down Expand Up @@ -761,19 +745,6 @@ impl<T:Hash + Eq> HashSet<T> {
self.map.reserve_at_least(n)
}

/// Consumes all of the elements in the set, emptying it out
pub fn consume(&mut self, f: &fn(T)) {
self.map.consume(|k, _| f(k))
}

/// Creates a consuming iterator, that is, one that moves each value out
/// of the set in arbitrary order. The set cannot be used after calling
/// this.
pub fn consume_iter(self) -> HashSetConsumeIterator<T> {
// `consume_rev_iter` is more efficient than `consume_iter` for vectors
HashSetConsumeIterator {iter: self.map.buckets.consume_rev_iter()}
}

/// Returns true if the hash set contains a value equivalent to the
/// given query value.
pub fn contains_equiv<Q:Hash + Equiv<T>>(&self, value: &Q) -> bool {
Expand All @@ -786,6 +757,14 @@ impl<T:Hash + Eq> HashSet<T> {
HashSetIterator { iter: self.map.buckets.iter() }
}

/// Creates a consuming iterator, that is, one that moves each value out
/// of the set in arbitrary order. The set cannot be used after calling
/// this.
pub fn consume(self) -> HashSetConsumeIterator<T> {
// `consume_rev_iter` is more efficient than `consume_iter` for vectors
HashSetConsumeIterator {iter: self.map.buckets.consume_rev_iter()}
}

/// Visit the values representing the difference
pub fn difference_iter<'a>(&'a self, other: &'a HashSet<T>)
-> SetAlgebraIter<'a, T> {
Expand Down Expand Up @@ -975,29 +954,6 @@ mod test_map {

#[test]
fn test_consume() {
let mut m = HashMap::new();
assert!(m.insert(1, 2));
assert!(m.insert(2, 3));
let mut m2 = HashMap::new();
do m.consume |k, v| {
m2.insert(k, v);
}
assert_eq!(m.len(), 0);
assert_eq!(m2.len(), 2);
assert_eq!(m2.get(&1), &2);
assert_eq!(m2.get(&2), &3);
}

#[test]
fn test_consume_still_usable() {
let mut m = HashMap::new();
assert!(m.insert(1, 2));
do m.consume |_, _| {}
assert!(m.insert(1, 2));
}

#[test]
fn test_consume_iter() {
let hm = {
let mut hm = HashMap::new();

Expand All @@ -1007,7 +963,7 @@ mod test_map {
hm
};

let v = hm.consume_iter().collect::<~[(char, int)]>();
let v = hm.consume().collect::<~[(char, int)]>();
assert!([('a', 1), ('b', 2)] == v || [('b', 2), ('a', 1)] == v);
}

Expand Down Expand Up @@ -1293,7 +1249,7 @@ mod test_set {
}

#[test]
fn test_consume_iter() {
fn test_consume() {
let hs = {
let mut hs = HashSet::new();

Expand All @@ -1303,7 +1259,7 @@ mod test_set {
hs
};

let v = hs.consume_iter().collect::<~[char]>();
let v = hs.consume().collect::<~[char]>();
assert!(['a', 'b'] == v || ['b', 'a'] == v);
}
}
2 changes: 1 addition & 1 deletion src/libstd/unstable/weak_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fn run_weak_task_service(port: Port<ServiceMsg>) {
}
}

do shutdown_map.consume |_, shutdown_chan| {
for shutdown_map.consume().advance |(_, shutdown_chan)| {
// Weak task may have already exited
shutdown_chan.send(());
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/bench/graph500-bfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ fn make_graph(N: uint, edges: ~[(node_id, node_id)]) -> graph {
}
}

do graph.consume_iter().transform |mut v| {
do graph.consume_iter().transform |v| {
let mut vec = ~[];
do v.consume |i| {
for v.consume().advance |i| {
vec.push(i);
}
vec
Expand All @@ -119,7 +119,7 @@ fn gen_search_keys(graph: &[~[node_id]], n: uint) -> ~[node_id] {
}
}
let mut vec = ~[];
do keys.consume |i| {
for keys.consume().advance |i| {
vec.push(i);
}
return vec;
Expand Down