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