diff options
| author | blake2-ppc <blake2-ppc> | 2013-09-10 05:50:11 +0200 |
|---|---|---|
| committer | blake2-ppc <blake2-ppc> | 2013-09-10 05:50:11 +0200 |
| commit | c11ee0fb6719e38820762308d67f70cee000bed8 (patch) | |
| tree | 256b9032dc36f372e00882660c1ee221ee5129e5 /src/libstd | |
| parent | 5f69a58e0ccf3d85b5f12f26ecf78ee7e7fec270 (diff) | |
| download | rust-c11ee0fb6719e38820762308d67f70cee000bed8.tar.gz rust-c11ee0fb6719e38820762308d67f70cee000bed8.zip | |
std::at_vec and vec: Unify build_sized, build_sized_opt into build
These functions have very few users since they are mostly replaced by iterator-based constructions. Convert a few remaining users in-tree, and reduce the number of functions by basically renaming build_sized_opt to build, and removing the other two. This for both the vec and the at_vec versions.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/at_vec.rs | 54 | ||||
| -rw-r--r-- | src/libstd/io.rs | 2 | ||||
| -rw-r--r-- | src/libstd/vec.rs | 42 |
3 files changed, 17 insertions, 81 deletions
diff --git a/src/libstd/at_vec.rs b/src/libstd/at_vec.rs index c192803efff..27bef1214b2 100644 --- a/src/libstd/at_vec.rs +++ b/src/libstd/at_vec.rs @@ -33,62 +33,30 @@ pub fn capacity<T>(v: @[T]) -> uint { /** * Builds a vector by calling a provided function with an argument * function that pushes an element to the back of a vector. - * This version takes an initial size for the vector. + * The initial size for the vector may optionally be specified * * # Arguments * - * * size - An initial size of the vector to reserve + * * size - An option, maybe containing initial size of the vector to reserve * * builder - A function that will construct the vector. It receives * as an argument a function that will push an element * onto the vector being constructed. */ #[inline] -pub fn build_sized<A>(size: uint, builder: &fn(push: &fn(v: A))) -> @[A] { +pub fn build<A>(size: Option<uint>, builder: &fn(push: &fn(v: A))) -> @[A] { let mut vec = @[]; - unsafe { raw::reserve(&mut vec, size); } + unsafe { raw::reserve(&mut vec, size.unwrap_or_default(4)); } builder(|x| unsafe { raw::push(&mut vec, x) }); vec } -/** - * Builds a vector by calling a provided function with an argument - * function that pushes an element to the back of a vector. - * - * # Arguments - * - * * builder - A function that will construct the vector. It receives - * as an argument a function that will push an element - * onto the vector being constructed. - */ -#[inline] -pub fn build<A>(builder: &fn(push: &fn(v: A))) -> @[A] { - build_sized(4, builder) -} - -/** - * Builds a vector by calling a provided function with an argument - * function that pushes an element to the back of a vector. - * This version takes an initial size for the vector. - * - * # Arguments - * - * * size - An option, maybe containing initial size of the vector to reserve - * * builder - A function that will construct the vector. It receives - * as an argument a function that will push an element - * onto the vector being constructed. - */ -#[inline] -pub fn build_sized_opt<A>(size: Option<uint>, builder: &fn(push: &fn(v: A))) -> @[A] { - build_sized(size.unwrap_or_default(4), builder) -} - // Appending /// Iterates over the `rhs` vector, copying each element and appending it to the /// `lhs`. Afterwards, the `lhs` is then returned for use again. #[inline] pub fn append<T:Clone>(lhs: @[T], rhs: &[T]) -> @[T] { - do build_sized(lhs.len() + rhs.len()) |push| { + do build(Some(lhs.len() + rhs.len())) |push| { for x in lhs.iter() { push((*x).clone()); } @@ -101,7 +69,7 @@ pub fn append<T:Clone>(lhs: @[T], rhs: &[T]) -> @[T] { /// Apply a function to each element of a vector and return the results pub fn map<T, U>(v: &[T], f: &fn(x: &T) -> U) -> @[U] { - do build_sized(v.len()) |push| { + do build(Some(v.len())) |push| { for elem in v.iter() { push(f(elem)); } @@ -115,7 +83,7 @@ pub fn map<T, U>(v: &[T], f: &fn(x: &T) -> U) -> @[U] { * to the value returned by the function `op`. */ pub fn from_fn<T>(n_elts: uint, op: &fn(uint) -> T) -> @[T] { - do build_sized(n_elts) |push| { + do build(Some(n_elts)) |push| { let mut i: uint = 0u; while i < n_elts { push(op(i)); i += 1u; } } @@ -128,7 +96,7 @@ pub fn from_fn<T>(n_elts: uint, op: &fn(uint) -> T) -> @[T] { * to the value `t`. */ pub fn from_elem<T:Clone>(n_elts: uint, t: T) -> @[T] { - do build_sized(n_elts) |push| { + do build(Some(n_elts)) |push| { let mut i: uint = 0u; while i < n_elts { push(t.clone()); @@ -312,7 +280,7 @@ mod test { fn test() { // Some code that could use that, then: fn seq_range(lo: uint, hi: uint) -> @[uint] { - do build |push| { + do build(None) |push| { for i in range(lo, hi) { push(i); } @@ -359,7 +327,7 @@ mod test { fn bench_build_sized(b: &mut bh) { let len = 64; do b.iter { - build_sized(len, |push| for i in range(0, 1024) { push(i) }); + build(Some(len), |push| for i in range(0, 1024) { push(i) }); } } @@ -367,7 +335,7 @@ mod test { fn bench_build(b: &mut bh) { do b.iter { for i in range(0, 95) { - build(|push| push(i)); + build(None, |push| push(i)); } } } diff --git a/src/libstd/io.rs b/src/libstd/io.rs index ee948446614..ee4100f1ec9 100644 --- a/src/libstd/io.rs +++ b/src/libstd/io.rs @@ -777,7 +777,7 @@ impl<T:Reader> ReaderUtil for T { } fn read_lines(&self) -> ~[~str] { - do vec::build |push| { + do vec::build(None) |push| { do self.each_line |line| { push(line.to_owned()); true diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 27566bf23c8..c45e37477ce 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -194,54 +194,22 @@ pub fn with_capacity<T>(capacity: uint) -> ~[T] { /** * Builds a vector by calling a provided function with an argument * function that pushes an element to the back of a vector. - * This version takes an initial capacity for the vector. + * The initial capacity for the vector may optionally be specified. * * # Arguments * - * * size - An initial size of the vector to reserve + * * size - An option, maybe containing initial size of the vector to reserve * * builder - A function that will construct the vector. It receives * as an argument a function that will push an element * onto the vector being constructed. */ #[inline] -pub fn build_sized<A>(size: uint, builder: &fn(push: &fn(v: A))) -> ~[A] { - let mut vec = with_capacity(size); +pub fn build<A>(size: Option<uint>, builder: &fn(push: &fn(v: A))) -> ~[A] { + let mut vec = with_capacity(size.unwrap_or_default(4)); builder(|x| vec.push(x)); vec } -/** - * Builds a vector by calling a provided function with an argument - * function that pushes an element to the back of a vector. - * - * # Arguments - * - * * builder - A function that will construct the vector. It receives - * as an argument a function that will push an element - * onto the vector being constructed. - */ -#[inline] -pub fn build<A>(builder: &fn(push: &fn(v: A))) -> ~[A] { - build_sized(4, builder) -} - -/** - * Builds a vector by calling a provided function with an argument - * function that pushes an element to the back of a vector. - * This version takes an initial size for the vector. - * - * # Arguments - * - * * size - An option, maybe containing initial size of the vector to reserve - * * builder - A function that will construct the vector. It receives - * as an argument a function that will push an element - * onto the vector being constructed. - */ -#[inline] -pub fn build_sized_opt<A>(size: Option<uint>, builder: &fn(push: &fn(v: A))) -> ~[A] { - build_sized(size.unwrap_or_default(4), builder) -} - /// An iterator over the slices of a vector separated by elements that /// match a predicate function. pub struct SplitIterator<'self, T> { @@ -3248,7 +3216,7 @@ mod tests { #[test] #[should_fail] fn test_build_fail() { - do build |push| { + do build(None) |push| { push((~0, @0)); push((~0, @0)); push((~0, @0)); |
