Commit f43db9c4 authored by SirBlueRabbit's avatar SirBlueRabbit
Browse files

small edits

parent 7f07183e
......@@ -6,59 +6,59 @@ pub use splay_forest::Node;
use std::fmt::Debug;
#[derive(Clone, Debug)]
pub struct LinkCutTree<V: Copy> {
rep: Pool<Node<V>>,
pub struct LinkCutTree<V> {
nodes: Pool<Node<V>>,
}
impl<V: Copy> LinkCutTree<V> {
impl<V> LinkCutTree<V> {
pub fn with_capacity(capacity: usize) -> Self {
LinkCutTree {
rep: Pool::<Node<V>>::with_capacity(capacity),
nodes: Pool::<Node<V>>::with_capacity(capacity),
}
}
pub fn make_tree(&mut self, val: V) -> Label<Node<V>> {
self.rep.add_node(val)
self.nodes.add_node(val)
}
pub fn expose(&mut self, node_idx: Label<Node<V>>) {
self.rep.splay(node_idx);
self.rep.split_right_and_attach_new(node_idx, None);
let mut v = node_idx;
pub fn expose(&mut self, label: Label<Node<V>>) {
self.nodes.splay(label);
self.nodes.split_right_and_attach_new(label, None);
let mut v = label;
loop {
match self.rep.path_parent(v) {
match self.nodes.path_parent(v) {
None => {
break;
}
Some(w) => {
self.rep.splay(w);
self.rep.split_right_and_attach_new(w, v.into());
self.nodes.splay(w);
self.nodes.split_right_and_attach_new(w, v.into());
v = w;
}
}
}
self.rep.splay(node_idx);
self.nodes.splay(label);
}
pub fn find_root(&mut self, node_idx: Label<Node<V>>) -> Label<Node<V>> {
self.expose(node_idx);
let root = self.rep.leftmost(node_idx);
pub fn find_root(&mut self, label: Label<Node<V>>) -> Label<Node<V>> {
self.expose(label);
let root = self.nodes.leftmost(label);
self.expose(root);
root
}
pub fn cut(&mut self, node_idx: Label<Node<V>>) {
self.expose(node_idx);
self.rep.split_left(node_idx);
pub fn cut(&mut self, label: Label<Node<V>>) {
self.expose(label);
self.nodes.split_left(label);
}
pub fn link(&mut self, parent_idx: Label<Node<V>>, child_idx: Label<Node<V>>) {
self.expose(child_idx);
self.expose(parent_idx);
self.rep.join_left(child_idx, parent_idx);
self.nodes.join_left(child_idx, parent_idx);
}
pub fn val(&mut self, node_idx: Label<Node<V>>) -> &V {
self.rep.value(node_idx)
pub fn val(&mut self, label: Label<Node<V>>) -> &V {
self.nodes.value(label)
}
}
......
......@@ -2,7 +2,7 @@ use super::{Label, Pool};
use std::fmt::Debug;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Node<V: Copy> {
pub struct Node<V> {
value: V,
path_parent: Option<Label<Node<V>>>,
parent: Option<Label<Node<V>>>,
......@@ -10,7 +10,7 @@ pub struct Node<V: Copy> {
right: Option<Label<Node<V>>>,
}
impl<V: Copy> Node<V> {
impl<V> Node<V> {
pub fn new(value: V) -> Self {
Node {
value,
......@@ -22,38 +22,38 @@ impl<V: Copy> Node<V> {
}
}
impl<V: Copy> Pool<Node<V>> {
impl<V> Pool<Node<V>> {
pub fn add_node(&mut self, value: V) -> Label<Node<V>> {
let node = Node::new(value);
self.insert(node)
}
fn node(&self, label: Label<Node<V>>) -> &Node<V> {
&self[label]
}
/* fn node(&self, label: Label<Node<V>>) -> &Node<V> {
&self[label]
}
*/
fn node_mut(&mut self, label: Label<Node<V>>) -> &mut Node<V> {
&mut self[label]
}
pub fn value(&self, label: Label<Node<V>>) -> &V {
&self.node(label).value
&self[label].value
}
pub fn parent(&self, label: Label<Node<V>>) -> Option<Label<Node<V>>> {
self.node(label).parent
self[label].parent
}
pub fn path_parent(&self, label: Label<Node<V>>) -> Option<Label<Node<V>>> {
self.node(label).path_parent
self[label].path_parent
}
pub fn left(&self, label: Label<Node<V>>) -> Option<Label<Node<V>>> {
self.node(label).left
self[label].left
}
pub fn right(&self, label: Label<Node<V>>) -> Option<Label<Node<V>>> {
self.node(label).right
self[label].right
}
fn while_some<F>(&self, label: Label<Node<V>>, func: F) -> Label<Node<V>>
......@@ -86,12 +86,12 @@ impl<V: Copy> Pool<Node<V>> {
}
fn is_left_child(&self, label: Label<Node<V>>) -> bool {
match self.parent(label).map(|label| self.node(label)) {
None => false,
match self.parent(label).map(|parent_label| &self[parent_label]) {
Some(parent) => match parent.left {
None => false,
Some(left_label) => left_label == label,
None => false,
},
None => false,
}
}
......@@ -115,11 +115,11 @@ impl<V: Copy> Pool<Node<V>> {
}
fn rotate_right(&mut self, root_label: Label<Node<V>>) {
let left_label = self.node(root_label).left;
let left_label = self[root_label].left;
match left_label {
None => {}
Some(new_root_label) => {
self.set_left(root_label, self.node(new_root_label).right);
self.set_left(root_label, self[new_root_label].right);
self.update_parent(root_label, new_root_label);
self.set_right(new_root_label, Some(root_label));
}
......@@ -127,11 +127,11 @@ impl<V: Copy> Pool<Node<V>> {
}
fn rotate_left(&mut self, root_label: Label<Node<V>>) {
let right_label = self.node(root_label).right;
let right_label = self[root_label].right;
match right_label {
None => {}
Some(new_root_label) => {
self.set_right(root_label, self.node(new_root_label).left);
self.set_right(root_label, self[new_root_label].left);
self.update_parent(root_label, new_root_label);
self.set_left(new_root_label, Some(root_label));
}
......@@ -256,11 +256,11 @@ mod tests {
lct.splay(node3);
}
impl<V: Copy> Pool<Node<V>> {
impl<V> Pool<Node<V>> {
fn rotate_down(&mut self, label: Label<Node<V>>) {
if self.node(label).left.is_some() {
if self[label].left.is_some() {
self.rotate_right(label);
} else if self.node(label).right.is_some() {
} else if self[label].right.is_some() {
self.rotate_left(label);
}
}
......
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