diff --git a/examples/linked_list.rs b/examples/linked_list.rs index 36b1ee7..a25c1c8 100644 --- a/examples/linked_list.rs +++ b/examples/linked_list.rs @@ -2,6 +2,8 @@ extern crate vec_arena; use vec_arena::VecArena; +const NULL: usize = !0; + struct Node { prev: usize, next: usize, @@ -18,8 +20,8 @@ impl List { fn new() -> Self { List { arena: VecArena::new(), - head: !0, - tail: !0, + head: NULL, + tail: NULL, } } @@ -29,15 +31,15 @@ impl List { fn new_node(&mut self, value: T) -> usize { self.arena.insert(Node { - prev: !0, - next: !0, + prev: NULL, + next: NULL, value: value, }) } fn link(&mut self, a: usize, b: usize) { - if a != !0 { self.arena[a].next = b; } - if b != !0 { self.arena[b].prev = a; } + if a != NULL { self.arena[a].next = b; } + if b != NULL { self.arena[b].prev = a; } } fn push_back(&mut self, value: T) -> usize { @@ -47,7 +49,7 @@ impl List { self.link(tail, node); self.tail = node; - if self.head == !0 { + if self.head == NULL { self.head = node; } node @@ -56,10 +58,10 @@ impl List { fn pop_front(&mut self) -> T { let node = self.arena.remove(self.head); - self.link(!0, node.next); + self.link(NULL, node.next); self.head = node.next; - if node.next == !0 { - self.tail = !0; + if node.next == NULL { + self.tail = NULL; } node.value } diff --git a/examples/splay_tree.rs b/examples/splay_tree.rs index fdee7dd..cb77384 100644 --- a/examples/splay_tree.rs +++ b/examples/splay_tree.rs @@ -2,6 +2,9 @@ extern crate vec_arena; use vec_arena::VecArena; +/// The null index, akin to null pointers. +const NULL: usize = !0; + struct Node { parent: usize, children: [usize; 2], @@ -11,8 +14,8 @@ struct Node { impl Node { fn new(value: T) -> Node { Node { - parent: !0, - children: [!0, !0], + parent: NULL, + children: [NULL, NULL], value: value, } } @@ -28,7 +31,7 @@ impl Splay where T: Ord { fn new() -> Splay { Splay { arena: VecArena::new(), - root: !0, + root: NULL, } } @@ -36,7 +39,7 @@ impl Splay where T: Ord { #[inline(always)] fn link(&mut self, p: usize, c: usize, dir: usize) { self.arena[p].children[dir] = c; - if c != !0 { + if c != NULL { self.arena[c].parent = p; } } @@ -61,10 +64,10 @@ impl Splay where T: Ord { self.link(p, t, dir); self.link(c, p, dir ^ 1); - if g == !0 { + if g == NULL { // There is no grandparent, so `c` becomes the root. self.root = c; - self.arena[c].parent = !0; + self.arena[c].parent = NULL; } else { // Link `g` and `c` together. let dir = if self.arena[g].children[0] == p { 0 } else { 1 }; @@ -82,14 +85,14 @@ impl Splay where T: Ord { // Find the parent. let p = self.arena[c].parent; - if p == !0 { + if p == NULL { // There is no parent. That means `c` is the root. break; } // Find the grandparent. let g = self.arena[p].parent; - if g == !0 { + if g == NULL { // There is no grandparent. Just one rotation is left. // Zig step. self.rotate(p, c); @@ -117,7 +120,7 @@ impl Splay where T: Ord { let n = self.arena.insert(Node::new(value)); - if self.root == !0 { + if self.root == NULL { self.root = n; } else { let mut p = self.root; @@ -126,7 +129,7 @@ impl Splay where T: Ord { let dir = if self.arena[n].value < self.arena[p].value { 0 } else { 1 }; let c = self.arena[p].children[dir]; - if c == !0 { + if c == NULL { self.link(p, n, dir); self.splay(n); break; @@ -138,7 +141,7 @@ impl Splay where T: Ord { /// Pretty-prints the subtree rooted at `node`, indented by `depth` spaces. fn print(&self, node: usize, depth: usize) where T: std::fmt::Display { - if node != !0 { + if node != NULL { // Print the left subtree. self.print(self.arena[node].children[0], depth + 1);