From 6efc43e7d929d72401ecff3af175fb57148bae26 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Fri, 31 Oct 2014 23:16:57 -0400 Subject: [PATCH] Utilize less generalized methods --- src/lib.rs | 107 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 66 insertions(+), 41 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 65616d4..9811fec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,26 +20,28 @@ impl Four { } /// Convert a Split into a TwoNode with the middle value as the value of the new parent node - fn to_two_node(self) -> Node { + fn to_two(self) -> Two { match self { Four(v1, v2, v3, None) => - TwoNode(Two(v2, + Two(v2, box LeafTwoNode(LeafTwo(v1)), - box LeafTwoNode(LeafTwo(v3)))), + box LeafTwoNode(LeafTwo(v3))), Four(v1, v2, v3, Some((n1, n2, n3, n4))) => - TwoNode(Two(v2, + Two(v2, box TwoNode(Two(v1, n1, n2)), - box TwoNode(Two(v3, n3, n4)))), + box TwoNode(Two(v3, n3, n4))), } } } + /// InsertResult enum InsertResult { Fit(Node), // box this node? Split(Four), // split should always contain a ThreeNode/LeafThreeNode. box this? } + /// Direction enum Direction { Left, @@ -48,13 +50,60 @@ enum Direction { Leaf, } -/// Node -pub struct LeafTwo(pub V); +/// ToNode +trait ToNode { + fn to_node(self) -> Node; +} + + +/// Two pub struct Two(pub V, pub Box>, pub Box>); -pub struct LeafThree(pub V, pub V); + +impl Two { + fn to_three(self, other_value: V, other_node: Box>) -> Three { + let Two(self_value, self_left, self_middle) = self; + if self_value > other_value { + Three(other_value, self_value, other_node, self_left, self_middle) + } else { + Three(self_value, other_value, self_left, self_middle, other_node) + } + } +} + +impl ToNode for Two { + fn to_node(self) -> Node { TwoNode(self) } +} + + +/// Three pub struct Three(pub V, pub V, pub Box>, pub Box>, pub Box>); +impl Three { + fn to_four(self, other_value: V, other_node: Box>) -> Four { + let Three(self_value1, self_value2, self_left, self_middle, self_right) = self; + if other_value < self_value1 { + Four(other_value, self_value1, self_value2, Some((other_node, self_left, self_middle, self_right))) + } else { + Four(self_value1, self_value2, other_value, Some((self_left, self_middle, self_right, other_node))) + } + } +} + +impl ToNode for Three { + fn to_node(self) -> Node { ThreeNode(self) } +} + + +/// LeafTwo +pub struct LeafTwo(pub V); + + +/// LeafThree +pub struct LeafThree(pub V, pub V); + + +/// Node pub enum Node { LeafTwoNode(LeafTwo), LeafThreeNode(LeafThree), @@ -73,34 +122,10 @@ impl ToString for Node { } } + + impl Node { // PRIVATE - /// Extend this TwoNode with the provided value and child node to make it a ThreeNode - fn extend(self, other_value: V, other_node: Box>) -> Node { - match self { - TwoNode(Two(self_value, self_left, self_middle)) => { - if self_value > other_value { - ThreeNode(Three(other_value, self_value, other_node, self_left, self_middle)) - } else { - ThreeNode(Three(self_value, other_value, self_left, self_middle, other_node)) - } - } - _ => fail!(""), - } - } - - fn extend4(self, other_value: V, other_node: Box>) -> Four { - match self { - ThreeNode(Three(self_value1, self_value2, self_left, self_middle, self_right)) => { - if other_value < self_value1 { - Four(other_value, self_value1, self_value2, Some((other_node, self_left, self_middle, self_right))) - } else { - Four(self_value1, self_value2, other_value, Some((self_left, self_middle, self_right, other_node))) - } - } - _ => fail!(""), - } - } fn next_direction(&self, to_insert: &V) -> Direction { match self { @@ -166,11 +191,11 @@ impl Node { let new_node = match insert_result { Fit(returned_node) => match next_direction { - Left => TwoNode(Two(value, box returned_node, box other_node)), - Middle => TwoNode(Two(value, box other_node, box returned_node)), + Left => Two(value, box returned_node, box other_node).to_node(), + Middle => Two(value, box other_node, box returned_node).to_node(), _ => fail!(""), }, - Split(four_node) => four_node.to_two_node().extend(value, box other_node), + Split(four_node) => four_node.to_two().to_three(value, box other_node).to_node(), }; Fit(new_node) @@ -201,9 +226,9 @@ impl Node { }, Split(four_node) => { let new_node: Four = match next_direction { - Left => four_node.to_two_node().extend(value1, other_node1).extend4(value2, other_node2), - Middle => four_node.to_two_node().extend(value1, other_node1).extend4(value2, other_node2), - Right => four_node.to_two_node().extend(value2, other_node2).extend4(value1, other_node1), + Left => four_node.to_two().to_three(value1, other_node1).to_four(value2, other_node2), + Middle => four_node.to_two().to_three(value1, other_node1).to_four(value2, other_node2), + Right => four_node.to_two().to_three(value2, other_node2).to_four(value1, other_node1), _ => fail!(""), }; Split(new_node) @@ -242,7 +267,7 @@ impl TTTree { let result = root.insert(value); match result { Fit(node) => node, - Split(node @ Four(..)) => node.to_two_node(), + Split(node @ Four(..)) => node.to_two().to_node(), } } }; -- 2.51.2