Skip to content

Rename Iterator::len to count #14692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1545,7 +1545,7 @@ fn disassemble_extract(config: &Config, _props: &TestProps,
fn count_extracted_lines(p: &Path) -> uint {
let x = File::open(&p.with_extension("ll")).read_to_end().unwrap();
let x = str::from_utf8(x.as_slice()).unwrap();
x.lines().len()
x.lines().count()
}


Expand Down
8 changes: 4 additions & 4 deletions src/libcollections/bitv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,17 +241,17 @@ enum Op {Union, Intersect, Assign, Difference}
/// bv.set(5, true);
/// bv.set(7, true);
/// println!("{}", bv.to_str());
/// println!("total bits set to true: {}", bv.iter().count(|x| x));
/// println!("total bits set to true: {}", bv.iter().filter(|x| *x).count());
///
/// // flip all values in bitvector, producing non-primes less than 10
/// bv.negate();
/// println!("{}", bv.to_str());
/// println!("total bits set to true: {}", bv.iter().count(|x| x));
/// println!("total bits set to true: {}", bv.iter().filter(|x| *x).count());
///
/// // reset bitvector to empty
/// bv.clear();
/// println!("{}", bv.to_str());
/// println!("total bits set to true: {}", bv.iter().count(|x| x));
/// println!("total bits set to true: {}", bv.iter().filter(|x| *x).count());
/// ```
#[deriving(Clone)]
pub struct Bitv {
Expand Down Expand Up @@ -461,7 +461,7 @@ impl Bitv {
/// bv.set(5, true);
/// bv.set(8, true);
/// // Count bits set to 1; result should be 5
/// println!("{}", bv.iter().count(|x| x));
/// println!("{}", bv.iter().filter(|x| *x).count());
/// ```
#[inline]
pub fn iter<'a>(&'a self) -> Bits<'a> {
Expand Down
8 changes: 4 additions & 4 deletions src/libcollections/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1131,31 +1131,31 @@ mod tests {
let v = &[0, ..128];
let m: DList<int> = v.iter().map(|&x|x).collect();
b.iter(|| {
assert!(m.iter().len() == 128);
assert!(m.iter().count() == 128);
})
}
#[bench]
fn bench_iter_mut(b: &mut test::Bencher) {
let v = &[0, ..128];
let mut m: DList<int> = v.iter().map(|&x|x).collect();
b.iter(|| {
assert!(m.mut_iter().len() == 128);
assert!(m.mut_iter().count() == 128);
})
}
#[bench]
fn bench_iter_rev(b: &mut test::Bencher) {
let v = &[0, ..128];
let m: DList<int> = v.iter().map(|&x|x).collect();
b.iter(|| {
assert!(m.iter().rev().len() == 128);
assert!(m.iter().rev().count() == 128);
})
}
#[bench]
fn bench_iter_mut_rev(b: &mut test::Bencher) {
let v = &[0, ..128];
let mut m: DList<int> = v.iter().map(|&x|x).collect();
b.iter(|| {
assert!(m.mut_iter().rev().len() == 128);
assert!(m.mut_iter().rev().count() == 128);
})
}
}
2 changes: 1 addition & 1 deletion src/libcollections/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2155,7 +2155,7 @@ mod tests {
#[test]
fn test_mut_splitator() {
let mut xs = [0,1,0,2,3,0,0,4,5,0];
assert_eq!(xs.mut_split(|x| *x == 0).len(), 6);
assert_eq!(xs.mut_split(|x| *x == 0).count(), 6);
for slice in xs.mut_split(|x| *x == 0) {
slice.reverse();
}
Expand Down
2 changes: 1 addition & 1 deletion src/libcollections/smallintmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct SmallIntMap<T> {
impl<V> Container for SmallIntMap<V> {
/// Return the number of elements in the map
fn len(&self) -> uint {
self.v.iter().count(|elt| elt.is_some())
self.v.iter().filter(|elt| elt.is_some()).count()
}

/// Return true if there are no elements in the map
Expand Down
34 changes: 17 additions & 17 deletions src/libcollections/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2181,7 +2181,7 @@ mod bench {
let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
let len = s.char_len();

b.iter(|| assert_eq!(s.chars().len(), len));
b.iter(|| assert_eq!(s.chars().count(), len));
}

#[bench]
Expand All @@ -2194,38 +2194,38 @@ mod bench {
Mary had a little lamb, Little lamb";
let len = s.char_len();

b.iter(|| assert_eq!(s.chars().len(), len));
b.iter(|| assert_eq!(s.chars().count(), len));
}

#[bench]
fn char_iterator_rev(b: &mut Bencher) {
let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
let len = s.char_len();

b.iter(|| assert_eq!(s.chars().rev().len(), len));
b.iter(|| assert_eq!(s.chars().rev().count(), len));
}

#[bench]
fn char_indicesator(b: &mut Bencher) {
let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
let len = s.char_len();

b.iter(|| assert_eq!(s.char_indices().len(), len));
b.iter(|| assert_eq!(s.char_indices().count(), len));
}

#[bench]
fn char_indicesator_rev(b: &mut Bencher) {
let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
let len = s.char_len();

b.iter(|| assert_eq!(s.char_indices().rev().len(), len));
b.iter(|| assert_eq!(s.char_indices().rev().count(), len));
}

#[bench]
fn split_unicode_ascii(b: &mut Bencher) {
let s = "ประเทศไทย中华Việt Namประเทศไทย中华Việt Nam";

b.iter(|| assert_eq!(s.split('V').len(), 3));
b.iter(|| assert_eq!(s.split('V').count(), 3));
}

#[bench]
Expand All @@ -2240,16 +2240,16 @@ mod bench {
}
let s = "ประเทศไทย中华Việt Namประเทศไทย中华Việt Nam";

b.iter(|| assert_eq!(s.split(NotAscii('V')).len(), 3));
b.iter(|| assert_eq!(s.split(NotAscii('V')).count(), 3));
}


#[bench]
fn split_ascii(b: &mut Bencher) {
let s = "Mary had a little lamb, Little lamb, little-lamb.";
let len = s.split(' ').len();
let len = s.split(' ').count();

b.iter(|| assert_eq!(s.split(' ').len(), len));
b.iter(|| assert_eq!(s.split(' ').count(), len));
}

#[bench]
Expand All @@ -2264,34 +2264,34 @@ mod bench {
fn only_ascii(&self) -> bool { false }
}
let s = "Mary had a little lamb, Little lamb, little-lamb.";
let len = s.split(' ').len();
let len = s.split(' ').count();

b.iter(|| assert_eq!(s.split(NotAscii(' ')).len(), len));
b.iter(|| assert_eq!(s.split(NotAscii(' ')).count(), len));
}

#[bench]
fn split_extern_fn(b: &mut Bencher) {
let s = "Mary had a little lamb, Little lamb, little-lamb.";
let len = s.split(' ').len();
let len = s.split(' ').count();
fn pred(c: char) -> bool { c == ' ' }

b.iter(|| assert_eq!(s.split(pred).len(), len));
b.iter(|| assert_eq!(s.split(pred).count(), len));
}

#[bench]
fn split_closure(b: &mut Bencher) {
let s = "Mary had a little lamb, Little lamb, little-lamb.";
let len = s.split(' ').len();
let len = s.split(' ').count();

b.iter(|| assert_eq!(s.split(|c: char| c == ' ').len(), len));
b.iter(|| assert_eq!(s.split(|c: char| c == ' ').count(), len));
}

#[bench]
fn split_slice(b: &mut Bencher) {
let s = "Mary had a little lamb, Little lamb, little-lamb.";
let len = s.split(' ').len();
let len = s.split(' ').count();

b.iter(|| assert_eq!(s.split(&[' ']).len(), len));
b.iter(|| assert_eq!(s.split(&[' ']).count(), len));
}

#[bench]
Expand Down
14 changes: 7 additions & 7 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1772,23 +1772,23 @@ mod tests {
assert_eq!(v.pop(), Some(()));
assert_eq!(v.pop(), None);

assert_eq!(v.iter().len(), 0);
assert_eq!(v.iter().count(), 0);
v.push(());
assert_eq!(v.iter().len(), 1);
assert_eq!(v.iter().count(), 1);
v.push(());
assert_eq!(v.iter().len(), 2);
assert_eq!(v.iter().count(), 2);

for &() in v.iter() {}

assert_eq!(v.mut_iter().len(), 2);
assert_eq!(v.mut_iter().count(), 2);
v.push(());
assert_eq!(v.mut_iter().len(), 3);
assert_eq!(v.mut_iter().count(), 3);
v.push(());
assert_eq!(v.mut_iter().len(), 4);
assert_eq!(v.mut_iter().count(), 4);

for &() in v.mut_iter() {}
unsafe { v.set_len(0); }
assert_eq!(v.mut_iter().len(), 0);
assert_eq!(v.mut_iter().count(), 0);
}

#[test]
Expand Down
48 changes: 23 additions & 25 deletions src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,11 +529,11 @@ pub trait Iterator<A> {
/// ```rust
/// let a = [1, 2, 3, 4, 5];
/// let mut it = a.iter();
/// assert!(it.len() == 5);
/// assert!(it.len() == 0);
/// assert!(it.count() == 5);
/// assert!(it.count() == 0);
/// ```
#[inline]
fn len(&mut self) -> uint {
fn count(&mut self) -> uint {
self.fold(0, |cnt, _x| cnt + 1)
}

Expand Down Expand Up @@ -591,16 +591,6 @@ pub trait Iterator<A> {
None
}

/// Count the number of elements satisfying the specified predicate
#[inline]
fn count(&mut self, predicate: |A| -> bool) -> uint {
let mut i = 0;
for x in *self {
if predicate(x) { i += 1 }
}
i
}

/// Return the element that gives the maximum value from the
/// specified function.
///
Expand Down Expand Up @@ -738,6 +728,14 @@ pub trait ExactSize<A> : DoubleEndedIterator<A> {
}
None
}

#[inline]
/// Return the exact length of the iterator.
fn len(&self) -> uint {
let (lower, upper) = self.size_hint();
assert!(upper == Some(lower));
lower
}
}

// All adaptors that preserve the size of the wrapped iterator are fine
Expand Down Expand Up @@ -2594,9 +2592,9 @@ mod tests {
#[test]
fn test_iterator_len() {
let v = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
assert_eq!(v.slice(0, 4).iter().len(), 4);
assert_eq!(v.slice(0, 10).iter().len(), 10);
assert_eq!(v.slice(0, 0).iter().len(), 0);
assert_eq!(v.slice(0, 4).iter().count(), 4);
assert_eq!(v.slice(0, 10).iter().count(), 10);
assert_eq!(v.slice(0, 0).iter().count(), 0);
}

#[test]
Expand Down Expand Up @@ -2712,9 +2710,9 @@ mod tests {
#[test]
fn test_count() {
let xs = &[1, 2, 2, 1, 5, 9, 0, 2];
assert_eq!(xs.iter().count(|x| *x == 2), 3);
assert_eq!(xs.iter().count(|x| *x == 5), 1);
assert_eq!(xs.iter().count(|x| *x == 95), 0);
assert_eq!(xs.iter().filter(|x| **x == 2).count(), 3);
assert_eq!(xs.iter().filter(|x| **x == 5).count(), 1);
assert_eq!(xs.iter().filter(|x| **x == 95).count(), 0);
}

#[test]
Expand Down Expand Up @@ -3044,10 +3042,10 @@ mod tests {
assert!(range(-10i, -1).collect::<Vec<int>>() ==
vec![-10, -9, -8, -7, -6, -5, -4, -3, -2]);
assert!(range(0i, 5).rev().collect::<Vec<int>>() == vec![4, 3, 2, 1, 0]);
assert_eq!(range(200, -5).len(), 0);
assert_eq!(range(200, -5).rev().len(), 0);
assert_eq!(range(200, 200).len(), 0);
assert_eq!(range(200, 200).rev().len(), 0);
assert_eq!(range(200, -5).count(), 0);
assert_eq!(range(200, -5).rev().count(), 0);
assert_eq!(range(200, 200).count(), 0);
assert_eq!(range(200, 200).rev().count(), 0);

assert_eq!(range(0i, 100).size_hint(), (100, Some(100)));
// this test is only meaningful when sizeof uint < sizeof u64
Expand All @@ -3062,8 +3060,8 @@ mod tests {
vec![0i, 1, 2, 3, 4, 5]);
assert!(range_inclusive(0i, 5).rev().collect::<Vec<int>>() ==
vec![5i, 4, 3, 2, 1, 0]);
assert_eq!(range_inclusive(200, -5).len(), 0);
assert_eq!(range_inclusive(200, -5).rev().len(), 0);
assert_eq!(range_inclusive(200, -5).count(), 0);
assert_eq!(range_inclusive(200, -5).rev().count(), 0);
assert!(range_inclusive(200, 200).collect::<Vec<int>>() == vec![200]);
assert!(range_inclusive(200, 200).rev().collect::<Vec<int>>() == vec![200]);
}
Expand Down
3 changes: 1 addition & 2 deletions src/libcore/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ pub mod traits {
use super::*;

use cmp::{PartialEq, PartialOrd, Eq, Ord, Ordering, Equiv};
use iter::{order, Iterator};
use iter::order;
use container::Container;

impl<'a,T:PartialEq> PartialEq for &'a [T] {
Expand Down Expand Up @@ -1141,7 +1141,6 @@ impl<'a, T:Clone> MutableCloneableVector<T> for &'a mut [T] {
/// Unsafe operations
pub mod raw {
use mem::transmute;
use iter::Iterator;
use ptr::RawPtr;
use raw::Slice;
use option::{None, Option, Some};
Expand Down
3 changes: 1 addition & 2 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,6 @@ static TAG_CONT_U8: u8 = 128u8;
pub mod raw {
use mem;
use container::Container;
use iter::Iterator;
use ptr::RawPtr;
use raw::Slice;
use slice::{ImmutableVector};
Expand Down Expand Up @@ -1725,7 +1724,7 @@ impl<'a> StrSlice<'a> for &'a str {
fn is_alphanumeric(&self) -> bool { self.chars().all(char::is_alphanumeric) }

#[inline]
fn char_len(&self) -> uint { self.chars().len() }
fn char_len(&self) -> uint { self.chars().count() }

#[inline]
fn slice(&self, begin: uint, end: uint) -> &'a str {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ fn encode_enum_variant_info(ecx: &EncodeContext,
fn encode_path<PI: Iterator<PathElem> + Clone>(ebml_w: &mut Encoder,
mut path: PI) {
ebml_w.start_tag(tag_path);
ebml_w.wr_tagged_u32(tag_path_len, path.clone().len() as u32);
ebml_w.wr_tagged_u32(tag_path_len, path.clone().count() as u32);
for pe in path {
let tag = match pe {
ast_map::PathMod(_) => tag_path_elem_mod,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fn find_item(item: &Item, ctxt: &mut EntryContext) {
ItemFn(..) => {
if item.ident.name == ctxt.main_name {
ctxt.ast_map.with_path(item.id, |mut path| {
if path.len() == 1 {
if path.count() == 1 {
// This is a top-level function so can be 'main'
if ctxt.main_fn.is_none() {
ctxt.main_fn = Some((item.id, item.span));
Expand Down
Loading