make rand code use slices

This commit is contained in:
Niko Matsakis
2012-08-27 19:55:18 -07:00
parent 0a01d82f6f
commit 206edf66c9
2 changed files with 14 additions and 14 deletions

View File

@@ -165,12 +165,12 @@ impl Rng {
} }
/// Choose an item randomly, failing if values is empty /// Choose an item randomly, failing if values is empty
fn choose<T:copy>(values: ~[T]) -> T { fn choose<T:copy>(values: &[T]) -> T {
self.choose_option(values).get() self.choose_option(values).get()
} }
/// Choose Some(item) randomly, returning None if values is empty /// Choose Some(item) randomly, returning None if values is empty
fn choose_option<T:copy>(values: ~[T]) -> Option<T> { fn choose_option<T:copy>(values: &[T]) -> Option<T> {
if values.is_empty() { if values.is_empty() {
None None
} else { } else {
@@ -182,7 +182,7 @@ impl Rng {
* Choose an item respecting the relative weights, failing if the sum of * Choose an item respecting the relative weights, failing if the sum of
* the weights is 0 * the weights is 0
*/ */
fn choose_weighted<T: copy>(v : ~[Weighted<T>]) -> T { fn choose_weighted<T: copy>(v : &[Weighted<T>]) -> T {
self.choose_weighted_option(v).get() self.choose_weighted_option(v).get()
} }
@@ -190,7 +190,7 @@ impl Rng {
* Choose Some(item) respecting the relative weights, returning none if * Choose Some(item) respecting the relative weights, returning none if
* the sum of the weights is 0 * the sum of the weights is 0
*/ */
fn choose_weighted_option<T:copy>(v: ~[Weighted<T>]) -> Option<T> { fn choose_weighted_option<T:copy>(v: &[Weighted<T>]) -> Option<T> {
let mut total = 0u; let mut total = 0u;
for v.each |item| { for v.each |item| {
total += item.weight; total += item.weight;
@@ -213,7 +213,7 @@ impl Rng {
* Return a vec containing copies of the items, in order, where * Return a vec containing copies of the items, in order, where
* the weight of the item determines how many copies there are * the weight of the item determines how many copies there are
*/ */
fn weighted_vec<T:copy>(v: ~[Weighted<T>]) -> ~[T] { fn weighted_vec<T:copy>(v: &[Weighted<T>]) -> ~[T] {
let mut r = ~[]; let mut r = ~[];
for v.each |item| { for v.each |item| {
for uint::range(0u, item.weight) |_i| { for uint::range(0u, item.weight) |_i| {
@@ -224,14 +224,14 @@ impl Rng {
} }
/// Shuffle a vec /// Shuffle a vec
fn shuffle<T:copy>(values: ~[T]) -> ~[T] { fn shuffle<T:copy>(values: &[T]) -> ~[T] {
let mut m = vec::to_mut(values); let mut m = vec::from_slice(values);
self.shuffle_mut(m); self.shuffle_mut(m);
return vec::from_mut(m); return m;
} }
/// Shuffle a mutable vec in place /// Shuffle a mutable vec in place
fn shuffle_mut<T>(&&values: ~[mut T]) { fn shuffle_mut<T>(values: &[mut T]) {
let mut i = values.len(); let mut i = values.len();
while i >= 2u { while i >= 2u {
// invariant: elements with index >= i have been locked in place. // invariant: elements with index >= i have been locked in place.
@@ -402,14 +402,14 @@ mod tests {
#[test] #[test]
fn choose() { fn choose() {
let r = rand::Rng(); let r = rand::Rng();
assert r.choose(~[1, 1, 1]) == 1; assert r.choose([1, 1, 1]) == 1;
} }
#[test] #[test]
fn choose_option() { fn choose_option() {
let r = rand::Rng(); let r = rand::Rng();
assert r.choose_option(~[]).is_none(); assert r.choose_option::<int>([]).is_none();
assert r.choose_option(~[1, 1, 1]) == Some(1); assert r.choose_option([1, 1, 1]) == Some(1);
} }
#[test] #[test]
@@ -431,7 +431,7 @@ mod tests {
{weight: 0u, item: 42}, {weight: 0u, item: 42},
{weight: 1u, item: 43} {weight: 1u, item: 43}
]) == Some(43); ]) == Some(43);
assert r.choose_weighted_option(~[]).is_none(); assert r.choose_weighted_option::<int>([]).is_none();
} }
#[test] #[test]

View File

@@ -1086,7 +1086,7 @@ pure fn zip<T, U>(+v: ~[const T], +u: ~[const U]) -> ~[(T, U)] {
* * a - The index of the first element * * a - The index of the first element
* * b - The index of the second element * * b - The index of the second element
*/ */
fn swap<T>(&&v: ~[mut T], a: uint, b: uint) { fn swap<T>(v: &[mut T], a: uint, b: uint) {
v[a] <-> v[b]; v[a] <-> v[b];
} }