Commit 54fac573 authored by SirBlueRabbit's avatar SirBlueRabbit
Browse files

separate splice

parent 88f9e132
......@@ -173,30 +173,24 @@ impl<V> Pool<Node<V>> {
}
pub fn depth(&self, u: Label<Node<V>>) -> usize {
let mut label = u;
let mut x = u;
let mut depth: isize = 0;
loop {
let d_depth = self[label].d_depth as isize;
if self.parent(label).is_some() {
let d_flip = self[self.parent(label).unwrap()].d_flip;
let child_type = self.child_type(label);
match child_type {
ChildType::Left => depth -= d_depth,
ChildType::Right => depth += d_depth,
ChildType::Path => depth += d_depth,
ChildType::Root => unreachable!(),
}
if d_flip {
depth = -depth;
}
label = self.parent(label).unwrap();
} else {
return (depth + d_depth).abs() as usize;
while let Some(p) = self.parent(x) {
let d_depth = self[x].d_depth as isize;
match self.child_type(x) {
ChildType::Left => depth -= d_depth,
ChildType::Right => depth += d_depth,
ChildType::Path => depth += d_depth,
ChildType::Root => unreachable!(),
}
if self[p].d_flip {
depth = -depth;
}
x = p;
}
let d_depth = self[x].d_depth as isize;
(depth + d_depth).abs() as usize
}
// always expose before indexing
......@@ -231,23 +225,25 @@ impl<V> Pool<Node<V>> {
}
fn expose(&mut self, u: Label<Node<V>>) {
self.splay(u);
if let Some(c) = self[u].children[ChildType::from(true)] {
self[c].parent = Parent::Path(u);
self[u].children[ChildType::from(true)] = None;
}
self.splice(u, None);
while let Parent::Path(p) = self[u].parent {
self.splay(p);
if let Some(x) = self[p].children[ChildType::from(true)] {
self[x].parent = Parent::Path(p);
}
self[p].children[ChildType::from(true)] = Some(u);
self[u].parent = Parent::BinTree(p);
self.splice(p, Some(u));
self.rotate(u);
}
}
fn splice(&mut self, u: Label<Node<V>>, c_opt: Option<Label<Node<V>>>) {
self.splay(u);
if let Some(x) = self[u].children[ChildType::from(true)] {
self[x].parent = Parent::Path(u);
}
if let Some(c) = c_opt {
self[c].parent = Parent::BinTree(u);
}
self[u].children[ChildType::from(true)] = c_opt;
}
fn splay(&mut self, u: Label<Node<V>>) {
self.cascade_flipped(u);
while let Parent::BinTree(p) = self[u].parent {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment