Skip to content

Commit 1ad0cf5

Browse files
committed
auto merge of #8503 : thestinger/rust/iterator, r=alexcrichton
2 parents c4656cf + 3cec67b commit 1ad0cf5

File tree

11 files changed

+23
-237
lines changed

11 files changed

+23
-237
lines changed

doc/tutorial-container.md

+11-15
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ iterator object. For example, vector slices several iterators available:
112112
113113
* `iter()` and `rev_iter()`, for immutable references to the elements
114114
* `mut_iter()` and `mut_rev_iter()`, for mutable references to the elements
115-
* `consume_iter()` and `consume_rev_iter`, to move the elements out by-value
115+
* `move_iter()` and `move_rev_iter`, to move the elements out by-value
116116
117117
A typical mutable container will implement at least `iter()`, `mut_iter()` and
118-
`consume_iter()` along with the reverse variants if it maintains an order.
118+
`move_iter()` along with the reverse variants if it maintains an order.
119119
120120
### Freezing
121121
@@ -139,9 +139,9 @@ and `&mut`.
139139
140140
## Iterator adaptors
141141
142-
The `IteratorUtil` trait implements common algorithms as methods extending
143-
every `Iterator` implementation. For example, the `fold` method will accumulate
144-
the items yielded by an `Iterator` into a single value:
142+
The `Iterator` trait provides many common algorithms as default methods. For
143+
example, the `fold` method will accumulate the items yielded by an `Iterator`
144+
into a single value:
145145
146146
~~~
147147
let xs = [1, 9, 2, 3, 14, 12];
@@ -154,14 +154,10 @@ Some adaptors return an adaptor object implementing the `Iterator` trait itself:
154154
~~~
155155
let xs = [1, 9, 2, 3, 14, 12];
156156
let ys = [5, 2, 1, 8];
157-
let sum = xs.iter().chain_(ys.iter()).fold(0, |a, b| a + *b);
157+
let sum = xs.iter().chain(ys.iter()).fold(0, |a, b| a + *b);
158158
assert_eq!(sum, 57);
159159
~~~
160160
161-
Note that some adaptors like the `chain_` method above use a trailing
162-
underscore to work around an issue with method resolve. The underscores will be
163-
dropped when they become unnecessary.
164-
165161
## For loops
166162
167163
The `for` keyword can be used as sugar for iterating through any iterator:
@@ -212,7 +208,7 @@ Iterators offer generic conversion to containers with the `collect` adaptor:
212208
213209
~~~
214210
let xs = [0, 1, 1, 2, 3, 5, 8];
215-
let ys = xs.rev_iter().skip(1).transform(|&x| x * 2).collect::<~[int]>();
211+
let ys = xs.rev_iter().skip(1).map(|&x| x * 2).collect::<~[int]>();
216212
assert_eq!(ys, ~[10, 6, 4, 2, 2, 0]);
217213
~~~
218214
@@ -307,13 +303,13 @@ for &x in it.invert() {
307303
The `rev_iter` and `mut_rev_iter` methods on vectors just return an inverted
308304
version of the standard immutable and mutable vector iterators.
309305
310-
The `chain_`, `transform`, `filter`, `filter_map` and `peek` adaptors are
306+
The `chain`, `map`, `filter`, `filter_map` and `inspect` adaptors are
311307
`DoubleEndedIterator` implementations if the underlying iterators are.
312308
313309
~~~
314310
let xs = [1, 2, 3, 4];
315311
let ys = [5, 6, 7, 8];
316-
let mut it = xs.iter().chain_(ys.iter()).transform(|&x| x * 2);
312+
let mut it = xs.iter().chain(ys.iter()).map(|&x| x * 2);
317313

318314
printfln!("%?", it.next()); // prints `Some(2)`
319315

@@ -329,13 +325,13 @@ The `RandomAccessIterator` trait represents an iterator offering random access
329325
to the whole range. The `indexable` method retrieves the number of elements
330326
accessible with the `idx` method.
331327
332-
The `chain_` adaptor is an implementation of `RandomAccessIterator` if the
328+
The `chain` adaptor is an implementation of `RandomAccessIterator` if the
333329
underlying iterators are.
334330
335331
~~~
336332
let xs = [1, 2, 3, 4, 5];
337333
let ys = ~[7, 9, 11];
338-
let mut it = xs.iter().chain_(ys.iter());
334+
let mut it = xs.iter().chain(ys.iter());
339335
printfln!("%?", it.idx(0)); // prints `Some(&1)`
340336
printfln!("%?", it.idx(5)); // prints `Some(&7)`
341337
printfln!("%?", it.idx(7)); // prints `Some(&11)`

src/libextra/sort.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -782,11 +782,8 @@ mod test_qsort3 {
782782

783783
#[cfg(test)]
784784
mod test_qsort {
785-
786785
use sort::*;
787786

788-
use std::vec;
789-
790787
fn check_sort(v1: &mut [int], v2: &mut [int]) {
791788
let len = v1.len();
792789
fn leual(a: &int, b: &int) -> bool { *a <= *b }
@@ -835,9 +832,7 @@ mod test_qsort {
835832

836833
let immut_names = names;
837834

838-
let pairs = vec::zip_slice(expected, immut_names);
839-
for p in pairs.iter() {
840-
let (a, b) = *p;
835+
for (&a, &b) in expected.iter().zip(immut_names.iter()) {
841836
debug!("%d %d", a, b);
842837
assert_eq!(a, b);
843838
}

src/libextra/test.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,6 @@ mod tests {
11211121

11221122
use std::either;
11231123
use std::comm::{stream, SharedChan};
1124-
use std::vec;
11251124
use tempfile;
11261125
use std::os;
11271126

@@ -1309,14 +1308,8 @@ mod tests {
13091308
~"test::parse_ignored_flag",
13101309
~"test::sort_tests"];
13111310
1312-
let pairs = vec::zip(expected, filtered);
1313-
1314-
for p in pairs.iter() {
1315-
match *p {
1316-
(ref a, ref b) => {
1317-
assert!(*a == b.desc.name.to_str());
1318-
}
1319-
}
1311+
for (a, b) in expected.iter().zip(filtered.iter()) {
1312+
assert!(*a == b.desc.name.to_str());
13201313
}
13211314
}
13221315

src/libstd/iterator.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ pub trait Iterator<A> {
174174
/// assert!(it.peek().is_none());
175175
/// assert!(it.next().is_none());
176176
/// ~~~
177+
#[inline]
177178
fn peekable(self) -> Peekable<A, Self> {
178179
Peekable{iter: self, peeked: None}
179180
}
@@ -931,17 +932,15 @@ impl<'self, A, B, T: Iterator<A>> Iterator<B> for Map<'self, A, B, T> {
931932
}
932933
}
933934

934-
impl<'self, A, B, T: DoubleEndedIterator<A>> DoubleEndedIterator<B>
935-
for Map<'self, A, B, T> {
935+
impl<'self, A, B, T: DoubleEndedIterator<A>> DoubleEndedIterator<B> for Map<'self, A, B, T> {
936936
#[inline]
937937
fn next_back(&mut self) -> Option<B> {
938938
let next = self.iter.next_back();
939939
self.do_map(next)
940940
}
941941
}
942942

943-
impl<'self, A, B, T: RandomAccessIterator<A>> RandomAccessIterator<B>
944-
for Map<'self, A, B, T> {
943+
impl<'self, A, B, T: RandomAccessIterator<A>> RandomAccessIterator<B> for Map<'self, A, B, T> {
945944
#[inline]
946945
fn indexable(&self) -> uint {
947946
self.iter.indexable()

src/libstd/kinds.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,21 @@ intrinsic properties of the type. These classifications, often called
1818
They cannot be implemented by user code, but are instead implemented
1919
by the compiler automatically for the types to which they apply.
2020
21-
The 2 kinds are
22-
23-
* Send - owned types and types containing owned types. These types
24-
may be transferred across task boundaries.
25-
26-
* Freeze - types that are deeply immutable.
27-
2821
*/
2922

30-
#[allow(missing_doc)];
31-
23+
/// Types able to be transferred across task boundaries.
3224
#[lang="send"]
3325
pub trait Send {
3426
// empty.
3527
}
3628

29+
/// Types that are either immutable or have inherited mutability.
3730
#[lang="freeze"]
3831
pub trait Freeze {
3932
// empty.
4033
}
4134

35+
/// Types with a constant size known at compile-time.
4236
#[lang="sized"]
4337
pub trait Sized {
4438
// Empty.

src/libstd/prelude.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub use str::{Str, StrVector, StrSlice, OwnedStr};
6969
pub use from_str::FromStr;
7070
pub use to_bytes::IterBytes;
7171
pub use to_str::{ToStr, ToStrConsume};
72-
pub use tuple::{CopyableTuple, ImmutableTuple, ExtendedTupleOps};
72+
pub use tuple::{CopyableTuple, ImmutableTuple};
7373
pub use tuple::{CloneableTuple1, ImmutableTuple1};
7474
pub use tuple::{CloneableTuple2, CloneableTuple3, CloneableTuple4, CloneableTuple5};
7575
pub use tuple::{CloneableTuple6, CloneableTuple7, CloneableTuple8, CloneableTuple9};

src/libstd/ptr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub unsafe fn buf_len<T>(buf: **T) -> uint {
4747
}
4848

4949
impl<T> Clone for *T {
50+
#[inline]
5051
fn clone(&self) -> *T {
5152
*self
5253
}

src/libstd/tuple.rs

-52
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
#[allow(missing_doc)];
1414

1515
use clone::Clone;
16-
use vec;
17-
use vec::ImmutableVector;
18-
use iterator::Iterator;
1916

2017
pub use self::inner::*;
2118

@@ -79,55 +76,6 @@ impl<T, U> ImmutableTuple<T, U> for (T, U) {
7976
}
8077
}
8178

82-
pub trait ExtendedTupleOps<A,B> {
83-
fn zip(&self) -> ~[(A, B)];
84-
fn map<C>(&self, f: &fn(a: &A, b: &B) -> C) -> ~[C];
85-
}
86-
87-
impl<'self,
88-
A:Clone,
89-
B:Clone>
90-
ExtendedTupleOps<A,B> for
91-
(&'self [A], &'self [B]) {
92-
#[inline]
93-
fn zip(&self) -> ~[(A, B)] {
94-
match *self {
95-
(ref a, ref b) => {
96-
vec::zip_slice(*a, *b)
97-
}
98-
}
99-
}
100-
101-
#[inline]
102-
fn map<C>(&self, f: &fn(a: &A, b: &B) -> C) -> ~[C] {
103-
match *self {
104-
(ref a, ref b) => {
105-
a.iter().zip(b.iter()).map(|(aa, bb)| f(aa, bb)).collect()
106-
}
107-
}
108-
}
109-
}
110-
111-
impl<A:Clone, B:Clone> ExtendedTupleOps<A,B> for (~[A], ~[B]) {
112-
#[inline]
113-
fn zip(&self) -> ~[(A, B)] {
114-
match *self {
115-
(ref a, ref b) => {
116-
vec::zip_slice(*a, *b)
117-
}
118-
}
119-
}
120-
121-
#[inline]
122-
fn map<C>(&self, f: &fn(a: &A, b: &B) -> C) -> ~[C] {
123-
match *self {
124-
(ref a, ref b) => {
125-
a.iter().zip(b.iter()).map(|(aa, bb)| f(aa, bb)).collect()
126-
}
127-
}
128-
}
129-
}
130-
13179
// macro for implementing n-ary tuple functions and operations
13280

13381
macro_rules! tuple_impls {

src/libstd/vec.rs

+1-53
Original file line numberDiff line numberDiff line change
@@ -390,39 +390,6 @@ pub fn unzip<T,U>(v: ~[(T, U)]) -> (~[T], ~[U]) {
390390
(ts, us)
391391
}
392392

393-
/**
394-
* Convert two vectors to a vector of pairs, by reference. As zip().
395-
*/
396-
pub fn zip_slice<T:Clone,U:Clone>(v: &[T], u: &[U]) -> ~[(T, U)] {
397-
let mut zipped = ~[];
398-
let sz = v.len();
399-
let mut i = 0u;
400-
assert_eq!(sz, u.len());
401-
while i < sz {
402-
zipped.push((v[i].clone(), u[i].clone()));
403-
i += 1u;
404-
}
405-
zipped
406-
}
407-
408-
/**
409-
* Convert two vectors to a vector of pairs.
410-
*
411-
* Returns a vector of tuples, where the i-th tuple contains the
412-
* i-th elements from each of the input vectors.
413-
*/
414-
pub fn zip<T, U>(mut v: ~[T], mut u: ~[U]) -> ~[(T, U)] {
415-
let mut i = v.len();
416-
assert_eq!(i, u.len());
417-
let mut w = with_capacity(i);
418-
while i > 0 {
419-
w.push((v.pop(),u.pop()));
420-
i -= 1;
421-
}
422-
w.reverse();
423-
w
424-
}
425-
426393
/**
427394
* Iterate over all permutations of vector `v`.
428395
*
@@ -724,12 +691,6 @@ impl<T> Vector<T> for @[T] {
724691
}
725692

726693
impl<'self, T> Container for &'self [T] {
727-
/// Returns true if a vector contains no elements
728-
#[inline]
729-
fn is_empty(&self) -> bool {
730-
self.as_imm_buf(|_p, len| len == 0u)
731-
}
732-
733694
/// Returns the length of a vector
734695
#[inline]
735696
fn len(&self) -> uint {
@@ -738,12 +699,6 @@ impl<'self, T> Container for &'self [T] {
738699
}
739700

740701
impl<T> Container for ~[T] {
741-
/// Returns true if a vector contains no elements
742-
#[inline]
743-
fn is_empty(&self) -> bool {
744-
self.as_imm_buf(|_p, len| len == 0u)
745-
}
746-
747702
/// Returns the length of a vector
748703
#[inline]
749704
fn len(&self) -> uint {
@@ -2865,14 +2820,7 @@ mod tests {
28652820

28662821
#[test]
28672822
fn test_zip_unzip() {
2868-
let v1 = ~[1, 2, 3];
2869-
let v2 = ~[4, 5, 6];
2870-
2871-
let z1 = zip(v1, v2);
2872-
2873-
assert_eq!((1, 4), z1[0]);
2874-
assert_eq!((2, 5), z1[1]);
2875-
assert_eq!((3, 6), z1[2]);
2823+
let z1 = ~[(1, 4), (2, 5), (3, 6)];
28762824

28772825
let (left, right) = unzip(z1);
28782826

src/test/run-fail/zip-different-lengths.rs

-44
This file was deleted.

0 commit comments

Comments
 (0)