From d3c831ba4a4a2284f73e4f9147aa4900cab56815 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Mon, 7 Apr 2014 22:32:49 +1000 Subject: [PATCH 1/2] std: use a `match` in `assert_eq!` to extend the lifetime of the args. This enables assert_eq!(foo.collect::>().as_slice(), &[1,2,3,4]); to work, by extending the lifetime of the .as_slice() rvalue. --- src/libstd/macros.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index fbb48f2ebcb26..9d06e38dd8e9c 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -117,13 +117,15 @@ macro_rules! assert( #[macro_export] macro_rules! assert_eq( ($given:expr , $expected:expr) => ({ - let given_val = &($given); - let expected_val = &($expected); - // check both directions of equality.... - if !((*given_val == *expected_val) && - (*expected_val == *given_val)) { - fail!("assertion failed: `(left == right) && (right == left)` \ - (left: `{}`, right: `{}`)", *given_val, *expected_val) + match (&($given), &($expected)) { + (given_val, expected_val) => { + // check both directions of equality.... + if !((*given_val == *expected_val) && + (*expected_val == *given_val)) { + fail!("assertion failed: `(left == right) && (right == left)` \ + (left: `{}`, right: `{}`)", *given_val, *expected_val) + } + } } }) ) From 4b9a7a2588798ab4ffaa4805397fa428348d6a2a Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Sat, 5 Apr 2014 16:45:42 +1100 Subject: [PATCH 2/2] collections: replace all ~[T] with Vec. --- src/libcollections/bitv.rs | 67 ++++++++++----------- src/libcollections/btree.rs | 58 +++++++++--------- src/libcollections/deque.rs | 7 +-- src/libcollections/dlist.rs | 40 ++++++------- src/libcollections/enum_set.rs | 32 +++++----- src/libcollections/hashmap.rs | 22 +++---- src/libcollections/lib.rs | 2 + src/libcollections/lru_cache.rs | 8 +-- src/libcollections/priority_queue.rs | 90 ++++++++++++++-------------- src/libcollections/ringbuf.rs | 45 +++++++------- src/libcollections/smallintmap.rs | 20 +++---- src/libcollections/treemap.rs | 30 +++++----- src/libcollections/trie.rs | 4 +- 13 files changed, 212 insertions(+), 213 deletions(-) diff --git a/src/libcollections/bitv.rs b/src/libcollections/bitv.rs index 510e8908427c0..cdcc05bf72983 100644 --- a/src/libcollections/bitv.rs +++ b/src/libcollections/bitv.rs @@ -111,7 +111,7 @@ impl SmallBitv { #[deriving(Clone)] struct BigBitv { - storage: ~[uint] + storage: Vec } /** @@ -131,7 +131,7 @@ fn big_mask(nbits: uint, elem: uint) -> uint { } impl BigBitv { - pub fn new(storage: ~[uint]) -> BigBitv { + pub fn new(storage: Vec) -> BigBitv { BigBitv {storage: storage} } @@ -193,7 +193,7 @@ impl BigBitv { pub fn get(&self, i: uint) -> bool { let w = i / uint::BITS; let b = i % uint::BITS; - let x = 1 & self.storage[w] >> b; + let x = 1 & self.storage.get(w) >> b; x == 1 } @@ -202,15 +202,15 @@ impl BigBitv { let w = i / uint::BITS; let b = i % uint::BITS; let flag = 1 << b; - self.storage[w] = if x { self.storage[w] | flag } - else { self.storage[w] & !flag }; + *self.storage.get_mut(w) = if x { *self.storage.get(w) | flag } + else { *self.storage.get(w) & !flag }; } #[inline] pub fn equals(&self, b: &BigBitv, nbits: uint) -> bool { for (i, elt) in b.storage.iter().enumerate() { let mask = big_mask(nbits, i); - if mask & self.storage[i] != mask & *elt { + if mask & *self.storage.get(i) != mask & *elt { return false; } } @@ -278,13 +278,13 @@ impl Bitv { let s = if init { if exact { - slice::from_elem(nelems, !0u) + Vec::from_elem(nelems, !0u) } else { - let mut v = slice::from_elem(nelems-1, !0u); + let mut v = Vec::from_elem(nelems-1, !0u); v.push((1< ~[uint] { - slice::from_fn(self.nbits, |x| self.init_to_vec(x)) + pub fn to_vec(&self) -> Vec { + Vec::from_fn(self.nbits, |x| self.init_to_vec(x)) } /** @@ -461,7 +461,7 @@ impl Bitv { * size of the `Bitv` is not a multiple of 8 then trailing bits * will be filled-in with false/0 */ - pub fn to_bytes(&self) -> ~[u8] { + pub fn to_bytes(&self) -> Vec { fn bit (bitv: &Bitv, byte: uint, bit: uint) -> u8 { let offset = byte * 8 + bit; if offset >= bitv.nbits { @@ -473,7 +473,7 @@ impl Bitv { let len = self.nbits/8 + if self.nbits % 8 == 0 { 0 } else { 1 }; - slice::from_fn(len, |i| + Vec::from_fn(len, |i| bit(self, i, 0) | bit(self, i, 1) | bit(self, i, 2) | @@ -486,10 +486,10 @@ impl Bitv { } /** - * Transform `self` into a `[bool]` by turning each bit into a `bool`. + * Transform `self` into a `Vec` by turning each bit into a `bool`. */ - pub fn to_bools(&self) -> ~[bool] { - slice::from_fn(self.nbits, |i| self[i]) + pub fn to_bools(&self) -> Vec { + Vec::from_fn(self.nbits, |i| self[i]) } /** @@ -659,7 +659,7 @@ pub struct BitvSet { impl BitvSet { /// Creates a new bit vector set with initially no contents pub fn new() -> BitvSet { - BitvSet{ size: 0, bitv: BigBitv::new(~[0]) } + BitvSet{ size: 0, bitv: BigBitv::new(vec!(0)) } } /// Creates a new bit vector set from the given bit vector @@ -673,7 +673,7 @@ impl BitvSet { match rep { Big(b) => BitvSet{ size: size, bitv: b }, Small(SmallBitv{bits}) => - BitvSet{ size: size, bitv: BigBitv{ storage: ~[bits] } }, + BitvSet{ size: size, bitv: BigBitv{ storage: vec!(bits) } }, } } @@ -705,9 +705,9 @@ impl BitvSet { self.bitv.storage.grow(other.capacity() / uint::BITS, &0); } for (i, &w) in other.bitv.storage.iter().enumerate() { - let old = self.bitv.storage[i]; + let old = *self.bitv.storage.get(i); let new = f(old, w); - self.bitv.storage[i] = new; + *self.bitv.storage.get_mut(i) = new; self.size += nbits(new) - nbits(old); } } @@ -863,7 +863,7 @@ impl MutableSet for BitvSet { // Attempt to truncate our storage let mut i = self.bitv.storage.len(); - while i > 1 && self.bitv.storage[i - 1] == 0 { + while i > 1 && *self.bitv.storage.get(i - 1) == 0 { i -= 1; } self.bitv.storage.truncate(i); @@ -878,12 +878,12 @@ impl BitvSet { /// w1, w2) where the bit location is the number of bits offset so far, /// and w1/w2 are the words coming from the two vectors self, other. fn commons<'a>(&'a self, other: &'a BitvSet) - -> Map<'static, ((uint, &'a uint), &'a ~[uint]), (uint, uint, uint), - Zip>, Repeat<&'a ~[uint]>>> { + -> Map<'static, ((uint, &'a uint), &'a Vec), (uint, uint, uint), + Zip>, Repeat<&'a Vec>>> { let min = cmp::min(self.bitv.storage.len(), other.bitv.storage.len()); self.bitv.storage.slice(0, min).iter().enumerate() .zip(Repeat::new(&other.bitv.storage)) - .map(|((i, &w), o_store)| (i * uint::BITS, w, o_store[i])) + .map(|((i, &w), o_store)| (i * uint::BITS, w, *o_store.get(i))) } /// Visits each word in `self` or `other` that extends beyond the other. This @@ -946,7 +946,6 @@ mod tests { use bitv; use std::uint; - use std::slice; use rand; use rand::Rng; @@ -964,8 +963,8 @@ mod tests { #[test] fn test_0_elements() { let act = Bitv::new(0u, false); - let exp = slice::from_elem::(0u, false); - assert!(act.eq_vec(exp)); + let exp = Vec::from_elem(0u, false); + assert!(act.eq_vec(exp.as_slice())); } #[test] @@ -1299,12 +1298,12 @@ mod tests { fn test_to_bytes() { let mut bv = Bitv::new(3, true); bv.set(1, false); - assert_eq!(bv.to_bytes(), ~[0b10100000]); + assert_eq!(bv.to_bytes(), vec!(0b10100000)); let mut bv = Bitv::new(9, false); bv.set(2, true); bv.set(8, true); - assert_eq!(bv.to_bytes(), ~[0b00100000, 0b10000000]); + assert_eq!(bv.to_bytes(), vec!(0b00100000, 0b10000000)); } #[test] @@ -1315,7 +1314,7 @@ mod tests { #[test] fn test_to_bools() { - let bools = ~[false, false, true, false, false, true, true, false]; + let bools = vec!(false, false, true, false, false, true, true, false); assert_eq!(from_bytes([0b00100110]).to_bools(), bools); } @@ -1334,8 +1333,8 @@ mod tests { let bools = [true, false, true, true]; let bitv = BitvSet::from_bitv(from_bools(bools)); - let idxs: ~[uint] = bitv.iter().collect(); - assert_eq!(idxs, ~[0, 2, 3]); + let idxs: Vec = bitv.iter().collect(); + assert_eq!(idxs, vec!(0, 2, 3)); } #[test] @@ -1579,7 +1578,7 @@ mod tests { #[bench] fn bench_big_bitv_small(b: &mut BenchHarness) { let mut r = rng(); - let mut bitv = BigBitv::new(~[0]); + let mut bitv = BigBitv::new(vec!(0)); b.iter(|| { bitv.set((r.next_u32() as uint) % uint::BITS, true); &bitv @@ -1589,7 +1588,7 @@ mod tests { #[bench] fn bench_big_bitv_big(b: &mut BenchHarness) { let mut r = rng(); - let mut storage = ~[]; + let mut storage = vec!(); storage.grow(BENCH_BITS / uint::BITS, &0u); let mut bitv = BigBitv::new(storage); b.iter(|| { diff --git a/src/libcollections/btree.rs b/src/libcollections/btree.rs index b516997b81e77..c317ed926f668 100644 --- a/src/libcollections/btree.rs +++ b/src/libcollections/btree.rs @@ -35,7 +35,7 @@ impl BTree { ///The lower bound applies to every node except the root node. pub fn new(k: K, v: V, lb: uint) -> BTree { BTree { - root: Node::new_leaf(~[LeafElt::new(k, v)]), + root: Node::new_leaf(vec!(LeafElt::new(k, v))), len: 1, lower_bound: lb, upper_bound: 2 * lb @@ -135,12 +135,12 @@ enum Node { //Node functions/methods impl Node { ///Creates a new leaf node given a vector of elements. - fn new_leaf(vec: ~[LeafElt]) -> Node { + fn new_leaf(vec: Vec>) -> Node { LeafNode(Leaf::new(vec)) } ///Creates a new branch node given a vector of an elements and a pointer to a rightmost child. - fn new_branch(vec: ~[BranchElt], right: ~Node) -> Node { + fn new_branch(vec: Vec>, right: ~Node) -> Node { BranchNode(Branch::new(vec, right)) } @@ -264,19 +264,19 @@ impl fmt::Show for Node { //A leaf is a vector with elements that contain no children. A leaf also //does not contain a rightmost child. struct Leaf { - elts: ~[LeafElt] + elts: Vec> } //Vector of values with children, plus a rightmost child (greater than all) struct Branch { - elts: ~[BranchElt], + elts: Vec>, rightmost_child: ~Node } impl Leaf { ///Creates a new Leaf from a vector of LeafElts. - fn new(vec: ~[LeafElt]) -> Leaf { + fn new(vec: Vec>) -> Leaf { Leaf { elts: vec } @@ -292,14 +292,14 @@ impl Leaf { midpoint = 0; } loop { - let order = self.elts[midpoint].key.cmp(&k); + let order = self.elts.get(midpoint).key.cmp(&k); match order { Equal => { return None; } Greater => { if midpoint > 0 { - if self.elts[midpoint - 1].key.cmp(&k) == Less { + if self.elts.get(midpoint - 1).key.cmp(&k) == Less { return Some(midpoint); } else { @@ -315,7 +315,7 @@ impl Leaf { } Less => { if midpoint + 1 < self.elts.len() { - if self.elts[midpoint + 1].key.cmp(&k) == Greater { + if self.elts.get(midpoint + 1).key.cmp(&k) == Greater { return Some(midpoint); } else { @@ -375,9 +375,9 @@ impl Leaf { let (left_leaf, right_leaf) = self.elts.partition(|le| le.key.cmp(&midpoint.key.clone()) == Less); - let branch_return = Node::new_branch(~[BranchElt::new(midpoint.key.clone(), + let branch_return = Node::new_branch(vec!(BranchElt::new(midpoint.key.clone(), midpoint.value.clone(), - ~Node::new_leaf(left_leaf))], + ~Node::new_leaf(left_leaf))), ~Node::new_leaf(right_leaf)); return (branch_return, true); } @@ -415,7 +415,7 @@ impl TotalOrd for Leaf { if self.elts.len() < other.elts.len() { return Less; } - self.elts[0].cmp(&other.elts[0]) + self.elts.get(0).cmp(other.elts.get(0)) } } @@ -434,7 +434,7 @@ impl fmt::Show for Leaf { impl Branch { ///Creates a new Branch from a vector of BranchElts and a rightmost child (a node). - fn new(vec: ~[BranchElt], right: ~Node) -> Branch { + fn new(vec: Vec>, right: ~Node) -> Branch { Branch { elts: vec, rightmost_child: right @@ -449,14 +449,14 @@ impl Branch { midpoint = 0u; } loop { - let order = self.elts[midpoint].key.cmp(&k); + let order = self.elts.get(midpoint).key.cmp(&k); match order { Equal => { return None; } Greater => { if midpoint > 0 { - if self.elts[midpoint - 1].key.cmp(&k) == Less { + if self.elts.get(midpoint - 1).key.cmp(&k) == Less { return Some(midpoint); } else { @@ -472,7 +472,7 @@ impl Branch { } Less => { if midpoint + 1 < self.elts.len() { - if self.elts[midpoint + 1].key.cmp(&k) == Greater { + if self.elts.get(midpoint + 1).key.cmp(&k) == Greater { return Some(midpoint); } else { @@ -527,7 +527,7 @@ impl Branch { outcome = new_outcome.val1(); } else { - let new_outcome = self.clone().elts[index.unwrap()].left.insert(k.clone(), + let new_outcome = self.elts.get(index.unwrap()).left.clone().insert(k.clone(), v.clone(), ub.clone()); new_branch = new_outcome.clone().val0(); @@ -543,7 +543,7 @@ impl Branch { self.rightmost_child = ~new_branch.clone(); } else { - self.elts[index.unwrap()].left = ~new_branch.clone(); + self.elts.get_mut(index.unwrap()).left = ~new_branch.clone(); } return (Node::new_branch(self.clone().elts, self.clone().rightmost_child), @@ -564,7 +564,7 @@ impl Branch { self.rightmost_child = ~new_branch; } else { - self.elts[index.unwrap()].left = ~new_branch; + self.elts.get_mut(index.unwrap()).left = ~new_branch; } return (Node::new_branch(self.clone().elts, self.clone().rightmost_child), @@ -573,7 +573,7 @@ impl Branch { //If we have a new branch node, attempt to insert it into the tree //as with the key-value pair, then check to see if the node is overfull. BranchNode(branch) => { - let new_elt = branch.clone().elts[0]; + let new_elt = branch.elts.get(0).clone(); let new_elt_index = self.bsearch_branch(new_elt.clone().key); match new_elt_index { None => { @@ -587,7 +587,7 @@ impl Branch { self.rightmost_child = branch.clone().rightmost_child; } else { - self.elts[new_elt_index.unwrap() + 1].left = + self.elts.get_mut(new_elt_index.unwrap() + 1).left = branch.clone().rightmost_child; } } @@ -602,10 +602,10 @@ impl Branch { midpoint.key.cmp(&le.key) == Greater); new_branch = Node::new_branch( - ~[BranchElt::new(midpoint.clone().key, + vec!(BranchElt::new(midpoint.clone().key, midpoint.clone().value, ~Node::new_branch(new_left, - midpoint.clone().left))], + midpoint.clone().left))), ~Node::new_branch(new_right, self.clone().rightmost_child)); return (new_branch, true); } @@ -644,7 +644,7 @@ impl TotalOrd for Branch { if self.elts.len() < other.elts.len() { return Less; } - self.elts[0].cmp(&other.elts[0]) + self.elts.get(0).cmp(other.elts.get(0)) } } @@ -787,7 +787,7 @@ mod test_btree { let leaf_elt_1 = LeafElt::new(1, ~"aaa"); let leaf_elt_2 = LeafElt::new(2, ~"bbb"); let leaf_elt_3 = LeafElt::new(3, ~"ccc"); - let n = Node::new_leaf(~[leaf_elt_1, leaf_elt_2, leaf_elt_3]); + let n = Node::new_leaf(vec!(leaf_elt_1, leaf_elt_2, leaf_elt_3)); let b = BTree::new_with_node_len(n, 3, 2); //println!("{}", b.clone().insert(4, ~"ddd").to_str()); assert!(b.insert(4, ~"ddd").root.is_leaf()); @@ -799,7 +799,7 @@ mod test_btree { let leaf_elt_2 = LeafElt::new(2, ~"bbb"); let leaf_elt_3 = LeafElt::new(3, ~"ccc"); let leaf_elt_4 = LeafElt::new(4, ~"ddd"); - let n = Node::new_leaf(~[leaf_elt_1, leaf_elt_2, leaf_elt_3, leaf_elt_4]); + let n = Node::new_leaf(vec!(leaf_elt_1, leaf_elt_2, leaf_elt_3, leaf_elt_4)); let b = BTree::new_with_node_len(n, 3, 2); //println!("{}", b.clone().insert(5, ~"eee").to_str()); assert!(!b.insert(5, ~"eee").root.is_leaf()); @@ -811,7 +811,7 @@ mod test_btree { let leaf_elt_2 = LeafElt::new(2, ~"bbb"); let leaf_elt_3 = LeafElt::new(3, ~"ccc"); let leaf_elt_4 = LeafElt::new(4, ~"ddd"); - let n = Node::new_leaf(~[leaf_elt_1, leaf_elt_2, leaf_elt_3, leaf_elt_4]); + let n = Node::new_leaf(vec!(leaf_elt_1, leaf_elt_2, leaf_elt_3, leaf_elt_4)); let mut b = BTree::new_with_node_len(n, 3, 2); b = b.clone().insert(5, ~"eee"); b = b.clone().insert(6, ~"fff"); @@ -840,7 +840,7 @@ mod test_btree { let leaf_elt_2 = LeafElt::new(2, ~"bbb"); let leaf_elt_3 = LeafElt::new(4, ~"ccc"); let leaf_elt_4 = LeafElt::new(5, ~"ddd"); - let n = Node::new_leaf(~[leaf_elt_1, leaf_elt_2, leaf_elt_3, leaf_elt_4]); + let n = Node::new_leaf(vec!(leaf_elt_1, leaf_elt_2, leaf_elt_3, leaf_elt_4)); let b = BTree::new_with_node_len(n, 3, 2); assert_eq!(Some(2), b.root.bsearch_node(3)); } @@ -851,7 +851,7 @@ mod test_btree { let leaf_elt_2 = LeafElt::new(2, ~"bbb"); let leaf_elt_3 = LeafElt::new(4, ~"ccc"); let leaf_elt_4 = LeafElt::new(5, ~"ddd"); - let n = Node::new_leaf(~[leaf_elt_1, leaf_elt_2, leaf_elt_3, leaf_elt_4]); + let n = Node::new_leaf(vec!(leaf_elt_1, leaf_elt_2, leaf_elt_3, leaf_elt_4)); let b = BTree::new_with_node_len(n, 3, 2); assert_eq!(Some(4), b.root.bsearch_node(800)); } diff --git a/src/libcollections/deque.rs b/src/libcollections/deque.rs index def8f4f356113..24d3e2c617ce8 100644 --- a/src/libcollections/deque.rs +++ b/src/libcollections/deque.rs @@ -44,7 +44,6 @@ pub mod bench { extern crate test; use self::test::BenchHarness; use std::container::MutableMap; - use std::slice; use rand; use rand::Rng; @@ -90,18 +89,18 @@ pub mod bench { bh: &mut BenchHarness) { // setup let mut rng = rand::weak_rng(); - let mut keys = slice::from_fn(n, |_| rng.gen::() % n); + let mut keys = Vec::from_fn(n, |_| rng.gen::() % n); for k in keys.iter() { map.insert(*k, 1); } - rng.shuffle(keys); + rng.shuffle(keys.as_mut_slice()); // measure let mut i = 0; bh.iter(|| { - map.find(&(keys[i])); + map.find(keys.get(i)); i = (i + 1) % n; }) } diff --git a/src/libcollections/dlist.rs b/src/libcollections/dlist.rs index 1c3a01a355d3c..3c1c7a3a88642 100644 --- a/src/libcollections/dlist.rs +++ b/src/libcollections/dlist.rs @@ -739,12 +739,12 @@ mod tests { check_links(&m); } - let v = ~[1,2,3,4,5]; - let u = ~[9,8,1,2,3,4,5]; - let mut m = list_from(v); - m.append(list_from(u)); + let v = vec![1,2,3,4,5]; + let u = vec![9,8,1,2,3,4,5]; + let mut m = list_from(v.as_slice()); + m.append(list_from(u.as_slice())); check_links(&m); - let sum = v + u; + let sum = v.append(u.as_slice()); assert_eq!(sum.len(), m.len()); for elt in sum.move_iter() { assert_eq!(m.pop_front(), Some(elt)) @@ -763,12 +763,12 @@ mod tests { check_links(&m); } - let v = ~[1,2,3,4,5]; - let u = ~[9,8,1,2,3,4,5]; - let mut m = list_from(v); - m.prepend(list_from(u)); + let v = vec![1,2,3,4,5]; + let u = vec![9,8,1,2,3,4,5]; + let mut m = list_from(v.as_slice()); + m.prepend(list_from(u.as_slice())); check_links(&m); - let sum = u + v; + let sum = u.append(v.as_slice()); assert_eq!(sum.len(), m.len()); for elt in sum.move_iter() { assert_eq!(m.pop_front(), Some(elt)) @@ -783,11 +783,11 @@ mod tests { n.rotate_forward(); check_links(&n); assert_eq!(n.len(), 0); - let v = ~[1,2,3,4,5]; - let mut m = list_from(v); + let v = vec![1,2,3,4,5]; + let mut m = list_from(v.as_slice()); m.rotate_backward(); check_links(&m); m.rotate_forward(); check_links(&m); - assert_eq!(v.iter().collect::<~[&int]>(), m.iter().collect()); + assert_eq!(v.iter().collect::>(), m.iter().collect()); m.rotate_forward(); check_links(&m); m.rotate_forward(); check_links(&m); m.pop_front(); check_links(&m); @@ -795,7 +795,7 @@ mod tests { m.rotate_backward(); check_links(&m); m.push_front(9); check_links(&m); m.rotate_forward(); check_links(&m); - assert_eq!(~[3,9,5,1,2], m.move_iter().collect()); + assert_eq!(vec![3,9,5,1,2], m.move_iter().collect()); } #[test] @@ -925,7 +925,7 @@ mod tests { } check_links(&m); assert_eq!(m.len(), 3 + len * 2); - assert_eq!(m.move_iter().collect::<~[int]>(), ~[-2,0,1,2,3,4,5,6,7,8,9,0,1]); + assert_eq!(m.move_iter().collect::>(), vec![-2,0,1,2,3,4,5,6,7,8,9,0,1]); } #[test] @@ -936,8 +936,8 @@ mod tests { m.merge(n, |a, b| a <= b); assert_eq!(m.len(), len); check_links(&m); - let res = m.move_iter().collect::<~[int]>(); - assert_eq!(res, ~[-1, 0, 0, 0, 1, 3, 5, 6, 7, 2, 7, 7, 9]); + let res = m.move_iter().collect::>(); + assert_eq!(res, vec![-1, 0, 0, 0, 1, 3, 5, 6, 7, 2, 7, 7, 9]); } #[test] @@ -952,7 +952,7 @@ mod tests { m.push_back(4); m.insert_ordered(3); check_links(&m); - assert_eq!(~[2,3,4], m.move_iter().collect::<~[int]>()); + assert_eq!(vec![2,3,4], m.move_iter().collect::>()); } #[test] @@ -974,7 +974,7 @@ mod tests { let n = list_from([1,2,3]); spawn(proc() { check_links(&n); - assert_eq!(~[&1,&2,&3], n.iter().collect::<~[&int]>()); + assert_eq!(&[&1,&2,&3], n.iter().collect::>().as_slice()); }); } @@ -1047,7 +1047,7 @@ mod tests { #[cfg(test)] fn fuzz_test(sz: int) { let mut m: DList = DList::new(); - let mut v = ~[]; + let mut v = vec![]; for i in range(0, sz) { check_links(&m); let r: u8 = rand::random(); diff --git a/src/libcollections/enum_set.rs b/src/libcollections/enum_set.rs index 83ba09ac68b6c..8249faad19a08 100644 --- a/src/libcollections/enum_set.rs +++ b/src/libcollections/enum_set.rs @@ -246,24 +246,24 @@ mod test { fn test_iterator() { let mut e1: EnumSet = EnumSet::empty(); - let elems: ~[Foo] = e1.iter().collect(); - assert_eq!(~[], elems) + let elems: Vec = e1.iter().collect(); + assert!(elems.is_empty()) e1.add(A); - let elems: ~[Foo] = e1.iter().collect(); - assert_eq!(~[A], elems) + let elems = e1.iter().collect(); + assert_eq!(vec![A], elems) e1.add(C); - let elems: ~[Foo] = e1.iter().collect(); - assert_eq!(~[A,C], elems) + let elems = e1.iter().collect(); + assert_eq!(vec![A,C], elems) e1.add(C); - let elems: ~[Foo] = e1.iter().collect(); - assert_eq!(~[A,C], elems) + let elems = e1.iter().collect(); + assert_eq!(vec![A,C], elems) e1.add(B); - let elems: ~[Foo] = e1.iter().collect(); - assert_eq!(~[A,B,C], elems) + let elems = e1.iter().collect(); + assert_eq!(vec![A,B,C], elems) } /////////////////////////////////////////////////////////////////////////// @@ -280,15 +280,15 @@ mod test { e2.add(C); let e_union = e1 | e2; - let elems: ~[Foo] = e_union.iter().collect(); - assert_eq!(~[A,B,C], elems) + let elems = e_union.iter().collect(); + assert_eq!(vec![A,B,C], elems) let e_intersection = e1 & e2; - let elems: ~[Foo] = e_intersection.iter().collect(); - assert_eq!(~[C], elems) + let elems = e_intersection.iter().collect(); + assert_eq!(vec![C], elems) let e_subtract = e1 - e2; - let elems: ~[Foo] = e_subtract.iter().collect(); - assert_eq!(~[A], elems) + let elems = e_subtract.iter().collect(); + assert_eq!(vec![A], elems) } } diff --git a/src/libcollections/hashmap.rs b/src/libcollections/hashmap.rs index 8090b2cea8c6e..dadf92a58e029 100644 --- a/src/libcollections/hashmap.rs +++ b/src/libcollections/hashmap.rs @@ -81,7 +81,7 @@ mod table { /// You can kind of think of this module/data structure as a safe wrapper /// around just the "table" part of the hashtable. It enforces some /// invariants at the type level and employs some performance trickery, - /// but in general is just a tricked out `~[Option]`. + /// but in general is just a tricked out `Vec>`. /// /// FIXME(cgaebel): /// @@ -1833,8 +1833,8 @@ mod test_map { hm }; - let v = hm.move_iter().collect::<~[(char, int)]>(); - assert!([('a', 1), ('b', 2)] == v || [('b', 2), ('a', 1)] == v); + let v = hm.move_iter().collect::>(); + assert!([('a', 1), ('b', 2)] == v.as_slice() || [('b', 2), ('a', 1)] == v.as_slice()); } #[test] @@ -1856,9 +1856,9 @@ mod test_map { #[test] fn test_keys() { - let vec = ~[(1, 'a'), (2, 'b'), (3, 'c')]; + let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')]; let map = vec.move_iter().collect::>(); - let keys = map.keys().map(|&k| k).collect::<~[int]>(); + let keys = map.keys().map(|&k| k).collect::>(); assert_eq!(keys.len(), 3); assert!(keys.contains(&1)); assert!(keys.contains(&2)); @@ -1867,9 +1867,9 @@ mod test_map { #[test] fn test_values() { - let vec = ~[(1, 'a'), (2, 'b'), (3, 'c')]; + let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')]; let map = vec.move_iter().collect::>(); - let values = map.values().map(|&v| v).collect::<~[char]>(); + let values = map.values().map(|&v| v).collect::>(); assert_eq!(values.len(), 3); assert!(values.contains(&'a')); assert!(values.contains(&'b')); @@ -1942,7 +1942,7 @@ mod test_map { #[test] fn test_from_iter() { - let xs = ~[(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]; + let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]; let map: HashMap = xs.iter().map(|&x| x).collect(); @@ -2133,7 +2133,7 @@ mod test_set { #[test] fn test_from_iter() { - let xs = ~[1, 2, 3, 4, 5, 6, 7, 8, 9]; + let xs = [1, 2, 3, 4, 5, 6, 7, 8, 9]; let set: HashSet = xs.iter().map(|&x| x).collect(); @@ -2153,8 +2153,8 @@ mod test_set { hs }; - let v = hs.move_iter().collect::<~[char]>(); - assert!(['a', 'b'] == v || ['b', 'a'] == v); + let v = hs.move_iter().collect::>(); + assert!(['a', 'b'] == v.as_slice() || ['b', 'a'] == v.as_slice()); } #[test] diff --git a/src/libcollections/lib.rs b/src/libcollections/lib.rs index ef5289ae373d8..2121e129c3523 100644 --- a/src/libcollections/lib.rs +++ b/src/libcollections/lib.rs @@ -22,6 +22,8 @@ #![feature(macro_rules, managed_boxes, default_type_params, phase)] +#![deny(deprecated_owned_vector)] + extern crate rand; #[cfg(test)] extern crate test; diff --git a/src/libcollections/lru_cache.rs b/src/libcollections/lru_cache.rs index bd40d18394555..87b1fee1d27d9 100644 --- a/src/libcollections/lru_cache.rs +++ b/src/libcollections/lru_cache.rs @@ -294,10 +294,10 @@ mod tests { #[test] fn test_put_update() { - let mut cache: LruCache<~str, ~[u8]> = LruCache::new(1); - cache.put(~"1", ~[10, 10]); - cache.put(~"1", ~[10, 19]); - assert_opt_eq(cache.get(&~"1"), ~[10, 19]); + let mut cache: LruCache<~str, Vec> = LruCache::new(1); + cache.put(~"1", vec![10, 10]); + cache.put(~"1", vec![10, 19]); + assert_opt_eq(cache.get(&~"1"), vec![10, 19]); assert_eq!(cache.len(), 1); } diff --git a/src/libcollections/priority_queue.rs b/src/libcollections/priority_queue.rs index a13785104abd5..2ebbf65b9e5d1 100644 --- a/src/libcollections/priority_queue.rs +++ b/src/libcollections/priority_queue.rs @@ -19,7 +19,7 @@ use std::slice; /// A priority queue implemented with a binary heap #[deriving(Clone)] pub struct PriorityQueue { - data: ~[T], + data: Vec, } impl Container for PriorityQueue { @@ -40,7 +40,7 @@ impl PriorityQueue { } /// Returns the greatest item in the queue - fails if empty - pub fn top<'a>(&'a self) -> &'a T { &self.data[0] } + pub fn top<'a>(&'a self) -> &'a T { self.data.get(0) } /// Returns the greatest item in the queue - None if empty pub fn maybe_top<'a>(&'a self) -> Option<&'a T> { @@ -64,7 +64,7 @@ impl PriorityQueue { pub fn pop(&mut self) -> T { let mut item = self.data.pop().unwrap(); if !self.is_empty() { - swap(&mut item, &mut self.data[0]); + swap(&mut item, self.data.get_mut(0)); self.siftdown(0); } item @@ -84,8 +84,8 @@ impl PriorityQueue { /// Optimized version of a push followed by a pop pub fn push_pop(&mut self, mut item: T) -> T { - if !self.is_empty() && self.data[0] > item { - swap(&mut item, &mut self.data[0]); + if !self.is_empty() && *self.top() > item { + swap(&mut item, self.data.get_mut(0)); self.siftdown(0); } item @@ -93,37 +93,37 @@ impl PriorityQueue { /// Optimized version of a pop followed by a push - fails if empty pub fn replace(&mut self, mut item: T) -> T { - swap(&mut item, &mut self.data[0]); + swap(&mut item, self.data.get_mut(0)); self.siftdown(0); item } /// Consume the PriorityQueue and return the underlying vector - pub fn to_vec(self) -> ~[T] { let PriorityQueue{data: v} = self; v } + pub fn to_vec(self) -> Vec { let PriorityQueue{data: v} = self; v } /// Consume the PriorityQueue and return a vector in sorted /// (ascending) order - pub fn to_sorted_vec(self) -> ~[T] { + pub fn to_sorted_vec(self) -> Vec { let mut q = self; let mut end = q.len(); while end > 1 { end -= 1; - q.data.swap(0, end); + q.data.as_mut_slice().swap(0, end); q.siftdown_range(0, end) } q.to_vec() } /// Create an empty PriorityQueue - pub fn new() -> PriorityQueue { PriorityQueue{data: ~[],} } + pub fn new() -> PriorityQueue { PriorityQueue{data: vec!(),} } /// Create an empty PriorityQueue with capacity `capacity` pub fn with_capacity(capacity: uint) -> PriorityQueue { - PriorityQueue { data: slice::with_capacity(capacity) } + PriorityQueue { data: Vec::with_capacity(capacity) } } /// Create a PriorityQueue from a vector (heapify) - pub fn from_vec(xs: ~[T]) -> PriorityQueue { + pub fn from_vec(xs: Vec) -> PriorityQueue { let mut q = PriorityQueue{data: xs,}; let mut n = q.len() / 2; while n > 0 { @@ -140,40 +140,40 @@ impl PriorityQueue { // compared to using swaps, which involves twice as many moves. fn siftup(&mut self, start: uint, mut pos: uint) { unsafe { - let new = replace(&mut self.data[pos], init()); + let new = replace(self.data.get_mut(pos), init()); while pos > start { let parent = (pos - 1) >> 1; - if new > self.data[parent] { - let x = replace(&mut self.data[parent], init()); - move_val_init(&mut self.data[pos], x); + if new > *self.data.get(parent) { + let x = replace(self.data.get_mut(parent), init()); + move_val_init(self.data.get_mut(pos), x); pos = parent; continue } break } - move_val_init(&mut self.data[pos], new); + move_val_init(self.data.get_mut(pos), new); } } fn siftdown_range(&mut self, mut pos: uint, end: uint) { unsafe { let start = pos; - let new = replace(&mut self.data[pos], init()); + let new = replace(self.data.get_mut(pos), init()); let mut child = 2 * pos + 1; while child < end { let right = child + 1; - if right < end && !(self.data[child] > self.data[right]) { + if right < end && !(*self.data.get(child) > *self.data.get(right)) { child = right; } - let x = replace(&mut self.data[child], init()); - move_val_init(&mut self.data[pos], x); + let x = replace(self.data.get_mut(child), init()); + move_val_init(self.data.get_mut(pos), x); pos = child; child = 2 * pos + 1; } - move_val_init(&mut self.data[pos], new); + move_val_init(self.data.get_mut(pos), new); self.siftup(start, pos); } } @@ -224,8 +224,8 @@ mod tests { #[test] fn test_iterator() { - let data = ~[5, 9, 3]; - let iterout = ~[9, 5, 3]; + let data = vec!(5, 9, 3); + let iterout = [9, 5, 3]; let pq = PriorityQueue::from_vec(data); let mut i = 0; for el in pq.iter() { @@ -236,7 +236,7 @@ mod tests { #[test] fn test_top_and_pop() { - let data = ~[2u, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1]; + let data = vec!(2u, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1); let mut sorted = data.clone(); sorted.sort(); let mut heap = PriorityQueue::from_vec(data); @@ -248,7 +248,7 @@ mod tests { #[test] fn test_push() { - let mut heap = PriorityQueue::from_vec(~[2, 4, 9]); + let mut heap = PriorityQueue::from_vec(vec!(2, 4, 9)); assert_eq!(heap.len(), 3); assert!(*heap.top() == 9); heap.push(11); @@ -270,7 +270,7 @@ mod tests { #[test] fn test_push_unique() { - let mut heap = PriorityQueue::from_vec(~[~2, ~4, ~9]); + let mut heap = PriorityQueue::from_vec(vec!(~2, ~4, ~9)); assert_eq!(heap.len(), 3); assert!(*heap.top() == ~9); heap.push(~11); @@ -292,7 +292,7 @@ mod tests { #[test] fn test_push_pop() { - let mut heap = PriorityQueue::from_vec(~[5, 5, 2, 1, 3]); + let mut heap = PriorityQueue::from_vec(vec!(5, 5, 2, 1, 3)); assert_eq!(heap.len(), 5); assert_eq!(heap.push_pop(6), 6); assert_eq!(heap.len(), 5); @@ -306,7 +306,7 @@ mod tests { #[test] fn test_replace() { - let mut heap = PriorityQueue::from_vec(~[5, 5, 2, 1, 3]); + let mut heap = PriorityQueue::from_vec(vec!(5, 5, 2, 1, 3)); assert_eq!(heap.len(), 5); assert_eq!(heap.replace(6), 5); assert_eq!(heap.len(), 5); @@ -318,7 +318,7 @@ mod tests { assert_eq!(heap.len(), 5); } - fn check_to_vec(mut data: ~[int]) { + fn check_to_vec(mut data: Vec) { let heap = PriorityQueue::from_vec(data.clone()); let mut v = heap.clone().to_vec(); v.sort(); @@ -330,19 +330,19 @@ mod tests { #[test] fn test_to_vec() { - check_to_vec(~[]); - check_to_vec(~[5]); - check_to_vec(~[3, 2]); - check_to_vec(~[2, 3]); - check_to_vec(~[5, 1, 2]); - check_to_vec(~[1, 100, 2, 3]); - check_to_vec(~[1, 3, 5, 7, 9, 2, 4, 6, 8, 0]); - check_to_vec(~[2, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1]); - check_to_vec(~[9, 11, 9, 9, 9, 9, 11, 2, 3, 4, 11, 9, 0, 0, 0, 0]); - check_to_vec(~[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); - check_to_vec(~[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]); - check_to_vec(~[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 1, 2]); - check_to_vec(~[5, 4, 3, 2, 1, 5, 4, 3, 2, 1, 5, 4, 3, 2, 1]); + check_to_vec(vec!()); + check_to_vec(vec!(5)); + check_to_vec(vec!(3, 2)); + check_to_vec(vec!(2, 3)); + check_to_vec(vec!(5, 1, 2)); + check_to_vec(vec!(1, 100, 2, 3)); + check_to_vec(vec!(1, 3, 5, 7, 9, 2, 4, 6, 8, 0)); + check_to_vec(vec!(2, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1)); + check_to_vec(vec!(9, 11, 9, 9, 9, 9, 11, 2, 3, 4, 11, 9, 0, 0, 0, 0)); + check_to_vec(vec!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); + check_to_vec(vec!(10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)); + check_to_vec(vec!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 1, 2)); + check_to_vec(vec!(5, 4, 3, 2, 1, 5, 4, 3, 2, 1, 5, 4, 3, 2, 1)); } #[test] @@ -380,9 +380,9 @@ mod tests { #[test] fn test_from_iter() { - let xs = ~[9u, 8, 7, 6, 5, 4, 3, 2, 1]; + let xs = vec!(9u, 8, 7, 6, 5, 4, 3, 2, 1); - let mut q: PriorityQueue = xs.rev_iter().map(|&x| x).collect(); + let mut q: PriorityQueue = xs.as_slice().rev_iter().map(|&x| x).collect(); for &x in xs.iter() { assert_eq!(q.pop(), x); diff --git a/src/libcollections/ringbuf.rs b/src/libcollections/ringbuf.rs index 705e0f7d3d486..c61fcd0c76bbd 100644 --- a/src/libcollections/ringbuf.rs +++ b/src/libcollections/ringbuf.rs @@ -14,7 +14,6 @@ //! collections::deque::Deque`. use std::cmp; -use std::slice; use std::iter::{Rev, RandomAccessIterator}; use deque::Deque; @@ -27,7 +26,7 @@ static MINIMUM_CAPACITY: uint = 2u; pub struct RingBuf { nelts: uint, lo: uint, - elts: ~[Option] + elts: Vec> } impl Container for RingBuf { @@ -67,7 +66,7 @@ impl Deque for RingBuf { /// Remove and return the first element in the RingBuf, or None if it is empty fn pop_front(&mut self) -> Option { - let result = self.elts[self.lo].take(); + let result = self.elts.get_mut(self.lo).take(); if result.is_some() { self.lo = (self.lo + 1u) % self.elts.len(); self.nelts -= 1u; @@ -80,7 +79,7 @@ impl Deque for RingBuf { if self.nelts > 0 { self.nelts -= 1; let hi = self.raw_index(self.nelts); - self.elts[hi].take() + self.elts.get_mut(hi).take() } else { None } @@ -94,7 +93,7 @@ impl Deque for RingBuf { if self.lo == 0u { self.lo = self.elts.len() - 1u; } else { self.lo -= 1u; } - self.elts[self.lo] = Some(t); + *self.elts.get_mut(self.lo) = Some(t); self.nelts += 1u; } @@ -104,7 +103,7 @@ impl Deque for RingBuf { grow(self.nelts, &mut self.lo, &mut self.elts); } let hi = self.raw_index(self.nelts); - self.elts[hi] = Some(t); + *self.elts.get_mut(hi) = Some(t); self.nelts += 1u; } } @@ -118,7 +117,7 @@ impl RingBuf { /// Create an empty RingBuf with space for at least `n` elements. pub fn with_capacity(n: uint) -> RingBuf { RingBuf{nelts: 0, lo: 0, - elts: slice::from_fn(cmp::max(MINIMUM_CAPACITY, n), |_| None)} + elts: Vec::from_fn(cmp::max(MINIMUM_CAPACITY, n), |_| None)} } /// Retrieve an element in the RingBuf by index @@ -126,7 +125,7 @@ impl RingBuf { /// Fails if there is no element with the given index pub fn get<'a>(&'a self, i: uint) -> &'a T { let idx = self.raw_index(i); - match self.elts[idx] { + match *self.elts.get(idx) { None => fail!(), Some(ref v) => v } @@ -137,7 +136,7 @@ impl RingBuf { /// Fails if there is no element with the given index pub fn get_mut<'a>(&'a mut self, i: uint) -> &'a mut T { let idx = self.raw_index(i); - match self.elts[idx] { + match *self.elts.get_mut(idx) { None => fail!(), Some(ref mut v) => v } @@ -153,7 +152,7 @@ impl RingBuf { assert!(j < self.len()); let ri = self.raw_index(i); let rj = self.raw_index(j); - self.elts.swap(ri, rj); + self.elts.as_mut_slice().swap(ri, rj); } /// Return index in underlying vec for a given logical element index @@ -188,7 +187,7 @@ impl RingBuf { /// Front-to-back iterator. pub fn iter<'a>(&'a self) -> Items<'a, T> { - Items{index: 0, rindex: self.nelts, lo: self.lo, elts: self.elts} + Items{index: 0, rindex: self.nelts, lo: self.lo, elts: self.elts.as_slice()} } /// Back-to-front iterator. @@ -333,7 +332,7 @@ impl<'a, T> ExactSize<&'a mut T> for MutItems<'a, T> {} /// Grow is only called on full elts, so nelts is also len(elts), unlike /// elsewhere. -fn grow(nelts: uint, loptr: &mut uint, elts: &mut ~[Option]) { +fn grow(nelts: uint, loptr: &mut uint, elts: &mut Vec>) { assert_eq!(nelts, elts.len()); let lo = *loptr; let newlen = nelts * 2; @@ -356,11 +355,11 @@ fn grow(nelts: uint, loptr: &mut uint, elts: &mut ~[Option]) { assert!(newlen - nelts/2 >= nelts); if lo <= (nelts - lo) { // A for i in range(0u, lo) { - elts.swap(i, nelts + i); + elts.as_mut_slice().swap(i, nelts + i); } } else { // B for i in range(lo, nelts) { - elts.swap(i, newlen - nelts + i); + elts.as_mut_slice().swap(i, newlen - nelts + i); } *loptr += newlen - nelts; } @@ -671,7 +670,7 @@ mod tests { let mut d: RingBuf = range(0, 5).collect(); d.pop_front(); d.swap(0, 3); - assert_eq!(d.iter().map(|&x|x).collect::<~[int]>(), ~[4, 2, 3, 1]); + assert_eq!(d.iter().map(|&x|x).collect::>(), vec!(4, 2, 3, 1)); } #[test] @@ -683,12 +682,12 @@ mod tests { for i in range(0, 5) { d.push_back(i); } - assert_eq!(d.iter().collect::<~[&int]>(), ~[&0,&1,&2,&3,&4]); + assert_eq!(d.iter().collect::>().as_slice(), &[&0,&1,&2,&3,&4]); for i in range(6, 9) { d.push_front(i); } - assert_eq!(d.iter().collect::<~[&int]>(), ~[&8,&7,&6,&0,&1,&2,&3,&4]); + assert_eq!(d.iter().collect::>().as_slice(), &[&8,&7,&6,&0,&1,&2,&3,&4]); let mut it = d.iter(); let mut len = d.len(); @@ -708,12 +707,12 @@ mod tests { for i in range(0, 5) { d.push_back(i); } - assert_eq!(d.rev_iter().collect::<~[&int]>(), ~[&4,&3,&2,&1,&0]); + assert_eq!(d.rev_iter().collect::>().as_slice(), &[&4,&3,&2,&1,&0]); for i in range(6, 9) { d.push_front(i); } - assert_eq!(d.rev_iter().collect::<~[&int]>(), ~[&4,&3,&2,&1,&0,&6,&7,&8]); + assert_eq!(d.rev_iter().collect::>().as_slice(), &[&4,&3,&2,&1,&0,&6,&7,&8]); } #[test] @@ -727,8 +726,8 @@ mod tests { assert_eq!(d.pop_front(), Some(1)); d.push_back(4); - assert_eq!(d.mut_rev_iter().map(|x| *x).collect::<~[int]>(), - ~[4, 3, 2]); + assert_eq!(d.mut_rev_iter().map(|x| *x).collect::>(), + vec!(4, 3, 2)); } #[test] @@ -780,9 +779,9 @@ mod tests { #[test] fn test_from_iter() { use std::iter; - let v = ~[1,2,3,4,5,6,7]; + let v = vec!(1,2,3,4,5,6,7); let deq: RingBuf = v.iter().map(|&x| x).collect(); - let u: ~[int] = deq.iter().map(|&x| x).collect(); + let u: Vec = deq.iter().map(|&x| x).collect(); assert_eq!(u, v); let mut seq = iter::count(0u, 2).take(256); diff --git a/src/libcollections/smallintmap.rs b/src/libcollections/smallintmap.rs index bd4f85aa81a78..4485987b15b40 100644 --- a/src/libcollections/smallintmap.rs +++ b/src/libcollections/smallintmap.rs @@ -17,11 +17,11 @@ use std::iter::{Enumerate, FilterMap, Rev}; use std::mem::replace; -use std::slice; +use std::{vec, slice}; #[allow(missing_doc)] pub struct SmallIntMap { - v: ~[Option], + v: Vec>, } impl Container for SmallIntMap { @@ -45,7 +45,7 @@ impl Map for SmallIntMap { /// Return a reference to the value corresponding to the key fn find<'a>(&'a self, key: &uint) -> Option<&'a V> { if *key < self.v.len() { - match self.v[*key] { + match *self.v.get(*key) { Some(ref value) => Some(value), None => None } @@ -59,7 +59,7 @@ impl MutableMap for SmallIntMap { /// 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() { - match self.v[*key] { + match *self.v.get_mut(*key) { Some(ref mut value) => Some(value), None => None } @@ -77,7 +77,7 @@ impl MutableMap for SmallIntMap { if len <= key { self.v.grow_fn(key - len + 1, |_| None); } - self.v[key] = Some(value); + *self.v.get_mut(key) = Some(value); !exists } @@ -104,17 +104,17 @@ impl MutableMap for SmallIntMap { if *key >= self.v.len() { return None; } - self.v[*key].take() + self.v.get_mut(*key).take() } } impl SmallIntMap { /// Create an empty SmallIntMap - pub fn new() -> SmallIntMap { SmallIntMap{v: ~[]} } + pub fn new() -> SmallIntMap { SmallIntMap{v: vec!()} } /// Create an empty SmallIntMap with capacity `capacity` pub fn with_capacity(capacity: uint) -> SmallIntMap { - SmallIntMap { v: slice::with_capacity(capacity) } + SmallIntMap { v: Vec::with_capacity(capacity) } } pub fn get<'a>(&'a self, key: &uint) -> &'a V { @@ -158,9 +158,9 @@ impl SmallIntMap { /// Empties the hash map, moving all values into the specified closure pub fn move_iter(&mut self) -> FilterMap<(uint, Option), (uint, V), - Enumerate>>> + Enumerate>>> { - let values = replace(&mut self.v, ~[]); + let values = replace(&mut self.v, vec!()); values.move_iter().enumerate().filter_map(|(i, v)| { v.map(|v| (i, v)) }) diff --git a/src/libcollections/treemap.rs b/src/libcollections/treemap.rs index 0a5e653f8ebd6..1846ca0013e86 100644 --- a/src/libcollections/treemap.rs +++ b/src/libcollections/treemap.rs @@ -139,7 +139,7 @@ impl TreeMap { /// Requires that it be frozen (immutable). pub fn iter<'a>(&'a self) -> Entries<'a, K, V> { Entries { - stack: ~[], + stack: vec!(), node: deref(&self.root), remaining_min: self.length, remaining_max: self.length @@ -156,7 +156,7 @@ impl TreeMap { /// map, with the values being mutable. pub fn mut_iter<'a>(&'a mut self) -> MutEntries<'a, K, V> { MutEntries { - stack: ~[], + stack: vec!(), node: mut_deref(&mut self.root), remaining_min: self.length, remaining_max: self.length @@ -173,8 +173,8 @@ impl TreeMap { pub fn move_iter(self) -> MoveEntries { let TreeMap { root: root, length: length } = self; let stk = match root { - None => ~[], - Some(~tn) => ~[tn] + None => vec!(), + Some(~tn) => vec!(tn) }; MoveEntries { stack: stk, @@ -222,7 +222,7 @@ impl TreeMap { /// `traverse_left`/`traverse_right`/`traverse_complete`. fn iter_for_traversal<'a>(&'a self) -> Entries<'a, K, V> { Entries { - stack: ~[], + stack: vec!(), node: deref(&self.root), remaining_min: 0, remaining_max: self.length @@ -245,7 +245,7 @@ impl TreeMap { /// `traverse_left`/`traverse_right`/`traverse_complete`. fn mut_iter_for_traversal<'a>(&'a mut self) -> MutEntries<'a, K, V> { MutEntries { - stack: ~[], + stack: vec!(), node: mut_deref(&mut self.root), remaining_min: 0, remaining_max: self.length @@ -273,7 +273,7 @@ impl TreeMap { /// Lazy forward iterator over a map pub struct Entries<'a, K, V> { - stack: ~[&'a TreeNode], + stack: Vec<&'a TreeNode>, // See the comment on MutEntries; this is just to allow // code-sharing (for this immutable-values iterator it *could* very // well be Option<&'a TreeNode>). @@ -290,7 +290,7 @@ pub struct RevEntries<'a, K, V> { /// Lazy forward iterator over a map that allows for the mutation of /// the values. pub struct MutEntries<'a, K, V> { - stack: ~[&'a mut TreeNode], + stack: Vec<&'a mut TreeNode>, // Unfortunately, we require some unsafe-ness to get around the // fact that we would be storing a reference *into* one of the // nodes in the stack. @@ -482,7 +482,7 @@ fn mut_deref(x: &mut Option<~TreeNode>) -> *mut TreeNode { /// Lazy forward iterator over a map that consumes the map while iterating pub struct MoveEntries { - stack: ~[TreeNode], + stack: Vec>, remaining: uint } @@ -1143,9 +1143,9 @@ mod test_treemap { #[test] fn test_rand_int() { let mut map: TreeMap = TreeMap::new(); - let mut ctrl = ~[]; + let mut ctrl = vec![]; - check_equal(ctrl, &map); + check_equal(ctrl.as_slice(), &map); assert!(map.find(&5).is_none()); let mut rng: rand::IsaacRng = rand::SeedableRng::from_seed(&[42]); @@ -1158,7 +1158,7 @@ mod test_treemap { assert!(map.insert(k, v)); ctrl.push((k, v)); check_structure(&map); - check_equal(ctrl, &map); + check_equal(ctrl.as_slice(), &map); } } @@ -1167,7 +1167,7 @@ mod test_treemap { let (key, _) = ctrl.remove(r).unwrap(); assert!(map.remove(&key)); check_structure(&map); - check_equal(ctrl, &map); + check_equal(ctrl.as_slice(), &map); } } } @@ -1414,7 +1414,7 @@ mod test_treemap { #[test] fn test_from_iter() { - let xs = ~[(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]; + let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]; let map: TreeMap = xs.iter().map(|&x| x).collect(); @@ -1725,7 +1725,7 @@ mod test_set { #[test] fn test_from_iter() { - let xs = ~[1, 2, 3, 4, 5, 6, 7, 8, 9]; + let xs = [1, 2, 3, 4, 5, 6, 7, 8, 9]; let set: TreeSet = xs.iter().map(|&x| x).collect(); diff --git a/src/libcollections/trie.rs b/src/libcollections/trie.rs index 8b83e65838697..5c290d56da39a 100644 --- a/src/libcollections/trie.rs +++ b/src/libcollections/trie.rs @@ -774,7 +774,7 @@ mod test_map { #[test] fn test_from_iter() { - let xs = ~[(1u, 1i), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]; + let xs = vec![(1u, 1i), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]; let map: TrieMap = xs.iter().map(|&x| x).collect(); @@ -1042,7 +1042,7 @@ mod test_set { #[test] fn test_from_iter() { - let xs = ~[9u, 8, 7, 6, 5, 4, 3, 2, 1]; + let xs = vec![9u, 8, 7, 6, 5, 4, 3, 2, 1]; let set: TrieSet = xs.iter().map(|&x| x).collect();