diff options
| author | Kevin Ballard <kevin@sb.org> | 2014-05-03 16:13:35 -0700 |
|---|---|---|
| committer | Kevin Ballard <kevin@sb.org> | 2014-05-08 12:06:21 -0700 |
| commit | 21dae8e1e0916afd2f789a2f1affdfe580d9ca0e (patch) | |
| tree | aeb73142fbdb6e19ce00251f9f75311d2862c423 /src/libstd | |
| parent | 4af84313d67e3062e43c0123093278b1887cac64 (diff) | |
| download | rust-21dae8e1e0916afd2f789a2f1affdfe580d9ca0e.tar.gz rust-21dae8e1e0916afd2f789a2f1affdfe580d9ca0e.zip | |
More fallout from removing FromIterator on ~[T]
A few methods in slice that used to return ~[T] now return Vec<T>: - VectorVector.concat/connect_vec() returns Vec<T> - slice::unzip() returns (Vec<T>, Vec<U>) - ImmutableCloneableVector.partitioned() returns (Vec<T>, Vec<T>) - OwnedVector.partition() returns (Vec<T>, Vec<T>)
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/num/int_macros.rs | 2 | ||||
| -rw-r--r-- | src/libstd/num/uint_macros.rs | 2 | ||||
| -rw-r--r-- | src/libstd/slice.rs | 72 | ||||
| -rw-r--r-- | src/libstd/vec.rs | 38 |
4 files changed, 57 insertions, 57 deletions
diff --git a/src/libstd/num/int_macros.rs b/src/libstd/num/int_macros.rs index 8a7bea46585..f7813c31d4d 100644 --- a/src/libstd/num/int_macros.rs +++ b/src/libstd/num/int_macros.rs @@ -83,7 +83,7 @@ impl ToStrRadix for $T { }); // We know we generated valid utf-8, so we don't need to go through that // check. - unsafe { str::raw::from_utf8_owned(buf.move_iter().collect()) } + unsafe { str::raw::from_utf8(buf.as_slice()).to_owned() } } } diff --git a/src/libstd/num/uint_macros.rs b/src/libstd/num/uint_macros.rs index 3e64c171613..814ea0e4274 100644 --- a/src/libstd/num/uint_macros.rs +++ b/src/libstd/num/uint_macros.rs @@ -84,7 +84,7 @@ impl ToStrRadix for $T { }); // We know we generated valid utf-8, so we don't need to go through that // check. - unsafe { str::raw::from_utf8_owned(buf.move_iter().collect()) } + unsafe { str::raw::from_utf8(buf.as_slice()).to_owned() } } } diff --git a/src/libstd/slice.rs b/src/libstd/slice.rs index a1cc99b4905..a93f209459a 100644 --- a/src/libstd/slice.rs +++ b/src/libstd/slice.rs @@ -127,23 +127,23 @@ pub trait VectorVector<T> { // FIXME #5898: calling these .concat and .connect conflicts with // StrVector::con{cat,nect}, since they have generic contents. /// Flattens a vector of vectors of T into a single vector of T. - fn concat_vec(&self) -> ~[T]; + fn concat_vec(&self) -> Vec<T>; /// Concatenate a vector of vectors, placing a given separator between each. - fn connect_vec(&self, sep: &T) -> ~[T]; + fn connect_vec(&self, sep: &T) -> Vec<T>; } impl<'a, T: Clone, V: Vector<T>> VectorVector<T> for &'a [V] { - fn concat_vec(&self) -> ~[T] { + fn concat_vec(&self) -> Vec<T> { let size = self.iter().fold(0u, |acc, v| acc + v.as_slice().len()); let mut result = Vec::with_capacity(size); for v in self.iter() { result.push_all(v.as_slice()) } - result.move_iter().collect() + result } - fn connect_vec(&self, sep: &T) -> ~[T] { + fn connect_vec(&self, sep: &T) -> Vec<T> { let size = self.iter().fold(0u, |acc, v| acc + v.as_slice().len()); let mut result = Vec::with_capacity(size + self.len()); let mut first = true; @@ -151,7 +151,7 @@ impl<'a, T: Clone, V: Vector<T>> VectorVector<T> for &'a [V] { if first { first = false } else { result.push(sep.clone()) } result.push_all(v.as_slice()) } - result.move_iter().collect() + result } } @@ -185,7 +185,7 @@ pub fn unzip<T, U, V: Iterator<(T, U)>>(mut iter: V) -> (~[T], ~[U]) { /// The last generated swap is always (0, 1), and it returns the /// sequence to its initial order. pub struct ElementSwaps { - sdir: ~[SizeDirection], + sdir: Vec<SizeDirection>, /// If true, emit the last swap that returns the sequence to initial state emit_reset: bool, swaps_made : uint, @@ -199,9 +199,7 @@ impl ElementSwaps { // element (equal to the original index). ElementSwaps{ emit_reset: true, - sdir: range(0, length) - .map(|i| SizeDirection{ size: i, dir: Neg }) - .collect::<~[_]>(), + sdir: range(0, length).map(|i| SizeDirection{ size: i, dir: Neg }).collect(), swaps_made: 0 } } @@ -228,12 +226,12 @@ impl Iterator<(uint, uint)> for ElementSwaps { let max = self.sdir.iter().map(|&x| x).enumerate() .filter(|&(i, sd)| new_pos(i, sd.dir) < self.sdir.len() && - self.sdir[new_pos(i, sd.dir)].size < sd.size) + self.sdir.get(new_pos(i, sd.dir)).size < sd.size) .max_by(|&(_, sd)| sd.size); match max { Some((i, sd)) => { let j = new_pos(i, sd.dir); - self.sdir.swap(i, j); + self.sdir.as_mut_slice().swap(i, j); // Swap the direction of each larger SizeDirection for x in self.sdir.mut_iter() { @@ -368,7 +366,7 @@ impl<T: Clone> CloneableVector<T> for ~[T] { pub trait ImmutableCloneableVector<T> { /// Partitions the vector into two vectors `(A,B)`, where all /// elements of `A` satisfy `f` and all elements of `B` do not. - fn partitioned(&self, f: |&T| -> bool) -> (~[T], ~[T]); + fn partitioned(&self, f: |&T| -> bool) -> (Vec<T>, Vec<T>); /// Create an iterator that yields every possible permutation of the /// vector in succession. @@ -377,7 +375,7 @@ pub trait ImmutableCloneableVector<T> { impl<'a,T:Clone> ImmutableCloneableVector<T> for &'a [T] { #[inline] - fn partitioned(&self, f: |&T| -> bool) -> (~[T], ~[T]) { + fn partitioned(&self, f: |&T| -> bool) -> (Vec<T>, Vec<T>) { let mut lefts = Vec::new(); let mut rights = Vec::new(); @@ -389,7 +387,7 @@ impl<'a,T:Clone> ImmutableCloneableVector<T> for &'a [T] { } } - (lefts.move_iter().collect(), rights.move_iter().collect()) + (lefts, rights) } fn permutations(self) -> Permutations<T> { @@ -426,7 +424,7 @@ pub trait OwnedVector<T> { * Partitions the vector into two vectors `(A,B)`, where all * elements of `A` satisfy `f` and all elements of `B` do not. */ - fn partition(self, f: |&T| -> bool) -> (~[T], ~[T]); + fn partition(self, f: |&T| -> bool) -> (Vec<T>, Vec<T>); } impl<T> OwnedVector<T> for ~[T] { @@ -446,7 +444,7 @@ impl<T> OwnedVector<T> for ~[T] { } #[inline] - fn partition(self, f: |&T| -> bool) -> (~[T], ~[T]) { + fn partition(self, f: |&T| -> bool) -> (Vec<T>, Vec<T>) { let mut lefts = Vec::new(); let mut rights = Vec::new(); @@ -458,7 +456,7 @@ impl<T> OwnedVector<T> for ~[T] { } } - (lefts.move_iter().collect(), rights.move_iter().collect()) + (lefts, rights) } } @@ -1250,6 +1248,7 @@ mod tests { let (left, right) = unzip(z1.iter().map(|&x| x)); + let (left, right) = (left.as_slice(), right.as_slice()); assert_eq!((1, 4), (left[0], right[0])); assert_eq!((2, 5), (left[1], right[1])); assert_eq!((3, 6), (left[2], right[2])); @@ -1456,43 +1455,6 @@ mod tests { } #[test] - fn test_partition() { - assert_eq!((box []).partition(|x: &int| *x < 3), (box [], box [])); - assert_eq!((box [1, 2, 3]).partition(|x: &int| *x < 4), (box [1, 2, 3], box [])); - assert_eq!((box [1, 2, 3]).partition(|x: &int| *x < 2), (box [1], box [2, 3])); - assert_eq!((box [1, 2, 3]).partition(|x: &int| *x < 0), (box [], box [1, 2, 3])); - } - - #[test] - fn test_partitioned() { - assert_eq!(([]).partitioned(|x: &int| *x < 3), (box [], box [])) - assert_eq!(([1, 2, 3]).partitioned(|x: &int| *x < 4), (box [1, 2, 3], box [])); - assert_eq!(([1, 2, 3]).partitioned(|x: &int| *x < 2), (box [1], box [2, 3])); - assert_eq!(([1, 2, 3]).partitioned(|x: &int| *x < 0), (box [], box [1, 2, 3])); - } - - #[test] - fn test_concat() { - let v: [~[int], ..0] = []; - assert_eq!(v.concat_vec(), box []); - assert_eq!([box [1], box [2,3]].concat_vec(), box [1, 2, 3]); - - assert_eq!([&[1], &[2,3]].concat_vec(), box [1, 2, 3]); - } - - #[test] - fn test_connect() { - let v: [~[int], ..0] = []; - assert_eq!(v.connect_vec(&0), box []); - assert_eq!([box [1], box [2, 3]].connect_vec(&0), box [1, 0, 2, 3]); - assert_eq!([box [1], box [2], box [3]].connect_vec(&0), box [1, 0, 2, 0, 3]); - - assert_eq!(v.connect_vec(&0), box []); - assert_eq!([&[1], &[2, 3]].connect_vec(&0), box [1, 0, 2, 3]); - assert_eq!([&[1], &[2], &[3]].connect_vec(&0), box [1, 0, 2, 0, 3]); - } - - #[test] fn test_shift() { let mut x = vec![1, 2, 3]; assert_eq!(x.shift(), Some(1)); diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index af146b96e50..9d83a5848fb 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -1649,4 +1649,42 @@ mod tests { unsafe { v.set_len(0); } assert_eq!(v.mut_iter().len(), 0); } + + #[test] + fn test_partition() { + assert_eq!(vec![].partition(|x: &int| *x < 3), (vec![], vec![])); + assert_eq!(vec![1, 2, 3].partition(|x: &int| *x < 4), (vec![1, 2, 3], vec![])); + assert_eq!(vec![1, 2, 3].partition(|x: &int| *x < 2), (vec![1], vec![2, 3])); + assert_eq!(vec![1, 2, 3].partition(|x: &int| *x < 0), (vec![], vec![1, 2, 3])); + } + + #[test] + fn test_partitioned() { + assert_eq!(([]).partitioned(|x: &int| *x < 3), (vec![], vec![])) + assert_eq!(([1, 2, 3]).partitioned(|x: &int| *x < 4), (vec![1, 2, 3], vec![])); + assert_eq!(([1, 2, 3]).partitioned(|x: &int| *x < 2), (vec![1], vec![2, 3])); + assert_eq!(([1, 2, 3]).partitioned(|x: &int| *x < 0), (vec![], vec![1, 2, 3])); + } + + #[test] + fn test_concat() { + let v: [Vec<int>, ..0] = []; + assert_eq!(v.concat_vec(), vec![]); + assert_eq!([vec![1], vec![2,3]].concat_vec(), vec![1, 2, 3]); + + assert_eq!([&[1], &[2,3]].concat_vec(), vec![1, 2, 3]); + } + + #[test] + fn test_connect() { + let v: [~[int], ..0] = []; + assert_eq!(v.connect_vec(&0), vec![]); + assert_eq!([vec![1], vec![2, 3]].connect_vec(&0), vec![1, 0, 2, 3]); + assert_eq!([vec![1], vec![2], vec![3]].connect_vec(&0), vec![1, 0, 2, 0, 3]); + + let v: [&[int], ..0] = []; + assert_eq!(v.connect_vec(&0), vec![]); + assert_eq!([&[1], &[2, 3]].connect_vec(&0), vec![1, 0, 2, 3]); + assert_eq!([&[1], &[2], &[3]].connect_vec(&0), vec![1, 0, 2, 0, 3]); + } } |
