@@ -33,62 +33,30 @@ pub fn capacity<T>(v: @[T]) -> uint {
3333/**
3434 * Builds a vector by calling a provided function with an argument
3535 * function that pushes an element to the back of a vector.
36- * This version takes an initial size for the vector.
36+ * The initial size for the vector may optionally be specified
3737 *
3838 * # Arguments
3939 *
40- * * size - An initial size of the vector to reserve
40+ * * size - An option, maybe containing initial size of the vector to reserve
4141 * * builder - A function that will construct the vector. It receives
4242 * as an argument a function that will push an element
4343 * onto the vector being constructed.
4444 */
4545#[ inline]
46- pub fn build_sized < A > ( size : uint , builder : & fn ( push : & fn ( v : A ) ) ) -> @[ A ] {
46+ pub fn build < A > ( size : Option < uint > , builder : & fn ( push : & fn ( v : A ) ) ) -> @[ A ] {
4747 let mut vec = @[ ] ;
48- unsafe { raw:: reserve ( & mut vec, size) ; }
48+ unsafe { raw:: reserve ( & mut vec, size. unwrap_or_default ( 4 ) ) ; }
4949 builder ( |x| unsafe { raw:: push ( & mut vec, x) } ) ;
5050 vec
5151}
5252
53- /**
54- * Builds a vector by calling a provided function with an argument
55- * function that pushes an element to the back of a vector.
56- *
57- * # Arguments
58- *
59- * * builder - A function that will construct the vector. It receives
60- * as an argument a function that will push an element
61- * onto the vector being constructed.
62- */
63- #[ inline]
64- pub fn build < A > ( builder : & fn ( push : & fn ( v : A ) ) ) -> @[ A ] {
65- build_sized ( 4 , builder)
66- }
67-
68- /**
69- * Builds a vector by calling a provided function with an argument
70- * function that pushes an element to the back of a vector.
71- * This version takes an initial size for the vector.
72- *
73- * # Arguments
74- *
75- * * size - An option, maybe containing initial size of the vector to reserve
76- * * builder - A function that will construct the vector. It receives
77- * as an argument a function that will push an element
78- * onto the vector being constructed.
79- */
80- #[ inline]
81- pub fn build_sized_opt < A > ( size : Option < uint > , builder : & fn ( push : & fn ( v : A ) ) ) -> @[ A ] {
82- build_sized ( size. unwrap_or_default ( 4 ) , builder)
83- }
84-
8553// Appending
8654
8755/// Iterates over the `rhs` vector, copying each element and appending it to the
8856/// `lhs`. Afterwards, the `lhs` is then returned for use again.
8957#[ inline]
9058pub fn append < T : Clone > ( lhs : @[ T ] , rhs : & [ T ] ) -> @[ T ] {
91- do build_sized ( lhs. len ( ) + rhs. len ( ) ) |push| {
59+ do build ( Some ( lhs. len ( ) + rhs. len ( ) ) ) |push| {
9260 for x in lhs. iter ( ) {
9361 push ( ( * x) . clone ( ) ) ;
9462 }
@@ -101,7 +69,7 @@ pub fn append<T:Clone>(lhs: @[T], rhs: &[T]) -> @[T] {
10169
10270/// Apply a function to each element of a vector and return the results
10371pub fn map < T , U > ( v : & [ T ] , f : & fn ( x : & T ) -> U ) -> @[ U ] {
104- do build_sized ( v. len ( ) ) |push| {
72+ do build ( Some ( v. len ( ) ) ) |push| {
10573 for elem in v. iter ( ) {
10674 push ( f ( elem) ) ;
10775 }
@@ -115,7 +83,7 @@ pub fn map<T, U>(v: &[T], f: &fn(x: &T) -> U) -> @[U] {
11583 * to the value returned by the function `op`.
11684 */
11785pub fn from_fn < T > ( n_elts : uint , op : & fn ( uint ) -> T ) -> @[ T ] {
118- do build_sized ( n_elts) |push| {
86+ do build ( Some ( n_elts) ) |push| {
11987 let mut i: uint = 0 u;
12088 while i < n_elts { push ( op ( i) ) ; i += 1 u; }
12189 }
@@ -128,7 +96,7 @@ pub fn from_fn<T>(n_elts: uint, op: &fn(uint) -> T) -> @[T] {
12896 * to the value `t`.
12997 */
13098pub fn from_elem < T : Clone > ( n_elts : uint , t : T ) -> @[ T ] {
131- do build_sized ( n_elts) |push| {
99+ do build ( Some ( n_elts) ) |push| {
132100 let mut i: uint = 0 u;
133101 while i < n_elts {
134102 push ( t. clone ( ) ) ;
@@ -312,7 +280,7 @@ mod test {
312280 fn test ( ) {
313281 // Some code that could use that, then:
314282 fn seq_range ( lo : uint , hi : uint ) -> @[ uint ] {
315- do build |push| {
283+ do build ( None ) |push| {
316284 for i in range ( lo, hi) {
317285 push ( i) ;
318286 }
@@ -359,15 +327,15 @@ mod test {
359327 fn bench_build_sized( b: & mut bh) {
360328 let len = 64 ;
361329 do b. iter {
362- build_sized ( len, |push| for i in range( 0 , 1024 ) { push( i) } ) ;
330+ build ( Some ( len) , |push| for i in range( 0 , 1024 ) { push( i) } ) ;
363331 }
364332 }
365333
366334 #[ bench]
367335 fn bench_build( b: & mut bh) {
368336 do b. iter {
369337 for i in range( 0 , 95 ) {
370- build( |push| push( i) ) ;
338+ build( None , |push| push( i) ) ;
371339 }
372340 }
373341 }
0 commit comments