Commit 369f3e1b authored by tgerstel's avatar tgerstel
Browse files

implement first_ancestor

parent 5f4dda3b
......@@ -6,8 +6,8 @@ use super::collections::{Label, Pool};
pub struct Node<V> {
value: V,
parent: Parent<V>,
flipped: bool,
children: Children<V>,
flipped: bool,
}
#[derive(Debug, Clone, Copy, PartialEq)]
......@@ -45,8 +45,8 @@ impl<V: Clone> Pool<Node<V>> {
self.insert(Node {
value,
parent: Parent::Root,
flipped: false,
children: Children(None, None),
flipped: false,
})
}
......@@ -54,7 +54,7 @@ impl<V: Clone> Pool<Node<V>> {
self.expose(v);
let mut r = v;
let mut flipped = self[v].flipped;
while let Some(w) = self[r].children[(flipped)] {
while let Some(w) = self[r].children[flipped] {
r = w;
flipped ^= self[w].flipped;
}
......@@ -65,17 +65,17 @@ impl<V: Clone> Pool<Node<V>> {
pub fn cut(&mut self, v: Label<Node<V>>) {
self.expose(v);
let flipped = self[v].flipped;
if let Some(l) = self[v].children[(flipped)] {
if let Some(l) = self[v].children[flipped] {
self[l].parent = Parent::Root;
self[v].children[(flipped)] = None;
self[v].children[flipped] = None;
}
}
pub fn link(&mut self, p: Label<Node<V>>, c: Label<Node<V>>) {
self.expose(c);
self.expose(p);
let child_flipped = self[c].flipped;
self[c].children[(child_flipped)] = Some(p);
let c_flipped = self[c].flipped;
self[c].children[c_flipped] = Some(p);
self[p].parent = Parent::BinTree(c);
}
......@@ -84,6 +84,24 @@ impl<V: Clone> Pool<Node<V>> {
self[v].flipped = !self[v].flipped;
}
pub fn first_ancestor(&mut self, v: Label<Node<V>>, w: Label<Node<V>>) -> Label<Node<V>> {
self.expose(v);
self.expose(w);
let mut x = v;
while let Parent::BinTree(p) = self[x].parent {
if x == w {
return w;
}
x = p;
}
match self[x].parent {
Parent::BinTree(_) => unreachable!(),
Parent::Path(p) => p,
Parent::Root => v,
}
}
pub fn value_mut(&mut self, v: Label<Node<V>>) -> &mut V {
&mut self[v].value
}
......@@ -151,12 +169,12 @@ impl<V: Clone> Pool<Node<V>> {
if let Parent::BinTree(p) = self[v].parent {
if let Parent::BinTree(g) = self[p].parent {
let g_side = self.side(p);
self[g].children[(g_side)] = Some(v);
self[g].children[g_side] = Some(v);
}
let b_opt = self[v].children[(!p_side)];
self[p].children[(p_side)] = b_opt;
self[v].children[(!p_side)] = Some(p);
let b_opt = self[v].children[!p_side];
self[p].children[p_side] = b_opt;
self[v].children[!p_side] = Some(p);
if let Some(b) = b_opt {
self[b].parent = Parent::BinTree(p);
......
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