From f2a01ea27734c9b20389f75d2b0c0da34f4ba5b7 Mon Sep 17 00:00:00 2001 From: Palmer Cox
Date: Sat, 30 Nov 2013 16:28:42 -0500
Subject: [PATCH 1/2] Implement mut_chunks() method for MutableVector trait.
mut_chunks() returns an iterator that produces mutable slices. This is the
mutable version of the existing chunks() method on the ImmutableVector trait.
---
src/libstd/vec.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 73 insertions(+)
diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs
index 293c9ed981728..d7b1f18831411 100644
--- a/src/libstd/vec.rs
+++ b/src/libstd/vec.rs
@@ -1933,6 +1933,18 @@ pub trait MutableVector<'self, T> {
/// Returns a reversed iterator that allows modifying each value
fn mut_rev_iter(self) -> MutRevIterator<'self, T>;
+ /**
+ * Returns an iterator over `size` elements of the vector at a time.
+ * The chunks are mutable and do not overlap. If `size` does not divide the
+ * length of the vector, then the last chunk will not have length
+ * `size`.
+ *
+ * # Failure
+ *
+ * Fails if `size` is 0.
+ */
+ fn mut_chunks(self, chunk_size: uint) -> MutChunkIter<'self, T>;
+
/**
* Returns a mutable reference to the first element in this slice
* and adjusts the slice in place so that it no longer contains
@@ -2069,6 +2081,13 @@ impl<'self,T> MutableVector<'self, T> for &'self mut [T] {
self.mut_iter().invert()
}
+ #[inline]
+ fn mut_chunks(self, chunk_size: uint) -> MutChunkIter<'self, T> {
+ assert!(chunk_size > 0);
+ let len = self.len();
+ MutChunkIter { v: self, chunk_size: chunk_size, remaining: len }
+ }
+
fn mut_shift_ref(&mut self) -> &'self mut T {
unsafe {
let s: &mut Slice
Date: Sat, 30 Nov 2013 19:54:28 -0500
Subject: [PATCH 2/2] Implement DoubleEndedIterator for MutChunkIter.
---
src/libstd/vec.rs | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs
index d7b1f18831411..45667bdad2ef6 100644
--- a/src/libstd/vec.rs
+++ b/src/libstd/vec.rs
@@ -2611,6 +2611,23 @@ impl<'self, T> Iterator<&'self mut [T]> for MutChunkIter<'self, T> {
}
}
+impl<'self, T> DoubleEndedIterator<&'self mut [T]> for MutChunkIter<'self, T> {
+ #[inline]
+ fn next_back(&mut self) -> Option<&'self mut [T]> {
+ if self.remaining == 0 {
+ None
+ } else {
+ let remainder = self.remaining % self.chunk_size;
+ let sz = if remainder != 0 { remainder } else { self.chunk_size };
+ let tmp = util::replace(&mut self.v, &mut []);
+ let (head, tail) = tmp.mut_split(self.remaining - sz);
+ self.v = head;
+ self.remaining -= sz;
+ Some(tail)
+ }
+ }
+}
+
/// An iterator that moves out of a vector.
#[deriving(Clone)]
pub struct MoveIterator