@@ -68,63 +68,6 @@ pub fn same_length<T, U>(xs: &const [T], ys: &const [U]) -> bool {
6868 xs. len ( ) == ys. len ( )
6969}
7070
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-
12871/**
12972 * Creates and initializes an owned vector.
13073 *
@@ -179,7 +122,7 @@ pub fn to_owned<T:Copy>(t: &[T]) -> ~[T] {
179122/// Creates a new vector with a capacity of `capacity`
180123pub fn with_capacity < T > ( capacity : uint ) -> ~[ T ] {
181124 let mut vec = ~[ ] ;
182- reserve ( & mut vec , capacity) ;
125+ vec . reserve ( capacity) ;
183126 vec
184127}
185128
@@ -466,7 +409,7 @@ pub fn append_one<T>(lhs: ~[T], x: T) -> ~[T] {
466409 */
467410pub fn grow < T : Copy > ( v : & mut ~[ T ] , n : uint , initval : & T ) {
468411 let new_len = v. len ( ) + n;
469- reserve_at_least ( & mut * v , new_len) ;
412+ v . reserve_at_least ( new_len) ;
470413 let mut i: uint = 0 u;
471414
472415 while i < n {
@@ -490,7 +433,7 @@ pub fn grow<T:Copy>(v: &mut ~[T], n: uint, initval: &T) {
490433 */
491434pub fn grow_fn < T > ( v : & mut ~[ T ] , n : uint , op : & fn ( uint ) -> T ) {
492435 let new_len = v. len ( ) + n;
493- reserve_at_least ( & mut * v , new_len) ;
436+ v . reserve_at_least ( new_len) ;
494437 let mut i: uint = 0 u;
495438 while i < n {
496439 v. push ( op ( i) ) ;
@@ -1298,13 +1241,11 @@ impl<'self,T:Copy> CopyableVector<T> for &'self [T] {
12981241 /// Returns a copy of `v`.
12991242 #[ inline]
13001243 fn to_owned( & self ) -> ~[ T ] {
1301- let mut result = ~[ ] ;
1302- reserve ( & mut result, self . len ( ) ) ;
1244+ let mut result = with_capacity ( self . len ( ) ) ;
13031245 for self . iter( ) . advance |e| {
13041246 result. push( copy * e) ;
13051247 }
13061248 result
1307-
13081249 }
13091250}
13101251
@@ -1555,6 +1496,10 @@ impl<'self,T:Copy> ImmutableCopyableVector<T> for &'self [T] {
15551496
15561497#[ allow( missing_doc) ]
15571498pub 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+
15581503 fn push( & mut self , t: T ) ;
15591504 unsafe fn push_fast( & mut self , t: T ) ;
15601505
@@ -1575,6 +1520,61 @@ pub trait OwnedVector<T> {
15751520}
15761521
15771522impl <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+
15781578 /// Append an element to a vector
15791579 #[ inline]
15801580 fn push( & mut self , t: T ) {
@@ -1595,7 +1595,7 @@ impl<T> OwnedVector<T> for ~[T] {
15951595 #[ inline( never) ]
15961596 fn reserve_no_inline < T > ( v : & mut ~[ T ] ) {
15971597 let new_len = v. len ( ) + 1 ;
1598- reserve_at_least ( v , new_len) ;
1598+ v . reserve_at_least ( new_len) ;
15991599 }
16001600 }
16011601
@@ -1625,7 +1625,7 @@ impl<T> OwnedVector<T> for ~[T] {
16251625 #[ inline]
16261626 fn push_all_move ( & mut self , mut rhs : ~[ T ] ) {
16271627 let new_len = self . len ( ) + rhs. len ( ) ;
1628- reserve ( self , new_len) ;
1628+ self . reserve ( new_len) ;
16291629 unsafe {
16301630 do as_mut_buf ( rhs) |p, len| {
16311631 for uint:: range( 0 , len) |i| {
@@ -1672,7 +1672,7 @@ impl<T> OwnedVector<T> for ~[T] {
16721672 // Save the last element. We're going to overwrite its position
16731673 let work_elt = self . pop ( ) ;
16741674 // We still should have room to work where what last element was
1675- assert ! ( capacity( self ) >= ln) ;
1675+ assert ! ( self . capacity( ) >= ln) ;
16761676 // Pretend like we have the original length so we can use
16771677 // the vector copy_memory to overwrite the hole we just made
16781678 raw:: set_len ( self , ln) ;
@@ -1859,7 +1859,7 @@ impl<T:Copy> OwnedCopyableVector<T> for ~[T] {
18591859 #[ inline]
18601860 fn push_all ( & mut self , rhs : & const [ T ] ) {
18611861 let new_len = self . len ( ) + rhs. len ( ) ;
1862- reserve ( self , new_len) ;
1862+ self . reserve ( new_len) ;
18631863
18641864 for uint:: range( 0 u, rhs. len( ) ) |i| {
18651865 self . push ( unsafe { raw:: get ( rhs, i) } )
@@ -3333,11 +3333,11 @@ mod tests {
33333333 #[ test]
33343334 fn test_capacity ( ) {
33353335 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) ;
33383338 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) ;
33413341 }
33423342
33433343 #[ test]
0 commit comments