diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2013-06-29 13:41:09 +1000 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2013-06-30 21:17:09 +1000 |
| commit | 2eea642c3064e2de643cf155082f08dc141eafaa (patch) | |
| tree | d2ccbc799b32e460cf7fee23e9f45cfe07af55aa /src | |
| parent | faa8f8ff8b7e457f74d74533ebbc0d5a56cf5c72 (diff) | |
| download | rust-2eea642c3064e2de643cf155082f08dc141eafaa.tar.gz rust-2eea642c3064e2de643cf155082f08dc141eafaa.zip | |
Convert vec::dedup to a method.
Diffstat (limited to 'src')
| -rw-r--r-- | src/librustc/middle/check_match.rs | 2 | ||||
| -rw-r--r-- | src/libstd/prelude.rs | 2 | ||||
| -rw-r--r-- | src/libstd/vec.rs | 73 |
3 files changed, 36 insertions, 41 deletions
diff --git a/src/librustc/middle/check_match.rs b/src/librustc/middle/check_match.rs index c27b60477c0..8cb671a97f4 100644 --- a/src/librustc/middle/check_match.rs +++ b/src/librustc/middle/check_match.rs @@ -417,7 +417,7 @@ pub fn missing_ctor(cx: &MatchCheckCtxt, } } ); - vec::dedup(&mut sorted_vec_lens); + sorted_vec_lens.dedup(); let mut found_slice = false; let mut next = 0; diff --git a/src/libstd/prelude.rs b/src/libstd/prelude.rs index c0049c7505d..d560ce621ea 100644 --- a/src/libstd/prelude.rs +++ b/src/libstd/prelude.rs @@ -74,7 +74,7 @@ pub use tuple::{ImmutableTuple6, ImmutableTuple7, ImmutableTuple8, ImmutableTupl pub use tuple::{ImmutableTuple10, ImmutableTuple11, ImmutableTuple12}; pub use vec::{VectorVector, CopyableVector, ImmutableVector}; pub use vec::{ImmutableEqVector, ImmutableTotalOrdVector, ImmutableCopyableVector}; -pub use vec::{OwnedVector, OwnedCopyableVector, MutableVector}; +pub use vec::{OwnedVector, OwnedCopyableVector,OwnedEqVector, MutableVector}; pub use io::{Reader, ReaderUtil, Writer, WriterUtil}; // Reexported runtime types diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index d7480617c12..dab379962a0 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -343,41 +343,6 @@ pub fn consume_reverse<T>(mut v: ~[T], f: &fn(uint, v: T)) { } } -/** - * Remove consecutive repeated elements from a vector; if the vector is - * sorted, this removes all duplicates. - */ -pub fn dedup<T:Eq>(v: &mut ~[T]) { - unsafe { - if v.len() < 1 { return; } - let mut last_written = 0; - let mut next_to_read = 1; - do as_mut_buf(*v) |p, ln| { - // last_written < next_to_read <= ln - while next_to_read < ln { - // last_written < next_to_read < ln - if *ptr::mut_offset(p, next_to_read) == - *ptr::mut_offset(p, last_written) { - ptr::replace_ptr(ptr::mut_offset(p, next_to_read), - intrinsics::uninit()); - } else { - last_written += 1; - // last_written <= next_to_read < ln - if next_to_read != last_written { - ptr::swap_ptr(ptr::mut_offset(p, last_written), - ptr::mut_offset(p, next_to_read)); - } - } - // last_written <= next_to_read < ln - next_to_read += 1; - // last_written < next_to_read <= ln - } - } - // last_written < next_to_read == ln - raw::set_len(v, last_written + 1); - } -} - // Appending /// Iterates over the `rhs` vector, copying each element and appending it to the @@ -1734,14 +1699,44 @@ impl<T:Copy> OwnedCopyableVector<T> for ~[T] { } #[allow(missing_doc)] -trait OwnedEqVector<T:Eq> { +pub trait OwnedEqVector<T:Eq> { fn dedup(&mut self); } impl<T:Eq> OwnedEqVector<T> for ~[T] { - #[inline] - fn dedup(&mut self) { - dedup(self) + /** + * Remove consecutive repeated elements from a vector; if the vector is + * sorted, this removes all duplicates. + */ + pub fn dedup(&mut self) { + unsafe { + if self.len() == 0 { return; } + let mut last_written = 0; + let mut next_to_read = 1; + do as_mut_buf(*self) |p, ln| { + // last_written < next_to_read <= ln + while next_to_read < ln { + // last_written < next_to_read < ln + if *ptr::mut_offset(p, next_to_read) == + *ptr::mut_offset(p, last_written) { + ptr::replace_ptr(ptr::mut_offset(p, next_to_read), + intrinsics::uninit()); + } else { + last_written += 1; + // last_written <= next_to_read < ln + if next_to_read != last_written { + ptr::swap_ptr(ptr::mut_offset(p, last_written), + ptr::mut_offset(p, next_to_read)); + } + } + // last_written <= next_to_read < ln + next_to_read += 1; + // last_written < next_to_read <= ln + } + } + // last_written < next_to_read == ln + raw::set_len(self, last_written + 1); + } } } |
