vec-arena/examples/linked-list.rs

139 lines
3.0 KiB
Rust
Raw Normal View History

2016-11-14 19:41:37 +00:00
use vec_arena::Arena;
/// The null index, akin to null pointers.
///
/// Just like a null pointer indicates an address no object is ever stored at,
/// the null index indicates an index no object is ever stored at.
///
/// Number `!0` is the largest possible value representable by `usize`.
2016-10-28 11:07:52 +00:00
const NULL: usize = !0;
2016-10-28 00:31:23 +00:00
struct Node<T> {
2016-11-14 19:41:37 +00:00
/// Previous node in the list.
2016-10-28 00:31:23 +00:00
prev: usize,
2016-11-14 19:41:37 +00:00
/// Next node in the list.
2016-10-28 00:31:23 +00:00
next: usize,
2016-11-14 19:41:37 +00:00
/// Actual value stored in node.
2016-10-28 00:31:23 +00:00
value: T,
}
struct List<T> {
2016-11-14 19:41:37 +00:00
/// This is where nodes are stored.
arena: Arena<Node<T>>,
/// First node in the list.
2016-10-28 00:31:23 +00:00
head: usize,
2016-11-14 19:41:37 +00:00
/// Last node in the list.
2016-10-28 00:31:23 +00:00
tail: usize,
}
impl<T> List<T> {
2016-11-14 19:41:37 +00:00
/// Constructs a new, empty doubly linked list.
2016-10-28 00:31:23 +00:00
fn new() -> Self {
List {
2016-11-14 19:41:37 +00:00
arena: Arena::new(),
2016-10-28 11:07:52 +00:00
head: NULL,
tail: NULL,
2016-10-28 00:31:23 +00:00
}
}
2016-11-14 19:41:37 +00:00
/// Returns the number of elements in the list.
2016-10-28 00:31:23 +00:00
fn len(&self) -> usize {
self.arena.len()
}
2016-11-14 19:41:37 +00:00
/// Links nodes `a` and `b` together, so that `a` comes before `b` in the list.
2016-10-28 00:31:23 +00:00
fn link(&mut self, a: usize, b: usize) {
if a != NULL {
self.arena[a].next = b;
}
if b != NULL {
self.arena[b].prev = a;
}
2016-10-28 00:31:23 +00:00
}
2016-11-14 19:41:37 +00:00
/// Appends `value` to the back of the list.
2016-10-28 00:31:23 +00:00
fn push_back(&mut self, value: T) -> usize {
2016-11-14 19:41:37 +00:00
let node = self.arena.insert(Node {
prev: NULL,
next: NULL,
value: value,
});
2016-10-28 00:31:23 +00:00
let tail = self.tail;
self.link(tail, node);
self.tail = node;
2016-10-28 11:07:52 +00:00
if self.head == NULL {
2016-10-28 00:31:23 +00:00
self.head = node;
}
node
}
2016-11-14 19:41:37 +00:00
/// Pops and returns the value at the front of the list.
2016-10-28 00:31:23 +00:00
fn pop_front(&mut self) -> T {
2016-11-14 19:41:37 +00:00
let node = self.arena.remove(self.head).unwrap();
2016-10-28 00:31:23 +00:00
2016-10-28 11:07:52 +00:00
self.link(NULL, node.next);
2016-10-28 00:31:23 +00:00
self.head = node.next;
2016-10-28 11:07:52 +00:00
if node.next == NULL {
self.tail = NULL;
2016-10-28 00:31:23 +00:00
}
node.value
}
2016-11-14 19:41:37 +00:00
/// Removes the element specified by `index`.
2016-10-28 00:31:23 +00:00
fn remove(&mut self, index: usize) -> T {
2016-11-14 19:41:37 +00:00
let node = self.arena.remove(index).unwrap();
2016-10-28 00:31:23 +00:00
self.link(node.prev, node.next);
if self.head == index {
self.head = node.next;
}
if self.tail == index {
self.tail = node.prev;
}
2016-10-28 00:31:23 +00:00
node.value
}
}
2016-10-26 11:18:16 +00:00
fn main() {
2016-10-28 00:31:23 +00:00
let mut list = List::new();
2016-11-14 19:41:37 +00:00
// The list is now [].
2016-10-28 00:31:23 +00:00
let one = list.push_back(1);
list.push_back(2);
list.push_back(3);
2016-11-14 19:41:37 +00:00
// The list is now [1, 2, 3].
2016-10-28 00:31:23 +00:00
list.push_back(10);
let twenty = list.push_back(20);
list.push_back(30);
2016-11-14 19:41:37 +00:00
// The list is now [1, 2, 3, 10, 20, 30].
2016-10-28 00:31:23 +00:00
assert!(list.len() == 6);
assert!(list.remove(one) == 1);
assert!(list.remove(twenty) == 20);
2016-11-14 19:41:37 +00:00
// The list is now [2, 3, 10, 30].
2016-10-28 00:31:23 +00:00
assert!(list.len() == 4);
assert!(list.pop_front() == 2);
assert!(list.pop_front() == 3);
assert!(list.pop_front() == 10);
assert!(list.pop_front() == 30);
2016-11-14 19:41:37 +00:00
// The list is now [].
2016-10-28 00:31:23 +00:00
assert!(list.len() == 0);
2016-10-26 11:18:16 +00:00
}