Skip to content

Commit 00f38cc

Browse files
author
blake2-ppc
committed
---
yaml --- r: 64213 b: refs/heads/snap-stage3 c: 92842d6 h: refs/heads/master i: 64211: 7a6e436 v: v3
1 parent e5e6c8f commit 00f38cc

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 2d28d645422c1617be58c8ca7ad9a457264ca850
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 4fa69ab97ca4a5d570eec28c0ef979cd829686c6
4+
refs/heads/snap-stage3: 92842d6516d2c5d92bad553585340f2ae3284308
55
refs/heads/try: 7b78b52e602bb3ea8174f9b2006bff3315f03ef9
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/src/libextra/dlist.rs

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ impl<T> List<T> {
272272
///
273273
/// O(N)
274274
#[inline]
275-
pub fn insert_before(&mut self, elt: T, f: &fn(&T, &T) -> bool) {
275+
pub fn insert_when(&mut self, elt: T, f: &fn(&T, &T) -> bool) {
276276
{
277277
let mut it = self.mut_iter();
278278
loop {
@@ -341,7 +341,7 @@ impl<T> List<T> {
341341
/// O(N)
342342
impl<T: cmp::TotalOrd> List<T> {
343343
fn insert_ordered(&mut self, elt: T) {
344-
self.insert_before(elt, |a, b| a.cmp(b) != cmp::Less);
344+
self.insert_when(elt, |a, b| a.cmp(b) != cmp::Less);
345345
}
346346
}
347347

@@ -363,7 +363,7 @@ impl<'self, A> Iterator<&'self A> for ForwardIterator<'self, A> {
363363
}
364364
}
365365

366-
// MutForwardIterator is different because it implements ListInsertCursor,
366+
// MutForwardIterator is different because it implements ListInsertion,
367367
// and can modify the list during traversal, used in insert_when and merge.
368368
impl<'self, A> Iterator<&'self mut A> for MutForwardIterator<'self, A> {
369369
#[inline]
@@ -433,19 +433,22 @@ impl<'self, A> Iterator<&'self mut A> for MutReverseIterator<'self, A> {
433433
}
434434
}
435435

436-
// XXX: Should this be `pub`?
437-
trait ListInsertCursor<A> {
436+
/// Allow mutating the List while iterating
437+
pub trait ListInsertion<A> {
438438
/// Insert `elt` just previous to the most recently yielded element
439439
fn insert_before(&mut self, elt: A);
440+
441+
/// Provide a reference to the next element, without changing the iterator
442+
fn peek_next<'a>(&'a mut self) -> Option<&'a mut A>;
440443
}
441444

442-
impl<'self, A> ListInsertCursor<A> for MutForwardIterator<'self, A> {
445+
impl<'self, A> ListInsertion<A> for MutForwardIterator<'self, A> {
443446
fn insert_before(&mut self, elt: A) {
444447
match self.curs.resolve() {
445-
None => self.list.push_front(elt),
448+
None => { self.list.push_front(elt); self.next(); }
446449
Some(node) => {
447450
let prev_node = match node.prev.resolve() {
448-
None => return self.list.push_front(elt), // at head
451+
None => return self.list.push_front(elt),
449452
Some(prev) => prev,
450453
};
451454
let mut ins_node = ~Node{value: elt, next: None, prev: Rawlink::none()};
@@ -456,6 +459,16 @@ impl<'self, A> ListInsertCursor<A> for MutForwardIterator<'self, A> {
456459
}
457460
}
458461
}
462+
463+
fn peek_next<'a>(&'a mut self) -> Option<&'a mut A> {
464+
match self.curs.resolve() {
465+
None => self.list.peek_front_mut(),
466+
Some(curs) => match curs.next {
467+
None => None,
468+
Some(ref mut node) => Some(&mut node.value),
469+
}
470+
}
471+
}
459472
}
460473

461474
impl<A> Iterator<A> for ConsumeIterator<A> {
@@ -695,20 +708,30 @@ mod tests {
695708
}
696709

697710
#[test]
698-
fn test_list_cursor() {
699-
let mut m = generate_test();
711+
fn test_insert_prev() {
712+
let mut m = list_from(&[0,2,4,6,8]);
700713
let len = m.len();
701714
{
702715
let mut it = m.mut_iter();
716+
it.insert_before(-2);
703717
loop {
704718
match it.next() {
705719
None => break,
706-
Some(elt) => it.insert_before(*elt * 2),
720+
Some(elt) => {
721+
it.insert_before(*elt + 1);
722+
match it.peek_next() {
723+
Some(x) => assert_eq!(*x, *elt + 2),
724+
None => assert_eq!(8, *elt),
725+
}
726+
}
707727
}
708728
}
729+
it.insert_before(0);
730+
it.insert_before(1);
709731
}
710-
assert_eq!(m.len(), len * 2);
711732
check_links(&m);
733+
assert_eq!(m.len(), 3 + len * 2);
734+
assert_eq!(m.consume_iter().collect::<~[int]>(), ~[-2,1,0,3,2,5,4,7,6,9,0,1,8]);
712735
}
713736

714737
#[test]

0 commit comments

Comments
 (0)