Commit 1aaf69b4 authored by SirBlueRabbit's avatar SirBlueRabbit
Browse files

splay with reversed (untested)

parent 35cf8110
......@@ -5,8 +5,10 @@ pub struct Node<V> {
value: V,
path_parent: Option<Label<Node<V>>>,
parent: Option<Label<Node<V>>>,
left: Option<Label<Node<V>>>,
right: Option<Label<Node<V>>>,
reversed: bool,
children: [Option<Label<Node<V>>>; 2],
//left: Option<Label<Node<V>>>,
//right: Option<Label<Node<V>>>,
}
impl<V> Node<V> {
......@@ -15,8 +17,10 @@ impl<V> Node<V> {
value,
path_parent: None,
parent: None,
left: None,
right: None,
reversed: false,
children: [None, None],
//left: None,
//right: None,
}
}
}
......@@ -27,7 +31,7 @@ impl<V> Pool<Node<V>> {
self.insert(node)
}
pub fn find_root(&mut self, label: Label<Node<V>>) -> Label<Node<V>> {
/* pub fn find_root(&mut self, label: Label<Node<V>>) -> Label<Node<V>> {
self.expose(label);
let mut root = label;
while let Some(next) = self.left(root) {
......@@ -50,7 +54,7 @@ impl<V> Pool<Node<V>> {
self.expose(parent_label);
self[child_label].left = parent_label.into();
self[parent_label].parent = child_label.into();
}
} */
pub fn value_mut(&mut self, label: Label<Node<V>>) -> &mut V {
&mut self[label].value
......@@ -60,7 +64,7 @@ impl<V> Pool<Node<V>> {
&self[label].value
}
fn expose(&mut self, label: Label<Node<V>>) {
/* fn expose(&mut self, label: Label<Node<V>>) {
self.splay(label);
self.split_right_and_attach_new(label, None);
let mut v = label;
......@@ -70,13 +74,15 @@ impl<V> Pool<Node<V>> {
v = w;
}
self.splay(label);
}
fn splay(&mut self, label: Label<Node<V>>) {
while let Some(parent_label) = self[label].parent {
if self[parent_label].parent.is_some() {
if self.is_left_child(label) == self.is_left_child(parent_label) {
self.rotate_up(self[label].parent.unwrap());
} */
pub fn splay(&mut self, label: Label<Node<V>>) {
while let Some(p_label) = self[label].parent {
if self[p_label].parent.is_some() {
if self[p_label].reversed
== (self.is_zero_child(label) == self.is_zero_child(p_label))
{
self.rotate_up(p_label);
self.rotate_up(label);
} else {
self.rotate_up(label);
......@@ -89,19 +95,46 @@ impl<V> Pool<Node<V>> {
}
}
fn is_left_child(&self, label: Label<Node<V>>) -> bool {
if let Some(self_label) = self[label]
.parent
.map(|parent_label| self[parent_label].left)
.flatten()
{
self_label == label
/* fn is_left_child(&self, label: Label<Node<V>>) -> bool {
if let Some(self_label) = self[label]
.parent
.and_then(|parent_label| self[parent_label].left)
{
self_label == label
} else {
false
}
}
*/
fn rotate_up(&mut self, label: Label<Node<V>>) {
let reversed = self[label].reversed;
let p_side = self.is_zero_child(label);
let p_label = self[label].parent.unwrap();
let g_option = self[p_label].parent;
self[label].parent = g_option;
if let Some(g_label) = g_option {
let g_side = self.is_zero_child(p_label);
self[g_label].children[usize::from(g_side)] = Some(label);
} else {
self[label].path_parent = self[p_label].path_parent;
self[p_label].path_parent = None;
}
self[p_label].children[usize::from(p_side)] =
self[label].children[usize::from(reversed ^ !p_side)];
self[label].children[usize::from(reversed ^ !p_side)] = Some(p_label);
}
fn is_zero_child(&self, label: Label<Node<V>>) -> bool {
let parent_label = self[label].parent.unwrap();
if let Some(zero_child_label) = self[parent_label].children[0] {
zero_child_label == label
} else {
false
}
}
fn rotate_up(&mut self, label: Label<Node<V>>) {
/* fn rotate_up(&mut self, label: Label<Node<V>>) {
if let Some(parent_label) = self[label].parent {
if self.is_left_child(label) {
self.rotate_right(parent_label);
......@@ -109,8 +142,8 @@ impl<V> Pool<Node<V>> {
self.rotate_left(parent_label);
}
}
}
} */
/*
fn rotate_right(&mut self, root_label: Label<Node<V>>) {
if let Some(new_root_label) = self[root_label].left {
self.set_left(root_label, self[new_root_label].right);
......@@ -162,9 +195,9 @@ impl<V> Pool<Node<V>> {
if let Some(label) = left_label {
self[label].parent = Some(parent_label);
}
}
} */
fn split_right_and_attach_new(
/* fn split_right_and_attach_new(
&mut self,
label: Label<Node<V>>,
new_right: Option<Label<Node<V>>>,
......@@ -179,9 +212,9 @@ impl<V> Pool<Node<V>> {
self[new_right_label].parent = label.into();
self[new_right_label].path_parent = None;
}
}
} */
}
/*
#[cfg(test)]
mod tests {
use super::*;
......@@ -340,3 +373,4 @@ mod tests {
assert_eq!(lct.find_root(node1), node1);
}
}
*/
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