Skip to content

remove old_iter #7334

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 8 commits 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
6 changes: 1 addition & 5 deletions src/libextra/bitv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,12 +666,8 @@ impl BitvSet {
pub fn symmetric_difference_with(&mut self, other: &BitvSet) {
self.other_op(other, |w1, w2| w1 ^ w2);
}
}

impl BaseIter<uint> for BitvSet {
fn size_hint(&self) -> Option<uint> { Some(self.len()) }

fn each(&self, blk: &fn(v: &uint) -> bool) -> bool {
pub fn each(&self, blk: &fn(v: &uint) -> bool) -> bool {
for self.bitv.storage.iter().enumerate().advance |(i, &w)| {
if !iterate_bits(i * uint::bits, w, |b| blk(&b)) {
return false;
Expand Down
94 changes: 36 additions & 58 deletions src/libextra/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ Do not use ==, !=, <, etc on doubly-linked lists -- it may not terminate.
use core::prelude::*;

use core::managed;
use core::old_iter;
use core::vec;

pub type DListLink<T> = Option<@mut DListNode<T>>;

Expand Down Expand Up @@ -213,6 +211,42 @@ impl<T> DList<T> {
}

impl<T> DList<T> {
/**
* Iterates through the current contents.
*
* Attempts to access this dlist during iteration are allowed (to
* allow for e.g. breadth-first search with in-place enqueues), but
* removing the current node is forbidden.
*/
pub fn each(@mut self, f: &fn(v: &T) -> bool) -> bool {
let mut link = self.peek_n();
while link.is_some() {
let nobe = link.get();
assert!(nobe.linked);

{
let frozen_nobe = &*nobe;
if !f(&frozen_nobe.data) { return false; }
}

// Check (weakly) that the user didn't do a remove.
if self.size == 0 {
fail!("The dlist became empty during iteration??")
}
if !nobe.linked ||
(!((nobe.prev.is_some()
|| managed::mut_ptr_eq(self.hd.expect("headless dlist?"),
nobe))
&& (nobe.next.is_some()
|| managed::mut_ptr_eq(self.tl.expect("tailless dlist?"),
nobe)))) {
fail!("Removing a dlist node during iteration is forbidden!")
}
link = nobe.next_link();
}
return true;
}

/// Get the size of the list. O(1).
pub fn len(@mut self) -> uint { self.size }
/// Returns true if the list is empty. O(1).
Expand Down Expand Up @@ -484,56 +518,6 @@ impl<T:Copy> DList<T> {

/// Get data at the list's tail, failing if empty. O(1).
pub fn tail(@mut self) -> T { copy self.tail_n().data }

/// Get the elements of the list as a vector. O(n).
pub fn to_vec(@mut self) -> ~[T] {
let mut v = vec::with_capacity(self.size);
for old_iter::eachi(&self) |index,data| {
v[index] = copy *data;
}
v
}
}

impl<T> BaseIter<T> for @mut DList<T> {
/**
* Iterates through the current contents.
*
* Attempts to access this dlist during iteration are allowed (to
* allow for e.g. breadth-first search with in-place enqueues), but
* removing the current node is forbidden.
*/
fn each(&self, f: &fn(v: &T) -> bool) -> bool {
let mut link = self.peek_n();
while link.is_some() {
let nobe = link.get();
assert!(nobe.linked);

{
let frozen_nobe = &*nobe;
if !f(&frozen_nobe.data) { return false; }
}

// Check (weakly) that the user didn't do a remove.
if self.size == 0 {
fail!("The dlist became empty during iteration??")
}
if !nobe.linked ||
(!((nobe.prev.is_some()
|| managed::mut_ptr_eq(self.hd.expect("headless dlist?"),
nobe))
&& (nobe.next.is_some()
|| managed::mut_ptr_eq(self.tl.expect("tailless dlist?"),
nobe)))) {
fail!("Removing a dlist node during iteration is forbidden!")
}
link = nobe.next_link();
}
return true;
}

#[inline]
fn size_hint(&self) -> Option<uint> { Some(self.len()) }
}

#[cfg(test)]
Expand All @@ -542,7 +526,6 @@ mod tests {

use super::*;

use core::old_iter;
use core::vec;

#[test]
Expand Down Expand Up @@ -759,11 +742,6 @@ mod tests {
assert_eq!(l.len(), 3);
}
#[test]
fn test_dlist_foldl() {
let l = from_vec(vec::from_fn(101, |x|x));
assert_eq!(old_iter::foldl(&l, 0, |accum,elem| *accum+*elem), 5050);
}
#[test]
fn test_dlist_break_early() {
let l = from_vec([1,2,3,4,5]);
let mut x = 0;
Expand Down
17 changes: 6 additions & 11 deletions src/libextra/priority_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,15 @@

use core::prelude::*;

use core::old_iter::BaseIter;
use core::unstable::intrinsics::{move_val_init, init};
use core::util::{replace, swap};
use core::vec;

#[allow(missing_doc)]
/// A priority queue implemented with a binary heap
pub struct PriorityQueue<T> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mostly an afterthought ;p

priv data: ~[T],
}

impl<T:Ord> BaseIter<T> for PriorityQueue<T> {
/// Visit all values in the underlying vector.
///
/// The values are **not** visited in order.
fn each(&self, f: &fn(&T) -> bool) -> bool { self.data.iter().advance(f) }

fn size_hint(&self) -> Option<uint> { Some(self.data.len()) }
}

impl<T:Ord> Container for PriorityQueue<T> {
/// Returns the length of the queue
fn len(&self) -> uint { self.data.len() }
Expand All @@ -47,6 +37,11 @@ impl<T:Ord> Mutable for PriorityQueue<T> {
}

impl<T:Ord> PriorityQueue<T> {
/// Visit all values in the underlying vector.
///
/// The values are **not** visited in order.
pub fn each(&self, f: &fn(&T) -> bool) -> bool { self.data.iter().advance(f) }

/// Returns the greatest item in the queue - fails if empty
pub fn top<'a>(&'a self) -> &'a T { &self.data[0] }

Expand Down
17 changes: 7 additions & 10 deletions src/libextra/smallintmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ use core::prelude::*;

use core::cmp;
use core::container::{Container, Mutable, Map, Set};
use core::old_iter::BaseIter;
use core::old_iter;
use core::uint;
use core::util::replace;
use core::vec;
Expand Down Expand Up @@ -212,12 +210,6 @@ impl Mutable for SmallIntSet {
fn clear(&mut self) { self.map.clear() }
}

impl BaseIter<uint> for SmallIntSet {
/// Visit all values in order
fn each(&self, f: &fn(&uint) -> bool) -> bool { self.map.each_key(f) }
fn size_hint(&self) -> Option<uint> { Some(self.len()) }
}

impl Set<uint> for SmallIntSet {
/// Return true if the set contains a value
fn contains(&self, value: &uint) -> bool { self.map.contains_key(value) }
Expand All @@ -233,12 +225,14 @@ impl Set<uint> for SmallIntSet {
/// Return true if the set has no elements in common with `other`.
/// This is equivalent to checking for an empty uintersection.
fn is_disjoint(&self, other: &SmallIntSet) -> bool {
old_iter::all(self, |v| !other.contains(v))
for self.each |v| { if other.contains(v) { return false } }
true
}

/// Return true if the set is a subset of another
fn is_subset(&self, other: &SmallIntSet) -> bool {
old_iter::all(self, |v| other.contains(v))
for self.each |v| { if !other.contains(v) { return false } }
true
}

/// Return true if the set is a superset of another
Expand Down Expand Up @@ -286,6 +280,9 @@ impl Set<uint> for SmallIntSet {
impl SmallIntSet {
/// Create an empty SmallIntSet
pub fn new() -> SmallIntSet { SmallIntSet{map: SmallIntMap::new()} }

/// Visit all values in order
pub fn each(&self, f: &fn(&uint) -> bool) -> bool { self.map.each_key(f) }
}

#[cfg(test)]
Expand Down
26 changes: 10 additions & 16 deletions src/libextra/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,22 +249,6 @@ pub struct TreeSet<T> {
priv map: TreeMap<T, ()>
}

impl<T: TotalOrd> BaseIter<T> for TreeSet<T> {
/// Visit all values in order
#[inline]
fn each(&self, f: &fn(&T) -> bool) -> bool { self.map.each_key(f) }
#[inline]
fn size_hint(&self) -> Option<uint> { Some(self.len()) }
}

impl<T: TotalOrd> ReverseIter<T> for TreeSet<T> {
/// Visit all values in reverse order
#[inline]
fn each_reverse(&self, f: &fn(&T) -> bool) -> bool {
self.map.each_key_reverse(f)
}
}

impl<T: Eq + TotalOrd> Eq for TreeSet<T> {
#[inline]
fn eq(&self, other: &TreeSet<T>) -> bool { self.map == other.map }
Expand Down Expand Up @@ -499,6 +483,16 @@ impl<T: TotalOrd> TreeSet<T> {
pub fn iter<'a>(&'a self) -> TreeSetIterator<'a, T> {
TreeSetIterator{iter: self.map.iter()}
}

/// Visit all values in order
#[inline]
pub fn each(&self, f: &fn(&T) -> bool) -> bool { self.map.each_key(f) }

/// Visit all values in reverse order
#[inline]
pub fn each_reverse(&self, f: &fn(&T) -> bool) -> bool {
self.map.each_key_reverse(f)
}
}

/// Lazy forward iterator over a set
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,8 +731,8 @@ fn encode_info_for_method(ecx: &EncodeContext,
}

let mut combined_ty_params = opt_vec::Empty;
combined_ty_params.push_all(&owner_generics.ty_params);
combined_ty_params.push_all(&method_generics.ty_params);
for owner_generics.ty_params.each |x| { combined_ty_params.push(copy *x) }
for method_generics.ty_params.each |x| { combined_ty_params.push(copy *x) }
let len = combined_ty_params.len();
encode_type_param_bounds(ebml_w, ecx, &combined_ty_params);

Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/borrowck/move_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ impl FlowedMoveData {
for self.dfcx_moves.each_bit_on_entry_frozen(id) |index| {
let move = &self.move_data.moves[index];
let moved_path = move.path;
if base_indices.contains(&moved_path) {
if base_indices.iter().any_(|x| x == &moved_path) {
// Scenario 1 or 2: `loan_path` or some base path of
// `loan_path` was moved.
if !f(move, self.move_data.path(moved_path).loan_path) {
Expand Down Expand Up @@ -536,7 +536,7 @@ impl FlowedMoveData {
-> bool {
//! True if `id` is the id of the LHS of an assignment

self.move_data.assignee_ids.contains(&id)
self.move_data.assignee_ids.iter().any_(|x| x == &id)
}

pub fn each_assignment_of(&self,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3710,7 +3710,7 @@ impl Resolver {
let function_type_rib = @Rib(rib_kind);
self.type_ribs.push(function_type_rib);

for generics.ty_params.eachi |index, type_parameter| {
for generics.ty_params.iter().enumerate().advance |(index, type_parameter)| {
let name = type_parameter.ident;
debug!("with_type_parameter_rib: %d %d", node_id,
type_parameter.id);
Expand Down
3 changes: 1 addition & 2 deletions src/libstd/at_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use cast::transmute;
use container::Container;
use iterator::IteratorUtil;
use kinds::Copy;
use old_iter;
use option::Option;
use sys;
use uint;
Expand Down Expand Up @@ -129,7 +128,7 @@ pub fn map<T, U>(v: &[T], f: &fn(x: &T) -> U) -> @[U] {
* Creates an immutable vector of size `n_elts` and initializes the elements
* to the value returned by the function `op`.
*/
pub fn from_fn<T>(n_elts: uint, op: old_iter::InitOp<T>) -> @[T] {
pub fn from_fn<T>(n_elts: uint, op: &fn(uint) -> T) -> @[T] {
do build_sized(n_elts) |push| {
let mut i: uint = 0u;
while i < n_elts { push(op(i)); i += 1u; }
Expand Down
12 changes: 12 additions & 0 deletions src/libstd/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ impl<'self, T> Clone for &'self T {
fn clone(&self) -> &'self T { *self }
}

impl<'self, T> Clone for &'self [T] {
/// Return a shallow copy of the slice.
#[inline]
fn clone(&self) -> &'self [T] { *self }
}

impl<'self> Clone for &'self str {
/// Return a shallow copy of the slice.
#[inline]
fn clone(&self) -> &'self str { *self }
}

macro_rules! clone_impl(
($t:ty) => {
impl Clone for $t {
Expand Down
1 change: 0 additions & 1 deletion src/libstd/core.rc
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ pub mod from_str;
#[path = "num/num.rs"]
pub mod num;
pub mod iter;
pub mod old_iter;
pub mod iterator;
pub mod to_str;
pub mod to_bytes;
Expand Down
10 changes: 5 additions & 5 deletions src/libstd/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ implementing the `Iterator` trait.
#[allow(default_methods)]; // solid enough for the use case here

use cmp;
use iter::{FromIter, Times};
use iter::Times;
use num::{Zero, One};
use option::{Option, Some, None};
use ops::{Add, Mul};
Expand Down Expand Up @@ -240,7 +240,7 @@ pub trait IteratorUtil<A> {
fn advance(&mut self, f: &fn(A) -> bool) -> bool;

/// Loops through the entire iterator, collecting all of the elements into
/// a container implementing `FromIter`.
/// a container implementing `FromIterator`.
///
/// # Example
///
Expand All @@ -249,7 +249,7 @@ pub trait IteratorUtil<A> {
/// let b: ~[int] = a.iter().transform(|&x| x).collect();
/// assert!(a == b);
/// ~~~
fn collect<B: FromIter<A>>(&mut self) -> B;
fn collect<B: FromIterator<A, Self>>(&mut self) -> B;

/// Loops through `n` iterations, returning the `n`th element of the
/// iterator.
Expand Down Expand Up @@ -411,8 +411,8 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T {
}

#[inline]
fn collect<B: FromIter<A>>(&mut self) -> B {
FromIter::from_iter::<A, B>(|f| self.advance(f))
fn collect<B: FromIterator<A, T>>(&mut self) -> B {
FromIterator::from_iterator(self)
}

/// Return the `n`th item yielded by an iterator.
Expand Down
Loading