Commit d6669f88 authored by SirBlueRabbit's avatar SirBlueRabbit
Browse files

index depth

parent fb866dcb
use std::{
cmp::Ordering,
fmt,
ops::{Index, IndexMut},
};
......@@ -133,10 +134,10 @@ impl<V> Pool<Node<V>> {
pub fn find_root(&mut self, u: Label<Node<V>>) -> Label<Node<V>> {
self.expose(u);
let mut x = u;
let mut d_flip = self[u].d_flip;
while let Some(y) = self[x].children[ChildType::from(d_flip)] {
let mut flip = self[u].d_flip;
while let Some(y) = self[x].children[ChildType::from(flip)] {
x = y;
d_flip ^= self[x].d_flip;
flip ^= self[x].d_flip;
}
self.expose(x);
x
......@@ -214,6 +215,28 @@ impl<V> Pool<Node<V>> {
}
}
pub fn index_depth(&mut self, u: Label<Node<V>>, i: usize) -> Label<Node<V>> {
self.expose(u);
let mut label = u;
let mut flip = self[u].d_flip;
let mut depth = self[u].d_depth;
loop {
let side = match i.cmp(&depth) {
Ordering::Less => flip,
Ordering::Equal => return label,
Ordering::Greater => !flip,
};
label = self[label].children[ChildType::from(side)].unwrap();
if flip ^ side {
depth -= self[label].d_depth;
} else {
depth += self[label].d_depth;
}
flip ^= self[label].d_flip;
}
}
pub fn value_mut(&mut self, u: Label<Node<V>>) -> &mut V {
&mut self[u].value
}
......@@ -336,16 +359,7 @@ impl<V> Pool<Node<V>> {
}
Parent::Path(_) => ChildType::Path,
Parent::Root => ChildType::Root,
} /*
if let Parent::BinTree(p) = self[u].parent {
if let Some(x) = self[p].children[true] {
Some(u == x)
} else {
Some(false)
}
} else {
None
} */
}
}
fn parent(&self, u: Label<Node<V>>) -> Option<Label<Node<V>>> {
......@@ -355,46 +369,6 @@ impl<V> Pool<Node<V>> {
Parent::Root => None,
}
}
/* // 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)]
......@@ -645,36 +619,6 @@ mod tests {
assert_eq!(lct.find_root(g), o);
}
/* #[test]
fn basic_sequence() {
let mut lct = Pool::with_capacity(7);
// define nodes
let o = lct.add_node("node");
let p = lct.add_node("parent");
let g = lct.add_node("grandparent");
let a = lct.add_node("a");
let b = lct.add_node("b");
let c = lct.add_node("c");
let d = lct.add_node("d");
// link nodes
lct[o].children = Children(Some(a), Some(b));
lct[p].children = Children(Some(o), Some(c));
lct[g].children = Children(Some(p), Some(d));
lct[o].parent = Parent::BinTree(p);
lct[p].parent = Parent::BinTree(g);
lct[a].parent = Parent::BinTree(o);
lct[b].parent = Parent::BinTree(o);
lct[c].parent = Parent::BinTree(p);
lct[d].parent = Parent::BinTree(g);
let seq1 = lct.sequence(g);
lct.splay(o);
let seq2 = lct.sequence(o);
assert_eq!(seq1, seq2);
} */
#[test]
fn basic_depth() {
// generate a basic lct
......@@ -719,14 +663,23 @@ mod tests {
// randomly mutate lct and check integrity
for _ in 0..(size * size * size) {
let label = Label::from(fastrand::usize(0..size));
let step = fastrand::usize(0..4);
if step == 0 {
if fastrand::bool() {
let label = Label::from(fastrand::usize(0..size));
lct.evert(label);
for i in 0..size {
let u = Label::from(i);
assert_eq!(lct.find_root(u), label);
}
} else {
let label1 = Label::from(fastrand::usize(0..size));
let label2 = Label::from(fastrand::usize(0..size));
if label1 != label2 {
let root = lct.first_ancestor(label1, label2);
lct.evert(root);
let depth1 = lct.depth(label1);
let depth2 = lct.depth(label2);
//todo
}
}
assert_clean(&lct);
}
......
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