Skip to content

Commit

Permalink
implement BaseIter for dlist (removing iter-trait)
Browse files Browse the repository at this point in the history
  • Loading branch information
thestinger committed Mar 8, 2013
1 parent eaed16c commit b69fb75
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 151 deletions.
2 changes: 0 additions & 2 deletions src/libcore/core.rc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
44 changes: 43 additions & 1 deletion src/libcore/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -489,14 +491,54 @@ 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;
}
}
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.
*/
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};
Expand Down
89 changes: 0 additions & 89 deletions src/libcore/iter-trait.rs

This file was deleted.

59 changes: 0 additions & 59 deletions src/libcore/iter-trait/dlist.rs

This file was deleted.

0 comments on commit b69fb75

Please sign in to comment.