Commit 007bf628 authored by SirBlueRabbit's avatar SirBlueRabbit
Browse files

fix basic sequence test

parent bafd9542
......@@ -48,8 +48,8 @@ pub struct Node<V> {
value: V,
parent: Parent<V>,
children: Children<V>,
flipped: bool,
depth: usize,
d_flip: bool,
d_depth: usize,
}
impl<V> Pool<Node<V>> {
......@@ -58,18 +58,18 @@ impl<V> Pool<Node<V>> {
value,
parent: Parent::Root,
children: Children(None, None),
flipped: false,
depth: 0,
d_flip: false,
d_depth: 0,
})
}
pub fn find_root(&mut self, u: Label<Node<V>>) -> Label<Node<V>> {
self.expose(u);
let mut x = u;
let mut flipped = self[u].flipped;
while let Some(y) = self[x].children[flipped] {
let mut d_flip = self[u].d_flip;
while let Some(y) = self[x].children[d_flip] {
x = y;
flipped ^= self[x].flipped;
d_flip ^= self[x].d_flip;
}
self.expose(x);
x
......@@ -77,30 +77,30 @@ impl<V> Pool<Node<V>> {
pub fn cut(&mut self, c: Label<Node<V>>) {
self.expose(c);
let flipped = self[c].flipped;
if let Some(p) = self[c].children[flipped] {
let d_flip = self[c].d_flip;
if let Some(p) = self[c].children[d_flip] {
self[p].parent = Parent::Root;
self[c].children[flipped] = None;
self[c].children[d_flip] = None;
self[p].depth = self[c].depth - 1;
self[c].depth = 0;
self[p].d_depth = self[c].d_depth - 1;
self[c].d_depth = 0;
}
}
pub fn link(&mut self, p: Label<Node<V>>, c: Label<Node<V>>) {
self.expose(c);
self.expose(p);
let flipped = self[c].flipped;
self[c].children[flipped] = Some(p);
let d_flip = self[c].d_flip;
self[c].children[d_flip] = Some(p);
self[p].parent = Parent::BinTree(c);
self[c].depth = self[p].depth + 1;
self[p].depth = 1;
self[c].d_depth = self[p].d_depth + 1;
self[p].d_depth = 1;
}
pub fn evert(&mut self, u: Label<Node<V>>) {
self.expose(u);
self[u].flipped = !self[u].flipped;
self[u].d_flip = !self[u].d_flip;
}
pub fn first_ancestor(&mut self, u: Label<Node<V>>, v: Label<Node<V>>) -> Label<Node<V>> {
......@@ -119,31 +119,6 @@ impl<V> Pool<Node<V>> {
}
}
// assume that u is the root of its binary tree
#[cfg(test)]
fn sequence(&self, u: Label<Node<V>>) -> Vec<Label<Node<V>>> {
let mut queue = vec![u];
let mut seq = vec![u];
while let Some(label) = queue.pop() {
if let Parent::BinTree(parent) = self[label].parent {
let index = seq.iter().position(|&label| label == parent).unwrap();
if self.side(label) {
seq.insert(index + 1, label);
} else {
seq.insert(index, label);
}
}
let Children(a_opt, b_opt) = self[label].children;
if let Some(a) = a_opt {
queue.push(a);
}
if let Some(b) = b_opt {
queue.push(b);
}
}
seq
}
pub fn value_mut(&mut self, u: Label<Node<V>>) -> &mut V {
&mut self[u].value
}
......@@ -174,7 +149,7 @@ impl<V> Pool<Node<V>> {
self.cascade_flipped(u);
while let Parent::BinTree(p) = self[u].parent {
if let Parent::BinTree(_) = self[p].parent {
if self[p].flipped == (self.side(u) == self.side(p)) {
if self[p].d_flip == (self.side(u) == self.side(p)) {
self.rotate(p);
self.rotate(u);
} else {
......@@ -193,15 +168,15 @@ impl<V> Pool<Node<V>> {
self.cascade_flipped(p);
}
if self[u].flipped {
if self[u].d_flip {
let Children(a_opt, b_opt) = self[u].children;
self[u].children = Children(b_opt, a_opt);
self[u].flipped = false;
self[u].d_flip = false;
if let Some(a) = a_opt {
self[a].flipped = !self[a].flipped;
self[a].d_flip = !self[a].d_flip;
}
if let Some(b) = b_opt {
self[b].flipped = !self[b].flipped;
self[b].d_flip = !self[b].d_flip;
}
}
}
......@@ -210,23 +185,23 @@ impl<V> Pool<Node<V>> {
let u_side = self.side(u);
let x_opt = self[u].children[!u_side];
if let Parent::BinTree(p) = self[u].parent {
let u_depth = self[u].depth;
let p_depth = self[p].depth;
let u_d_depth = self[u].d_depth;
let p_d_depth = self[p].d_depth;
self[p].depth = u_depth;
self[u].depth = if let Parent::BinTree(_) = self[p].parent {
if self[p].flipped ^ (self.side(u) == self.side(p)) {
u_depth + p_depth
self[p].d_depth = u_d_depth;
self[u].d_depth = if let Parent::BinTree(_) = self[p].parent {
if self[p].d_flip ^ (self.side(u) == self.side(p)) {
u_d_depth + p_d_depth
} else {
u_depth.abs_diff(p_depth)
u_d_depth.abs_diff(p_d_depth)
}
} else {
u_depth.abs_diff(p_depth)
u_d_depth.abs_diff(p_d_depth)
};
if let Some(x) = x_opt {
let x_depth = self[x].depth;
self[x].depth = x_depth.abs_diff(u_depth);
let x_d_depth = self[x].d_depth;
self[x].d_depth = x_d_depth.abs_diff(u_d_depth);
}
if let Parent::BinTree(g) = self[p].parent {
......@@ -259,6 +234,46 @@ impl<V> Pool<Node<V>> {
unreachable!();
}
}
// assume that u is the root of its binary tree
#[cfg(test)]
fn sequence(&self, u: Label<Node<V>>) -> Vec<Label<Node<V>>> {
let mut queue = vec![u];
let mut seq = vec![u];
while let Some(label) = queue.pop() {
if let Parent::BinTree(parent) = self[label].parent {
let index = seq.iter().position(|&label| label == parent).unwrap();
let flipped = self.flipped(label);
if flipped ^ self.side(label) {
seq.insert(index + 1, label);
} else {
seq.insert(index, label);
}
}
let Children(a_opt, b_opt) = self[label].children;
if let Some(a) = a_opt {
queue.push(a);
}
if let Some(b) = b_opt {
queue.push(b);
}
}
seq
}
#[cfg(test)]
fn flipped(&self, u: Label<Node<V>>) -> bool {
let mut flipped = false;
let mut u = u;
loop {
u = match self[u].parent {
Parent::BinTree(p) => p,
Parent::Path(p) => p,
Parent::Root => return flipped,
};
flipped ^= self[u].d_flip;
}
}
}
#[cfg(test)]
......@@ -370,7 +385,7 @@ mod tests {
lct[d].parent = Parent::BinTree(g);
// reverse origin
lct[o].flipped = true;
lct[o].d_flip = true;
// rotate
lct.cascade_flipped(o);
......@@ -396,13 +411,13 @@ mod tests {
assert_eq!(lct[d].parent, Parent::BinTree(g));
// check flipped
assert!(!lct[o].flipped);
assert!(!lct[p].flipped);
assert!(!lct[g].flipped);
assert!(lct[a].flipped);
assert!(lct[b].flipped);
assert!(!lct[c].flipped);
assert!(!lct[d].flipped);
assert!(!lct[o].d_flip);
assert!(!lct[p].d_flip);
assert!(!lct[g].d_flip);
assert!(lct[a].d_flip);
assert!(lct[b].d_flip);
assert!(!lct[c].d_flip);
assert!(!lct[d].d_flip);
}
#[test]
......@@ -533,7 +548,9 @@ mod tests {
lct[c].parent = Parent::BinTree(p);
lct[d].parent = Parent::BinTree(g);
dbg!(lct.sequence(g));
assert!(false);
let seq1 = lct.sequence(g);
lct.splay(o);
let seq2 = lct.sequence(o);
assert_eq!(seq1, seq2);
}
}
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