1
1
#[ doc = "Vectors" ] ;
2
2
3
3
import option:: { some, none} ;
4
- import uint:: next_power_of_two;
5
4
import ptr:: addr_of;
6
5
7
6
export init_op;
8
7
export is_empty;
9
8
export is_not_empty;
10
9
export same_length;
11
10
export reserve;
11
+ export reserve_at_least;
12
12
export len;
13
13
export from_fn;
14
14
export from_elem;
@@ -115,6 +115,25 @@ fn reserve<T>(&v: [const T], n: uint) {
115
115
rustrt:: vec_reserve_shared ( sys:: get_type_desc :: < T > ( ) , v, n) ;
116
116
}
117
117
118
+ #[ doc = "
119
+ Reserves capacity for at least `n` elements in the given vector.
120
+
121
+ This function will over-allocate in order to amortize the allocation costs
122
+ in scenarios where the caller may need to repeatedly reserve additional
123
+ space.
124
+
125
+ If the capacity for `v` is already equal to or greater than the requested
126
+ capacity, then no action is taken.
127
+
128
+ # Arguments
129
+
130
+ * v - A vector
131
+ * n - The number of elements to reserve space for
132
+ " ]
133
+ fn reserve_at_least < T > ( & v: [ const T ] , n : uint ) {
134
+ reserve ( v, uint:: next_power_of_two ( n) ) ;
135
+ }
136
+
118
137
#[ doc = "Returns the length of a vector" ]
119
138
#[ inline( always) ]
120
139
pure fn len < T > ( & & v: [ const T ] ) -> uint unsafe {
@@ -364,7 +383,7 @@ Expands a vector in place, initializing the new elements to a given value
364
383
* initval - The value for the new elements
365
384
" ]
366
385
fn grow < T : copy > ( & v : [ const T ] , n : uint , initval : T ) {
367
- reserve ( v , next_power_of_two ( len ( v ) + n ) ) ;
386
+ reserve_at_least ( v , len ( v ) + n ) ;
368
387
let mut i: uint = 0 u;
369
388
while i < n { v += [ initval] ; i += 1 u; }
370
389
}
@@ -383,7 +402,7 @@ Function `init_op` is called `n` times with the values [0..`n`)
383
402
value
384
403
" ]
385
404
fn grow_fn < T > ( & v : [ const T ] , n : uint , op : init_op < T > ) {
386
- reserve ( v , next_power_of_two ( len ( v ) + n ) ) ;
405
+ reserve_at_least ( v , len ( v ) + n ) ;
387
406
let mut i : uint = 0 u;
388
407
while i < n { v += [ op ( i) ] ; i += 1 u; }
389
408
}
0 commit comments