@@ -377,96 +377,6 @@ pub fn partitioned<T:Copy>(v: &[T], f: &fn(&T) -> bool) -> (~[T], ~[T]) {
377377 ( lefts, rights)
378378}
379379
380- // Mutators
381-
382- /// Removes the first element from a vector and return it
383- pub fn shift < T > ( v : & mut ~[ T ] ) -> T {
384- unsafe {
385- assert ! ( !v. is_empty( ) ) ;
386-
387- if v. len ( ) == 1 { return v. pop ( ) }
388-
389- if v. len ( ) == 2 {
390- let last = v. pop ( ) ;
391- let first = v. pop ( ) ;
392- v. push ( last) ;
393- return first;
394- }
395-
396- let ln = v. len ( ) ;
397- let next_ln = v. len ( ) - 1 ;
398-
399- // Save the last element. We're going to overwrite its position
400- let work_elt = v. pop ( ) ;
401- // We still should have room to work where what last element was
402- assert ! ( capacity( v) >= ln) ;
403- // Pretend like we have the original length so we can use
404- // the vector copy_memory to overwrite the hole we just made
405- raw:: set_len ( & mut * v, ln) ;
406-
407- // Memcopy the head element (the one we want) to the location we just
408- // popped. For the moment it unsafely exists at both the head and last
409- // positions
410- {
411- let first_slice = v. slice ( 0 , 1 ) ;
412- let last_slice = v. slice ( next_ln, ln) ;
413- raw:: copy_memory ( transmute ( last_slice) , first_slice, 1 ) ;
414- }
415-
416- // Memcopy everything to the left one element
417- {
418- let init_slice = v. slice ( 0 , next_ln) ;
419- let tail_slice = v. slice ( 1 , ln) ;
420- raw:: copy_memory ( transmute ( init_slice) ,
421- tail_slice,
422- next_ln) ;
423- }
424-
425- // Set the new length. Now the vector is back to normal
426- raw:: set_len ( & mut * v, next_ln) ;
427-
428- // Swap out the element we want from the end
429- let vp = raw:: to_mut_ptr ( * v) ;
430- let vp = ptr:: mut_offset ( vp, next_ln - 1 ) ;
431-
432- ptr:: replace_ptr ( vp, work_elt)
433- }
434- }
435-
436- /// Prepend an element to the vector
437- pub fn unshift < T > ( v : & mut ~[ T ] , x : T ) {
438- let vv = util:: replace ( v, ~[ x] ) ;
439- v. push_all_move ( vv) ;
440- }
441-
442- /// Insert an element at position i within v, shifting all
443- /// elements after position i one position to the right.
444- pub fn insert < T > ( v : & mut ~[ T ] , i : uint , x : T ) {
445- let len = v. len ( ) ;
446- assert ! ( i <= len) ;
447-
448- v. push ( x) ;
449- let mut j = len;
450- while j > i {
451- swap ( * v, j, j - 1 ) ;
452- j -= 1 ;
453- }
454- }
455-
456- /// Remove and return the element at position i within v, shifting
457- /// all elements after position i one position to the left.
458- pub fn remove < T > ( v : & mut ~[ T ] , i : uint ) -> T {
459- let len = v. len ( ) ;
460- assert ! ( i < len) ;
461-
462- let mut j = i;
463- while j < len - 1 {
464- swap ( * v, j, j + 1 ) ;
465- j += 1 ;
466- }
467- v. pop ( )
468- }
469-
470380/// Consumes all elements, in a vector, moving them out into the / closure
471381 /// provided. The vector is traversed from the start to the end.
472382///
@@ -528,37 +438,6 @@ pub fn consume_reverse<T>(mut v: ~[T], f: &fn(uint, v: T)) {
528438 }
529439}
530440
531- /// Remove the last element from a vector and return it
532- pub fn pop < T > ( v : & mut ~[ T ] ) -> T {
533- let ln = v. len ( ) ;
534- if ln == 0 {
535- fail ! ( "sorry, cannot vec::pop an empty vector" )
536- }
537- let valptr = ptr:: to_mut_unsafe_ptr ( & mut v[ ln - 1 u] ) ;
538- unsafe {
539- let val = ptr:: replace_ptr ( valptr, intrinsics:: init ( ) ) ;
540- raw:: set_len ( v, ln - 1 u) ;
541- val
542- }
543- }
544-
545- /**
546- * Remove an element from anywhere in the vector and return it, replacing it
547- * with the last element. This does not preserve ordering, but is O(1).
548- *
549- * Fails if index >= length.
550- */
551- pub fn swap_remove < T > ( v : & mut ~[ T ] , index : uint ) -> T {
552- let ln = v. len ( ) ;
553- if index >= ln {
554- fail ! ( "vec::swap_remove - index %u >= length %u" , index, ln) ;
555- }
556- if index < ln - 1 {
557- swap ( * v, index, ln - 1 ) ;
558- }
559- v. pop ( )
560- }
561-
562441/// Append an element to a vector
563442#[ inline]
564443pub fn push < T > ( v : & mut ~[ T ] , initval : T ) {
@@ -1847,34 +1726,123 @@ impl<T> OwnedVector<T> for ~[T] {
18471726 push_all_move ( self , rhs) ;
18481727 }
18491728
1850- # [ inline ]
1729+ /// Remove the last element from a vector and return it
18511730 fn pop ( & mut self ) -> T {
1852- pop ( self )
1731+ let ln = self . len ( ) ;
1732+ if ln == 0 {
1733+ fail ! ( "sorry, cannot pop an empty vector" )
1734+ }
1735+ let valptr = ptr:: to_mut_unsafe_ptr ( & mut self [ ln - 1 u] ) ;
1736+ unsafe {
1737+ let val = ptr:: replace_ptr ( valptr, intrinsics:: init ( ) ) ;
1738+ raw:: set_len ( self , ln - 1 u) ;
1739+ val
1740+ }
18531741 }
18541742
1855- # [ inline ]
1743+ /// Removes the first element from a vector and return it
18561744 fn shift ( & mut self ) -> T {
1857- shift ( self )
1745+ unsafe {
1746+ assert ! ( !self . is_empty( ) ) ;
1747+
1748+ if self . len ( ) == 1 { return self . pop ( ) }
1749+
1750+ if self . len ( ) == 2 {
1751+ let last = self . pop ( ) ;
1752+ let first = self . pop ( ) ;
1753+ self . push ( last) ;
1754+ return first;
1755+ }
1756+
1757+ let ln = self . len ( ) ;
1758+ let next_ln = self . len ( ) - 1 ;
1759+
1760+ // Save the last element. We're going to overwrite its position
1761+ let work_elt = self . pop ( ) ;
1762+ // We still should have room to work where what last element was
1763+ assert ! ( capacity( self ) >= ln) ;
1764+ // Pretend like we have the original length so we can use
1765+ // the vector copy_memory to overwrite the hole we just made
1766+ raw:: set_len ( self , ln) ;
1767+
1768+ // Memcopy the head element (the one we want) to the location we just
1769+ // popped. For the moment it unsafely exists at both the head and last
1770+ // positions
1771+ {
1772+ let first_slice = self . slice ( 0 , 1 ) ;
1773+ let last_slice = self . slice ( next_ln, ln) ;
1774+ raw:: copy_memory ( transmute ( last_slice) , first_slice, 1 ) ;
1775+ }
1776+
1777+ // Memcopy everything to the left one element
1778+ {
1779+ let init_slice = self . slice ( 0 , next_ln) ;
1780+ let tail_slice = self . slice ( 1 , ln) ;
1781+ raw:: copy_memory ( transmute ( init_slice) ,
1782+ tail_slice,
1783+ next_ln) ;
1784+ }
1785+
1786+ // Set the new length. Now the vector is back to normal
1787+ raw:: set_len ( self , next_ln) ;
1788+
1789+ // Swap out the element we want from the end
1790+ let vp = raw:: to_mut_ptr ( * self ) ;
1791+ let vp = ptr:: mut_offset ( vp, next_ln - 1 ) ;
1792+
1793+ ptr:: replace_ptr ( vp, work_elt)
1794+ }
18581795 }
18591796
1860- # [ inline ]
1797+ /// Prepend an element to the vector
18611798 fn unshift ( & mut self , x : T ) {
1862- unshift ( self , x)
1799+ let v = util:: replace ( self , ~[ x] ) ;
1800+ self . push_all_move ( v) ;
18631801 }
18641802
1865- #[ inline]
1803+ /// Insert an element at position i within v, shifting all
1804+ /// elements after position i one position to the right.
18661805 fn insert ( & mut self , i : uint , x : T ) {
1867- insert ( self , i, x)
1806+ let len = self . len ( ) ;
1807+ assert ! ( i <= len) ;
1808+
1809+ self . push ( x) ;
1810+ let mut j = len;
1811+ while j > i {
1812+ swap ( * self , j, j - 1 ) ;
1813+ j -= 1 ;
1814+ }
18681815 }
18691816
1870- #[ inline]
1817+ /// Remove and return the element at position i within v, shifting
1818+ /// all elements after position i one position to the left.
18711819 fn remove ( & mut self , i : uint ) -> T {
1872- remove ( self , i)
1820+ let len = self . len ( ) ;
1821+ assert ! ( i < len) ;
1822+
1823+ let mut j = i;
1824+ while j < len - 1 {
1825+ swap ( * self , j, j + 1 ) ;
1826+ j += 1 ;
1827+ }
1828+ self . pop ( )
18731829 }
18741830
1875- #[ inline]
1831+ /**
1832+ * Remove an element from anywhere in the vector and return it, replacing it
1833+ * with the last element. This does not preserve ordering, but is O(1).
1834+ *
1835+ * Fails if index >= length.
1836+ */
18761837 fn swap_remove ( & mut self , index : uint ) -> T {
1877- swap_remove ( self , index)
1838+ let ln = self . len ( ) ;
1839+ if index >= ln {
1840+ fail ! ( "vec::swap_remove - index %u >= length %u" , index, ln) ;
1841+ }
1842+ if index < ln - 1 {
1843+ swap ( * self , index, ln - 1 ) ;
1844+ }
1845+ self . pop ( )
18781846 }
18791847
18801848 #[ inline]
0 commit comments