about summary refs log tree commit diff
path: root/src/libstd/vec.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstd/vec.rs')
-rw-r--r--src/libstd/vec.rs37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs
index 825dc4cc187..f8f4ea0be2c 100644
--- a/src/libstd/vec.rs
+++ b/src/libstd/vec.rs
@@ -1671,6 +1671,15 @@ pub trait MutableVector<'self, T> {
 
     fn swap(self, a: uint, b: uint);
 
+    /**
+     * Divides one `&mut` into two. The first will
+     * contain all indices from `0..mid` (excluding the index `mid`
+     * itself) and the second will contain all indices from
+     * `mid..len` (excluding the index `len` itself).
+     */
+    fn mut_split(self, mid: uint) -> (&'self mut [T],
+                                      &'self mut [T]);
+
     fn reverse(self);
 
     /**
@@ -1709,6 +1718,15 @@ impl<'self,T> MutableVector<'self, T> for &'self mut [T] {
     }
 
     #[inline]
+    fn mut_split(self, mid: uint) -> (&'self mut [T], &'self mut [T]) {
+        unsafe {
+            let len = self.len();
+            let self2: &'self mut [T] = cast::transmute_copy(&self);
+            (self.mut_slice(0, mid), self2.mut_slice(mid, len))
+        }
+    }
+
+    #[inline]
     fn mut_iter(self) -> VecMutIterator<'self, T> {
         unsafe {
             let p = vec::raw::to_mut_ptr(self);
@@ -3355,4 +3373,23 @@ mod tests {
         v.push(1);
         v.push(2);
     }
+
+    #[test]
+    fn test_mut_split() {
+        let mut values = [1u8,2,3,4,5];
+        {
+            let (left, right) = values.mut_split(2);
+            assert_eq!(left.slice(0, left.len()), [1, 2]);
+            for left.mut_iter().advance |p| {
+                *p += 1;
+            }
+
+            assert_eq!(right.slice(0, right.len()), [3, 4, 5]);
+            for right.mut_iter().advance |p| {
+                *p += 2;
+            }
+        }
+
+        assert_eq!(values, [2, 3, 5, 6, 7]);
+    }
 }