diff options
| author | bors <bors@rust-lang.org> | 2013-03-26 16:21:59 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-03-26 16:21:59 -0700 |
| commit | fa82b9af2ae1f5a32a6f0d34366f0a26aee151d6 (patch) | |
| tree | 65b4789c0d2ba30391c2cef2681c753bc4e40ff6 /src/libstd/oldmap.rs | |
| parent | 3d588c528685fa0590ff91f189f0ef44a3815ec2 (diff) | |
| parent | d69108d8f78a9b5a8669606b62fe6df6409d17e7 (diff) | |
| download | rust-fa82b9af2ae1f5a32a6f0d34366f0a26aee151d6.tar.gz rust-fa82b9af2ae1f5a32a6f0d34366f0a26aee151d6.zip | |
auto merge of #5523 : alexcrichton/rust/less-oldmap, r=thestinger
I started out just removing a few instances of `HashMap` throughout rustc, but it ended up snowballing to remove the entire thing. Most uses translated to just using `@mut LinearMap` instead of `HashMap`, although I tried where possible to drop the `@mut` modifier. This ended up working out some of the time, but definitely not in the major use cases. Things got kinda weird in some cases like: * https://github.com/alexcrichton/rust/compare/mozilla:a56ec8c1342453a88be79e192a11501844375d40...alexcrichton:621b63300358cacad088ddd7f78180f29c40e66e#L39R1587 * https://github.com/alexcrichton/rust/compare/mozilla:a56ec8c1342453a88be79e192a11501844375d40...alexcrichton:621b63300358cacad088ddd7f78180f29c40e66e#L61R3760 * https://github.com/alexcrichton/rust/compare/mozilla:a56ec8c1342453a88be79e192a11501844375d40...alexcrichton:621b63300358cacad088ddd7f78180f29c40e66e#L71R917 * https://github.com/alexcrichton/rust/compare/mozilla:a56ec8c1342453a88be79e192a11501844375d40...alexcrichton:621b63300358cacad088ddd7f78180f29c40e66e#L91R127 I tried to tag them all with bugs which I thought would make them less weird, but I may have the wrong bug in a few places. These cases only came up when I tried to pass around `&mut LinearMap` instead of an `@mut LinearMap`. I also ran into a few bugs when migrating to `LinearMap`, one of which is #5521. There's another set of bugs which a00d779042fb8753c716e07b4f1aac0d5ab7bf66 addresses (all marked with `XXX`). I have a feeling they're all the same bug, but all I've been able is to reproduce them. I tried to whittle down the test cases and try to get some input which causes a failure, but I've been unable to do so. All I know is that it's vaguely related to `*T` pointers being used as `&*T` (return value of `find`). I'm not able to open a very descriptive issue, but I'll do so if there seems no other better route. I realize this is a very large pull request, so if it'd be better to split this up into multiple segments I'd be more than willing to do so. So far the tests all pass locally, although I'm sure bors will turn something up. I also don't mind keeping this up to date with rebasing. This maybe should wait until after 0.6 because it is a fairly large change...
Diffstat (limited to 'src/libstd/oldmap.rs')
| -rw-r--r-- | src/libstd/oldmap.rs | 388 |
1 files changed, 0 insertions, 388 deletions
diff --git a/src/libstd/oldmap.rs b/src/libstd/oldmap.rs deleted file mode 100644 index b40237cf584..00000000000 --- a/src/libstd/oldmap.rs +++ /dev/null @@ -1,388 +0,0 @@ -// Copyright 2012 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! A deprecated compatibility layer on top of `core::hashmap` - -use core::prelude::*; -use core::hash::Hash; -use core::prelude::*; -use core::to_bytes::IterBytes; -use core::vec; - -/// A convenience type to treat a hashmap as a set -pub type Set<K> = HashMap<K, ()>; - -pub type HashMap<K, V> = chained::T<K, V>; - -pub mod chained { - use core::ops; - use core::prelude::*; - use core::hashmap::linear::LinearMap; - - struct HashMap_<K, V> { - priv map: LinearMap<K, V> - } - - pub type T<K, V> = @mut HashMap_<K, V>; - - pub impl<K:Eq + IterBytes + Hash,V> HashMap_<K, V> { - fn clear(&mut self) { - self.map.clear() - } - } - - impl<K:Eq + IterBytes + Hash,V> Container for HashMap_<K, V> { - fn len(&const self) -> uint { self.map.len() } - fn is_empty(&const self) -> bool { self.map.is_empty() } - } - - pub impl<K:Eq + IterBytes + Hash,V> HashMap_<K, V> { - fn contains_key(&self, k: &K) -> bool { - self.map.contains_key(k) - } - - fn insert(&mut self, k: K, v: V) -> bool { - self.map.insert(k, v) - } - - fn remove(&mut self, k: &K) -> bool { - self.map.remove(k) - } - - fn each(&self, blk: &fn(key: &K, value: &V) -> bool) { - do self.map.each |&(k, v)| { blk(k, v) } - } - - fn each_key(&self, blk: &fn(key: &K) -> bool) { - self.map.each_key(blk) - } - - fn each_value(&self, blk: &fn(value: &V) -> bool) { - self.map.each_value(blk) - } - } - - pub impl<K:Eq + IterBytes + Hash + Copy,V:Copy> HashMap_<K, V> { - fn find(&self, k: &K) -> Option<V> { - self.map.find(k).map(|&x| copy *x) - } - - fn update(&mut self, key: K, newval: V, ff: &fn(V, V) -> V) -> bool { - match self.find(&key) { - None => self.insert(key, newval), - Some(orig) => self.insert(key, ff(orig, newval)) - } - } - - fn get(&self, k: &K) -> V { - copy *self.map.get(k) - } - } - - impl<K:Eq + IterBytes + Hash + Copy,V:Copy> ops::Index<K, V> - for HashMap_<K, V> { - fn index(&self, k: K) -> V { - self.get(&k) - } - } - - pub fn mk<K:Eq + IterBytes + Hash,V:Copy>() -> T<K,V> { - @mut HashMap_{map: LinearMap::new()} - } -} - -/* -Function: hashmap - -Construct a hashmap. -*/ -pub fn HashMap<K:Eq + IterBytes + Hash + Const,V:Copy>() - -> HashMap<K, V> { - chained::mk() -} - -/// Convenience function for adding keys to a hashmap with nil type keys -pub fn set_add<K:Eq + IterBytes + Hash + Const + Copy>(set: Set<K>, key: K) - -> bool { - set.insert(key, ()) -} - -/// Convert a set into a vector. -pub fn vec_from_set<T:Eq + IterBytes + Hash + Copy>(s: Set<T>) -> ~[T] { - do vec::build_sized(s.len()) |push| { - for s.each_key() |&k| { - push(k); - } - } -} - -/// Construct a hashmap from a vector -pub fn hash_from_vec<K:Eq + IterBytes + Hash + Const + Copy,V:Copy>( - items: &[(K, V)]) -> HashMap<K, V> { - let map = HashMap(); - for vec::each(items) |item| { - match *item { - (copy key, copy value) => { - map.insert(key, value); - } - } - } - map -} - -#[cfg(test)] -mod tests { - use core::uint; - - use super::*; - - #[test] - fn test_simple() { - debug!("*** starting test_simple"); - fn eq_uint(x: &uint, y: &uint) -> bool { *x == *y } - fn uint_id(x: &uint) -> uint { *x } - debug!("uint -> uint"); - let hm_uu: HashMap<uint, uint> = - HashMap::<uint, uint>(); - fail_unless!((hm_uu.insert(10u, 12u))); - fail_unless!((hm_uu.insert(11u, 13u))); - fail_unless!((hm_uu.insert(12u, 14u))); - fail_unless!((hm_uu.get(&11) == 13u)); - fail_unless!((hm_uu.get(&12) == 14u)); - fail_unless!((hm_uu.get(&10) == 12u)); - fail_unless!((!hm_uu.insert(12u, 14u))); - fail_unless!((hm_uu.get(&12) == 14u)); - fail_unless!((!hm_uu.insert(12u, 12u))); - fail_unless!((hm_uu.get(&12) == 12u)); - let ten: ~str = ~"ten"; - let eleven: ~str = ~"eleven"; - let twelve: ~str = ~"twelve"; - debug!("str -> uint"); - let hm_su: HashMap<~str, uint> = - HashMap::<~str, uint>(); - fail_unless!((hm_su.insert(~"ten", 12u))); - fail_unless!((hm_su.insert(eleven, 13u))); - fail_unless!((hm_su.insert(~"twelve", 14u))); - fail_unless!((hm_su.get(&eleven) == 13u)); - fail_unless!((hm_su.get(&~"eleven") == 13u)); - fail_unless!((hm_su.get(&~"twelve") == 14u)); - fail_unless!((hm_su.get(&~"ten") == 12u)); - fail_unless!((!hm_su.insert(~"twelve", 14u))); - fail_unless!((hm_su.get(&~"twelve") == 14u)); - fail_unless!((!hm_su.insert(~"twelve", 12u))); - fail_unless!((hm_su.get(&~"twelve") == 12u)); - debug!("uint -> str"); - let hm_us: HashMap<uint, ~str> = - HashMap::<uint, ~str>(); - fail_unless!((hm_us.insert(10u, ~"twelve"))); - fail_unless!((hm_us.insert(11u, ~"thirteen"))); - fail_unless!((hm_us.insert(12u, ~"fourteen"))); - fail_unless!(hm_us.get(&11) == ~"thirteen"); - fail_unless!(hm_us.get(&12) == ~"fourteen"); - fail_unless!(hm_us.get(&10) == ~"twelve"); - fail_unless!((!hm_us.insert(12u, ~"fourteen"))); - fail_unless!(hm_us.get(&12) == ~"fourteen"); - fail_unless!((!hm_us.insert(12u, ~"twelve"))); - fail_unless!(hm_us.get(&12) == ~"twelve"); - debug!("str -> str"); - let hm_ss: HashMap<~str, ~str> = - HashMap::<~str, ~str>(); - fail_unless!((hm_ss.insert(ten, ~"twelve"))); - fail_unless!((hm_ss.insert(eleven, ~"thirteen"))); - fail_unless!((hm_ss.insert(twelve, ~"fourteen"))); - fail_unless!(hm_ss.get(&~"eleven") == ~"thirteen"); - fail_unless!(hm_ss.get(&~"twelve") == ~"fourteen"); - fail_unless!(hm_ss.get(&~"ten") == ~"twelve"); - fail_unless!((!hm_ss.insert(~"twelve", ~"fourteen"))); - fail_unless!(hm_ss.get(&~"twelve") == ~"fourteen"); - fail_unless!((!hm_ss.insert(~"twelve", ~"twelve"))); - fail_unless!(hm_ss.get(&~"twelve") == ~"twelve"); - debug!("*** finished test_simple"); - } - - - /** - * Force map growth - */ - #[test] - fn test_growth() { - debug!("*** starting test_growth"); - let num_to_insert: uint = 64u; - fn eq_uint(x: &uint, y: &uint) -> bool { *x == *y } - fn uint_id(x: &uint) -> uint { *x } - debug!("uint -> uint"); - let hm_uu: HashMap<uint, uint> = - HashMap::<uint, uint>(); - let mut i: uint = 0u; - while i < num_to_insert { - fail_unless!((hm_uu.insert(i, i * i))); - debug!("inserting %u -> %u", i, i*i); - i += 1u; - } - debug!("-----"); - i = 0u; - while i < num_to_insert { - debug!("get(%u) = %u", i, hm_uu.get(&i)); - fail_unless!((hm_uu.get(&i) == i * i)); - i += 1u; - } - fail_unless!((hm_uu.insert(num_to_insert, 17u))); - fail_unless!((hm_uu.get(&num_to_insert) == 17u)); - debug!("-----"); - i = 0u; - while i < num_to_insert { - debug!("get(%u) = %u", i, hm_uu.get(&i)); - fail_unless!((hm_uu.get(&i) == i * i)); - i += 1u; - } - debug!("str -> str"); - let hm_ss: HashMap<~str, ~str> = - HashMap::<~str, ~str>(); - i = 0u; - while i < num_to_insert { - fail_unless!(hm_ss.insert(uint::to_str_radix(i, 2u), - uint::to_str_radix(i * i, 2u))); - debug!("inserting \"%s\" -> \"%s\"", - uint::to_str_radix(i, 2u), - uint::to_str_radix(i*i, 2u)); - i += 1u; - } - debug!("-----"); - i = 0u; - while i < num_to_insert { - debug!("get(\"%s\") = \"%s\"", - uint::to_str_radix(i, 2u), - hm_ss.get(&uint::to_str_radix(i, 2u))); - fail_unless!(hm_ss.get(&uint::to_str_radix(i, 2u)) == - uint::to_str_radix(i * i, 2u)); - i += 1u; - } - fail_unless!(hm_ss.insert(uint::to_str_radix(num_to_insert, 2u), - uint::to_str_radix(17u, 2u))); - fail_unless!(hm_ss.get(&uint::to_str_radix(num_to_insert, 2u)) == - uint::to_str_radix(17u, 2u)); - debug!("-----"); - i = 0u; - while i < num_to_insert { - debug!("get(\"%s\") = \"%s\"", - uint::to_str_radix(i, 2u), - hm_ss.get(&uint::to_str_radix(i, 2u))); - fail_unless!(hm_ss.get(&uint::to_str_radix(i, 2u)) == - uint::to_str_radix(i * i, 2u)); - i += 1u; - } - debug!("*** finished test_growth"); - } - - #[test] - fn test_removal() { - debug!("*** starting test_removal"); - let num_to_insert: uint = 64u; - let hm: HashMap<uint, uint> = - HashMap::<uint, uint>(); - let mut i: uint = 0u; - while i < num_to_insert { - fail_unless!((hm.insert(i, i * i))); - debug!("inserting %u -> %u", i, i*i); - i += 1u; - } - fail_unless!((hm.len() == num_to_insert)); - debug!("-----"); - debug!("removing evens"); - i = 0u; - while i < num_to_insert { - let v = hm.remove(&i); - fail_unless!(v); - i += 2u; - } - fail_unless!((hm.len() == num_to_insert / 2u)); - debug!("-----"); - i = 1u; - while i < num_to_insert { - debug!("get(%u) = %u", i, hm.get(&i)); - fail_unless!((hm.get(&i) == i * i)); - i += 2u; - } - debug!("-----"); - i = 1u; - while i < num_to_insert { - debug!("get(%u) = %u", i, hm.get(&i)); - fail_unless!((hm.get(&i) == i * i)); - i += 2u; - } - debug!("-----"); - i = 0u; - while i < num_to_insert { - fail_unless!((hm.insert(i, i * i))); - debug!("inserting %u -> %u", i, i*i); - i += 2u; - } - fail_unless!((hm.len() == num_to_insert)); - debug!("-----"); - i = 0u; - while i < num_to_insert { - debug!("get(%u) = %u", i, hm.get(&i)); - fail_unless!((hm.get(&i) == i * i)); - i += 1u; - } - debug!("-----"); - fail_unless!((hm.len() == num_to_insert)); - i = 0u; - while i < num_to_insert { - debug!("get(%u) = %u", i, hm.get(&i)); - fail_unless!((hm.get(&i) == i * i)); - i += 1u; - } - debug!("*** finished test_removal"); - } - - #[test] - fn test_contains_key() { - let key = ~"k"; - let map = HashMap::<~str, ~str>(); - fail_unless!((!map.contains_key(&key))); - map.insert(key, ~"val"); - fail_unless!((map.contains_key(&key))); - } - - #[test] - fn test_find() { - let key = ~"k"; - let map = HashMap::<~str, ~str>(); - fail_unless!(map.find(&key).is_none()); - map.insert(key, ~"val"); - fail_unless!(map.find(&key).get() == ~"val"); - } - - #[test] - fn test_clear() { - let key = ~"k"; - let mut map = HashMap::<~str, ~str>(); - map.insert(key, ~"val"); - fail_unless!((map.len() == 1)); - fail_unless!((map.contains_key(&key))); - map.clear(); - fail_unless!((map.len() == 0)); - fail_unless!((!map.contains_key(&key))); - } - - #[test] - fn test_hash_from_vec() { - let map = hash_from_vec(~[ - (~"a", 1), - (~"b", 2), - (~"c", 3) - ]); - fail_unless!(map.len() == 3u); - fail_unless!(map.get(&~"a") == 1); - fail_unless!(map.get(&~"b") == 2); - fail_unless!(map.get(&~"c") == 3); - } -} |
