提交 8d06efb8 编写于 作者: B blake2-ppc

dlist: Collect a common pattern into link_with_prev()

上级 7b1c5771
...@@ -98,6 +98,12 @@ fn resolve(&mut self) -> Option<&mut T> { ...@@ -98,6 +98,12 @@ fn resolve(&mut self) -> Option<&mut T> {
} }
} }
/// Set the .prev field on `next`, then return `Some(next)`
fn link_with_prev<T>(mut next: ~Node<T>, prev: Rawlink<Node<T>>) -> Link<T> {
next.prev = prev;
Some(next)
}
impl<T> Container for List<T> { impl<T> Container for List<T> {
/// O(1) /// O(1)
fn is_empty(&self) -> bool { fn is_empty(&self) -> bool {
...@@ -216,20 +222,17 @@ pub fn push_front(&mut self, elt: T) { ...@@ -216,20 +222,17 @@ pub fn push_front(&mut self, elt: T) {
/// ///
/// O(1) /// O(1)
pub fn pop_front(&mut self) -> Option<T> { pub fn pop_front(&mut self) -> Option<T> {
match self.list_head { match util::replace(&mut self.list_head, None) {
None => None, None => None,
ref mut head @ Some(*) => { Some(old_head) => {
self.length -= 1; self.length -= 1;
match *head.swap_unwrap() { match *old_head {
Node{value: value, next: Some(next), prev: _} => { Node{value: value, next: Some(next), prev: _} => {
let mut mnext = next; self.list_head = link_with_prev(next, Rawlink::none());
mnext.prev = Rawlink::none();
*head = Some(mnext);
Some(value) Some(value)
} }
Node{value: value, next: None, prev: _} => { Node{value: value, next: None, prev: _} => {
self.list_tail = Rawlink::none(); self.list_tail = Rawlink::none();
*head = None;
Some(value) Some(value)
} }
} }
...@@ -247,9 +250,7 @@ pub fn append(&mut self, other: List<T>) { ...@@ -247,9 +250,7 @@ pub fn append(&mut self, other: List<T>) {
match other { match other {
List{list_head: None, list_tail: _, length: _} => return, List{list_head: None, list_tail: _, length: _} => return,
List{list_head: Some(node), list_tail: o_tail, length: o_length} => { List{list_head: Some(node), list_tail: o_tail, length: o_length} => {
let mut lnk_node = node; tail.next = link_with_prev(node, self.list_tail);
lnk_node.prev = self.list_tail;
tail.next = Some(lnk_node);
self.list_tail = o_tail; self.list_tail = o_tail;
self.length += o_length; self.length += o_length;
} }
...@@ -447,13 +448,10 @@ fn insert_before(&mut self, elt: A) { ...@@ -447,13 +448,10 @@ fn insert_before(&mut self, elt: A) {
None => return self.list.push_front(elt), // at head None => return self.list.push_front(elt), // at head
Some(prev) => prev, Some(prev) => prev,
}; };
let mut node_own = prev_node.next.swap_unwrap(); let mut ins_node = ~Node{value: elt, next: None, prev: Rawlink::none()};
let mut ins_node = ~Node{value: elt, let node_own = prev_node.next.swap_unwrap();
next: None, ins_node.next = link_with_prev(node_own, Rawlink::some(ins_node));
prev: Rawlink::some(prev_node)}; prev_node.next = link_with_prev(ins_node, Rawlink::some(prev_node));
node_own.prev = Rawlink::some(ins_node);
ins_node.next = Some(node_own);
prev_node.next = Some(ins_node);
self.list.length += 1; self.list.length += 1;
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册