diff --git a/src/libcore/core.rc b/src/libcore/core.rc index 79a5d297178e3e1644fa3fc9b00ec61fd3417a7c..42312d96d1becef1313d280581891bbaf4fbfb1c 100644 --- a/src/libcore/core.rc +++ b/src/libcore/core.rc @@ -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; diff --git a/src/libcore/dlist.rs b/src/libcore/dlist.rs index 5654d4b9c9b5806a2eec897829c35459cd9f6976..9cb872a0542a135f1cd7bf891c8c86af0f46977d 100644 --- a/src/libcore/dlist.rs +++ b/src/libcore/dlist.rs @@ -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 { 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 { } } +impl BaseIter for @mut DList { + /** + * 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 { Some(self.len()) } +} + #[cfg(test)] mod tests { use dlist::{DList, concat, from_vec, new_dlist_node}; diff --git a/src/libcore/iter-trait.rs b/src/libcore/iter-trait.rs deleted file mode 100644 index 7c4a99133bbdbff0fcc34e1884367e850ddab17a..0000000000000000000000000000000000000000 --- a/src/libcore/iter-trait.rs +++ /dev/null @@ -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 or the MIT license -// , 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 iter::BaseIter for IMPL_T { - #[inline(always)] - pure fn each(&self, blk: fn(v: &A) -> bool) { EACH(self, blk) } - #[inline(always)] - pure fn size_hint(&self) -> Option { SIZE_HINT(self) } -} - -impl iter::ExtendedIter for IMPL_T { - #[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(&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 { - iter::position(self, f) - } - #[inline(always)] - pure fn map_to_vec(&self, op: fn(&A) -> B) -> ~[B] { - iter::map_to_vec(self, op) - } - #[inline(always)] - pure fn flat_map_to_vec>(&self, op: fn(&A) -> IB) - -> ~[B] { - iter::flat_map_to_vec(self, op) - } - -} - -impl iter::EqIter for IMPL_T { - #[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 iter::CopyableIter for IMPL_T { - #[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 { - iter::find(self, f) - } -} - -impl iter::CopyableOrderedIter for IMPL_T { - #[inline(always)] - pure fn min(&self) -> A { iter::min(self) } - #[inline(always)] - pure fn max(&self) -> A { iter::max(self) } -} - diff --git a/src/libcore/iter-trait/dlist.rs b/src/libcore/iter-trait/dlist.rs deleted file mode 100644 index 245c1db2bb4b8c5eddf4ae93911480a7bae9e2e5..0000000000000000000000000000000000000000 --- a/src/libcore/iter-trait/dlist.rs +++ /dev/null @@ -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 or the MIT license -// , 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 = @mut DList; - - /** - * 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(self: &IMPL_T, 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(self: &IMPL_T) -> Option { - Some(self.len()) - } -}