diff options
| author | Alex Ozdemir <aozdemir@hmc.edu> | 2015-08-07 00:10:31 -0700 |
|---|---|---|
| committer | Alex Ozdemir <aozdemir@fb.com> | 2015-08-12 08:34:51 -0700 |
| commit | e09f83ea4491ae7c1e48d667b9c552641de0ce5b (patch) | |
| tree | 9283a6ccfae1921b70e1219e3f038ff25cfe40cc /src/libcore | |
| parent | 6a3545ef055a6c3b46593c2b17512486dc3fa0ee (diff) | |
| download | rust-e09f83ea4491ae7c1e48d667b9c552641de0ce5b.tar.gz rust-e09f83ea4491ae7c1e48d667b9c552641de0ce5b.zip | |
O(1) count,nth,last for slice::Windows,Chunks(Mut)
Implemented count, nth, and last in constant time for Windows, Chunks, and ChunksMut created from a slice. Included checks for overflow in the implementation of nth(). Also added a test for each implemented method to libcoretest. Addresses #24214
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/slice.rs | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index f765cdc54d8..6513c49a06e 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -51,6 +51,7 @@ use ptr; use mem; use mem::size_of; use marker::{Send, Sync, self}; +use num::wrapping::OverflowingOps; use raw::Repr; // Avoid conflicts with *both* the Slice trait (buggy) and the `slice::raw` module. use raw::Slice as RawSlice; @@ -1183,6 +1184,34 @@ impl<'a, T> Iterator for Windows<'a, T> { (size, Some(size)) } } + + #[inline] + fn count(self) -> usize { + self.size_hint().0 + } + + #[inline] + fn nth(&mut self, n: usize) -> Option<Self::Item> { + let (end, overflow) = self.size.overflowing_add(n); + if end > self.v.len() || overflow { + self.v = &[]; + None + } else { + let nth = &self.v[n..end]; + self.v = &self.v[n+1..]; + Some(nth) + } + } + + #[inline] + fn last(self) -> Option<Self::Item> { + if self.size > self.v.len() { + None + } else { + let start = self.v.len() - self.size; + Some(&self.v[start..]) + } + } } #[stable(feature = "rust1", since = "1.0.0")] @@ -1269,6 +1298,38 @@ impl<'a, T> Iterator for Chunks<'a, T> { (n, Some(n)) } } + + #[inline] + fn count(self) -> usize { + self.size_hint().0 + } + + #[inline] + fn nth(&mut self, n: usize) -> Option<Self::Item> { + let (start, overflow) = n.overflowing_mul(self.size); + if start >= self.v.len() || overflow { + self.v = &[]; + None + } else { + let end = match start.checked_add(self.size) { + Some(sum) => cmp::min(self.v.len(), sum), + None => self.v.len(), + }; + let nth = &self.v[start..end]; + self.v = &self.v[end..]; + Some(nth) + } + } + + #[inline] + fn last(self) -> Option<Self::Item> { + if self.v.is_empty() { + None + } else { + let start = (self.v.len() - 1) / self.size * self.size; + Some(&self.v[start..]) + } + } } #[stable(feature = "rust1", since = "1.0.0")] @@ -1349,6 +1410,40 @@ impl<'a, T> Iterator for ChunksMut<'a, T> { (n, Some(n)) } } + + #[inline] + fn count(self) -> usize { + self.size_hint().0 + } + + #[inline] + fn nth(&mut self, n: usize) -> Option<&'a mut [T]> { + let (start, overflow) = n.overflowing_mul(self.chunk_size); + if start >= self.v.len() || overflow { + self.v = &mut []; + None + } else { + let end = match start.checked_add(self.chunk_size) { + Some(sum) => cmp::min(self.v.len(), sum), + None => self.v.len(), + }; + let tmp = mem::replace(&mut self.v, &mut []); + let (head, tail) = tmp.split_at_mut(end); + let (_, nth) = head.split_at_mut(start); + self.v = tail; + Some(nth) + } + } + + #[inline] + fn last(self) -> Option<Self::Item> { + if self.v.is_empty() { + None + } else { + let start = (self.v.len() - 1) / self.chunk_size * self.chunk_size; + Some(&mut self.v[start..]) + } + } } #[stable(feature = "rust1", since = "1.0.0")] |
