diff --git a/src/libextra/dlist.rs b/src/libextra/dlist.rs index 77060c4b11e..3cd2d91459f 100644 --- a/src/libextra/dlist.rs +++ b/src/libextra/dlist.rs @@ -356,7 +356,7 @@ impl DList { /// Provide a reverse iterator #[inline] - pub fn rev_iter<'a>(&'a self) -> InvertIterator<&'a T, DListIterator<'a, T>> { + pub fn rev_iter<'a>(&'a self) -> InvertIterator> { self.iter().invert() } @@ -376,8 +376,7 @@ impl DList { } /// Provide a reverse iterator with mutable references #[inline] - pub fn mut_rev_iter<'a>(&'a mut self) -> InvertIterator<&'a mut T, - MutDListIterator<'a, T>> { + pub fn mut_rev_iter<'a>(&'a mut self) -> InvertIterator> { self.mut_iter().invert() } @@ -390,7 +389,7 @@ impl DList { /// Consume the list into an iterator yielding elements by value, in reverse #[inline] - pub fn consume_rev_iter(self) -> InvertIterator> { + pub fn consume_rev_iter(self) -> InvertIterator> { self.consume_iter().invert() } } diff --git a/src/libextra/ringbuf.rs b/src/libextra/ringbuf.rs index aa670ec5ff8..334bf8f351f 100644 --- a/src/libextra/ringbuf.rs +++ b/src/libextra/ringbuf.rs @@ -181,7 +181,7 @@ impl RingBuf { } /// Back-to-front iterator. - pub fn rev_iter<'a>(&'a self) -> InvertIterator<&'a T, RingBufIterator<'a, T>> { + pub fn rev_iter<'a>(&'a self) -> InvertIterator> { self.iter().invert() } @@ -192,7 +192,7 @@ impl RingBuf { } /// Back-to-front iterator which returns mutable values. - pub fn mut_rev_iter<'a>(&'a mut self) -> InvertIterator<&'a mut T, RingBufMutIterator<'a, T>> { + pub fn mut_rev_iter<'a>(&'a mut self) -> InvertIterator> { self.mut_iter().invert() } } diff --git a/src/libextra/smallintmap.rs b/src/libextra/smallintmap.rs index bf6f80534b4..3c8d289b8b4 100644 --- a/src/libextra/smallintmap.rs +++ b/src/libextra/smallintmap.rs @@ -204,7 +204,7 @@ impl SmallIntMap { /// Empties the hash map, moving all values into the specified closure pub fn consume(&mut self) -> FilterMapIterator<(uint, Option), (uint, V), - EnumerateIterator, VecConsumeIterator>>> + EnumerateIterator>>> { let values = replace(&mut self.v, ~[]); values.consume_iter().enumerate().filter_map(|(i, v)| { @@ -290,8 +290,7 @@ pub struct SmallIntMapIterator<'self, T> { iterator!(impl SmallIntMapIterator -> (uint, &'self T), get_ref) double_ended_iterator!(impl SmallIntMapIterator -> (uint, &'self T), get_ref) -pub type SmallIntMapRevIterator<'self, T> = InvertIterator<(uint, &'self T), - SmallIntMapIterator<'self, T>>; +pub type SmallIntMapRevIterator<'self, T> = InvertIterator>; pub struct SmallIntMapMutIterator<'self, T> { priv front: uint, @@ -301,8 +300,7 @@ pub struct SmallIntMapMutIterator<'self, T> { iterator!(impl SmallIntMapMutIterator -> (uint, &'self mut T), get_mut_ref) double_ended_iterator!(impl SmallIntMapMutIterator -> (uint, &'self mut T), get_mut_ref) -pub type SmallIntMapMutRevIterator<'self, T> = InvertIterator<(uint, &'self mut T), - SmallIntMapMutIterator<'self, T>>; +pub type SmallIntMapMutRevIterator<'self, T> = InvertIterator>; #[cfg(test)] mod test_map { diff --git a/src/libstd/hashmap.rs b/src/libstd/hashmap.rs index 6bef110bfe5..36b666ad5de 100644 --- a/src/libstd/hashmap.rs +++ b/src/libstd/hashmap.rs @@ -751,7 +751,7 @@ impl HashSet { /// Visit the values representing the symmetric difference pub fn symmetric_difference_iter<'a>(&'a self, other: &'a HashSet) - -> ChainIterator<&'a T, SetAlgebraIter<'a, T>, SetAlgebraIter<'a, T>> { + -> ChainIterator, SetAlgebraIter<'a, T>> { self.difference_iter(other).chain_(other.difference_iter(self)) } @@ -764,7 +764,7 @@ impl HashSet { /// Visit the values representing the union pub fn union_iter<'a>(&'a self, other: &'a HashSet) - -> ChainIterator<&'a T, HashSetIterator<'a, T>, SetAlgebraIter<'a, T>> { + -> ChainIterator, SetAlgebraIter<'a, T>> { self.iter().chain_(other.difference_iter(self)) } diff --git a/src/libstd/iterator.rs b/src/libstd/iterator.rs index 36645a555bb..2ec8ea41bfb 100644 --- a/src/libstd/iterator.rs +++ b/src/libstd/iterator.rs @@ -29,7 +29,13 @@ use uint; /// Conversion from an `Iterator` pub trait FromIterator> { /// Build a container with elements from an external iterator. - pub fn from_iterator(iterator: &mut T) -> Self; + fn from_iterator(iterator: &mut T) -> Self; +} + +/// A type growable from an `Iterator` implementation +pub trait Extendable>: FromIterator { + /// Extend a container with the elements yielded by an iterator + fn extend(&mut self, iterator: &mut T); } /// An interface for dealing with "external iterators". These types of iterators @@ -52,7 +58,9 @@ pub trait DoubleEndedIterator: Iterator { } /// An object implementing random access indexing by `uint` -pub trait RandomAccessIterator { +/// +/// A `RandomAccessIterator` should be either infinite or a `DoubleEndedIterator`. +pub trait RandomAccessIterator: Iterator { /// Return the number of indexable elements. At most `std::uint::max_value` /// elements are indexable, even if the iterator represents a longer range. fn indexable(&self) -> uint; @@ -64,37 +72,36 @@ pub trait RandomAccessIterator { /// Iterator adaptors provided for every `DoubleEndedIterator` implementation. /// /// In the future these will be default methods instead of a utility trait. -pub trait DoubleEndedIteratorUtil { +pub trait DoubleEndedIteratorUtil { /// Flip the direction of the iterator - fn invert(self) -> InvertIterator; + fn invert(self) -> InvertIterator; } /// Iterator adaptors provided for every `DoubleEndedIterator` implementation. /// /// In the future these will be default methods instead of a utility trait. -impl> DoubleEndedIteratorUtil for T { +impl> DoubleEndedIteratorUtil for T { /// Flip the direction of the iterator #[inline] - fn invert(self) -> InvertIterator { + fn invert(self) -> InvertIterator { InvertIterator{iter: self} } } /// An double-ended iterator with the direction inverted -// FIXME #6967: Dummy A parameter to get around type inference bug #[deriving(Clone)] -pub struct InvertIterator { +pub struct InvertIterator { priv iter: T } -impl> Iterator for InvertIterator { +impl> Iterator for InvertIterator { #[inline] fn next(&mut self) -> Option { self.iter.next_back() } #[inline] fn size_hint(&self) -> (uint, Option) { self.iter.size_hint() } } -impl> DoubleEndedIterator for InvertIterator { +impl> DoubleEndedIterator for InvertIterator { #[inline] fn next_back(&mut self) -> Option { self.iter.next() } } @@ -118,7 +125,7 @@ pub trait IteratorUtil { /// assert_eq!(it.next().get(), &1); /// assert!(it.next().is_none()); /// ~~~ - fn chain_>(self, other: U) -> ChainIterator; + fn chain_>(self, other: U) -> ChainIterator; /// Creates an iterator which iterates over both this and the specified /// iterators simultaneously, yielding the two elements as pairs. When @@ -134,7 +141,7 @@ pub trait IteratorUtil { /// assert_eq!(it.next().get(), (&0, &1)); /// assert!(it.next().is_none()); /// ~~~ - fn zip>(self, other: U) -> ZipIterator; + fn zip>(self, other: U) -> ZipIterator; // FIXME: #5898: should be called map /// Creates a new iterator which will apply the specified function to each @@ -191,7 +198,7 @@ pub trait IteratorUtil { /// assert_eq!(it.next().get(), (1, &200)); /// assert!(it.next().is_none()); /// ~~~ - fn enumerate(self) -> EnumerateIterator; + fn enumerate(self) -> EnumerateIterator; /// Creates an iterator which invokes the predicate on elements until it /// returns false. Once the predicate returns false, all further elements are @@ -236,7 +243,7 @@ pub trait IteratorUtil { /// assert_eq!(it.next().get(), &5); /// assert!(it.next().is_none()); /// ~~~ - fn skip(self, n: uint) -> SkipIterator; + fn skip(self, n: uint) -> SkipIterator; // FIXME: #5898: should be called take /// Creates an iterator which yields the first `n` elements of this @@ -252,7 +259,7 @@ pub trait IteratorUtil { /// assert_eq!(it.next().get(), &3); /// assert!(it.next().is_none()); /// ~~~ - fn take_(self, n: uint) -> TakeIterator; + fn take_(self, n: uint) -> TakeIterator; /// Creates a new iterator which behaves in a similar fashion to foldl. /// There is a state which is passed between each iteration and can be @@ -295,7 +302,7 @@ pub trait IteratorUtil { /// ~~~ // FIXME: #5898: should be called `flat_map` fn flat_map_<'r, B, U: Iterator>(self, f: &'r fn(A) -> U) - -> FlatMapIterator<'r, A, B, Self, U>; + -> FlatMapIterator<'r, A, Self, U>; /// Creates an iterator that calls a function with a reference to each /// element before yielding it. This is often useful for debugging an @@ -462,12 +469,12 @@ pub trait IteratorUtil { /// In the future these will be default methods instead of a utility trait. impl> IteratorUtil for T { #[inline] - fn chain_>(self, other: U) -> ChainIterator { + fn chain_>(self, other: U) -> ChainIterator { ChainIterator{a: self, b: other, flag: false} } #[inline] - fn zip>(self, other: U) -> ZipIterator { + fn zip>(self, other: U) -> ZipIterator { ZipIterator{a: self, b: other} } @@ -488,7 +495,7 @@ impl> IteratorUtil for T { } #[inline] - fn enumerate(self) -> EnumerateIterator { + fn enumerate(self) -> EnumerateIterator { EnumerateIterator{iter: self, count: 0} } @@ -503,13 +510,13 @@ impl> IteratorUtil for T { } #[inline] - fn skip(self, n: uint) -> SkipIterator { + fn skip(self, n: uint) -> SkipIterator { SkipIterator{iter: self, n: n} } // FIXME: #5898: should be called take #[inline] - fn take_(self, n: uint) -> TakeIterator { + fn take_(self, n: uint) -> TakeIterator { TakeIterator{iter: self, n: n} } @@ -521,7 +528,7 @@ impl> IteratorUtil for T { #[inline] fn flat_map_<'r, B, U: Iterator>(self, f: &'r fn(A) -> U) - -> FlatMapIterator<'r, A, B, T, U> { + -> FlatMapIterator<'r, A, T, U> { FlatMapIterator{iter: self, f: f, subiter: None } } @@ -756,8 +763,7 @@ impl> OrdIterator for T { } /// A trait for iterators that are clonable. -// FIXME #6967: Dummy A parameter to get around type inference bug -pub trait ClonableIterator { +pub trait ClonableIterator { /// Repeats an iterator endlessly /// /// # Example @@ -768,24 +774,24 @@ pub trait ClonableIterator { /// assert_eq!(cy.next(), Some(1)); /// assert_eq!(cy.next(), Some(1)); /// ~~~ - fn cycle(self) -> CycleIterator; + fn cycle(self) -> CycleIterator; } -impl> ClonableIterator for T { +impl> ClonableIterator for T { #[inline] - fn cycle(self) -> CycleIterator { + fn cycle(self) -> CycleIterator { CycleIterator{orig: self.clone(), iter: self} } } /// An iterator that repeats endlessly #[deriving(Clone)] -pub struct CycleIterator { +pub struct CycleIterator { priv orig: T, priv iter: T, } -impl> Iterator for CycleIterator { +impl> Iterator for CycleIterator { #[inline] fn next(&mut self) -> Option { match self.iter.next() { @@ -806,15 +812,14 @@ impl> Iterator for CycleIterator { } /// An iterator which strings two iterators together -// FIXME #6967: Dummy A parameter to get around type inference bug #[deriving(Clone)] -pub struct ChainIterator { +pub struct ChainIterator { priv a: T, priv b: U, priv flag: bool } -impl, U: Iterator> Iterator for ChainIterator { +impl, U: Iterator> Iterator for ChainIterator { #[inline] fn next(&mut self) -> Option { if self.flag { @@ -851,7 +856,7 @@ impl, U: Iterator> Iterator for ChainIterator { } impl, U: DoubleEndedIterator> DoubleEndedIterator -for ChainIterator { +for ChainIterator { #[inline] fn next_back(&mut self) -> Option { match self.b.next_back() { @@ -862,7 +867,7 @@ for ChainIterator { } impl, U: RandomAccessIterator> RandomAccessIterator -for ChainIterator { +for ChainIterator { #[inline] fn indexable(&self) -> uint { let (a, b) = (self.a.indexable(), self.b.indexable()); @@ -886,14 +891,13 @@ for ChainIterator { } /// An iterator which iterates two other iterators simultaneously -// FIXME #6967: Dummy A & B parameters to get around type inference bug #[deriving(Clone)] -pub struct ZipIterator { +pub struct ZipIterator { priv a: T, priv b: U } -impl, U: Iterator> Iterator<(A, B)> for ZipIterator { +impl, U: Iterator> Iterator<(A, B)> for ZipIterator { #[inline] fn next(&mut self) -> Option<(A, B)> { match (self.a.next(), self.b.next()) { @@ -1040,14 +1044,13 @@ for FilterMapIterator<'self, A, B, T> { } /// An iterator which yields the current count and the element during iteration -// FIXME #6967: Dummy A parameter to get around type inference bug #[deriving(Clone)] -pub struct EnumerateIterator { +pub struct EnumerateIterator { priv iter: T, priv count: uint } -impl> Iterator<(uint, A)> for EnumerateIterator { +impl> Iterator<(uint, A)> for EnumerateIterator { #[inline] fn next(&mut self) -> Option<(uint, A)> { match self.iter.next() { @@ -1139,14 +1142,13 @@ impl<'self, A, T: Iterator> Iterator for TakeWhileIterator<'self, A, T> { } /// An iterator which skips over `n` elements of `iter`. -// FIXME #6967: Dummy A parameter to get around type inference bug #[deriving(Clone)] -pub struct SkipIterator { +pub struct SkipIterator { priv iter: T, priv n: uint } -impl> Iterator for SkipIterator { +impl> Iterator for SkipIterator { #[inline] fn next(&mut self) -> Option { let mut next = self.iter.next(); @@ -1188,14 +1190,13 @@ impl> Iterator for SkipIterator { } /// An iterator which only iterates over the first `n` iterations of `iter`. -// FIXME #6967: Dummy A parameter to get around type inference bug #[deriving(Clone)] -pub struct TakeIterator { +pub struct TakeIterator { priv iter: T, priv n: uint } -impl> Iterator for TakeIterator { +impl> Iterator for TakeIterator { #[inline] fn next(&mut self) -> Option { let next = self.iter.next(); @@ -1247,15 +1248,14 @@ impl<'self, A, B, T: Iterator, St> Iterator for ScanIterator<'self, A, B, /// An iterator that maps each element to an iterator, /// and yields the elements of the produced iterators /// -// FIXME #6967: Dummy B parameter to get around type inference bug -pub struct FlatMapIterator<'self, A, B, T, U> { +pub struct FlatMapIterator<'self, A, T, U> { priv iter: T, priv f: &'self fn(A) -> U, priv subiter: Option, } impl<'self, A, T: Iterator, B, U: Iterator> Iterator for - FlatMapIterator<'self, A, B, T, U> { + FlatMapIterator<'self, A, T, U> { #[inline] fn next(&mut self) -> Option { loop { diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 87ac4037e8e..379deff233c 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -49,12 +49,11 @@ pub fn same_length(xs: &[T], ys: &[U]) -> bool { pub fn from_fn(n_elts: uint, op: &fn(uint) -> T) -> ~[T] { unsafe { let mut v = with_capacity(n_elts); - do v.as_mut_buf |p, _len| { - let mut i: uint = 0u; - while i < n_elts { - intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i)), op(i)); - i += 1u; - } + let p = raw::to_mut_ptr(v); + let mut i: uint = 0u; + while i < n_elts { + intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i)), op(i)); + i += 1u; } raw::set_len(&mut v, n_elts); v @@ -74,12 +73,11 @@ pub fn from_elem(n_elts: uint, t: T) -> ~[T] { // vec::with_capacity/ptr::set_memory for primitive types. unsafe { let mut v = with_capacity(n_elts); - do v.as_mut_buf |p, _len| { - let mut i = 0u; - while i < n_elts { - intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i)), t.clone()); - i += 1u; - } + let p = raw::to_mut_ptr(v); + let mut i = 0u; + while i < n_elts { + intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i)), t.clone()); + i += 1u; } raw::set_len(&mut v, n_elts); v @@ -1656,6 +1654,8 @@ impl OwnedEqVector for ~[T] { #[allow(missing_doc)] pub trait MutableVector<'self, T> { fn mut_slice(self, start: uint, end: uint) -> &'self mut [T]; + fn mut_slice_from(self, start: uint) -> &'self mut [T]; + fn mut_slice_to(self, end: uint) -> &'self mut [T]; fn mut_iter(self) -> VecMutIterator<'self, T>; fn mut_rev_iter(self) -> VecMutRevIterator<'self, T>; @@ -1709,6 +1709,27 @@ impl<'self,T> MutableVector<'self, T> for &'self mut [T] { } } + /** + * Returns a slice of self from `start` to the end of the vec. + * + * Fails when `start` points outside the bounds of self. + */ + #[inline] + fn mut_slice_from(self, start: uint) -> &'self mut [T] { + let len = self.len(); + self.mut_slice(start, len) + } + + /** + * Returns a slice of self from the start of the vec to `end`. + * + * Fails when `end` points outside the bounds of self. + */ + #[inline] + fn mut_slice_to(self, end: uint) -> &'self mut [T] { + self.mut_slice(0, end) + } + #[inline] fn mut_split(self, mid: uint) -> (&'self mut [T], &'self mut [T]) { unsafe { @@ -2145,7 +2166,7 @@ pub struct VecIterator<'self, T> { iterator!{impl VecIterator -> &'self T} double_ended_iterator!{impl VecIterator -> &'self T} random_access_iterator!{impl VecIterator -> &'self T} -pub type VecRevIterator<'self, T> = InvertIterator<&'self T, VecIterator<'self, T>>; +pub type VecRevIterator<'self, T> = InvertIterator>; impl<'self, T> Clone for VecIterator<'self, T> { fn clone(&self) -> VecIterator<'self, T> { *self } @@ -2161,7 +2182,7 @@ pub struct VecMutIterator<'self, T> { iterator!{impl VecMutIterator -> &'self mut T} double_ended_iterator!{impl VecMutIterator -> &'self mut T} random_access_iterator!{impl VecMutIterator -> &'self mut T} -pub type VecMutRevIterator<'self, T> = InvertIterator<&'self mut T, VecMutIterator<'self, T>>; +pub type VecMutRevIterator<'self, T> = InvertIterator>; /// An iterator that moves out of a vector. #[deriving(Clone)] @@ -2203,7 +2224,7 @@ impl Iterator for VecConsumeRevIterator { } impl> FromIterator for ~[A] { - pub fn from_iterator(iterator: &mut T) -> ~[A] { + fn from_iterator(iterator: &mut T) -> ~[A] { let (lower, _) = iterator.size_hint(); let mut xs = with_capacity(lower); for iterator.advance |x| { @@ -2213,6 +2234,17 @@ impl> FromIterator for ~[A] { } } +impl> Extendable for ~[A] { + fn extend(&mut self, iterator: &mut T) { + let (lower, _) = iterator.size_hint(); + let len = self.len(); + self.reserve(len + lower); + for iterator.advance |x| { + self.push(x); + } + } +} + #[cfg(test)] mod tests { use option::{None, Option, Some};