@@ -211,40 +211,41 @@ pub pure fn build_sized_opt<A>(size: Option<uint>,
211
211
// Accessors
212
212
213
213
/// Returns the first element of a vector
214
- pub pure fn head < T : Copy > ( v : & [ const T ] ) -> T { v[ 0 ] }
215
-
216
- /// Returns a vector containing all but the first element of a slice
217
- pub pure fn tail < T : Copy > ( v : & [ const T ] ) -> ~[ T ] {
218
- slice ( v, 1 u, len ( v) ) . to_vec ( )
214
+ pub pure fn head < T > ( v : & r/[ T ] ) -> & r /T {
215
+ if v. len ( ) == 0 { fail ! ( ~"head: empty vector") }
216
+ &v[0]
219
217
}
220
218
221
- /**
222
- * Returns a vector containing all but the first `n` \
223
- * elements of a slice
224
- */
225
- pub pure fn tailn < T : Copy > ( v : & [ const T ] , n : uint ) -> ~[ T ] {
226
- slice ( v, n, len ( v) ) . to_vec ( )
219
+ /// Returns `Some(x)` where `x` is the first element of the slice `v`,
220
+ /// or `None` if the vector is empty.
221
+ pub pure fn head_opt<T>(v: &r/[T]) -> Option<&r/T> {
222
+ if v.len() == 0 { None } else { Some(&v[0]) }
227
223
}
228
224
225
+ /// Returns a vector containing all but the first element of a slice
226
+ pub pure fn tail<T>(v: &r/[T]) -> &r/[T] { slice(v, 1, v.len()) }
227
+
228
+ /// Returns a vector containing all but the first `n` elements of a slice
229
+ pub pure fn tailn<T>(v: &r/[T], n: uint) -> &r/[T] { slice(v, n, v.len()) }
230
+
229
231
/// Returns a vector containing all but the last element of a slice
230
- pub pure fn init < T : Copy > ( v : & [ const T ] ) -> ~[ T ] {
231
- assert len( v) != 0 u;
232
- slice ( v, 0 u, len ( v) - 1 u) . 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)
233
237
}
234
238
235
239
/// Returns the last element of the slice `v`, failing if the slice is empty.
236
- pub pure fn last < T : Copy > ( v : & [ const T ] ) -> T {
237
- if len ( v ) == 0 u { fail ! ( ~"last_unsafe : empty vector") }
238
- v[ len(v ) - 1u ]
240
+ pub pure fn last<T>(v: &r/[ T]) -> &r/ T {
241
+ if v. len() == 0 { fail!(~" last : empty vector") }
242
+ &v[v. len() - 1 ]
239
243
}
240
244
241
- /**
242
- * Returns `Some(x)` where `x` is the last element of the slice `v`,
243
- * or `none` if the vector is empty.
244
- */
245
- pub pure fn last_opt<T:Copy>(v: &[const T]) -> Option<T> {
246
- if len(v) == 0u { return None; }
247
- Some(v[len(v) - 1u])
245
+ /// Returns `Some(x)` where `x` is the last element of the slice `v`, or
246
+ /// `None` if the vector is empty.
247
+ pub pure fn last_opt<T>(v: &r/[T]) -> Option<&r/T> {
248
+ if v.len() == 0 { None } else { Some(&v[v.len() - 1]) }
248
249
}
249
250
250
251
/// Return a slice that points into another slice.
@@ -1692,41 +1693,29 @@ impl<T> Container for &[const T] {
1692
1693
}
1693
1694
1694
1695
pub trait CopyableVector < T > {
1695
- pure fn head ( & self ) -> T ;
1696
- pure fn init ( & self ) -> ~[ T ] ;
1697
- pure fn last ( & self ) -> T ;
1698
1696
pure fn slice ( & self , start : uint , end : uint ) -> ~[ T ] ;
1699
- pure fn tail ( & self ) -> ~[ T ] ;
1700
1697
}
1701
1698
1702
1699
/// Extension methods for vectors
1703
- impl < T : Copy > CopyableVector < T > for & [ const T ] {
1704
- /// Returns the first element of a vector
1705
- #[ inline]
1706
- pure fn head ( & self ) -> T { head ( * self ) }
1707
-
1708
- /// Returns all but the last elemnt of a vector
1709
- #[ inline]
1710
- pure fn init ( & self ) -> ~[ T ] { init ( * self ) }
1711
-
1712
- /// Returns the last element of a `v`, failing if the vector is empty.
1713
- #[ inline]
1714
- pure fn last ( & self ) -> T { last ( * self ) }
1715
-
1700
+ impl < T : Copy > CopyableVector < T > for & [ const T ] {
1716
1701
/// Returns a copy of the elements from [`start`..`end`) from `v`.
1717
1702
#[ inline]
1718
1703
pure fn slice ( & self , start : uint , end : uint ) -> ~[ T ] {
1719
1704
slice ( * self , start, end) . to_vec ( )
1720
1705
}
1721
-
1722
- /// Returns all but the first element of a vector
1723
- #[ inline]
1724
- pure fn tail ( & self ) -> ~[ T ] { tail ( * self ) }
1725
1706
}
1726
1707
1727
1708
pub trait ImmutableVector < T > {
1728
1709
pure fn view ( & self , start : uint , end : uint ) -> & self /[ T ] ;
1729
- pure fn foldr < U : Copy > ( & self , z : U , p : fn ( t : & T , u : U ) -> U ) -> U ;
1710
+ pure fn head ( & self ) -> & self /T ;
1711
+ pure fn head_opt ( & self ) -> Option < & self /T > ;
1712
+ pure fn tail ( & self ) -> & self /[ T ] ;
1713
+ pure fn tailn ( & self , n : uint ) -> & self /[ T ] ;
1714
+ pure fn init ( & self ) -> & self /[ T ] ;
1715
+ pure fn initn ( & self , n : uint ) -> & self /[ T ] ;
1716
+ pure fn last ( & self ) -> & self /T ;
1717
+ pure fn last_opt ( & self ) -> Option < & self /T > ;
1718
+ pure fn foldr < U : Copy > ( & self , z : U , p : fn ( t : & T , u : U ) -> U ) -> U ;
1730
1719
pure fn map < U > ( & self , f : fn ( t : & T ) -> U ) -> ~[ U ] ;
1731
1720
pure fn mapi < U > ( & self , f : fn ( uint , t : & T ) -> U ) -> ~[ U ] ;
1732
1721
fn map_r < U > ( & self , f : fn ( x : & T ) -> U ) -> ~[ U ] ;
@@ -1743,6 +1732,38 @@ impl<T> ImmutableVector<T> for &[T] {
1743
1732
slice ( * self , start, end)
1744
1733
}
1745
1734
1735
+ /// Returns the first element of a vector, failing if the vector is empty.
1736
+ #[ inline]
1737
+ pure fn head ( & self ) -> & self /T { head ( * self ) }
1738
+
1739
+ /// Returns the first element of a vector
1740
+ #[ inline]
1741
+ pure fn head_opt ( & self ) -> Option < & self /T > { head_opt ( * self ) }
1742
+
1743
+ /// Returns all but the first element of a vector
1744
+ #[ inline]
1745
+ pure fn tail ( & self ) -> & self /[ T ] { tail ( * self ) }
1746
+
1747
+ /// Returns all but the first `n' elements of a vector
1748
+ #[ inline]
1749
+ pure fn tailn ( & self , n : uint ) -> & self /[ T ] { tailn ( * self , n) }
1750
+
1751
+ /// Returns all but the last elemnt of a vector
1752
+ #[ inline]
1753
+ pure fn init ( & self ) -> & self /[ T ] { init ( * self ) }
1754
+
1755
+ /// Returns all but the last `n' elemnts of a vector
1756
+ #[ inline]
1757
+ pure fn initn ( & self , n : uint ) -> & self /[ T ] { initn ( * self , n) }
1758
+
1759
+ /// Returns the last element of a `v`, failing if the vector is empty.
1760
+ #[ inline]
1761
+ pure fn last ( & self ) -> & self /T { last ( * self ) }
1762
+
1763
+ /// Returns the last element of a `v`, failing if the vector is empty.
1764
+ #[ inline]
1765
+ pure fn last_opt ( & self ) -> Option < & self /T > { last_opt ( * self ) }
1766
+
1746
1767
/// Reduce a vector from right to left
1747
1768
#[ inline]
1748
1769
pure fn foldr < U : Copy > ( & self , z : U , p : fn ( t : & T , u : U ) -> U ) -> U {
@@ -2570,27 +2591,117 @@ mod tests {
2570
2591
2571
2592
#[ test]
2572
2593
fn test_head ( ) {
2573
- let a = ~[ 11 , 12 ] ;
2574
- assert ( head ( a) == 11 ) ;
2594
+ let mut a = ~[ 11 ] ;
2595
+ assert a. head ( ) == & 11 ;
2596
+ a = ~[ 11 , 12 ] ;
2597
+ assert a. head ( ) == & 11 ;
2598
+ }
2599
+
2600
+ #[ test]
2601
+ #[ should_fail]
2602
+ #[ ignore( cfg( windows) ) ]
2603
+ fn test_head_empty ( ) {
2604
+ let a: ~[ int ] = ~[ ] ;
2605
+ a. head ( ) ;
2606
+ }
2607
+
2608
+ #[ test]
2609
+ fn test_head_opt ( ) {
2610
+ let mut a = ~[ ] ;
2611
+ assert a. head_opt ( ) == None ;
2612
+ a = ~[ 11 ] ;
2613
+ assert a. head_opt ( ) . unwrap ( ) == & 11 ;
2614
+ a = ~[ 11 , 12 ] ;
2615
+ assert a. head_opt ( ) . unwrap ( ) == & 11 ;
2575
2616
}
2576
2617
2577
2618
#[ test]
2578
2619
fn test_tail ( ) {
2579
2620
let mut a = ~[ 11 ] ;
2580
- assert ( tail ( a) == ~[ ] ) ;
2621
+ assert a. tail ( ) == & [ ] ;
2622
+ a = ~[ 11 , 12 ] ;
2623
+ assert a. tail ( ) == & [ 12 ] ;
2624
+ }
2625
+
2626
+ #[ test]
2627
+ #[ should_fail]
2628
+ #[ ignore( cfg( windows) ) ]
2629
+ fn test_tail_empty ( ) {
2630
+ let a: ~[ int ] = ~[ ] ;
2631
+ a. tail ( ) ;
2632
+ }
2581
2633
2634
+ #[ test]
2635
+ fn test_tailn ( ) {
2636
+ let mut a = ~[ 11 , 12 , 13 ] ;
2637
+ assert a. tailn ( 0 ) == & [ 11 , 12 , 13 ] ;
2638
+ a = ~[ 11 , 12 , 13 ] ;
2639
+ assert a. tailn ( 2 ) == & [ 13 ] ;
2640
+ }
2641
+
2642
+ #[ test]
2643
+ #[ should_fail]
2644
+ #[ ignore( cfg( windows) ) ]
2645
+ fn test_tailn_empty ( ) {
2646
+ let a: ~[ int ] = ~[ ] ;
2647
+ a. tailn ( 2 ) ;
2648
+ }
2649
+
2650
+ #[ test]
2651
+ fn test_init ( ) {
2652
+ let mut a = ~[ 11 ] ;
2653
+ assert a. init ( ) == & [ ] ;
2582
2654
a = ~[ 11 , 12 ] ;
2583
- assert ( tail ( a) == ~[ 12 ] ) ;
2655
+ assert a. init ( ) == & [ 11 ] ;
2656
+ }
2657
+
2658
+ #[ init]
2659
+ #[ should_fail]
2660
+ #[ ignore( cfg( windows) ) ]
2661
+ fn test_init_empty ( ) {
2662
+ let a: ~[ int ] = ~[ ] ;
2663
+ a. init ( ) ;
2664
+ }
2665
+
2666
+ #[ test]
2667
+ fn test_initn ( ) {
2668
+ let mut a = ~[ 11 , 12 , 13 ] ;
2669
+ assert a. initn ( 0 ) == & [ 11 , 12 , 13 ] ;
2670
+ a = ~[ 11 , 12 , 13 ] ;
2671
+ assert a. initn ( 2 ) == & [ 11 ] ;
2672
+ }
2673
+
2674
+ #[ init]
2675
+ #[ should_fail]
2676
+ #[ ignore( cfg( windows) ) ]
2677
+ fn test_initn_empty ( ) {
2678
+ let a: ~[ int ] = ~[ ] ;
2679
+ a. initn ( 2 ) ;
2584
2680
}
2585
2681
2586
2682
#[ test]
2587
2683
fn test_last ( ) {
2588
- let mut n = last_opt ( ~[ ] ) ;
2589
- assert ( n. is_none ( ) ) ;
2590
- n = last_opt ( ~[ 1 , 2 , 3 ] ) ;
2591
- assert ( n == Some ( 3 ) ) ;
2592
- n = last_opt ( ~[ 1 , 2 , 3 , 4 , 5 ] ) ;
2593
- assert ( n == Some ( 5 ) ) ;
2684
+ let mut a = ~[ 11 ] ;
2685
+ assert a. last ( ) == & 11 ;
2686
+ a = ~[ 11 , 12 ] ;
2687
+ assert a. last ( ) == & 12 ;
2688
+ }
2689
+
2690
+ #[ test]
2691
+ #[ should_fail]
2692
+ fn test_last_empty ( ) {
2693
+ let a: ~[ int ] = ~[ ] ;
2694
+ a. last ( ) ;
2695
+ }
2696
+
2697
+ #[ test]
2698
+ fn test_last_opt ( ) {
2699
+ let mut a = ~[ ] ;
2700
+ assert a. last_opt ( ) == None ;
2701
+ a = ~[ 11 ] ;
2702
+ assert a. last_opt ( ) . unwrap ( ) == & 11 ;
2703
+ a = ~[ 11 , 12 ] ;
2704
+ assert a. last_opt ( ) . unwrap ( ) == & 12 ;
2594
2705
}
2595
2706
2596
2707
#[ test]
@@ -3262,12 +3373,6 @@ mod tests {
3262
3373
assert ( v2[ 1 ] == 10 ) ;
3263
3374
}
3264
3375
3265
- #[ test]
3266
- fn test_init ( ) {
3267
- let v = init ( ~[ 1 , 2 , 3 ] ) ;
3268
- assert v == ~[ 1 , 2 ] ;
3269
- }
3270
-
3271
3376
#[ test]
3272
3377
fn test_split ( ) {
3273
3378
fn f ( x : & int ) -> bool { * x == 3 }
@@ -3332,13 +3437,6 @@ mod tests {
3332
3437
( ~[ ] , ~[ 1 , 2 , 3 ] ) ;
3333
3438
}
3334
3439
3335
- #[ test]
3336
- #[ should_fail]
3337
- #[ ignore( cfg( windows) ) ]
3338
- fn test_init_empty ( ) {
3339
- init :: < int > ( ~[ ] ) ;
3340
- }
3341
-
3342
3440
#[ test]
3343
3441
fn test_concat ( ) {
3344
3442
assert concat ( ~[ ~[ 1 ] , ~[ 2 , 3 ] ] ) == ~[ 1 , 2 , 3 ] ;
0 commit comments