@@ -438,86 +438,6 @@ pub fn consume_reverse<T>(mut v: ~[T], f: &fn(uint, v: T)) {
438438 }
439439}
440440
441- /// Append an element to a vector
442- #[ inline]
443- pub fn push < T > ( v : & mut ~[ T ] , initval : T ) {
444- unsafe {
445- let repr: * * raw :: VecRepr = transmute ( & mut * v) ;
446- let fill = ( * * repr) . unboxed . fill ;
447- if ( * * repr) . unboxed . alloc > fill {
448- push_fast ( v, initval) ;
449- }
450- else {
451- push_slow ( v, initval) ;
452- }
453- }
454- }
455-
456- // This doesn't bother to make sure we have space.
457- #[ inline] // really pretty please
458- unsafe fn push_fast < T > ( v : & mut ~[ T ] , initval : T ) {
459- let repr: * * mut raw:: VecRepr = transmute ( v) ;
460- let fill = ( * * repr) . unboxed . fill ;
461- ( * * repr) . unboxed . fill += sys:: nonzero_size_of :: < T > ( ) ;
462- let p = to_unsafe_ptr ( & ( ( * * repr) . unboxed . data ) ) ;
463- let p = ptr:: offset ( p, fill) as * mut T ;
464- intrinsics:: move_val_init ( & mut ( * p) , initval) ;
465- }
466-
467- #[ inline( never) ]
468- fn push_slow < T > ( v : & mut ~[ T ] , initval : T ) {
469- let new_len = v. len ( ) + 1 ;
470- reserve_at_least ( & mut * v, new_len) ;
471- unsafe { push_fast ( v, initval) }
472- }
473-
474- /// Iterates over the slice `rhs`, copies each element, and then appends it to
475- /// the vector provided `v`. The `rhs` vector is traversed in-order.
476- ///
477- /// # Example
478- ///
479- /// ~~~ {.rust}
480- /// let mut a = ~[1];
481- /// vec::push_all(&mut a, [2, 3, 4]);
482- /// assert!(a == ~[1, 2, 3, 4]);
483- /// ~~~
484- #[ inline]
485- pub fn push_all < T : Copy > ( v : & mut ~[ T ] , rhs : & const [ T ] ) {
486- let new_len = v. len ( ) + rhs. len ( ) ;
487- reserve ( & mut * v, new_len) ;
488-
489- for uint:: range( 0 u, rhs. len( ) ) |i| {
490- push( & mut * v, unsafe { raw : : get( rhs, i) } )
491- }
492- }
493-
494- /// Takes ownership of the vector `rhs`, moving all elements into the specified
495- /// vector `v`. This does not copy any elements, and it is illegal to use the
496- /// `rhs` vector after calling this method (because it is moved here).
497- ///
498- /// # Example
499- ///
500- /// ~~~ {.rust}
501- /// let mut a = ~[~1];
502- /// vec::push_all_move(&mut a, ~[~2, ~3, ~4]);
503- /// assert!(a == ~[~1, ~2, ~3, ~4]);
504- /// ~~~
505- #[ inline]
506- pub fn push_all_move < T > ( v : & mut ~[ T ] , mut rhs : ~[ T ] ) {
507- let new_len = v. len ( ) + rhs. len ( ) ;
508- reserve ( & mut * v, new_len) ;
509- unsafe {
510- do as_mut_buf ( rhs) |p, len| {
511- for uint:: range( 0 , len) |i| {
512- let x = ptr:: replace_ptr ( ptr:: mut_offset ( p, i) ,
513- intrinsics:: uninit ( ) ) ;
514- push ( & mut * v, x) ;
515- }
516- }
517- raw:: set_len ( & mut rhs, 0 ) ;
518- }
519- }
520-
521441/// Shorten a vector, dropping excess elements.
522442pub fn truncate < T > ( v : & mut ~[ T ] , newlen : uint ) {
523443 do as_mut_buf ( * v) |p, oldlen| {
@@ -1699,6 +1619,8 @@ impl<'self,T:Copy> ImmutableCopyableVector<T> for &'self [T] {
16991619#[ allow( missing_doc) ]
17001620pub trait OwnedVector < T > {
17011621 fn push ( & mut self , t : T ) ;
1622+ unsafe fn push_fast ( & mut self , t : T ) ;
1623+
17021624 fn push_all_move ( & mut self , rhs : ~[ T ] ) ;
17031625 fn pop ( & mut self ) -> T ;
17041626 fn shift ( & mut self ) -> T ;
@@ -1716,14 +1638,67 @@ pub trait OwnedVector<T> {
17161638}
17171639
17181640impl < T > OwnedVector < T > for ~[ T ] {
1641+ /// Append an element to a vector
17191642 #[ inline]
17201643 fn push ( & mut self , t : T ) {
1721- push ( self , t) ;
1644+ unsafe {
1645+ let repr: * * raw :: VecRepr = transmute ( & mut * self ) ;
1646+ let fill = ( * * repr) . unboxed . fill ;
1647+ if ( * * repr) . unboxed . alloc <= fill {
1648+ // need more space
1649+ reserve_no_inline ( self ) ;
1650+ }
1651+
1652+ self . push_fast ( t) ;
1653+ }
1654+
1655+ // this peculiar function is because reserve_at_least is very
1656+ // large (because of reserve), and will be inlined, which
1657+ // makes push too large.
1658+ #[ inline( never) ]
1659+ fn reserve_no_inline < T > ( v : & mut ~[ T ] ) {
1660+ let new_len = v. len ( ) + 1 ;
1661+ reserve_at_least ( v, new_len) ;
1662+ }
17221663 }
17231664
1724- #[ inline]
1725- fn push_all_move ( & mut self , rhs : ~[ T ] ) {
1726- push_all_move ( self , rhs) ;
1665+ // This doesn't bother to make sure we have space.
1666+ #[ inline] // really pretty please
1667+ unsafe fn push_fast ( & mut self , t : T ) {
1668+ let repr: * * mut raw:: VecRepr = transmute ( self ) ;
1669+ let fill = ( * * repr) . unboxed . fill ;
1670+ ( * * repr) . unboxed . fill += sys:: nonzero_size_of :: < T > ( ) ;
1671+ let p = to_unsafe_ptr ( & ( ( * * repr) . unboxed . data ) ) ;
1672+ let p = ptr:: offset ( p, fill) as * mut T ;
1673+ intrinsics:: move_val_init ( & mut ( * p) , t) ;
1674+ }
1675+
1676+ /// Takes ownership of the vector `rhs`, moving all elements into
1677+ /// the current vector. This does not copy any elements, and it is
1678+ /// illegal to use the `rhs` vector after calling this method
1679+ /// (because it is moved here).
1680+ ///
1681+ /// # Example
1682+ ///
1683+ /// ~~~ {.rust}
1684+ /// let mut a = ~[~1];
1685+ /// a.push_all_move(~[~2, ~3, ~4]);
1686+ /// assert!(a == ~[~1, ~2, ~3, ~4]);
1687+ /// ~~~
1688+ #[ inline]
1689+ fn push_all_move ( & mut self , mut rhs : ~[ T ] ) {
1690+ let new_len = self . len ( ) + rhs. len ( ) ;
1691+ reserve ( self , new_len) ;
1692+ unsafe {
1693+ do as_mut_buf ( rhs) |p, len| {
1694+ for uint:: range( 0 , len) |i| {
1695+ let x = ptr:: replace_ptr ( ptr:: mut_offset ( p, i) ,
1696+ intrinsics:: uninit ( ) ) ;
1697+ self . push ( x) ;
1698+ }
1699+ }
1700+ raw:: set_len ( & mut rhs, 0 ) ;
1701+ }
17271702 }
17281703
17291704 /// Remove the last element from a vector and return it
@@ -1898,9 +1873,24 @@ pub trait OwnedCopyableVector<T:Copy> {
18981873}
18991874
19001875impl < T : Copy > OwnedCopyableVector < T > for ~[ T ] {
1876+ /// Iterates over the slice `rhs`, copies each element, and then appends it to
1877+ /// the vector provided `v`. The `rhs` vector is traversed in-order.
1878+ ///
1879+ /// # Example
1880+ ///
1881+ /// ~~~ {.rust}
1882+ /// let mut a = ~[1];
1883+ /// a.push_all([2, 3, 4]);
1884+ /// assert!(a == ~[1, 2, 3, 4]);
1885+ /// ~~~
19011886 #[ inline]
19021887 fn push_all ( & mut self , rhs : & const [ T ] ) {
1903- push_all ( self , rhs) ;
1888+ let new_len = self . len ( ) + rhs. len ( ) ;
1889+ reserve ( self , new_len) ;
1890+
1891+ for uint:: range( 0 u, rhs. len( ) ) |i| {
1892+ self . push ( unsafe { raw:: get ( rhs, i) } )
1893+ }
19041894 }
19051895
19061896 #[ inline]
0 commit comments