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.
This commit is contained in:
parent
5f69a58e0c
commit
c11ee0fb67
src
libextra
librustc/middle/typeck/infer
libstd
libsyntax/ext/deriving
test/run-pass
@ -315,11 +315,8 @@ mod test {
|
||||
use std::vec;
|
||||
|
||||
do 1000.times {
|
||||
let v: ~[u8] = do vec::build |push| {
|
||||
do task_rng().gen_uint_range(1, 100).times {
|
||||
push(random());
|
||||
}
|
||||
};
|
||||
let times = task_rng().gen_uint_range(1, 100);
|
||||
let v = vec::from_fn(times, |_| random::<u8>());
|
||||
assert_eq!(v.to_base64(STANDARD).from_base64().unwrap(), v);
|
||||
}
|
||||
}
|
||||
|
@ -61,7 +61,6 @@ use middle::typeck::infer::{TypeTrace};
|
||||
use util::common::indent;
|
||||
|
||||
use std::result;
|
||||
use std::vec;
|
||||
use syntax::ast::{Onceness, purity};
|
||||
use syntax::ast;
|
||||
use syntax::opt_vec;
|
||||
|
@ -373,14 +373,12 @@ impl RegionVarBindings {
|
||||
|
||||
pub fn vars_created_since_snapshot(&mut self, snapshot: uint)
|
||||
-> ~[RegionVid] {
|
||||
do vec::build |push| {
|
||||
for &elt in self.undo_log.slice_from(snapshot).iter() {
|
||||
match elt {
|
||||
AddVar(vid) => push(vid),
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
}
|
||||
self.undo_log.slice_from(snapshot).iter()
|
||||
.filter_map(|&elt| match elt {
|
||||
AddVar(vid) => Some(vid),
|
||||
_ => None
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn tainted(&mut self, snapshot: uint, r0: Region) -> ~[Region] {
|
||||
|
@ -33,42 +33,7 @@ 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.
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * size - An 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 = @[];
|
||||
unsafe { raw::reserve(&mut vec, size); }
|
||||
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.
|
||||
* The initial size for the vector may optionally be specified
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
@ -78,8 +43,11 @@ pub fn build<A>(builder: &fn(push: &fn(v: A))) -> @[A] {
|
||||
* 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)
|
||||
pub fn build<A>(size: Option<uint>, builder: &fn(push: &fn(v: A))) -> @[A] {
|
||||
let mut vec = @[];
|
||||
unsafe { raw::reserve(&mut vec, size.unwrap_or_default(4)); }
|
||||
builder(|x| unsafe { raw::push(&mut vec, x) });
|
||||
vec
|
||||
}
|
||||
|
||||
// Appending
|
||||
@ -88,7 +56,7 @@ pub fn build_sized_opt<A>(size: Option<uint>, builder: &fn(push: &fn(v: A))) ->
|
||||
/// `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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -194,41 +194,7 @@ 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.
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * size - An 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);
|
||||
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.
|
||||
* The initial capacity for the vector may optionally be specified.
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
@ -238,8 +204,10 @@ pub fn build<A>(builder: &fn(push: &fn(v: A))) -> ~[A] {
|
||||
* 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)
|
||||
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
|
||||
}
|
||||
|
||||
/// An iterator over the slices of a vector separated by elements that
|
||||
@ -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));
|
||||
|
@ -958,7 +958,7 @@ fn create_struct_pattern(cx: @ExtCtxt,
|
||||
// struct_type is definitely not Unknown, since struct_def.fields
|
||||
// must be nonempty to reach here
|
||||
let pattern = if struct_type == Record {
|
||||
let field_pats = do vec::build |push| {
|
||||
let field_pats = do vec::build(None) |push| {
|
||||
for (&pat, &(id, _)) in subpats.iter().zip(ident_expr.iter()) {
|
||||
// id is guaranteed to be Some
|
||||
push(ast::FieldPat { ident: id.unwrap(), pat: pat })
|
||||
|
@ -66,7 +66,7 @@ impl Drop for AsciiArt {
|
||||
fn AsciiArt(width: uint, height: uint, fill: char) -> AsciiArt {
|
||||
// Use an anonymous function to build a vector of vectors containing
|
||||
// blank characters for each position in our canvas.
|
||||
let lines = do vec::build_sized(height) |push| {
|
||||
let lines = do vec::build(Some(height)) |push| {
|
||||
do height.times {
|
||||
push(vec::from_elem(width, '.'));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user