diff options
| author | bors <bors@rust-lang.org> | 2013-06-02 21:37:38 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-06-02 21:37:38 -0700 |
| commit | 9b60ecc30ea9f8b951a4114971e190891fa97a91 (patch) | |
| tree | 3aa30be1b0a9c189719f9210ac1f3f796786f05a /src/libstd/vec.rs | |
| parent | c40baf68cb3306f1beaf1d5443bb2433043b7da7 (diff) | |
| parent | dee7c5af6991b02c86e6a84b57551fd5cc71caaf (diff) | |
| download | rust-9b60ecc30ea9f8b951a4114971e190891fa97a91.tar.gz rust-9b60ecc30ea9f8b951a4114971e190891fa97a91.zip | |
auto merge of #6908 : bjz/rust/concat-connect, r=Aatch
This adds the following traits to `prelude`:
In `std::str`, impled on `&[~[T]]` and `&[&[T]]`:
~~~rust
pub trait StrVector {
pub fn concat(&self) -> ~str;
pub fn connect(&self, sep: &str) -> ~str;
}
~~~
In `std::vec`, impled on `&[~str]` and `&[&str]`:
~~~rust
pub trait VectorVector<T> {
pub fn concat(&self) -> ~[T];
pub fn connect(&self, sep: &T) -> ~[T];
}
~~~
Diffstat (limited to 'src/libstd/vec.rs')
| -rw-r--r-- | src/libstd/vec.rs | 78 |
1 files changed, 61 insertions, 17 deletions
diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 8eedb70b3a6..b748ca54cf4 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -1011,26 +1011,58 @@ pub fn retain<T>(v: &mut ~[T], f: &fn(t: &T) -> bool) { } } -/** - * Concatenate a vector of vectors. - * - * Flattens a vector of vectors of T into a single vector of T. - */ -pub fn concat<T:Copy>(v: &[~[T]]) -> ~[T] { - let mut r = ~[]; - for each(v) |inner| { r.push_all(*inner); } - r -} +/// Flattens a vector of vectors of T into a single vector of T. +pub fn concat<T:Copy>(v: &[~[T]]) -> ~[T] { v.concat() } + +/// Concatenate a vector of vectors, placing a given separator between each +pub fn connect<T:Copy>(v: &[~[T]], sep: &T) -> ~[T] { v.connect(sep) } + +/// Flattens a vector of vectors of T into a single vector of T. +pub fn concat_slices<T:Copy>(v: &[&[T]]) -> ~[T] { v.concat() } /// Concatenate a vector of vectors, placing a given separator between each -pub fn connect<T:Copy>(v: &[~[T]], sep: &T) -> ~[T] { - let mut r: ~[T] = ~[]; - let mut first = true; - for each(v) |inner| { - if first { first = false; } else { r.push(*sep); } - r.push_all(*inner); +pub fn connect_slices<T:Copy>(v: &[&[T]], sep: &T) -> ~[T] { v.connect(sep) } + +#[allow(missing_doc)] +pub trait VectorVector<T> { + pub fn concat(&self) -> ~[T]; + pub fn connect(&self, sep: &T) -> ~[T]; +} + +impl<'self, T:Copy> VectorVector<T> for &'self [~[T]] { + /// Flattens a vector of slices of T into a single vector of T. + pub fn concat(&self) -> ~[T] { + self.flat_map(|&inner| inner) + } + + /// Concatenate a vector of vectors, placing a given separator between each. + pub fn connect(&self, sep: &T) -> ~[T] { + let mut r = ~[]; + let mut first = true; + for self.each |&inner| { + if first { first = false; } else { r.push(*sep); } + r.push_all(inner); + } + r + } +} + +impl<'self, T:Copy> VectorVector<T> for &'self [&'self [T]] { + /// Flattens a vector of slices of T into a single vector of T. + pub fn concat(&self) -> ~[T] { + self.flat_map(|&inner| inner.to_owned()) + } + + /// Concatenate a vector of slices, placing a given separator between each. + pub fn connect(&self, sep: &T) -> ~[T] { + let mut r = ~[]; + let mut first = true; + for self.each |&inner| { + if first { first = false; } else { r.push(*sep); } + r.push_all(inner); + } + r } - r } /** @@ -3941,6 +3973,10 @@ mod tests { #[test] fn test_concat() { assert_eq!(concat([~[1], ~[2,3]]), ~[1, 2, 3]); + assert_eq!([~[1], ~[2,3]].concat(), ~[1, 2, 3]); + + assert_eq!(concat_slices([&[1], &[2,3]]), ~[1, 2, 3]); + assert_eq!([&[1], &[2,3]].concat(), ~[1, 2, 3]); } #[test] @@ -3948,6 +3984,14 @@ mod tests { assert_eq!(connect([], &0), ~[]); assert_eq!(connect([~[1], ~[2, 3]], &0), ~[1, 0, 2, 3]); assert_eq!(connect([~[1], ~[2], ~[3]], &0), ~[1, 0, 2, 0, 3]); + assert_eq!([~[1], ~[2, 3]].connect(&0), ~[1, 0, 2, 3]); + assert_eq!([~[1], ~[2], ~[3]].connect(&0), ~[1, 0, 2, 0, 3]); + + assert_eq!(connect_slices([], &0), ~[]); + assert_eq!(connect_slices([&[1], &[2, 3]], &0), ~[1, 0, 2, 3]); + assert_eq!(connect_slices([&[1], &[2], &[3]], &0), ~[1, 0, 2, 0, 3]); + assert_eq!([&[1], &[2, 3]].connect(&0), ~[1, 0, 2, 3]); + assert_eq!([&[1], &[2], &[3]].connect(&0), ~[1, 0, 2, 0, 3]); } #[test] |
