diff options
| author | blake2-ppc <blake2-ppc> | 2013-07-10 03:49:32 +0200 |
|---|---|---|
| committer | blake2-ppc <blake2-ppc> | 2013-07-11 15:54:34 +0200 |
| commit | 8d06efb8ea0857844f856ab5fd87aed89d4bf718 (patch) | |
| tree | 49c1435f9e2c6bc974f6fea0b5fbd18b9b109301 /src/libextra | |
| parent | 7b1c57713d331266d632c4fa11d4cdfaaa895ac7 (diff) | |
| download | rust-8d06efb8ea0857844f856ab5fd87aed89d4bf718.tar.gz rust-8d06efb8ea0857844f856ab5fd87aed89d4bf718.zip | |
dlist: Collect a common pattern into link_with_prev()
Diffstat (limited to 'src/libextra')
| -rw-r--r-- | src/libextra/dlist.rs | 32 |
1 files changed, 15 insertions, 17 deletions
diff --git a/src/libextra/dlist.rs b/src/libextra/dlist.rs index b197ea83fdc..46a3a7d6e15 100644 --- a/src/libextra/dlist.rs +++ b/src/libextra/dlist.rs @@ -98,6 +98,12 @@ impl<T> Rawlink<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> { /// O(1) fn is_empty(&self) -> bool { @@ -216,20 +222,17 @@ impl<T> List<T> { /// /// O(1) pub fn pop_front(&mut self) -> Option<T> { - match self.list_head { + match util::replace(&mut self.list_head, None) { None => None, - ref mut head @ Some(*) => { + Some(old_head) => { self.length -= 1; - match *head.swap_unwrap() { + match *old_head { Node{value: value, next: Some(next), prev: _} => { - let mut mnext = next; - mnext.prev = Rawlink::none(); - *head = Some(mnext); + self.list_head = link_with_prev(next, Rawlink::none()); Some(value) } Node{value: value, next: None, prev: _} => { self.list_tail = Rawlink::none(); - *head = None; Some(value) } } @@ -247,9 +250,7 @@ impl<T> List<T> { match other { List{list_head: None, list_tail: _, length: _} => return, List{list_head: Some(node), list_tail: o_tail, length: o_length} => { - let mut lnk_node = node; - lnk_node.prev = self.list_tail; - tail.next = Some(lnk_node); + tail.next = link_with_prev(node, self.list_tail); self.list_tail = o_tail; self.length += o_length; } @@ -447,13 +448,10 @@ impl<'self, A> ListInsertCursor<A> for MutForwardIterator<'self, A> { None => return self.list.push_front(elt), // at head Some(prev) => prev, }; - let mut node_own = prev_node.next.swap_unwrap(); - let mut ins_node = ~Node{value: elt, - next: None, - prev: Rawlink::some(prev_node)}; - node_own.prev = Rawlink::some(ins_node); - ins_node.next = Some(node_own); - prev_node.next = Some(ins_node); + let mut ins_node = ~Node{value: elt, next: None, prev: Rawlink::none()}; + let node_own = prev_node.next.swap_unwrap(); + ins_node.next = link_with_prev(node_own, Rawlink::some(ins_node)); + prev_node.next = link_with_prev(ins_node, Rawlink::some(prev_node)); self.list.length += 1; } } |
