@@ -229,9 +229,11 @@ pub pure fn tail<T>(v: &r/[T]) -> &r/[T] { slice(v, 1, v.len()) }
229
229
pub pure fn tailn<T>(v: &r/[T], n: uint) -> &r/[T] { slice(v, n, v.len()) }
230
230
231
231
/// Returns a vector containing all but the last element of a slice
232
- pub pure fn init<T:Copy>(v: &[const T]) -> ~[T] {
233
- assert len(v) != 0u;
234
- slice(v, 0u, len(v) - 1u).to_vec()
232
+ pub pure fn init<T>(v: &r/[T]) -> &r/[T] { slice(v, 0, v.len() - 1) }
233
+
234
+ /// Returns a vector containing all but the last `n' elements of a slice
235
+ pub pure fn initn<T>(v: &r/[T], n: uint) -> &r/[T] {
236
+ slice(v, 0, v.len() - n)
235
237
}
236
238
237
239
/// Returns the last element of the slice `v`, failing if the slice is empty.
@@ -1694,17 +1696,12 @@ impl<T> Container for &[const T] {
1694
1696
}
1695
1697
1696
1698
pub trait CopyableVector < T > {
1697
- pure fn init ( & self ) -> ~[ T ] ;
1698
1699
pure fn last ( & self ) -> T ;
1699
1700
pure fn slice ( & self , start : uint , end : uint ) -> ~[ T ] ;
1700
1701
}
1701
1702
1702
1703
/// Extension methods for vectors
1703
- impl < T : Copy > CopyableVector < T > for & [ const T ] {
1704
- /// Returns all but the last elemnt of a vector
1705
- #[ inline]
1706
- pure fn init ( & self ) -> ~[ T ] { init ( * self ) }
1707
-
1704
+ impl < T : Copy > CopyableVector < T > for & [ const T ] {
1708
1705
/// Returns the last element of a `v`, failing if the vector is empty.
1709
1706
#[ inline]
1710
1707
pure fn last ( & self ) -> T { last ( * self ) }
@@ -1722,6 +1719,8 @@ pub trait ImmutableVector<T> {
1722
1719
pure fn head_opt ( & self ) -> Option < & self /T > ;
1723
1720
pure fn tail ( & self ) -> & self /[ T ] ;
1724
1721
pure fn tailn ( & self , n : uint ) -> & self /[ T ] ;
1722
+ pure fn init ( & self ) -> & self /[ T ] ;
1723
+ pure fn initn ( & self , n : uint ) -> & self /[ T ] ;
1725
1724
pure fn foldr < U : Copy > ( & self , z : U , p : fn ( t : & T , u : U ) -> U ) -> U ;
1726
1725
pure fn map < U > ( & self , f : fn ( t : & T ) -> U ) -> ~[ U ] ;
1727
1726
pure fn mapi < U > ( & self , f : fn ( uint , t : & T ) -> U ) -> ~[ U ] ;
@@ -1755,6 +1754,14 @@ impl<T> ImmutableVector<T> for &[T] {
1755
1754
#[ inline]
1756
1755
pure fn tailn ( & self , n : uint ) -> & self /[ T ] { tailn ( * self , n) }
1757
1756
1757
+ /// Returns all but the last elemnt of a vector
1758
+ #[ inline]
1759
+ pure fn init ( & self ) -> & self /[ T ] { init ( * self ) }
1760
+
1761
+ /// Returns all but the last `n' elemnts of a vector
1762
+ #[ inline]
1763
+ pure fn initn ( & self , n : uint ) -> & self /[ T ] { initn ( * self , n) }
1764
+
1758
1765
/// Reduce a vector from right to left
1759
1766
#[ inline]
1760
1767
pure fn foldr < U : Copy > ( & self , z : U , p : fn ( t : & T , u : U ) -> U ) -> U {
@@ -2638,6 +2645,38 @@ mod tests {
2638
2645
a. tailn ( 2 ) ;
2639
2646
}
2640
2647
2648
+ #[ test]
2649
+ fn test_init ( ) {
2650
+ let mut a = ~[ 11 ] ;
2651
+ assert a. init ( ) == & [ ] ;
2652
+ a = ~[ 11 , 12 ] ;
2653
+ assert a. init ( ) == & [ 11 ] ;
2654
+ }
2655
+
2656
+ #[ init]
2657
+ #[ should_fail]
2658
+ #[ ignore( cfg( windows) ) ]
2659
+ fn test_init_empty ( ) {
2660
+ let a: ~[ int ] = ~[ ] ;
2661
+ a. init ( ) ;
2662
+ }
2663
+
2664
+ #[ test]
2665
+ fn test_initn ( ) {
2666
+ let mut a = ~[ 11 , 12 , 13 ] ;
2667
+ assert a. initn ( 0 ) == & [ 11 , 12 , 13 ] ;
2668
+ a = ~[ 11 , 12 , 13 ] ;
2669
+ assert a. initn ( 2 ) == & [ 11 ] ;
2670
+ }
2671
+
2672
+ #[ init]
2673
+ #[ should_fail]
2674
+ #[ ignore( cfg( windows) ) ]
2675
+ fn test_initn_empty ( ) {
2676
+ let a: ~[ int ] = ~[ ] ;
2677
+ a. initn ( 2 ) ;
2678
+ }
2679
+
2641
2680
#[ test]
2642
2681
fn test_last ( ) {
2643
2682
let mut n = last_opt ( ~[ ] ) ;
@@ -3317,12 +3356,6 @@ mod tests {
3317
3356
assert ( v2[ 1 ] == 10 ) ;
3318
3357
}
3319
3358
3320
- #[ test]
3321
- fn test_init ( ) {
3322
- let v = init ( ~[ 1 , 2 , 3 ] ) ;
3323
- assert v == ~[ 1 , 2 ] ;
3324
- }
3325
-
3326
3359
#[ test]
3327
3360
fn test_split ( ) {
3328
3361
fn f ( x : & int ) -> bool { * x == 3 }
@@ -3387,13 +3420,6 @@ mod tests {
3387
3420
( ~[ ] , ~[ 1 , 2 , 3 ] ) ;
3388
3421
}
3389
3422
3390
- #[ test]
3391
- #[ should_fail]
3392
- #[ ignore( cfg( windows) ) ]
3393
- fn test_init_empty ( ) {
3394
- init :: < int > ( ~[ ] ) ;
3395
- }
3396
-
3397
3423
#[ test]
3398
3424
fn test_concat ( ) {
3399
3425
assert concat ( ~[ ~[ 1 ] , ~[ 2 , 3 ] ] ) == ~[ 1 , 2 , 3 ] ;
0 commit comments