From a396e1e2e95acc07f2804be2079d5b692753d4bb Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Sat, 29 Jun 2013 14:02:20 +1000 Subject: [PATCH] Convert vec::{grow, grow_fn, grow_set} to methods. --- src/libextra/smallintmap.rs | 3 +- src/libstd/vec.rs | 109 +++++++++++++----------------- src/test/run-pass/issue-3563-3.rs | 4 +- 3 files changed, 48 insertions(+), 68 deletions(-) diff --git a/src/libextra/smallintmap.rs b/src/libextra/smallintmap.rs index b07c05ad76a..9cfe7cf5e4a 100644 --- a/src/libextra/smallintmap.rs +++ b/src/libextra/smallintmap.rs @@ -20,7 +20,6 @@ use std::cmp; use std::container::{Container, Mutable, Map, Set}; use std::uint; use std::util::replace; -use std::vec; #[allow(missing_doc)] pub struct SmallIntMap { @@ -86,7 +85,7 @@ impl Map for SmallIntMap { let exists = self.contains_key(&key); let len = self.v.len(); if len <= key { - vec::grow_fn(&mut self.v, key - len + 1, |_| None); + self.v.grow_fn(key - len + 1, |_| None); } self.v[key] = Some(value); !exists diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index dab379962a0..976a67ef422 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -363,63 +363,6 @@ pub fn append_one(lhs: ~[T], x: T) -> ~[T] { v } -/** - * Expands a vector in place, initializing the new elements to a given value - * - * # Arguments - * - * * v - The vector to grow - * * n - The number of elements to add - * * initval - The value for the new elements - */ -pub fn grow(v: &mut ~[T], n: uint, initval: &T) { - let new_len = v.len() + n; - v.reserve_at_least(new_len); - let mut i: uint = 0u; - - while i < n { - v.push(copy *initval); - i += 1u; - } -} - -/** - * Expands a vector in place, initializing the new elements to the result of - * a function - * - * Function `init_op` is called `n` times with the values [0..`n`) - * - * # Arguments - * - * * v - The vector to grow - * * n - The number of elements to add - * * init_op - A function to call to retreive each appended element's - * value - */ -pub fn grow_fn(v: &mut ~[T], n: uint, op: &fn(uint) -> T) { - let new_len = v.len() + n; - v.reserve_at_least(new_len); - let mut i: uint = 0u; - while i < n { - v.push(op(i)); - i += 1u; - } -} - -/** - * Sets the value of a vector element at a given index, growing the vector as - * needed - * - * Sets the element at position `index` to `val`. If `index` is past the end - * of the vector, expands the vector by replicating `initval` to fill the - * intervening space. - */ -pub fn grow_set(v: &mut ~[T], index: uint, initval: &T, val: T) { - let l = v.len(); - if index >= l { grow(&mut *v, index - l + 1u, initval); } - v[index] = val; -} - // Functional utilities /// Apply a function to each element of a vector and return the results @@ -1648,9 +1591,26 @@ impl OwnedVector for ~[T] { (lefts, rights) } - #[inline] + /** + * Expands a vector in place, initializing the new elements to the result of + * a function + * + * Function `init_op` is called `n` times with the values [0..`n`) + * + * # Arguments + * + * * n - The number of elements to add + * * init_op - A function to call to retreive each appended element's + * value + */ fn grow_fn(&mut self, n: uint, op: &fn(uint) -> T) { - grow_fn(self, n, op); + let new_len = self.len() + n; + self.reserve_at_least(new_len); + let mut i: uint = 0u; + while i < n { + self.push(op(i)); + i += 1u; + } } } @@ -1687,14 +1647,37 @@ impl OwnedCopyableVector for ~[T] { } } - #[inline] + /** + * Expands a vector in place, initializing the new elements to a given value + * + * # Arguments + * + * * n - The number of elements to add + * * initval - The value for the new elements + */ fn grow(&mut self, n: uint, initval: &T) { - grow(self, n, initval); + let new_len = self.len() + n; + self.reserve_at_least(new_len); + let mut i: uint = 0u; + + while i < n { + self.push(copy *initval); + i += 1u; + } } - #[inline] + /** + * Sets the value of a vector element at a given index, growing the vector as + * needed + * + * Sets the element at position `index` to `val`. If `index` is past the end + * of the vector, expands the vector by replicating `initval` to fill the + * intervening space. + */ fn grow_set(&mut self, index: uint, initval: &T, val: T) { - grow_set(self, index, initval, val); + let l = self.len(); + if index >= l { self.grow(index - l + 1u, initval); } + self[index] = val; } } diff --git a/src/test/run-pass/issue-3563-3.rs b/src/test/run-pass/issue-3563-3.rs index e574502a9fb..a24800e4dbb 100644 --- a/src/test/run-pass/issue-3563-3.rs +++ b/src/test/run-pass/issue-3563-3.rs @@ -69,9 +69,7 @@ fn AsciiArt(width: uint, height: uint, fill: char) -> AsciiArt { // blank characters for each position in our canvas. let mut lines = do vec::build_sized(height) |push| { for height.times { - let mut line = ~[]; - vec::grow_set(&mut line, width-1, &'.', '.'); - push(line); + push(vec::from_elem(width, '.')); } };