collections: replace all ~[T] with Vec<T>.

This commit is contained in:
Huon Wilson 2014-04-05 16:45:42 +11:00
parent d3c831ba4a
commit 4b9a7a2588
13 changed files with 212 additions and 213 deletions

View File

@ -111,7 +111,7 @@ impl SmallBitv {
#[deriving(Clone)]
struct BigBitv {
storage: ~[uint]
storage: Vec<uint>
}
/**
@ -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<uint>) -> 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<<nbits % uint::BITS)-1);
v
}
} else { slice::from_elem(nelems, 0u)};
} else { Vec::from_elem(nelems, 0u)};
Big(BigBitv::new(s))
};
Bitv {rep: rep, nbits: nbits}
@ -451,8 +451,8 @@ impl Bitv {
*
* Each `uint` in the resulting vector has either value `0u` or `1u`.
*/
pub fn to_vec(&self) -> ~[uint] {
slice::from_fn(self.nbits, |x| self.init_to_vec(x))
pub fn to_vec(&self) -> Vec<uint> {
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<u8> {
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<bool>` 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<bool> {
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<uint> 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<Enumerate<slice::Items<'a, uint>>, Repeat<&'a ~[uint]>>> {
-> Map<'static, ((uint, &'a uint), &'a Vec<uint>), (uint, uint, uint),
Zip<Enumerate<slice::Items<'a, uint>>, Repeat<&'a Vec<uint>>>> {
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::<bool>(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<uint> = 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(|| {

View File

@ -35,7 +35,7 @@ impl<K: TotalOrd, V> BTree<K, V> {
///The lower bound applies to every node except the root node.
pub fn new(k: K, v: V, lb: uint) -> BTree<K, V> {
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<K, V> {
//Node functions/methods
impl<K: TotalOrd, V> Node<K, V> {
///Creates a new leaf node given a vector of elements.
fn new_leaf(vec: ~[LeafElt<K, V>]) -> Node<K,V> {
fn new_leaf(vec: Vec<LeafElt<K, V>>) -> Node<K,V> {
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<K, V>], right: ~Node<K, V>) -> Node<K, V> {
fn new_branch(vec: Vec<BranchElt<K, V>>, right: ~Node<K, V>) -> Node<K, V> {
BranchNode(Branch::new(vec, right))
}
@ -264,19 +264,19 @@ impl<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for Node<K, V> {
//A leaf is a vector with elements that contain no children. A leaf also
//does not contain a rightmost child.
struct Leaf<K, V> {
elts: ~[LeafElt<K, V>]
elts: Vec<LeafElt<K, V>>
}
//Vector of values with children, plus a rightmost child (greater than all)
struct Branch<K, V> {
elts: ~[BranchElt<K,V>],
elts: Vec<BranchElt<K,V>>,
rightmost_child: ~Node<K, V>
}
impl<K: TotalOrd, V> Leaf<K, V> {
///Creates a new Leaf from a vector of LeafElts.
fn new(vec: ~[LeafElt<K, V>]) -> Leaf<K, V> {
fn new(vec: Vec<LeafElt<K, V>>) -> Leaf<K, V> {
Leaf {
elts: vec
}
@ -292,14 +292,14 @@ impl<K: TotalOrd, V> Leaf<K, V> {
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<K: TotalOrd, V> Leaf<K, V> {
}
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<K: Clone + TotalOrd, V: Clone> Leaf<K, V> {
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<K: TotalOrd, V: TotalEq> TotalOrd for Leaf<K, V> {
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<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for Leaf<K, V> {
impl<K: TotalOrd, V> Branch<K, V> {
///Creates a new Branch from a vector of BranchElts and a rightmost child (a node).
fn new(vec: ~[BranchElt<K, V>], right: ~Node<K, V>) -> Branch<K, V> {
fn new(vec: Vec<BranchElt<K, V>>, right: ~Node<K, V>) -> Branch<K, V> {
Branch {
elts: vec,
rightmost_child: right
@ -449,14 +449,14 @@ impl<K: TotalOrd, V> Branch<K, V> {
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<K: TotalOrd, V> Branch<K, V> {
}
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<K: Clone + TotalOrd, V: Clone> Branch<K, V> {
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<K: Clone + TotalOrd, V: Clone> Branch<K, V> {
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<K: Clone + TotalOrd, V: Clone> Branch<K, V> {
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<K: Clone + TotalOrd, V: Clone> Branch<K, V> {
//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<K: Clone + TotalOrd, V: Clone> Branch<K, V> {
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<K: Clone + TotalOrd, V: Clone> Branch<K, V> {
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<K: TotalOrd, V: TotalEq> TotalOrd for Branch<K, V> {
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));
}

View File

@ -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::<uint>() % n);
let mut keys = Vec::from_fn(n, |_| rng.gen::<uint>() % 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;
})
}

View File

@ -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::<Vec<&int>>(), 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<int>>(), 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::<Vec<int>>();
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::<Vec<int>>());
}
#[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::<Vec<&int>>().as_slice());
});
}
@ -1047,7 +1047,7 @@ mod tests {
#[cfg(test)]
fn fuzz_test(sz: int) {
let mut m: DList<int> = DList::new();
let mut v = ~[];
let mut v = vec![];
for i in range(0, sz) {
check_links(&m);
let r: u8 = rand::random();

View File

@ -246,24 +246,24 @@ mod test {
fn test_iterator() {
let mut e1: EnumSet<Foo> = EnumSet::empty();
let elems: ~[Foo] = e1.iter().collect();
assert_eq!(~[], elems)
let elems: Vec<Foo> = 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)
}
}

View File

@ -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<u64, K, V>]`.
/// but in general is just a tricked out `Vec<Option<u64, K, V>>`.
///
/// 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::<Vec<(char, int)>>();
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::<HashMap<int, char>>();
let keys = map.keys().map(|&k| k).collect::<~[int]>();
let keys = map.keys().map(|&k| k).collect::<Vec<int>>();
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::<HashMap<int, char>>();
let values = map.values().map(|&v| v).collect::<~[char]>();
let values = map.values().map(|&v| v).collect::<Vec<char>>();
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<int, int> = 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<int> = 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::<Vec<char>>();
assert!(['a', 'b'] == v.as_slice() || ['b', 'a'] == v.as_slice());
}
#[test]

View File

@ -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;

View File

@ -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<u8>> = 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);
}

View File

@ -19,7 +19,7 @@ use std::slice;
/// A priority queue implemented with a binary heap
#[deriving(Clone)]
pub struct PriorityQueue<T> {
data: ~[T],
data: Vec<T>,
}
impl<T:Ord> Container for PriorityQueue<T> {
@ -40,7 +40,7 @@ impl<T:Ord> PriorityQueue<T> {
}
/// 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<T:Ord> PriorityQueue<T> {
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<T:Ord> PriorityQueue<T> {
/// 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<T:Ord> PriorityQueue<T> {
/// 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<T> { 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<T> {
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<T> { PriorityQueue{data: ~[],} }
pub fn new() -> PriorityQueue<T> { PriorityQueue{data: vec!(),} }
/// Create an empty PriorityQueue with capacity `capacity`
pub fn with_capacity(capacity: uint) -> PriorityQueue<T> {
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<T> {
pub fn from_vec(xs: Vec<T>) -> PriorityQueue<T> {
let mut q = PriorityQueue{data: xs,};
let mut n = q.len() / 2;
while n > 0 {
@ -140,40 +140,40 @@ impl<T:Ord> PriorityQueue<T> {
// 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<int>) {
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<uint> = xs.rev_iter().map(|&x| x).collect();
let mut q: PriorityQueue<uint> = xs.as_slice().rev_iter().map(|&x| x).collect();
for &x in xs.iter() {
assert_eq!(q.pop(), x);

View File

@ -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<T> {
nelts: uint,
lo: uint,
elts: ~[Option<T>]
elts: Vec<Option<T>>
}
impl<T> Container for RingBuf<T> {
@ -67,7 +66,7 @@ impl<T> Deque<T> for RingBuf<T> {
/// Remove and return the first element in the RingBuf, or None if it is empty
fn pop_front(&mut self) -> Option<T> {
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<T> Deque<T> for RingBuf<T> {
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<T> Deque<T> for RingBuf<T> {
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<T> Deque<T> for RingBuf<T> {
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<T> RingBuf<T> {
/// Create an empty RingBuf with space for at least `n` elements.
pub fn with_capacity(n: uint) -> RingBuf<T> {
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<T> RingBuf<T> {
/// 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<T> RingBuf<T> {
/// 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<T> RingBuf<T> {
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<T> RingBuf<T> {
/// 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<T>(nelts: uint, loptr: &mut uint, elts: &mut ~[Option<T>]) {
fn grow<T>(nelts: uint, loptr: &mut uint, elts: &mut Vec<Option<T>>) {
assert_eq!(nelts, elts.len());
let lo = *loptr;
let newlen = nelts * 2;
@ -356,11 +355,11 @@ fn grow<T>(nelts: uint, loptr: &mut uint, elts: &mut ~[Option<T>]) {
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<int> = 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<int>>(), 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::<Vec<&int>>().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::<Vec<&int>>().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::<Vec<&int>>().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::<Vec<&int>>().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<int>>(),
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<int> = v.iter().map(|&x| x).collect();
let u: ~[int] = deq.iter().map(|&x| x).collect();
let u: Vec<int> = deq.iter().map(|&x| x).collect();
assert_eq!(u, v);
let mut seq = iter::count(0u, 2).take(256);

View File

@ -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<T> {
v: ~[Option<T>],
v: Vec<Option<T>>,
}
impl<V> Container for SmallIntMap<V> {
@ -45,7 +45,7 @@ impl<V> Map<uint, V> for SmallIntMap<V> {
/// 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<V> MutableMap<uint, V> for SmallIntMap<V> {
/// 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<V> MutableMap<uint, V> for SmallIntMap<V> {
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<V> MutableMap<uint, V> for SmallIntMap<V> {
if *key >= self.v.len() {
return None;
}
self.v[*key].take()
self.v.get_mut(*key).take()
}
}
impl<V> SmallIntMap<V> {
/// Create an empty SmallIntMap
pub fn new() -> SmallIntMap<V> { SmallIntMap{v: ~[]} }
pub fn new() -> SmallIntMap<V> { SmallIntMap{v: vec!()} }
/// Create an empty SmallIntMap with capacity `capacity`
pub fn with_capacity(capacity: uint) -> SmallIntMap<V> {
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<V> SmallIntMap<V> {
/// Empties the hash map, moving all values into the specified closure
pub fn move_iter(&mut self)
-> FilterMap<(uint, Option<V>), (uint, V),
Enumerate<slice::MoveItems<Option<V>>>>
Enumerate<vec::MoveItems<Option<V>>>>
{
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))
})

View File

@ -139,7 +139,7 @@ impl<K: TotalOrd, V> TreeMap<K, V> {
/// 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<K: TotalOrd, V> TreeMap<K, V> {
/// 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<K: TotalOrd, V> TreeMap<K, V> {
pub fn move_iter(self) -> MoveEntries<K, V> {
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<K: TotalOrd, V> TreeMap<K, V> {
/// `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<K: TotalOrd, V> TreeMap<K, V> {
/// `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<K: TotalOrd, V> TreeMap<K, V> {
/// Lazy forward iterator over a map
pub struct Entries<'a, K, V> {
stack: ~[&'a TreeNode<K, V>],
stack: Vec<&'a TreeNode<K, V>>,
// 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<K,V>>).
@ -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<K, V>],
stack: Vec<&'a mut TreeNode<K, V>>,
// 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<K, V>(x: &mut Option<~TreeNode<K, V>>) -> *mut TreeNode<K, V> {
/// Lazy forward iterator over a map that consumes the map while iterating
pub struct MoveEntries<K, V> {
stack: ~[TreeNode<K, V>],
stack: Vec<TreeNode<K, V>>,
remaining: uint
}
@ -1143,9 +1143,9 @@ mod test_treemap {
#[test]
fn test_rand_int() {
let mut map: TreeMap<int,int> = 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<int, int> = 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<int> = xs.iter().map(|&x| x).collect();

View File

@ -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<int> = 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();