@@ -68,63 +68,6 @@ pub fn same_length<T, U>(xs: &const [T], ys: &const [U]) -> bool {
68
68
xs. len ( ) == ys. len ( )
69
69
}
70
70
71
- /**
72
- * Reserves capacity for exactly `n` elements in the given vector.
73
- *
74
- * If the capacity for `v` is already equal to or greater than the requested
75
- * capacity, then no action is taken.
76
- *
77
- * # Arguments
78
- *
79
- * * v - A vector
80
- * * n - The number of elements to reserve space for
81
- */
82
- #[ inline]
83
- pub fn reserve < T > ( v : & mut ~[ T ] , n : uint ) {
84
- // Only make the (slow) call into the runtime if we have to
85
- use managed;
86
- if capacity ( v) < n {
87
- unsafe {
88
- let ptr: * * raw :: VecRepr = cast:: transmute ( v) ;
89
- let td = get_tydesc :: < T > ( ) ;
90
- if ( ( * * ptr) . box_header . ref_count ==
91
- managed:: raw:: RC_MANAGED_UNIQUE ) {
92
- rustrt:: vec_reserve_shared_actual ( td, ptr, n as libc:: size_t ) ;
93
- } else {
94
- rustrt:: vec_reserve_shared ( td, ptr, n as libc:: size_t ) ;
95
- }
96
- }
97
- }
98
- }
99
-
100
- /**
101
- * Reserves capacity for at least `n` elements in the given vector.
102
- *
103
- * This function will over-allocate in order to amortize the allocation costs
104
- * in scenarios where the caller may need to repeatedly reserve additional
105
- * space.
106
- *
107
- * If the capacity for `v` is already equal to or greater than the requested
108
- * capacity, then no action is taken.
109
- *
110
- * # Arguments
111
- *
112
- * * v - A vector
113
- * * n - The number of elements to reserve space for
114
- */
115
- pub fn reserve_at_least < T > ( v : & mut ~[ T ] , n : uint ) {
116
- reserve ( v, uint:: next_power_of_two ( n) ) ;
117
- }
118
-
119
- /// Returns the number of elements the vector can hold without reallocating
120
- #[ inline]
121
- pub fn capacity < T > ( v : & const ~[ T ] ) -> uint {
122
- unsafe {
123
- let repr: * * raw :: VecRepr = transmute ( v) ;
124
- ( * * repr) . unboxed . alloc / sys:: nonzero_size_of :: < T > ( )
125
- }
126
- }
127
-
128
71
/**
129
72
* Creates and initializes an owned vector.
130
73
*
@@ -179,7 +122,7 @@ pub fn to_owned<T:Copy>(t: &[T]) -> ~[T] {
179
122
/// Creates a new vector with a capacity of `capacity`
180
123
pub fn with_capacity < T > ( capacity : uint ) -> ~[ T ] {
181
124
let mut vec = ~[ ] ;
182
- reserve ( & mut vec , capacity) ;
125
+ vec . reserve ( capacity) ;
183
126
vec
184
127
}
185
128
@@ -466,7 +409,7 @@ pub fn append_one<T>(lhs: ~[T], x: T) -> ~[T] {
466
409
*/
467
410
pub fn grow < T : Copy > ( v : & mut ~[ T ] , n : uint , initval : & T ) {
468
411
let new_len = v. len ( ) + n;
469
- reserve_at_least ( & mut * v , new_len) ;
412
+ v . reserve_at_least ( new_len) ;
470
413
let mut i: uint = 0 u;
471
414
472
415
while i < n {
@@ -490,7 +433,7 @@ pub fn grow<T:Copy>(v: &mut ~[T], n: uint, initval: &T) {
490
433
*/
491
434
pub fn grow_fn < T > ( v : & mut ~[ T ] , n : uint , op : & fn ( uint ) -> T ) {
492
435
let new_len = v. len ( ) + n;
493
- reserve_at_least ( & mut * v , new_len) ;
436
+ v . reserve_at_least ( new_len) ;
494
437
let mut i: uint = 0 u;
495
438
while i < n {
496
439
v. push ( op ( i) ) ;
@@ -1298,13 +1241,11 @@ impl<'self,T:Copy> CopyableVector<T> for &'self [T] {
1298
1241
/// Returns a copy of `v`.
1299
1242
#[ inline]
1300
1243
fn to_owned( & self ) -> ~[ T ] {
1301
- let mut result = ~[ ] ;
1302
- reserve ( & mut result, self . len ( ) ) ;
1244
+ let mut result = with_capacity ( self . len ( ) ) ;
1303
1245
for self . iter( ) . advance |e| {
1304
1246
result. push( copy * e) ;
1305
1247
}
1306
1248
result
1307
-
1308
1249
}
1309
1250
}
1310
1251
@@ -1555,6 +1496,10 @@ impl<'self,T:Copy> ImmutableCopyableVector<T> for &'self [T] {
1555
1496
1556
1497
#[ allow( missing_doc) ]
1557
1498
pub trait OwnedVector < T > {
1499
+ fn reserve( & mut self , n: uint) ;
1500
+ fn reserve_at_least( & mut self , n: uint) ;
1501
+ fn capacity( & self ) -> uint;
1502
+
1558
1503
fn push( & mut self , t: T ) ;
1559
1504
unsafe fn push_fast( & mut self , t: T ) ;
1560
1505
@@ -1575,6 +1520,61 @@ pub trait OwnedVector<T> {
1575
1520
}
1576
1521
1577
1522
impl <T > OwnedVector < T > for ~[ T ] {
1523
+ /**
1524
+ * Reserves capacity for exactly `n` elements in the given vector.
1525
+ *
1526
+ * If the capacity for `self` is already equal to or greater than the requested
1527
+ * capacity, then no action is taken.
1528
+ *
1529
+ * # Arguments
1530
+ *
1531
+ * * n - The number of elements to reserve space for
1532
+ */
1533
+ #[ inline]
1534
+ fn reserve( & mut self , n: uint) {
1535
+ // Only make the (slow) call into the runtime if we have to
1536
+ use managed;
1537
+ if self . capacity( ) < n {
1538
+ unsafe {
1539
+ let ptr: * * raw :: VecRepr = cast:: transmute( self ) ;
1540
+ let td = get_tydesc :: < T > ( ) ;
1541
+ if ( ( * * ptr) . box_header. ref_count ==
1542
+ managed:: raw:: RC_MANAGED_UNIQUE ) {
1543
+ rustrt:: vec_reserve_shared_actual( td, ptr, n as libc:: size_t) ;
1544
+ } else {
1545
+ rustrt:: vec_reserve_shared( td, ptr, n as libc:: size_t) ;
1546
+ }
1547
+ }
1548
+ }
1549
+ }
1550
+
1551
+ /**
1552
+ * Reserves capacity for at least `n` elements in the given vector.
1553
+ *
1554
+ * This function will over-allocate in order to amortize the allocation costs
1555
+ * in scenarios where the caller may need to repeatedly reserve additional
1556
+ * space.
1557
+ *
1558
+ * If the capacity for `self` is already equal to or greater than the requested
1559
+ * capacity, then no action is taken.
1560
+ *
1561
+ * # Arguments
1562
+ *
1563
+ * * n - The number of elements to reserve space for
1564
+ */
1565
+ fn reserve_at_least( & mut self , n: uint) {
1566
+ self . reserve( uint:: next_power_of_two( n) ) ;
1567
+ }
1568
+
1569
+ /// Returns the number of elements the vector can hold without reallocating.
1570
+ #[ inline]
1571
+ fn capacity( & self ) -> uint {
1572
+ unsafe {
1573
+ let repr: * * raw :: VecRepr = transmute( self ) ;
1574
+ ( * * repr) . unboxed. alloc / sys:: nonzero_size_of:: < T > ( )
1575
+ }
1576
+ }
1577
+
1578
1578
/// Append an element to a vector
1579
1579
#[ inline]
1580
1580
fn push( & mut self , t: T ) {
@@ -1595,7 +1595,7 @@ impl<T> OwnedVector<T> for ~[T] {
1595
1595
#[ inline( never) ]
1596
1596
fn reserve_no_inline < T > ( v : & mut ~[ T ] ) {
1597
1597
let new_len = v. len ( ) + 1 ;
1598
- reserve_at_least ( v , new_len) ;
1598
+ v . reserve_at_least ( new_len) ;
1599
1599
}
1600
1600
}
1601
1601
@@ -1625,7 +1625,7 @@ impl<T> OwnedVector<T> for ~[T] {
1625
1625
#[ inline]
1626
1626
fn push_all_move ( & mut self , mut rhs : ~[ T ] ) {
1627
1627
let new_len = self . len ( ) + rhs. len ( ) ;
1628
- reserve ( self , new_len) ;
1628
+ self . reserve ( new_len) ;
1629
1629
unsafe {
1630
1630
do as_mut_buf ( rhs) |p, len| {
1631
1631
for uint:: range( 0 , len) |i| {
@@ -1672,7 +1672,7 @@ impl<T> OwnedVector<T> for ~[T] {
1672
1672
// Save the last element. We're going to overwrite its position
1673
1673
let work_elt = self . pop ( ) ;
1674
1674
// We still should have room to work where what last element was
1675
- assert ! ( capacity( self ) >= ln) ;
1675
+ assert ! ( self . capacity( ) >= ln) ;
1676
1676
// Pretend like we have the original length so we can use
1677
1677
// the vector copy_memory to overwrite the hole we just made
1678
1678
raw:: set_len ( self , ln) ;
@@ -1859,7 +1859,7 @@ impl<T:Copy> OwnedCopyableVector<T> for ~[T] {
1859
1859
#[ inline]
1860
1860
fn push_all ( & mut self , rhs : & const [ T ] ) {
1861
1861
let new_len = self . len ( ) + rhs. len ( ) ;
1862
- reserve ( self , new_len) ;
1862
+ self . reserve ( new_len) ;
1863
1863
1864
1864
for uint:: range( 0 u, rhs. len( ) ) |i| {
1865
1865
self . push ( unsafe { raw:: get ( rhs, i) } )
@@ -3333,11 +3333,11 @@ mod tests {
3333
3333
#[ test]
3334
3334
fn test_capacity ( ) {
3335
3335
let mut v = ~[ 0u64 ] ;
3336
- reserve ( & mut v , 10 u) ;
3337
- assert_eq ! ( capacity( & v ) , 10 u) ;
3336
+ v . reserve ( 10 u) ;
3337
+ assert_eq ! ( v . capacity( ) , 10 u) ;
3338
3338
let mut v = ~[ 0u32 ] ;
3339
- reserve ( & mut v , 10 u) ;
3340
- assert_eq ! ( capacity( & v ) , 10 u) ;
3339
+ v . reserve ( 10 u) ;
3340
+ assert_eq ! ( v . capacity( ) , 10 u) ;
3341
3341
}
3342
3342
3343
3343
#[ test]
0 commit comments