auto merge of #5287 : thestinger/rust/iter-trait, r=pcwalton
Closes #2827
This commit is contained in:
commit
ddecef7944
@ -142,8 +142,6 @@ pub mod option;
|
||||
pub mod result;
|
||||
pub mod either;
|
||||
pub mod dlist;
|
||||
#[path="iter-trait.rs"] #[merge = "iter-trait/dlist.rs"]
|
||||
pub mod dlist_iter;
|
||||
pub mod hashmap;
|
||||
pub mod cell;
|
||||
pub mod trie;
|
||||
|
@ -18,6 +18,8 @@
|
||||
|
||||
*/
|
||||
|
||||
use iter;
|
||||
use iter::BaseIter;
|
||||
use kinds::Copy;
|
||||
use managed;
|
||||
use option::{None, Option, Some};
|
||||
@ -489,7 +491,7 @@ fn pop_tail(@mut self) -> Option<T> {
|
||||
let mut v = vec::with_capacity(self.size);
|
||||
unsafe {
|
||||
// Take this out of the unchecked when iter's functions are pure
|
||||
for self.eachi |index,data| {
|
||||
for iter::eachi(&self) |index,data| {
|
||||
v[index] = *data;
|
||||
}
|
||||
}
|
||||
@ -497,6 +499,46 @@ fn pop_tail(@mut self) -> Option<T> {
|
||||
}
|
||||
}
|
||||
|
||||
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.
|
||||
*/
|
||||
pure fn each(&self, f: fn(v: &T) -> bool) {
|
||||
let mut link = self.peek_n();
|
||||
while option::is_some(&link) {
|
||||
let nobe = option::get(link);
|
||||
fail_unless!(nobe.linked);
|
||||
|
||||
{
|
||||
let frozen_nobe = &*nobe;
|
||||
if !f(&frozen_nobe.data) { break; }
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn size_hint(&self) -> Option<uint> { Some(self.len()) }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use dlist::{DList, concat, from_vec, new_dlist_node};
|
||||
|
@ -1,89 +0,0 @@
|
||||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// This makes use of a clever hack that brson came up with to
|
||||
// workaround our lack of traits and lack of macros. See core.{rc,rs} for
|
||||
// how this file is used.
|
||||
|
||||
use cmp::{Eq, Ord};
|
||||
use iter::BaseIter;
|
||||
use iter;
|
||||
use kinds::Copy;
|
||||
use option::Option;
|
||||
|
||||
use self::inst::{IMPL_T, EACH, SIZE_HINT};
|
||||
|
||||
impl<A> iter::BaseIter<A> for IMPL_T<A> {
|
||||
#[inline(always)]
|
||||
pure fn each(&self, blk: fn(v: &A) -> bool) { EACH(self, blk) }
|
||||
#[inline(always)]
|
||||
pure fn size_hint(&self) -> Option<uint> { SIZE_HINT(self) }
|
||||
}
|
||||
|
||||
impl<A> iter::ExtendedIter<A> for IMPL_T<A> {
|
||||
#[inline(always)]
|
||||
pure fn eachi(&self, blk: fn(uint, v: &A) -> bool) {
|
||||
iter::eachi(self, blk)
|
||||
}
|
||||
#[inline(always)]
|
||||
pure fn all(&self, blk: fn(&A) -> bool) -> bool {
|
||||
iter::all(self, blk)
|
||||
}
|
||||
#[inline(always)]
|
||||
pure fn any(&self, blk: fn(&A) -> bool) -> bool {
|
||||
iter::any(self, blk)
|
||||
}
|
||||
#[inline(always)]
|
||||
pure fn foldl<B>(&self, b0: B, blk: fn(&B, &A) -> B) -> B {
|
||||
iter::foldl(self, b0, blk)
|
||||
}
|
||||
#[inline(always)]
|
||||
pure fn position(&self, f: fn(&A) -> bool) -> Option<uint> {
|
||||
iter::position(self, f)
|
||||
}
|
||||
#[inline(always)]
|
||||
pure fn map_to_vec<B>(&self, op: fn(&A) -> B) -> ~[B] {
|
||||
iter::map_to_vec(self, op)
|
||||
}
|
||||
#[inline(always)]
|
||||
pure fn flat_map_to_vec<B,IB:BaseIter<B>>(&self, op: fn(&A) -> IB)
|
||||
-> ~[B] {
|
||||
iter::flat_map_to_vec(self, op)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl<A:Eq> iter::EqIter<A> for IMPL_T<A> {
|
||||
#[inline(always)]
|
||||
pure fn contains(&self, x: &A) -> bool { iter::contains(self, x) }
|
||||
#[inline(always)]
|
||||
pure fn count(&self, x: &A) -> uint { iter::count(self, x) }
|
||||
}
|
||||
|
||||
impl<A:Copy> iter::CopyableIter<A> for IMPL_T<A> {
|
||||
#[inline(always)]
|
||||
pure fn filter_to_vec(&self, pred: fn(&A) -> bool) -> ~[A] {
|
||||
iter::filter_to_vec(self, pred)
|
||||
}
|
||||
#[inline(always)]
|
||||
pure fn to_vec(&self) -> ~[A] { iter::to_vec(self) }
|
||||
#[inline(always)]
|
||||
pure fn find(&self, f: fn(&A) -> bool) -> Option<A> {
|
||||
iter::find(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<A:Copy + Ord> iter::CopyableOrderedIter<A> for IMPL_T<A> {
|
||||
#[inline(always)]
|
||||
pure fn min(&self) -> A { iter::min(self) }
|
||||
#[inline(always)]
|
||||
pure fn max(&self) -> A { iter::max(self) }
|
||||
}
|
||||
|
@ -1,59 +0,0 @@
|
||||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
mod inst {
|
||||
use dlist::DList;
|
||||
use managed;
|
||||
use option::{Option, Some};
|
||||
use option;
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
pub type IMPL_T<A> = @mut DList<A>;
|
||||
|
||||
/**
|
||||
* 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 pure fn EACH<A>(self: &IMPL_T<A>, f: fn(v: &A) -> bool) {
|
||||
let mut link = self.peek_n();
|
||||
while option::is_some(&link) {
|
||||
let nobe = option::get(link);
|
||||
fail_unless!(nobe.linked);
|
||||
|
||||
{
|
||||
let frozen_nobe = &*nobe;
|
||||
if !f(&frozen_nobe.data) { break; }
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn SIZE_HINT<A>(self: &IMPL_T<A>) -> Option<uint> {
|
||||
Some(self.len())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user