@@ -33,62 +33,30 @@ pub fn capacity<T>(v: @[T]) -> uint {
33
33
/**
34
34
* Builds a vector by calling a provided function with an argument
35
35
* 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
37
37
*
38
38
* # Arguments
39
39
*
40
- * * size - An initial size of the vector to reserve
40
+ * * size - An option, maybe containing initial size of the vector to reserve
41
41
* * builder - A function that will construct the vector. It receives
42
42
* as an argument a function that will push an element
43
43
* onto the vector being constructed.
44
44
*/
45
45
#[ 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 ] {
47
47
let mut vec = @[ ] ;
48
- unsafe { raw:: reserve ( & mut vec, size) ; }
48
+ unsafe { raw:: reserve ( & mut vec, size. unwrap_or_default ( 4 ) ) ; }
49
49
builder ( |x| unsafe { raw:: push ( & mut vec, x) } ) ;
50
50
vec
51
51
}
52
52
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
-
85
53
// Appending
86
54
87
55
/// Iterates over the `rhs` vector, copying each element and appending it to the
88
56
/// `lhs`. Afterwards, the `lhs` is then returned for use again.
89
57
#[ inline]
90
58
pub 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| {
92
60
for x in lhs. iter ( ) {
93
61
push ( ( * x) . clone ( ) ) ;
94
62
}
@@ -101,7 +69,7 @@ pub fn append<T:Clone>(lhs: @[T], rhs: &[T]) -> @[T] {
101
69
102
70
/// Apply a function to each element of a vector and return the results
103
71
pub 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| {
105
73
for elem in v. iter ( ) {
106
74
push ( f ( elem) ) ;
107
75
}
@@ -115,7 +83,7 @@ pub fn map<T, U>(v: &[T], f: &fn(x: &T) -> U) -> @[U] {
115
83
* to the value returned by the function `op`.
116
84
*/
117
85
pub 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| {
119
87
let mut i: uint = 0 u;
120
88
while i < n_elts { push ( op ( i) ) ; i += 1 u; }
121
89
}
@@ -128,7 +96,7 @@ pub fn from_fn<T>(n_elts: uint, op: &fn(uint) -> T) -> @[T] {
128
96
* to the value `t`.
129
97
*/
130
98
pub 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| {
132
100
let mut i: uint = 0 u;
133
101
while i < n_elts {
134
102
push ( t. clone ( ) ) ;
@@ -312,7 +280,7 @@ mod test {
312
280
fn test ( ) {
313
281
// Some code that could use that, then:
314
282
fn seq_range ( lo : uint , hi : uint ) -> @[ uint ] {
315
- do build |push| {
283
+ do build ( None ) |push| {
316
284
for i in range ( lo, hi) {
317
285
push ( i) ;
318
286
}
@@ -359,15 +327,15 @@ mod test {
359
327
fn bench_build_sized( b: & mut bh) {
360
328
let len = 64 ;
361
329
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) } ) ;
363
331
}
364
332
}
365
333
366
334
#[ bench]
367
335
fn bench_build( b: & mut bh) {
368
336
do b. iter {
369
337
for i in range( 0 , 95 ) {
370
- build( |push| push( i) ) ;
338
+ build( None , |push| push( i) ) ;
371
339
}
372
340
}
373
341
}
0 commit comments