// Backlinks over List::prev are raw pointers that form a full chain in
// the reverse direction.
-
use std::cast;
use std::cmp;
use std::ptr;
use std::util;
use std::iterator::FromIterator;
+use container::Deque;
+
/// A doubly-linked list
pub struct List<T> {
priv length: uint,
}
}
-impl<T> List<T> {
- /// Create an empty List
- #[inline]
- pub fn new() -> List<T> {
- List{list_head: None, list_tail: Rawlink::none(), length: 0}
- }
-
+impl<T> Deque<T> for List<T> {
/// Provide a reference to the front element, or None if the list is empty
- pub fn peek_front<'a>(&'a self) -> Option<&'a T> {
+ fn front<'a>(&'a self) -> Option<&'a T> {
self.list_head.chain_ref(|x| Some(&x.value))
}
/// Provide a mutable reference to the front element, or None if the list is empty
- pub fn peek_front_mut<'a>(&'a mut self) -> Option<&'a mut T> {
+ fn front_mut<'a>(&'a mut self) -> Option<&'a mut T> {
match self.list_head {
None => None,
Some(ref mut head) => Some(&mut head.value),
}
/// Provide a reference to the back element, or None if the list is empty
- pub fn peek_back<'a>(&'a self) -> Option<&'a T> {
+ fn back<'a>(&'a self) -> Option<&'a T> {
match self.list_tail.resolve_immut() {
None => None,
Some(tail) => Some(&tail.value),
}
/// Provide a mutable reference to the back element, or None if the list is empty
- pub fn peek_back_mut<'a>(&'a mut self) -> Option<&'a mut T> {
+ fn back_mut<'a>(&'a mut self) -> Option<&'a mut T> {
match self.list_tail.resolve() {
None => None,
Some(tail) => Some(&mut tail.value),
/// Add an element last in the list
///
/// O(1)
- pub fn push_back(&mut self, elt: T) {
+ fn push_back(&mut self, elt: T) {
match self.list_tail.resolve() {
None => return self.push_front(elt),
Some(tail) => {
///
/// O(1)
#[inline]
- pub fn pop_back(&mut self) -> Option<T> {
+ fn pop_back(&mut self) -> Option<T> {
match self.list_tail.resolve() {
None => None,
Some(tail) => {
/// Add an element first in the list
///
/// O(1)
- pub fn push_front(&mut self, elt: T) {
+ fn push_front(&mut self, elt: T) {
let mut new_head = ~Node{value: elt, next: None, prev: Rawlink::none()};
match self.list_head {
None => {
/// Remove the first element and return it, or None if the list is empty
///
/// O(1)
- pub fn pop_front(&mut self) -> Option<T> {
+ fn pop_front(&mut self) -> Option<T> {
match util::replace(&mut self.list_head, None) {
None => None,
Some(old_head) => {
}
}
}
+}
+
+impl<T> List<T> {
+ /// Create an empty List
+ #[inline]
+ pub fn new() -> List<T> {
+ List{list_head: None, list_tail: Rawlink::none(), length: 0}
+ }
/// Add all elements from `other` to the end of the list
///
{
let mut it = self.mut_iter();
loop {
- match (it.next(), other.peek_front()) {
+ match (it.next(), other.front()) {
(None , _ ) => break,
(_ , None ) => return,
(Some(x), Some(y)) => if f(x, y) { loop }
fn peek_next<'a>(&'a mut self) -> Option<&'a mut A> {
match self.curs.resolve() {
- None => self.list.peek_front_mut(),
+ None => self.list.front_mut(),
Some(curs) => match curs.next {
None => None,
Some(ref mut node) => Some(&mut node.value),
n.push_front(2);
n.push_front(3);
{
- assert_eq!(n.peek_front().unwrap(), &3);
- let x = n.peek_front_mut().unwrap();
+ assert_eq!(n.front().unwrap(), &3);
+ let x = n.front_mut().unwrap();
assert_eq!(*x, 3);
*x = 0;
}
{
- assert_eq!(n.peek_back().unwrap(), &2);
- let y = n.peek_back_mut().unwrap();
+ assert_eq!(n.back().unwrap(), &2);
+ let y = n.back_mut().unwrap();
assert_eq!(*y, 2);
*y = 1;
}