summary refs log tree commit diff
path: root/src/libcore/vec.rs
diff options
context:
space:
mode:
authorErick Tryzelaar <erick.tryzelaar@gmail.com>2013-03-03 08:06:31 -0800
committerErick Tryzelaar <erick.tryzelaar@gmail.com>2013-03-05 19:37:04 -0800
commitd60747a24839c50165b0e2ea35592a7cb008f69b (patch)
tree13fd07a9dc857a6e931156c1defaf5ad810bd302 /src/libcore/vec.rs
parent5ae06ae9dea2f1dac157193b702f640e2216a5a9 (diff)
core: convert vec::{init,initn} to return references
Diffstat (limited to 'src/libcore/vec.rs')
-rw-r--r--src/libcore/vec.rs70
1 files changed, 48 insertions, 22 deletions
diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs
index 98316066fd1..2884d448610 100644
--- a/src/libcore/vec.rs
+++ b/src/libcore/vec.rs
@@ -229,9 +229,11 @@ pub pure fn tail<T>(v: &r/[T]) -> &r/[T] { slice(v, 1, v.len()) }
 pub pure fn tailn<T>(v: &r/[T], n: uint) -> &r/[T] { slice(v, n, v.len()) }
 
 /// Returns a vector containing all but the last element of a slice
-pub pure fn init<T:Copy>(v: &[const T]) -> ~[T] {
-    assert len(v) != 0u;
-    slice(v, 0u, len(v) - 1u).to_vec()
+pub pure fn init<T>(v: &r/[T]) -> &r/[T] { slice(v, 0, v.len() - 1) }
+
+/// Returns a vector containing all but the last `n' elements of a slice
+pub pure fn initn<T>(v: &r/[T], n: uint) -> &r/[T] {
+    slice(v, 0, v.len() - n)
 }
 
 /// Returns the last element of the slice `v`, failing if the slice is empty.
@@ -1694,17 +1696,12 @@ impl<T> Container for &[const T] {
 }
 
 pub trait CopyableVector<T> {
-    pure fn init(&self) -> ~[T];
     pure fn last(&self) -> T;
     pure fn slice(&self, start: uint, end: uint) -> ~[T];
 }
 
 /// Extension methods for vectors
-impl<T:Copy> CopyableVector<T> for &[const T] {
-    /// Returns all but the last elemnt of a vector
-    #[inline]
-    pure fn init(&self) -> ~[T] { init(*self) }
-
+impl<T: Copy> CopyableVector<T> for &[const T] {
     /// Returns the last element of a `v`, failing if the vector is empty.
     #[inline]
     pure fn last(&self) -> T { last(*self) }
@@ -1722,6 +1719,8 @@ pub trait ImmutableVector<T> {
     pure fn head_opt(&self) -> Option<&self/T>;
     pure fn tail(&self) -> &self/[T];
     pure fn tailn(&self, n: uint) -> &self/[T];
+    pure fn init(&self) -> &self/[T];
+    pure fn initn(&self, n: uint) -> &self/[T];
     pure fn foldr<U: Copy>(&self, z: U, p: fn(t: &T, u: U) -> U) -> U;
     pure fn map<U>(&self, f: fn(t: &T) -> U) -> ~[U];
     pure fn mapi<U>(&self, f: fn(uint, t: &T) -> U) -> ~[U];
@@ -1755,6 +1754,14 @@ impl<T> ImmutableVector<T> for &[T] {
     #[inline]
     pure fn tailn(&self, n: uint) -> &self/[T] { tailn(*self, n) }
 
+    /// Returns all but the last elemnt of a vector
+    #[inline]
+    pure fn init(&self) -> &self/[T] { init(*self) }
+
+    /// Returns all but the last `n' elemnts of a vector
+    #[inline]
+    pure fn initn(&self, n: uint) -> &self/[T] { initn(*self, n) }
+
     /// Reduce a vector from right to left
     #[inline]
     pure fn foldr<U:Copy>(&self, z: U, p: fn(t: &T, u: U) -> U) -> U {
@@ -2639,6 +2646,38 @@ mod tests {
     }
 
     #[test]
+    fn test_init() {
+        let mut a = ~[11];
+        assert a.init() == &[];
+        a = ~[11, 12];
+        assert a.init() == &[11];
+    }
+
+    #[init]
+    #[should_fail]
+    #[ignore(cfg(windows))]
+    fn test_init_empty() {
+        let a: ~[int] = ~[];
+        a.init();
+    }
+
+    #[test]
+    fn test_initn() {
+        let mut a = ~[11, 12, 13];
+        assert a.initn(0) == &[11, 12, 13];
+        a = ~[11, 12, 13];
+        assert a.initn(2) == &[11];
+    }
+
+    #[init]
+    #[should_fail]
+    #[ignore(cfg(windows))]
+    fn test_initn_empty() {
+        let a: ~[int] = ~[];
+        a.initn(2);
+    }
+
+    #[test]
     fn test_last() {
         let mut n = last_opt(~[]);
         assert (n.is_none());
@@ -3318,12 +3357,6 @@ mod tests {
     }
 
     #[test]
-    fn test_init() {
-        let v = init(~[1, 2, 3]);
-        assert v == ~[1, 2];
-    }
-
-    #[test]
     fn test_split() {
         fn f(x: &int) -> bool { *x == 3 }
 
@@ -3388,13 +3421,6 @@ mod tests {
     }
 
     #[test]
-    #[should_fail]
-    #[ignore(cfg(windows))]
-    fn test_init_empty() {
-        init::<int>(~[]);
-    }
-
-    #[test]
     fn test_concat() {
         assert concat(~[~[1], ~[2,3]]) == ~[1, 2, 3];
     }