@@ -1479,7 +1479,7 @@ pub fn reverse_part<T>(v: &mut [T], start: uint, end : uint) {
14791479 let mut i = start;
14801480 let mut j = end - 1 ;
14811481 while i < j {
1482- v [ i ] <-> v [ j ] ;
1482+ vec :: swap ( v , i , j ) ;
14831483 i += 1 ;
14841484 j -= 1 ;
14851485 }
@@ -1790,7 +1790,6 @@ pub fn each2_mut<U, T>(v1: &mut [U], v2: &mut [T], f: &fn(u: &mut U, t: &mut T)
17901790 *
17911791 * * `fun` - The function to iterate over the combinations
17921792 */
1793- #[ cfg( not( stage0) ) ]
17941793pub fn each_permutation < T : Copy > ( values : & [ T ] , fun : & fn ( perm : & [ T ] ) -> bool ) {
17951794 let length = values. len ( ) ;
17961795 let mut permutation = vec:: from_fn ( length, |i| values[ i] ) ;
@@ -1816,16 +1815,13 @@ pub fn each_permutation<T:Copy>(values: &[T], fun: &fn(perm : &[T]) -> bool) {
18161815 }
18171816 // swap indices[k] and indices[l]; sort indices[k+1..]
18181817 // (they're just reversed)
1819- indices[ k] <-> indices[ l] ;
1820- unsafe {
1821- reverse_part ( indices, k+1 , length) ;
1822- }
1818+ vec:: swap ( indices, k, l) ;
1819+ reverse_part ( indices, k+1 , length) ;
18231820 // fixup permutation based on indices
18241821 for uint:: range( k, length) |i| {
18251822 permutation[ i] = values[ indices[ i] ] ;
18261823 }
18271824 }
1828- return true ;
18291825}
18301826
18311827/**
@@ -1844,7 +1840,7 @@ pub fn each_permutation<T:Copy>(values: &[T], fun: &fn(perm : &[T]) -> bool) {
18441840 * * `fun` - The function to iterate over the permutations
18451841 */
18461842#[ cfg( not( stage0) ) ]
1847- pub fn each_permutation_ref < T > ( values : & ' v [ T ] , fun : & fn ( perm : & [ & ' v T ] ) -> bool ) {
1843+ pub fn each_permutation_ref < T > ( values : & [ T ] , fun : & fn ( perm : & [ & T ] ) -> bool ) {
18481844 each_permutation ( vec:: from_fn ( values. len ( ) , |i| & values[ i] ) , fun) ;
18491845}
18501846
@@ -4814,95 +4810,91 @@ mod tests {
48144810 }
48154811 }
48164812
4817- fn dup < T : Copy > ( values : & [ & T ] ) -> ~[ T ] {
4818- from_fn ( values. len ( ) , |i| * values[ i] )
4819- }
4820-
48214813 #[ test]
48224814 fn test_reverse_part ( ) {
48234815 let mut values = [ 1 , 2 , 3 , 4 , 5 ] ;
48244816 reverse_part ( values, 1 , 4 ) ;
4825- assert values == [ 1 , 4 , 3 , 2 , 5 ] ;
4817+ assert_eq ! ( values, [ 1 , 4 , 3 , 2 , 5 ] ) ;
48264818 }
48274819
48284820 #[ test]
48294821 fn test_permutations0 ( ) {
48304822 let values = [ ] ;
48314823 let mut v : ~[ ~[ int ] ] = ~[ ] ;
48324824 for each_permutation( values) |p| {
4833- v. push ( vec :: from_slice ( p ) ) ;
4825+ v. push ( p . to_owned ( ) ) ;
48344826 }
4835- assert v == ~[ ~[ ] ] ;
4827+ assert_eq ! ( v , ~[ ~[ ] ] ) ;
48364828 }
48374829
48384830 #[ test]
48394831 fn test_permutations0_ref ( ) {
48404832 let values = [ ] ;
48414833 let mut v : ~[ ~[ int ] ] = ~[ ] ;
48424834 for each_permutation_ref( values) |p| {
4843- v. push ( dup ( p ) ) ;
4835+ v. push ( p . to_owned ( ) ) ;
48444836 }
4845- assert v == ~[ ~[ ] ] ;
4837+ assert_eq ! ( v , ~[ ~[ ] ] ) ;
48464838 }
48474839
48484840 #[ test]
48494841 fn test_permutations1 ( ) {
48504842 let values = [ 1 ] ;
48514843 let mut v : ~[ ~[ int ] ] = ~[ ] ;
48524844 for each_permutation( values) |p| {
4853- v. push ( vec :: from_slice ( p ) ) ;
4845+ v. push ( p . to_owned ( ) ) ;
48544846 }
4855- assert v == ~[ ~[ 1 ] ] ;
4847+ assert_eq ! ( v , ~[ ~[ 1 ] ] ) ;
48564848 }
48574849
48584850 #[ test]
48594851 fn test_permutations1_ref ( ) {
48604852 let values = [ 1 ] ;
48614853 let mut v : ~[ ~[ int ] ] = ~[ ] ;
48624854 for each_permutation_ref( values) |p| {
4863- v. push ( dup ( p ) ) ;
4855+ v. push ( p . to_owned ( ) ) ;
48644856 }
4865- assert v == ~[ ~[ 1 ] ] ;
4857+ assert_eq ! ( v , ~[ ~[ 1 ] ] ) ;
48664858 }
48674859
48684860 #[ test]
48694861 fn test_permutations2 ( ) {
48704862 let values = [ 1 , 2 ] ;
48714863 let mut v : ~[ ~[ int ] ] = ~[ ] ;
48724864 for each_permutation( values) |p| {
4873- v. push ( vec :: from_slice ( p ) ) ;
4865+ v. push ( p . to_owned ( ) ) ;
48744866 }
4875- assert v == ~[ ~[ 1 , 2 ] , ~[ 2 , 1 ] ] ;
4867+ assert_eq ! ( v , ~[ ~[ 1 , 2 ] , ~[ 2 , 1 ] ] ) ;
48764868 }
48774869
48784870 #[ test]
48794871 fn test_permutations2_ref ( ) {
48804872 let values = [ 1 , 2 ] ;
48814873 let mut v : ~[ ~[ int ] ] = ~[ ] ;
48824874 for each_permutation_ref( values) |p| {
4883- v. push ( dup ( p ) ) ;
4875+ v. push ( p . to_owned ( ) ) ;
48844876 }
4885- assert v == ~[ ~[ 1 , 2 ] , ~[ 2 , 1 ] ] ;
4877+ assert_eq ! ( v , ~[ ~[ 1 , 2 ] , ~[ 2 , 1 ] ] ) ;
48864878 }
48874879
48884880 #[ test]
48894881 fn test_permutations3 ( ) {
48904882 let values = [ 1 , 2 , 3 ] ;
48914883 let mut v : ~[ ~[ int ] ] = ~[ ] ;
48924884 for each_permutation( values) |p| {
4893- v. push ( vec :: from_slice ( p ) ) ;
4885+ v. push ( p . to_owned ( ) ) ;
48944886 }
4895- assert v == ~[ ~[ 1 , 2 , 3 ] , ~[ 1 , 3 , 2 ] , ~[ 2 , 1 , 3 ] , ~[ 2 , 3 , 1 ] , ~[ 3 , 1 , 2 ] , ~[ 3 , 2 , 1 ] ] ;
4887+ assert_eq ! ( v , ~[ ~[ 1 , 2 , 3 ] , ~[ 1 , 3 , 2 ] , ~[ 2 , 1 , 3 ] , ~[ 2 , 3 , 1 ] , ~[ 3 , 1 , 2 ] , ~[ 3 , 2 , 1 ] ] ) ;
48964888 }
48974889
48984890 #[ test]
48994891 fn test_permutations3_ref ( ) {
49004892 let values = [ 1 , 2 , 3 ] ;
49014893 let mut v : ~[ ~[ int ] ] = ~[ ] ;
49024894 for each_permutation_ref( values) |p| {
4903- v. push ( dup ( p ) ) ;
4895+ v. push ( p . to_owned ( ) ) ;
49044896 }
4905- assert v == ~[ ~[ 1 , 2 , 3 ] , ~[ 1 , 3 , 2 ] , ~[ 2 , 1 , 3 ] , ~[ 2 , 3 , 1 ] , ~[ 3 , 1 , 2 ] , ~[ 3 , 2 , 1 ] ] ;
4897+ assert_eq ! ( v , ~[ ~[ 1 , 2 , 3 ] , ~[ 1 , 3 , 2 ] , ~[ 2 , 1 , 3 ] , ~[ 2 , 3 , 1 ] , ~[ 3 , 1 , 2 ] , ~[ 3 , 2 , 1 ] ] ) ;
49064898 }
49074899
49084900 #[ test]
0 commit comments