diff options
| author | Daniel Micay <danielmicay@gmail.com> | 2013-03-08 18:19:30 -0500 |
|---|---|---|
| committer | Daniel Micay <danielmicay@gmail.com> | 2013-03-08 18:19:30 -0500 |
| commit | b69fb75348b5aeb0988489d748373b1900f2766b (patch) | |
| tree | c7ceae356b8474e562c3e031cb3c6fd73af856e8 | |
| parent | eaed16cea6c83c49bfb3d4a663f9b715f86d1f37 (diff) | |
| download | rust-b69fb75348b5aeb0988489d748373b1900f2766b.tar.gz rust-b69fb75348b5aeb0988489d748373b1900f2766b.zip | |
implement BaseIter for dlist (removing iter-trait)
Closes #2827
| -rw-r--r-- | src/libcore/core.rc | 2 | ||||
| -rw-r--r-- | src/libcore/dlist.rs | 44 | ||||
| -rw-r--r-- | src/libcore/iter-trait.rs | 89 | ||||
| -rw-r--r-- | src/libcore/iter-trait/dlist.rs | 59 |
4 files changed, 43 insertions, 151 deletions
diff --git a/src/libcore/core.rc b/src/libcore/core.rc index 79a5d297178..42312d96d1b 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 5654d4b9c9b..9cb872a0542 100644 --- a/src/libcore/dlist.rs +++ b/src/libcore/dlist.rs @@ -18,6 +18,8 @@ Do not use ==, !=, <, etc on doubly-linked lists -- it may not terminate. */ +use iter; +use iter::BaseIter; use kinds::Copy; use managed; use option::{None, Option, Some}; @@ -489,7 +491,7 @@ pub impl<T:Copy> DList<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 @@ pub impl<T:Copy> DList<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}; diff --git a/src/libcore/iter-trait.rs b/src/libcore/iter-trait.rs deleted file mode 100644 index 7c4a99133bb..00000000000 --- 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 <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) } -} - diff --git a/src/libcore/iter-trait/dlist.rs b/src/libcore/iter-trait/dlist.rs deleted file mode 100644 index 245c1db2bb4..00000000000 --- 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 <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()) - } -} |
