Skip to content

Commit

Permalink
auto merge of #8430 : erickt/rust/cleanup-iterators, r=erickt
Browse files Browse the repository at this point in the history
This PR does a bunch of cleaning up of various APIs. The major one is that it merges `Iterator` and `IteratorUtil`, and renames functions like `transform` into `map`. I also merged `DoubleEndedIterator` and `DoubleEndedIteratorUtil`, as well as I renamed various .consume* functions to .move_iter(). This helps to implement part of #7887.
  • Loading branch information
bors committed Aug 10, 2013
2 parents 2ba36ec + c8a93ef commit 8b9e1ce
Show file tree
Hide file tree
Showing 109 changed files with 599 additions and 700 deletions.
2 changes: 1 addition & 1 deletion src/compiletest/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ fn parse_check_line(line: &str) -> Option<~str> {
fn parse_exec_env(line: &str) -> Option<(~str, ~str)> {
do parse_name_value_directive(line, ~"exec-env").map |nv| {
// nv is either FOO or FOO=BAR
let mut strs: ~[~str] = nv.splitn_iter('=', 1).transform(|s| s.to_owned()).collect();
let mut strs: ~[~str] = nv.splitn_iter('=', 1).map(|s| s.to_owned()).collect();

match strs.len() {
1u => (strs.pop(), ~""),
Expand Down
8 changes: 4 additions & 4 deletions src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,13 @@ fn check_expected_errors(expected_errors: ~[errors::ExpectedError],
fatal(~"process did not return an error status");
}

let prefixes = expected_errors.iter().transform(|ee| {
let prefixes = expected_errors.iter().map(|ee| {
fmt!("%s:%u:", testfile.to_str(), ee.line)
}).collect::<~[~str]>();

fn to_lower( s : &str ) -> ~str {
let i = s.iter();
let c : ~[char] = i.transform( |c| {
let c : ~[char] = i.map( |c| {
if c.is_ascii() {
c.to_ascii().to_lower().to_char()
} else {
Expand Down Expand Up @@ -760,7 +760,7 @@ fn _arm_exec_compiled_test(config: &config, props: &TestProps,
let cmdline = make_cmdline("", args.prog, args.args);

// get bare program string
let mut tvec: ~[~str] = args.prog.split_iter('/').transform(|ts| ts.to_owned()).collect();
let mut tvec: ~[~str] = args.prog.split_iter('/').map(|ts| ts.to_owned()).collect();
let prog_short = tvec.pop();

// copy to target
Expand Down Expand Up @@ -938,7 +938,7 @@ fn disassemble_extract(config: &config, _props: &TestProps,

fn count_extracted_lines(p: &Path) -> uint {
let x = io::read_whole_file_str(&p.with_filetype("ll")).unwrap();
x.line_iter().len_()
x.line_iter().len()
}


Expand Down
6 changes: 3 additions & 3 deletions src/libextra/bitv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ impl BitvSet {
let min = num::min(self.bitv.storage.len(), other.bitv.storage.len());
self.bitv.storage.slice(0, min).iter().enumerate()
.zip(Repeat::new(&other.bitv.storage))
.transform(|((i, &w), o_store)| (i * uint::bits, w, o_store[i]))
.map(|((i, &w), o_store)| (i * uint::bits, w, o_store[i]))
}

/// Visits each word in self or other that extends beyond the other. This
Expand All @@ -888,11 +888,11 @@ impl BitvSet {
if olen < slen {
self.bitv.storage.slice_from(olen).iter().enumerate()
.zip(Repeat::new(olen))
.transform(|((i, &w), min)| (true, (i + min) * uint::bits, w))
.map(|((i, &w), min)| (true, (i + min) * uint::bits, w))
} else {
other.bitv.storage.slice_from(slen).iter().enumerate()
.zip(Repeat::new(slen))
.transform(|((i, &w), min)| (false, (i + min) * uint::bits, w))
.map(|((i, &w), min)| (false, (i + min) * uint::bits, w))
}
}
}
Expand Down
50 changes: 25 additions & 25 deletions src/libextra/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub struct MutDListIterator<'self, T> {

/// DList consuming iterator
#[deriving(Clone)]
pub struct ConsumeIterator<T> {
pub struct MoveIterator<T> {
priv list: DList<T>
}

Expand Down Expand Up @@ -391,14 +391,14 @@ impl<T> DList<T> {

/// Consume the list into an iterator yielding elements by value
#[inline]
pub fn consume_iter(self) -> ConsumeIterator<T> {
ConsumeIterator{list: self}
pub fn move_iter(self) -> MoveIterator<T> {
MoveIterator{list: self}
}

/// Consume the list into an iterator yielding elements by value, in reverse
#[inline]
pub fn consume_rev_iter(self) -> Invert<ConsumeIterator<T>> {
self.consume_iter().invert()
pub fn move_rev_iter(self) -> Invert<MoveIterator<T>> {
self.move_iter().invert()
}
}

Expand Down Expand Up @@ -557,7 +557,7 @@ impl<'self, A> ListInsertion<A> for MutDListIterator<'self, A> {
}
}

impl<A> Iterator<A> for ConsumeIterator<A> {
impl<A> Iterator<A> for MoveIterator<A> {
#[inline]
fn next(&mut self) -> Option<A> { self.list.pop_front() }

Expand All @@ -567,7 +567,7 @@ impl<A> Iterator<A> for ConsumeIterator<A> {
}
}

impl<A> DoubleEndedIterator<A> for ConsumeIterator<A> {
impl<A> DoubleEndedIterator<A> for MoveIterator<A> {
#[inline]
fn next_back(&mut self) -> Option<A> { self.list.pop_back() }
}
Expand Down Expand Up @@ -600,7 +600,7 @@ impl<A: Eq> Eq for DList<A> {

impl<A: Clone> Clone for DList<A> {
fn clone(&self) -> DList<A> {
self.iter().transform(|x| x.clone()).collect()
self.iter().map(|x| x.clone()).collect()
}
}

Expand Down Expand Up @@ -690,7 +690,7 @@ mod tests {

#[cfg(test)]
fn list_from<T: Clone>(v: &[T]) -> DList<T> {
v.iter().transform(|x| (*x).clone()).collect()
v.iter().map(|x| (*x).clone()).collect()
}

#[test]
Expand Down Expand Up @@ -721,7 +721,7 @@ mod tests {
check_links(&m);
let sum = v + u;
assert_eq!(sum.len(), m.len());
for elt in sum.consume_iter() {
for elt in sum.move_iter() {
assert_eq!(m.pop_front(), Some(elt))
}
}
Expand All @@ -745,7 +745,7 @@ mod tests {
check_links(&m);
let sum = u + v;
assert_eq!(sum.len(), m.len());
for elt in sum.consume_iter() {
for elt in sum.move_iter() {
assert_eq!(m.pop_front(), Some(elt))
}
}
Expand All @@ -770,7 +770,7 @@ mod tests {
m.rotate_backward(); check_links(&m);
m.push_front(9); check_links(&m);
m.rotate_forward(); check_links(&m);
assert_eq!(~[3,9,5,1,2], m.consume_iter().collect());
assert_eq!(~[3,9,5,1,2], m.move_iter().collect());
}

#[test]
Expand Down Expand Up @@ -900,7 +900,7 @@ mod tests {
}
check_links(&m);
assert_eq!(m.len(), 3 + len * 2);
assert_eq!(m.consume_iter().collect::<~[int]>(), ~[-2,0,1,2,3,4,5,6,7,8,9,0,1]);
assert_eq!(m.move_iter().collect::<~[int]>(), ~[-2,0,1,2,3,4,5,6,7,8,9,0,1]);
}

#[test]
Expand All @@ -911,7 +911,7 @@ mod tests {
m.merge(n, |a, b| a <= b);
assert_eq!(m.len(), len);
check_links(&m);
let res = m.consume_iter().collect::<~[int]>();
let res = m.move_iter().collect::<~[int]>();
assert_eq!(res, ~[-1, 0, 0, 0, 1, 3, 5, 6, 7, 2, 7, 7, 9]);
}

Expand All @@ -927,7 +927,7 @@ mod tests {
m.push_back(4);
m.insert_ordered(3);
check_links(&m);
assert_eq!(~[2,3,4], m.consume_iter().collect::<~[int]>());
assert_eq!(~[2,3,4], m.move_iter().collect::<~[int]>());
}

#[test]
Expand Down Expand Up @@ -1003,7 +1003,7 @@ mod tests {
check_links(&m);

let mut i = 0u;
for (a, &b) in m.consume_iter().zip(v.iter()) {
for (a, &b) in m.move_iter().zip(v.iter()) {
i += 1;
assert_eq!(a, b);
}
Expand All @@ -1014,7 +1014,7 @@ mod tests {
fn bench_collect_into(b: &mut test::BenchHarness) {
let v = &[0, ..64];
do b.iter {
let _: DList<int> = v.iter().transform(|x| *x).collect();
let _: DList<int> = v.iter().map(|x| *x).collect();
}
}

Expand Down Expand Up @@ -1075,33 +1075,33 @@ mod tests {
#[bench]
fn bench_iter(b: &mut test::BenchHarness) {
let v = &[0, ..128];
let m: DList<int> = v.iter().transform(|&x|x).collect();
let m: DList<int> = v.iter().map(|&x|x).collect();
do b.iter {
assert!(m.iter().len_() == 128);
assert!(m.iter().len() == 128);
}
}
#[bench]
fn bench_iter_mut(b: &mut test::BenchHarness) {
let v = &[0, ..128];
let mut m: DList<int> = v.iter().transform(|&x|x).collect();
let mut m: DList<int> = v.iter().map(|&x|x).collect();
do b.iter {
assert!(m.mut_iter().len_() == 128);
assert!(m.mut_iter().len() == 128);
}
}
#[bench]
fn bench_iter_rev(b: &mut test::BenchHarness) {
let v = &[0, ..128];
let m: DList<int> = v.iter().transform(|&x|x).collect();
let m: DList<int> = v.iter().map(|&x|x).collect();
do b.iter {
assert!(m.rev_iter().len_() == 128);
assert!(m.rev_iter().len() == 128);
}
}
#[bench]
fn bench_iter_mut_rev(b: &mut test::BenchHarness) {
let v = &[0, ..128];
let mut m: DList<int> = v.iter().transform(|&x|x).collect();
let mut m: DList<int> = v.iter().map(|&x|x).collect();
do b.iter {
assert!(m.mut_rev_iter().len_() == 128);
assert!(m.mut_rev_iter().len() == 128);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/fileinput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ a literal `-`.
*/
// XXX: stupid, unclear name
pub fn pathify(vec: &[~str], stdin_hyphen : bool) -> ~[Option<Path>] {
vec.iter().transform(|str| {
vec.iter().map(|str| {
if stdin_hyphen && "-" == *str {
None
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/getopts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ pub mod groups {

let desc_sep = "\n" + " ".repeat(24);

let mut rows = opts.iter().transform(|optref| {
let mut rows = opts.iter().map(|optref| {
let OptGroup{short_name: short_name,
long_name: long_name,
hint: hint,
Expand Down
6 changes: 3 additions & 3 deletions src/libextra/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ impl serialize::Decoder for Decoder {
let name = match self.stack.pop() {
String(s) => s,
List(list) => {
for v in list.consume_rev_iter() {
for v in list.move_rev_iter() {
self.stack.push(v);
}
match self.stack.pop() {
Expand Down Expand Up @@ -1066,7 +1066,7 @@ impl serialize::Decoder for Decoder {
let len = match self.stack.pop() {
List(list) => {
let len = list.len();
for v in list.consume_rev_iter() {
for v in list.move_rev_iter() {
self.stack.push(v);
}
len
Expand All @@ -1086,7 +1086,7 @@ impl serialize::Decoder for Decoder {
let len = match self.stack.pop() {
Object(obj) => {
let len = obj.len();
for (key, value) in obj.consume_iter() {
for (key, value) in obj.move_iter() {
self.stack.push(value);
self.stack.push(String(key));
}
Expand Down
4 changes: 2 additions & 2 deletions src/libextra/num/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ impl Mul<BigUint, BigUint> for BigUint {
if n == 1 { return (*a).clone(); }

let mut carry = 0;
let mut prod = do a.data.iter().transform |ai| {
let mut prod = do a.data.iter().map |ai| {
let (hi, lo) = BigDigit::from_uint(
(*ai as uint) * (n as uint) + (carry as uint)
);
Expand Down Expand Up @@ -625,7 +625,7 @@ impl BigUint {
if n_bits == 0 || self.is_zero() { return (*self).clone(); }

let mut carry = 0;
let mut shifted = do self.data.iter().transform |elem| {
let mut shifted = do self.data.iter().map |elem| {
let (hi, lo) = BigDigit::from_uint(
(*elem as uint) << n_bits | (carry as uint)
);
Expand Down
6 changes: 3 additions & 3 deletions src/libextra/par.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn map_slices<A:Clone + Send,B:Clone + Send>(
info!("num_tasks: %?", (num_tasks, futures.len()));
assert_eq!(num_tasks, futures.len());

do futures.consume_iter().transform |ys| {
do futures.move_iter().map |ys| {
let mut ys = ys;
ys.get()
}.collect()
Expand All @@ -90,7 +90,7 @@ pub fn map<A:Clone + Send,B:Clone + Send>(
vec::concat(map_slices(xs, || {
let f = fn_factory();
let result: ~fn(uint, &[A]) -> ~[B] =
|_, slice| slice.iter().transform(|x| f(x)).collect();
|_, slice| slice.iter().map(|x| f(x)).collect();
result
}))
}
Expand All @@ -102,7 +102,7 @@ pub fn mapi<A:Clone + Send,B:Clone + Send>(
let slices = map_slices(xs, || {
let f = fn_factory();
let result: ~fn(uint, &[A]) -> ~[B] = |base, slice| {
slice.iter().enumerate().transform(|(i, x)| {
slice.iter().enumerate().map(|(i, x)| {
f(i + base, x)
}).collect()
};
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/priority_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ mod tests {
fn test_from_iter() {
let xs = ~[9u, 8, 7, 6, 5, 4, 3, 2, 1];

let mut q: PriorityQueue<uint> = xs.rev_iter().transform(|&x| x).collect();
let mut q: PriorityQueue<uint> = xs.rev_iter().map(|&x| x).collect();

for &x in xs.iter() {
assert_eq!(q.pop(), x);
Expand Down
6 changes: 3 additions & 3 deletions src/libextra/ringbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,11 +692,11 @@ mod tests {
fn test_from_iterator() {
use std::iterator;
let v = ~[1,2,3,4,5,6,7];
let deq: RingBuf<int> = v.iter().transform(|&x| x).collect();
let u: ~[int] = deq.iter().transform(|&x| x).collect();
let deq: RingBuf<int> = v.iter().map(|&x| x).collect();
let u: ~[int] = deq.iter().map(|&x| x).collect();
assert_eq!(u, v);

let mut seq = iterator::count(0u, 2).take_(256);
let mut seq = iterator::count(0u, 2).take(256);
let deq: RingBuf<uint> = seq.collect();
for (i, &x) in deq.iter().enumerate() {
assert_eq!(2*i, x);
Expand Down
12 changes: 6 additions & 6 deletions src/libextra/smallintmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#[allow(missing_doc)];

use std::iterator::{Iterator, IteratorUtil, Enumerate, FilterMap, Invert};
use std::iterator::{Iterator, Enumerate, FilterMap, Invert};
use std::util::replace;
use std::vec::{VecIterator, VecMutIterator};
use std::vec;
Expand Down Expand Up @@ -152,12 +152,12 @@ impl<V> SmallIntMap<V> {
}

/// Empties the hash map, moving all values into the specified closure
pub fn consume(&mut self)
pub fn move_iter(&mut self)
-> FilterMap<(uint, Option<V>), (uint, V),
Enumerate<vec::ConsumeIterator<Option<V>>>>
Enumerate<vec::MoveIterator<Option<V>>>>
{
let values = replace(&mut self.v, ~[]);
values.consume_iter().enumerate().filter_map(|(i, v)| {
values.move_iter().enumerate().filter_map(|(i, v)| {
v.map_move(|v| (i, v))
})
}
Expand Down Expand Up @@ -452,11 +452,11 @@ mod test_map {
}

#[test]
fn test_consume() {
fn test_move_iter() {
let mut m = SmallIntMap::new();
m.insert(1, ~2);
let mut called = false;
for (k, v) in m.consume() {
for (k, v) in m.move_iter() {
assert!(!called);
called = true;
assert_eq!(k, 1);
Expand Down
Loading

0 comments on commit 8b9e1ce

Please sign in to comment.